mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 16:42:42 +01:00
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:
parent
060c275fd3
commit
4e904c78af
9 changed files with 27 additions and 55 deletions
|
|
@ -480,25 +480,6 @@ def get_replacements():
|
||||||
return 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():
|
def term_width():
|
||||||
"""Get the width (columns) of the terminal."""
|
"""Get the width (columns) of the terminal."""
|
||||||
fallback = config['ui']['terminal_width'].get(int)
|
fallback = config['ui']['terminal_width'].get(int)
|
||||||
|
|
|
||||||
|
|
@ -952,17 +952,14 @@ def list_items(lib, query, album, fmt):
|
||||||
"""
|
"""
|
||||||
if album:
|
if album:
|
||||||
for album in lib.albums(query):
|
for album in lib.albums(query):
|
||||||
ui.print_obj(album, fmt)
|
ui.print_(format(album, fmt))
|
||||||
else:
|
else:
|
||||||
for item in lib.items(query):
|
for item in lib.items(query):
|
||||||
ui.print_obj(item, fmt)
|
ui.print_(format(item, fmt))
|
||||||
|
|
||||||
|
|
||||||
def list_func(lib, opts, args):
|
def list_func(lib, opts, args):
|
||||||
if opts.path:
|
fmt = '$path' if opts.path else opts.format
|
||||||
fmt = '$path'
|
|
||||||
else:
|
|
||||||
fmt = opts.format
|
|
||||||
list_items(lib, decargs(args), opts.album, fmt)
|
list_items(lib, decargs(args), opts.album, fmt)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -977,7 +974,7 @@ list_cmd.parser.add_option(
|
||||||
)
|
)
|
||||||
list_cmd.parser.add_option(
|
list_cmd.parser.add_option(
|
||||||
'-f', '--format', action='store',
|
'-f', '--format', action='store',
|
||||||
help='print with custom format', default=None
|
help='print with custom format', default=''
|
||||||
)
|
)
|
||||||
list_cmd.func = list_func
|
list_cmd.func = list_func
|
||||||
default_commands.append(list_cmd)
|
default_commands.append(list_cmd)
|
||||||
|
|
@ -1093,7 +1090,7 @@ update_cmd.parser.add_option(
|
||||||
)
|
)
|
||||||
update_cmd.parser.add_option(
|
update_cmd.parser.add_option(
|
||||||
'-f', '--format', action='store',
|
'-f', '--format', action='store',
|
||||||
help='print with custom format', default=None
|
help='print with custom format', default=''
|
||||||
)
|
)
|
||||||
update_cmd.func = update_func
|
update_cmd.func = update_func
|
||||||
default_commands.append(update_cmd)
|
default_commands.append(update_cmd)
|
||||||
|
|
@ -1114,13 +1111,13 @@ def remove_items(lib, query, album, delete):
|
||||||
fmt = u'$path - $title'
|
fmt = u'$path - $title'
|
||||||
prompt = 'Really DELETE %i files (y/n)?' % len(items)
|
prompt = 'Really DELETE %i files (y/n)?' % len(items)
|
||||||
else:
|
else:
|
||||||
fmt = None
|
fmt = ''
|
||||||
prompt = 'Really remove %i items from the library (y/n)?' % \
|
prompt = 'Really remove %i items from the library (y/n)?' % \
|
||||||
len(items)
|
len(items)
|
||||||
|
|
||||||
# Show all the items.
|
# Show all the items.
|
||||||
for item in items:
|
for item in items:
|
||||||
ui.print_obj(item, fmt)
|
ui.print_(format(item, fmt))
|
||||||
|
|
||||||
# Confirm with user.
|
# Confirm with user.
|
||||||
if not ui.input_yn(prompt, True):
|
if not ui.input_yn(prompt, True):
|
||||||
|
|
@ -1350,7 +1347,7 @@ modify_cmd.parser.add_option(
|
||||||
)
|
)
|
||||||
modify_cmd.parser.add_option(
|
modify_cmd.parser.add_option(
|
||||||
'-f', '--format', action='store',
|
'-f', '--format', action='store',
|
||||||
help='print with custom format', default=None
|
help='print with custom format', default=''
|
||||||
)
|
)
|
||||||
modify_cmd.func = modify_func
|
modify_cmd.func = modify_func
|
||||||
default_commands.append(modify_cmd)
|
default_commands.append(modify_cmd)
|
||||||
|
|
|
||||||
|
|
@ -356,7 +356,7 @@ class ConvertPlugin(BeetsPlugin):
|
||||||
self.config['pretend'].get(bool)
|
self.config['pretend'].get(bool)
|
||||||
|
|
||||||
if not pretend:
|
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)")):
|
if not (opts.yes or ui.input_yn("Convert? (Y/n)")):
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,14 @@
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
from beets.plugins import BeetsPlugin
|
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
|
from beets.util import command_output, displayable_path, subprocess
|
||||||
|
|
||||||
PLUGIN = 'duplicates'
|
PLUGIN = 'duplicates'
|
||||||
|
|
||||||
|
|
||||||
def _process_item(item, lib, copy=False, move=False, delete=False,
|
def _process_item(item, lib, copy=False, move=False, delete=False,
|
||||||
tag=False, format=None):
|
tag=False, format=''):
|
||||||
"""Process Item `item` in `lib`.
|
"""Process Item `item` in `lib`.
|
||||||
"""
|
"""
|
||||||
if copy:
|
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))
|
raise UserError('%s: can\'t parse k=v tag: %s' % (PLUGIN, tag))
|
||||||
setattr(k, v)
|
setattr(k, v)
|
||||||
item.store()
|
item.store()
|
||||||
print_obj(item, format)
|
print_(format(item, format))
|
||||||
|
|
||||||
|
|
||||||
def _checksum(item, prog, log):
|
def _checksum(item, prog, log):
|
||||||
|
|
@ -126,7 +126,7 @@ class DuplicatesPlugin(BeetsPlugin):
|
||||||
self._command.parser.add_option('-f', '--format', dest='format',
|
self._command.parser.add_option('-f', '--format', dest='format',
|
||||||
action='store', type='string',
|
action='store', type='string',
|
||||||
help='print with custom format',
|
help='print with custom format',
|
||||||
metavar='FMT')
|
metavar='FMT', default='')
|
||||||
|
|
||||||
self._command.parser.add_option('-a', '--album', dest='album',
|
self._command.parser.add_option('-a', '--album', dest='album',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ def similar(lib, src_item, threshold=0.15, fmt='${difference}: ${path}'):
|
||||||
d = diff(item, src_item)
|
d = diff(item, src_item)
|
||||||
if d < threshold:
|
if d < threshold:
|
||||||
s = fmt.replace('${difference}', '{:2.2f}'.format(d))
|
s = fmt.replace('${difference}', '{:2.2f}'.format(d))
|
||||||
ui.print_obj(item, s)
|
ui.print_(format(item, s))
|
||||||
|
|
||||||
|
|
||||||
class EchonestMetadataPlugin(plugins.BeetsPlugin):
|
class EchonestMetadataPlugin(plugins.BeetsPlugin):
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ class MBSyncPlugin(BeetsPlugin):
|
||||||
cmd.parser.add_option('-W', '--nowrite', action='store_false',
|
cmd.parser.add_option('-W', '--nowrite', action='store_false',
|
||||||
default=config['import']['write'], dest='write',
|
default=config['import']['write'], dest='write',
|
||||||
help="don't write updated metadata to files")
|
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')
|
help='print with custom format')
|
||||||
cmd.func = self.func
|
cmd.func = self.func
|
||||||
return [cmd]
|
return [cmd]
|
||||||
|
|
@ -71,7 +71,7 @@ class MBSyncPlugin(BeetsPlugin):
|
||||||
query.
|
query.
|
||||||
"""
|
"""
|
||||||
for item in lib.items(query + ['singleton:true']):
|
for item in lib.items(query + ['singleton:true']):
|
||||||
item_formatted = ui.format_(item, fmt)
|
item_formatted = format(item, fmt)
|
||||||
if not item.mb_trackid:
|
if not item.mb_trackid:
|
||||||
self._log.info(u'Skipping singleton with no mb_trackid: {0}',
|
self._log.info(u'Skipping singleton with no mb_trackid: {0}',
|
||||||
item_formatted)
|
item_formatted)
|
||||||
|
|
@ -96,7 +96,7 @@ class MBSyncPlugin(BeetsPlugin):
|
||||||
"""
|
"""
|
||||||
# Process matching albums.
|
# Process matching albums.
|
||||||
for a in lib.albums(query):
|
for a in lib.albums(query):
|
||||||
album_formatted = ui.format_(a, fmt)
|
album_formatted = format(a, fmt)
|
||||||
if not a.mb_albumid:
|
if not a.mb_albumid:
|
||||||
self._log.info(u'Skipping album with no mb_albumid: {0}',
|
self._log.info(u'Skipping album with no mb_albumid: {0}',
|
||||||
album_formatted)
|
album_formatted)
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
from beets.autotag import hooks
|
from beets.autotag import hooks
|
||||||
from beets.library import Item
|
from beets.library import Item
|
||||||
from beets.plugins import BeetsPlugin
|
from beets.plugins import BeetsPlugin
|
||||||
from beets.ui import decargs, print_obj, Subcommand
|
from beets.ui import decargs, print_, Subcommand
|
||||||
|
|
||||||
|
|
||||||
def _missing_count(album):
|
def _missing_count(album):
|
||||||
|
|
@ -95,7 +95,7 @@ class MissingPlugin(BeetsPlugin):
|
||||||
self._command.parser.add_option('-f', '--format', dest='format',
|
self._command.parser.add_option('-f', '--format', dest='format',
|
||||||
action='store', type='string',
|
action='store', type='string',
|
||||||
help='print with custom FORMAT',
|
help='print with custom FORMAT',
|
||||||
metavar='FORMAT')
|
metavar='FORMAT', default='')
|
||||||
|
|
||||||
self._command.parser.add_option('-c', '--count', dest='count',
|
self._command.parser.add_option('-c', '--count', dest='count',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
|
|
@ -123,13 +123,12 @@ class MissingPlugin(BeetsPlugin):
|
||||||
|
|
||||||
for album in albums:
|
for album in albums:
|
||||||
if count:
|
if count:
|
||||||
missing = _missing_count(album)
|
if _missing_count(album):
|
||||||
if missing:
|
print_(format(album, fmt))
|
||||||
print_obj(album, fmt)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
for item in self._missing(album):
|
for item in self._missing(album):
|
||||||
print_obj(item, fmt)
|
print_(format(item, fmt))
|
||||||
|
|
||||||
self._command.func = _miss
|
self._command.func = _miss
|
||||||
return [self._command]
|
return [self._command]
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,7 @@
|
||||||
"""
|
"""
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from beets.plugins import BeetsPlugin
|
from beets.plugins import BeetsPlugin
|
||||||
from beets.ui import Subcommand, decargs, print_obj
|
from beets.ui import Subcommand, decargs, print_
|
||||||
from beets.util.functemplate import Template
|
|
||||||
import random
|
import random
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
|
|
@ -25,11 +24,7 @@ from itertools import groupby
|
||||||
|
|
||||||
def random_item(lib, opts, args):
|
def random_item(lib, opts, args):
|
||||||
query = decargs(args)
|
query = decargs(args)
|
||||||
if opts.path:
|
fmt = '$path' if opts.path else opts.format
|
||||||
fmt = '$path'
|
|
||||||
else:
|
|
||||||
fmt = opts.format
|
|
||||||
template = Template(fmt) if fmt else None
|
|
||||||
|
|
||||||
if opts.album:
|
if opts.album:
|
||||||
objs = list(lib.albums(query))
|
objs = list(lib.albums(query))
|
||||||
|
|
@ -66,7 +61,7 @@ def random_item(lib, opts, args):
|
||||||
objs = random.sample(objs, number)
|
objs = random.sample(objs, number)
|
||||||
|
|
||||||
for item in objs:
|
for item in objs:
|
||||||
print_obj(item, template)
|
print_(format(item, fmt))
|
||||||
|
|
||||||
random_cmd = Subcommand('random',
|
random_cmd = Subcommand('random',
|
||||||
help='chose a random track or album')
|
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',
|
random_cmd.parser.add_option('-p', '--path', action='store_true',
|
||||||
help='print the path of the matched item')
|
help='print the path of the matched item')
|
||||||
random_cmd.parser.add_option('-f', '--format', action='store',
|
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",
|
random_cmd.parser.add_option('-n', '--number', action='store', type="int",
|
||||||
help='number of objects to choose', default=1)
|
help='number of objects to choose', default=1)
|
||||||
random_cmd.parser.add_option('-e', '--equal-chance', action='store_true',
|
random_cmd.parser.add_option('-e', '--equal-chance', action='store_true',
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ class ListTest(unittest.TestCase):
|
||||||
self.lib.add(self.item)
|
self.lib.add(self.item)
|
||||||
self.lib.add_album([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)
|
commands.list_items(self.lib, query, album, fmt)
|
||||||
|
|
||||||
def test_list_outputs_item(self):
|
def test_list_outputs_item(self):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue