From 5b761f029e72d67b4229585bcfeb0004ea9410e5 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 7 Nov 2015 13:14:33 -0800 Subject: [PATCH] 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. --- beets/ui/__init__.py | 27 ++++++++++++++++++++++++++- beets/ui/commands.py | 6 ++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 768eb76c7..a4c4c376b 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -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.) diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 348d12c88..69213ea97 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -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(