fuzzy_search: enable setting threshold value from the config file

This commit is contained in:
Philippe Mongeau 2012-09-22 17:35:39 -04:00
parent 1e48317e51
commit d6f2bf20f4
2 changed files with 18 additions and 2 deletions

View file

@ -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)

View file

@ -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