A new should_write configuration wrapper

The idea is that it is so common to check whether we need to write tags (or
move files), and we're constantly re-implementing the same logic everywhere.
It's not even the prettiest logic, as it commingles the importer settings with
general settings. So it's important that we encapsulate the decision so we can
make it better in the future.
This commit is contained in:
Adrian Sampson 2015-11-07 13:14:33 -08:00
parent cb6edb46ef
commit 5b761f029e
2 changed files with 28 additions and 5 deletions

View file

@ -70,7 +70,7 @@ class UserError(Exception):
"""
# Utilities.
# Encoding utilities.
def _out_encoding():
"""Get the encoding to use for *outputting* strings to the console.
@ -137,6 +137,27 @@ def print_(*strings, **kwargs):
sys.stdout.write(txt)
# Configuration wrappers.
def _bool_fallback(opt, view):
"""Given a boolean or None, return the original value or a
configuration option as a fallback.
"""
if opt is None:
return view.get(bool)
else:
return opt
def should_write(write_opt):
"""Decide whether a command that updates metadata should also write tags,
using the importer configuration as the default.
"""
return _bool_fallback(write_opt, config['import']['write'])
# Input prompts.
def input_(prompt=None):
"""Like `raw_input`, but decodes the result to a Unicode string.
Raises a UserError if stdin is not available. The prompt is sent to
@ -327,6 +348,8 @@ def input_yn(prompt, require=False):
return sel == 'y'
# Human output formatting.
def human_bytes(size):
"""Formats size, a number of bytes, in a human-readable way."""
powers = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'H']
@ -374,6 +397,8 @@ def human_seconds_short(interval):
return u'%i:%02i' % (interval // 60, interval % 60)
# Colorization.
# ANSI terminal colorization code heavily inspired by pygments:
# http://dev.pocoo.org/hg/pygments-main/file/b2deea5b5030/pygments/console.py
# (pygments is by Tim Hatch, Armin Ronacher, et al.)

View file

@ -1293,10 +1293,8 @@ def modify_func(lib, opts, args):
query, mods, dels = modify_parse_args(decargs(args))
if not mods and not dels:
raise ui.UserError('no modifications specified')
write = opts.write if opts.write is not None else \
config['import']['write'].get(bool)
modify_items(lib, mods, dels, query, write, opts.move, opts.album,
not opts.yes)
modify_items(lib, mods, dels, query, ui.should_write(opts.write),
opts.move, opts.album, not opts.yes)
modify_cmd = ui.Subcommand(