From d6f2bf20f41404c30f52d9714aac297738e0d5f3 Mon Sep 17 00:00:00 2001 From: Philippe Mongeau Date: Sat, 22 Sep 2012 17:35:39 -0400 Subject: [PATCH] fuzzy_search: enable setting threshold value from the config file --- beetsplug/fuzzy_search.py | 15 +++++++++++++-- docs/plugins/fuzzy_search.rst | 5 +++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/beetsplug/fuzzy_search.py b/beetsplug/fuzzy_search.py index 263502306..dfdb4b296 100644 --- a/beetsplug/fuzzy_search.py +++ b/beetsplug/fuzzy_search.py @@ -14,11 +14,13 @@ """Like beet list, but with fuzzy matching """ +import beets from beets.plugins import BeetsPlugin from beets.ui import Subcommand, decargs, print_ from beets.util.functemplate import Template import difflib + # THRESHOLD = 0.7 @@ -44,7 +46,10 @@ def is_match(query, item, album=False, verbose=False, threshold=0.7): def fuzzy_list(lib, config, opts, args): query = decargs(args) fmt = opts.format - threshold = float(opts.threshold) + if opts.threshold is not None: + threshold = float(opts.threshold) + else: + threshold = float(conf['threshold']) if fmt is None: # If no specific template is supplied, use a default @@ -84,10 +89,16 @@ fuzzy_cmd.parser.add_option('-v', '--verbose', action='store_true', help='output scores for matches') fuzzy_cmd.parser.add_option('-t', '--threshold', action='store', help='return result with a fuzzy score above threshold. \ - (default is 0.7)', default=0.7) + (default is 0.7)', default=None) fuzzy_cmd.func = fuzzy_list +conf = {} + class Fuzzy(BeetsPlugin): def commands(self): return [fuzzy_cmd] + + def configure(self, config): + conf['threshold'] = beets.ui.config_val(config, 'fuzzy', + 'threshold', 0.7) diff --git a/docs/plugins/fuzzy_search.rst b/docs/plugins/fuzzy_search.rst index 386fab892..9c9720af9 100644 --- a/docs/plugins/fuzzy_search.rst +++ b/docs/plugins/fuzzy_search.rst @@ -18,3 +18,8 @@ to use a custom format for printing, use ``-f FORMAT``. The ``-t NUMBER`` option lets you specify how precise the fuzzy match has to be (default is 0.7). To make a fuzzier search, try ``beet fuzzy -t 0.5 Varoeldur``. A value of ``1`` will show only perfect matches and a value of ``0`` will match everything. + +The default threshold can also be set from the config file.:: + + [fuzzy] + threshold: 0.8