mirror of
https://github.com/beetbox/beets.git
synced 2025-12-15 13:07:09 +01:00
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.
This commit is contained in:
parent
73d200184b
commit
62cd6e37aa
7 changed files with 31 additions and 38 deletions
|
|
@ -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))
|
||||
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
Loading…
Reference in a new issue