From 4c5fc8fd765171ab04ac45d7a935b8ee118d0d97 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 10 Feb 2014 22:08:01 -0800 Subject: [PATCH] echonest: use only one plugin class (fix #536) --- beetsplug/echonest.py | 66 +++++++++++++++++++++---------------------- docs/changelog.rst | 2 ++ 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/beetsplug/echonest.py b/beetsplug/echonest.py index 270e787b4..e1e3e387f 100644 --- a/beetsplug/echonest.py +++ b/beetsplug/echonest.py @@ -52,12 +52,35 @@ ATTRIBUTES = { ID_KEY = 'echonest_id' FINGERPRINT_KEY = 'echonest_fingerprint' + def _splitstrip(string, delim=u','): """Split string (at commas by default) and strip whitespace from the pieces. """ return [s.strip() for s in string.split(delim)] + +def diff(item1, item2, attributes): + result = 0.0 + for attr in attributes: + try: + result += abs( + float(item1.get(attr, None)) - + float(item2.get(attr, None)) + ) + except TypeError: + result += 1.0 + return result + + +def similar(lib, src_item, threshold=0.15): + for item in lib.items(): + if item.path != src_item.path: + d = diff(item, src_item, ATTRIBUTES.values()) + if d < threshold: + print(u'{1:2.2f}: {0}'.format(item.path, d)) + + class EchonestMetadataPlugin(plugins.BeetsPlugin): def __init__(self): super(EchonestMetadataPlugin, self).__init__() @@ -399,13 +422,13 @@ class EchonestMetadataPlugin(plugins.BeetsPlugin): return False def commands(self): - cmd = ui.Subcommand('echonest', + fetch_cmd = ui.Subcommand('echonest', help='Fetch metadata from the EchoNest') - cmd.parser.add_option('-f', '--force', dest='force', + fetch_cmd.parser.add_option('-f', '--force', dest='force', action='store_true', default=False, help='(re-)download information from the EchoNest') - def func(lib, opts, args): + def fetch_func(lib, opts, args): self.config.set_args(opts) write = config['import']['write'].get(bool) for item in lib.items(ui.decargs(args)): @@ -416,39 +439,16 @@ class EchonestMetadataPlugin(plugins.BeetsPlugin): if song: self.apply_metadata(item, song, write) - cmd.func = func - return [cmd] + fetch_cmd.func = fetch_func + sim_cmd = ui.Subcommand('echosim', help='show related files') -def diff(item1, item2, attributes): - result = 0.0 - for attr in attributes: - try: - result += abs( - float(item1.get(attr, None)) - - float(item2.get(attr, None)) - ) - except TypeError: - result += 1.0 - return result - - -def similar(lib, src_item, threshold=0.15): - for item in lib.items(): - if item.path != src_item.path: - d = diff(item, src_item, ATTRIBUTES.values()) - if d < threshold: - print(u'{1:2.2f}: {0}'.format(item.path, d)) - - -class EchonestSimilarPlugin(plugins.BeetsPlugin): - def commands(self): - cmd = ui.Subcommand('echosim', help='show related files') - - def func(lib, opts, args): + def sim_func(lib, opts, args): self.config.set_args(opts) for item in lib.items(ui.decargs(args)): similar(lib, item) - cmd.func = func - return [cmd] + sim_cmd.func = sim_func + return [sim_cmd] + + return [fetch_cmd, sim_cmd] diff --git a/docs/changelog.rst b/docs/changelog.rst index a23232d76..c25812530 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -79,6 +79,8 @@ Other little fixes: "replace" configuration. * :doc:`/plugins/fetchart`: When using the ``remote_priority`` config option, local image files are no longer completely ignored. +* :doc:`/plugins/echonest`: Fix an issue causing the plugin to appear twice in + the output of the ``beet version`` command. 1.3.2 (December 22, 2013)