From 62cd6e37aa902ab755e54eb087a19ac2d1bd8da5 Mon Sep 17 00:00:00 2001 From: Bruno Cauet Date: Sun, 25 Jan 2015 20:57:50 +0100 Subject: [PATCH] Update ui.print_obj_(), add ui.format_() Code now relies on `format()` for items and albums displaying/logging. `ui.print_()` calls `unicode()` or `str()` on the strings so for most usages calling `ui.print_(obj)` replaces `ui.print_(obj, lib, None)`. Where there is a special format `ui.print_(format(obj, fmt))` is fine, but when `fmt` can be None then one has to call `ui.print_(ui.format_(obj, fmt))` -- which is what `ui.print_obj` now does. --- beets/ui/__init__.py | 40 ++++++++++++++++++++-------------------- beets/ui/commands.py | 10 ++++------ beetsplug/duplicates.py | 2 +- beetsplug/echonest.py | 2 +- beetsplug/mbsync.py | 9 ++------- beetsplug/missing.py | 4 ++-- beetsplug/random.py | 2 +- 7 files changed, 31 insertions(+), 38 deletions(-) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index c541ba2de..0385584ac 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -99,6 +99,12 @@ def print_(*strings): replaces it. """ if strings: + if not isinstance(strings[0], basestring): + try: + strings = map(unicode, strings) + except UnicodeError: + strings = map(bytes, strings) + if isinstance(strings[0], unicode): txt = u' '.join(strings) else: @@ -471,29 +477,23 @@ def get_replacements(): return replacements -def _pick_format(album, fmt=None): - """Pick a format string for printing Album or Item objects, - falling back to config options and defaults. - """ +def format_(obj, fmt): + """Print a object, intended for Album and Item + + This is equivalent to `format`, but the spec can be None + `fmt` is mandatory for otherwise one can just call `format(my_object)`""" if fmt: - return fmt - if album: - return config['list_format_album'].get(unicode) + return format(obj, fmt) else: - return config['list_format_item'].get(unicode) + return format(obj) -def print_obj(obj, lib, fmt=None): - """Print an Album or Item object. If `fmt` is specified, use that - format string. Otherwise, use the configured template. - """ - album = isinstance(obj, library.Album) - fmt = _pick_format(album, fmt) - if isinstance(fmt, Template): - template = fmt - else: - template = Template(fmt) - print_(obj.evaluate_template(template)) +def print_obj(obj, fmt): + """Print a object, intended for Album and Item + + This is equivalent to `print_ o format`, but the spec can be None + `fmt` is mandatory for otherwise one can just call `print_(my_object)`""" + return print_(format_(obj, fmt)) def term_width(): @@ -587,7 +587,7 @@ def show_model_changes(new, old=None, fields=None, always=False): # Print changes. if changes or always: - print_obj(old, old._db) + print_(old) if changes: print_(u'\n'.join(changes)) diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 839263fc5..8fccc47a8 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -32,7 +32,6 @@ from beets import plugins from beets import importer from beets import util from beets.util import syspath, normpath, ancestry, displayable_path -from beets.util.functemplate import Template from beets import library from beets import config from beets import logging @@ -951,13 +950,12 @@ def list_items(lib, query, album, fmt): """Print out items in lib matching query. If album, then search for albums instead of single items. """ - tmpl = Template(ui._pick_format(album, fmt)) if album: for album in lib.albums(query): - ui.print_obj(album, lib, tmpl) + ui.print_obj(album, fmt) else: for item in lib.items(query): - ui.print_obj(item, lib, tmpl) + ui.print_obj(item, fmt) def list_func(lib, opts, args): @@ -999,7 +997,7 @@ def update_items(lib, query, album, move, pretend): for item in items: # Item deleted? if not os.path.exists(syspath(item.path)): - ui.print_obj(item, lib) + ui.print_(item) ui.print_(ui.colorize('red', u' deleted')) if not pretend: item.remove(True) @@ -1122,7 +1120,7 @@ def remove_items(lib, query, album, delete): # Show all the items. for item in items: - ui.print_obj(item, lib, fmt) + ui.print_obj(item, fmt) # Confirm with user. if not ui.input_yn(prompt, True): diff --git a/beetsplug/duplicates.py b/beetsplug/duplicates.py index 60a49d091..ea235295c 100644 --- a/beetsplug/duplicates.py +++ b/beetsplug/duplicates.py @@ -42,7 +42,7 @@ def _process_item(item, lib, copy=False, move=False, delete=False, raise UserError('%s: can\'t parse k=v tag: %s' % (PLUGIN, tag)) setattr(k, v) item.store() - print_obj(item, lib, fmt=format) + print_obj(item, format) def _checksum(item, prog, log): diff --git a/beetsplug/echonest.py b/beetsplug/echonest.py index a2b24bf20..817b96739 100644 --- a/beetsplug/echonest.py +++ b/beetsplug/echonest.py @@ -115,7 +115,7 @@ def similar(lib, src_item, threshold=0.15, fmt='${difference}: ${path}'): d = diff(item, src_item) if d < threshold: s = fmt.replace('${difference}', '{:2.2f}'.format(d)) - ui.print_obj(item, lib, s) + ui.print_obj(item, s) class EchonestMetadataPlugin(plugins.BeetsPlugin): diff --git a/beetsplug/mbsync.py b/beetsplug/mbsync.py index a4e242026..6c20140d3 100644 --- a/beetsplug/mbsync.py +++ b/beetsplug/mbsync.py @@ -18,7 +18,6 @@ from beets.plugins import BeetsPlugin from beets import autotag, library, ui, util from beets.autotag import hooks from beets import config -from beets.util.functemplate import Template from collections import defaultdict @@ -71,10 +70,8 @@ class MBSyncPlugin(BeetsPlugin): """Retrieve and apply info from the autotagger for items matched by query. """ - template = Template(ui._pick_format(False, fmt)) - for item in lib.items(query + ['singleton:true']): - item_formatted = item.evaluate_template(template) + item_formatted = ui.format_(item, fmt) if not item.mb_trackid: self._log.info(u'Skipping singleton with no mb_trackid: {0}', item_formatted) @@ -97,11 +94,9 @@ class MBSyncPlugin(BeetsPlugin): """Retrieve and apply info from the autotagger for albums matched by query and their items. """ - template = Template(ui._pick_format(True, fmt)) - # Process matching albums. for a in lib.albums(query): - album_formatted = a.evaluate_template(template) + album_formatted = ui.format_(a, fmt) if not a.mb_albumid: self._log.info(u'Skipping album with no mb_albumid: {0}', album_formatted) diff --git a/beetsplug/missing.py b/beetsplug/missing.py index a27be65d1..758669286 100644 --- a/beetsplug/missing.py +++ b/beetsplug/missing.py @@ -125,11 +125,11 @@ class MissingPlugin(BeetsPlugin): if count: missing = _missing_count(album) if missing: - print_obj(album, lib, fmt=fmt) + print_obj(album, fmt) else: for item in self._missing(album): - print_obj(item, lib, fmt=fmt) + print_obj(item, fmt) self._command.func = _miss return [self._command] diff --git a/beetsplug/random.py b/beetsplug/random.py index 2c4d0c000..668f08b2f 100644 --- a/beetsplug/random.py +++ b/beetsplug/random.py @@ -66,7 +66,7 @@ def random_item(lib, opts, args): objs = random.sample(objs, number) for item in objs: - print_obj(item, lib, template) + print_obj(item, template) random_cmd = Subcommand('random', help='chose a random track or album')