Simplify LibModel format management

Delete `ui.format_` and then `ui.print_obj`. Simply ensure that when
there is no format it defaults to '' = default format = config option.
This commit is contained in:
Bruno Cauet 2015-01-26 23:09:56 +01:00
parent 060c275fd3
commit 4e904c78af
9 changed files with 27 additions and 55 deletions

View file

@ -480,25 +480,6 @@ def get_replacements():
return replacements
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 format(obj, fmt)
else:
return format(obj)
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():
"""Get the width (columns) of the terminal."""
fallback = config['ui']['terminal_width'].get(int)

View file

@ -952,17 +952,14 @@ def list_items(lib, query, album, fmt):
"""
if album:
for album in lib.albums(query):
ui.print_obj(album, fmt)
ui.print_(format(album, fmt))
else:
for item in lib.items(query):
ui.print_obj(item, fmt)
ui.print_(format(item, fmt))
def list_func(lib, opts, args):
if opts.path:
fmt = '$path'
else:
fmt = opts.format
fmt = '$path' if opts.path else opts.format
list_items(lib, decargs(args), opts.album, fmt)
@ -977,7 +974,7 @@ list_cmd.parser.add_option(
)
list_cmd.parser.add_option(
'-f', '--format', action='store',
help='print with custom format', default=None
help='print with custom format', default=''
)
list_cmd.func = list_func
default_commands.append(list_cmd)
@ -1093,7 +1090,7 @@ update_cmd.parser.add_option(
)
update_cmd.parser.add_option(
'-f', '--format', action='store',
help='print with custom format', default=None
help='print with custom format', default=''
)
update_cmd.func = update_func
default_commands.append(update_cmd)
@ -1114,13 +1111,13 @@ def remove_items(lib, query, album, delete):
fmt = u'$path - $title'
prompt = 'Really DELETE %i files (y/n)?' % len(items)
else:
fmt = None
fmt = ''
prompt = 'Really remove %i items from the library (y/n)?' % \
len(items)
# Show all the items.
for item in items:
ui.print_obj(item, fmt)
ui.print_(format(item, fmt))
# Confirm with user.
if not ui.input_yn(prompt, True):
@ -1350,7 +1347,7 @@ modify_cmd.parser.add_option(
)
modify_cmd.parser.add_option(
'-f', '--format', action='store',
help='print with custom format', default=None
help='print with custom format', default=''
)
modify_cmd.func = modify_func
default_commands.append(modify_cmd)

View file

@ -356,7 +356,7 @@ class ConvertPlugin(BeetsPlugin):
self.config['pretend'].get(bool)
if not pretend:
ui.commands.list_items(lib, ui.decargs(args), opts.album, None)
ui.commands.list_items(lib, ui.decargs(args), opts.album, '')
if not (opts.yes or ui.input_yn("Convert? (Y/n)")):
return

View file

@ -17,14 +17,14 @@
import shlex
from beets.plugins import BeetsPlugin
from beets.ui import decargs, print_obj, vararg_callback, Subcommand, UserError
from beets.ui import decargs, print_, vararg_callback, Subcommand, UserError
from beets.util import command_output, displayable_path, subprocess
PLUGIN = 'duplicates'
def _process_item(item, lib, copy=False, move=False, delete=False,
tag=False, format=None):
tag=False, format=''):
"""Process Item `item` in `lib`.
"""
if copy:
@ -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, format)
print_(format(item, format))
def _checksum(item, prog, log):
@ -126,7 +126,7 @@ class DuplicatesPlugin(BeetsPlugin):
self._command.parser.add_option('-f', '--format', dest='format',
action='store', type='string',
help='print with custom format',
metavar='FMT')
metavar='FMT', default='')
self._command.parser.add_option('-a', '--album', dest='album',
action='store_true',

View file

@ -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, s)
ui.print_(format(item, s))
class EchonestMetadataPlugin(plugins.BeetsPlugin):

View file

@ -49,7 +49,7 @@ class MBSyncPlugin(BeetsPlugin):
cmd.parser.add_option('-W', '--nowrite', action='store_false',
default=config['import']['write'], dest='write',
help="don't write updated metadata to files")
cmd.parser.add_option('-f', '--format', action='store', default=None,
cmd.parser.add_option('-f', '--format', action='store', default='',
help='print with custom format')
cmd.func = self.func
return [cmd]
@ -71,7 +71,7 @@ class MBSyncPlugin(BeetsPlugin):
query.
"""
for item in lib.items(query + ['singleton:true']):
item_formatted = ui.format_(item, fmt)
item_formatted = format(item, fmt)
if not item.mb_trackid:
self._log.info(u'Skipping singleton with no mb_trackid: {0}',
item_formatted)
@ -96,7 +96,7 @@ class MBSyncPlugin(BeetsPlugin):
"""
# Process matching albums.
for a in lib.albums(query):
album_formatted = ui.format_(a, fmt)
album_formatted = format(a, fmt)
if not a.mb_albumid:
self._log.info(u'Skipping album with no mb_albumid: {0}',
album_formatted)

View file

@ -17,7 +17,7 @@
from beets.autotag import hooks
from beets.library import Item
from beets.plugins import BeetsPlugin
from beets.ui import decargs, print_obj, Subcommand
from beets.ui import decargs, print_, Subcommand
def _missing_count(album):
@ -95,7 +95,7 @@ class MissingPlugin(BeetsPlugin):
self._command.parser.add_option('-f', '--format', dest='format',
action='store', type='string',
help='print with custom FORMAT',
metavar='FORMAT')
metavar='FORMAT', default='')
self._command.parser.add_option('-c', '--count', dest='count',
action='store_true',
@ -123,13 +123,12 @@ class MissingPlugin(BeetsPlugin):
for album in albums:
if count:
missing = _missing_count(album)
if missing:
print_obj(album, fmt)
if _missing_count(album):
print_(format(album, fmt))
else:
for item in self._missing(album):
print_obj(item, fmt)
print_(format(item, fmt))
self._command.func = _miss
return [self._command]

View file

@ -16,8 +16,7 @@
"""
from __future__ import absolute_import
from beets.plugins import BeetsPlugin
from beets.ui import Subcommand, decargs, print_obj
from beets.util.functemplate import Template
from beets.ui import Subcommand, decargs, print_
import random
from operator import attrgetter
from itertools import groupby
@ -25,11 +24,7 @@ from itertools import groupby
def random_item(lib, opts, args):
query = decargs(args)
if opts.path:
fmt = '$path'
else:
fmt = opts.format
template = Template(fmt) if fmt else None
fmt = '$path' if opts.path else opts.format
if opts.album:
objs = list(lib.albums(query))
@ -66,7 +61,7 @@ def random_item(lib, opts, args):
objs = random.sample(objs, number)
for item in objs:
print_obj(item, template)
print_(format(item, fmt))
random_cmd = Subcommand('random',
help='chose a random track or album')
@ -75,7 +70,7 @@ random_cmd.parser.add_option('-a', '--album', action='store_true',
random_cmd.parser.add_option('-p', '--path', action='store_true',
help='print the path of the matched item')
random_cmd.parser.add_option('-f', '--format', action='store',
help='print with custom format', default=None)
help='print with custom format', default='')
random_cmd.parser.add_option('-n', '--number', action='store', type="int",
help='number of objects to choose', default=1)
random_cmd.parser.add_option('-e', '--equal-chance', action='store_true',

View file

@ -43,7 +43,7 @@ class ListTest(unittest.TestCase):
self.lib.add(self.item)
self.lib.add_album([self.item])
def _run_list(self, query='', album=False, path=False, fmt=None):
def _run_list(self, query='', album=False, path=False, fmt=''):
commands.list_items(self.lib, query, album, fmt)
def test_list_outputs_item(self):