From 52d5d2310b74d562faeca653b3d1e146d8df6b97 Mon Sep 17 00:00:00 2001 From: Bart Kleijngeld Date: Mon, 12 Jun 2017 16:46:09 +0200 Subject: [PATCH] refactoring according to feedback in pull request --- beets/ui/__init__.py | 53 ++++++++++++++++++++------------------- beets/ui/commands.py | 6 ++--- docs/changelog.rst | 2 +- docs/reference/cli.rst | 7 +++--- docs/reference/config.rst | 2 +- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 2cea00be7..8b0d57106 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -769,6 +769,33 @@ def show_path_changes(path_changes): pad = max_width - len(source) log.info(u'{0} {1} -> {2}', source, ' ' * pad, dest) +# Helper functions for option parsing. + +def _store_dict(option, opt_str, value, parser): + """Custom action callback to parse options which have ``key=value`` + pairs as values. All such pairs passed for this option are + aggregated into a dictionary. + """ + dest = option.dest + option_values = getattr(parser.values, dest, None) + + if option_values is None: + # This is the first supplied ``key=value`` pair of option. + # Initialize empty dictionary and get a reference to it. + setattr(parser.values, dest, dict()) + option_values = getattr(parser.values, dest) + + try: + key, value = map(lambda s: util.text_string(s), value.split('=')) + if not (key and value): + raise ValueError + except ValueError: + raise UserError( + "supplied argument `{0}' is not of the form `key=value'" + .format(value)) + + option_values[key] = value + class CommonOptionsParser(optparse.OptionParser, object): """Offers a simple way to add common formatting options. @@ -1060,32 +1087,6 @@ class SubcommandsOptionParser(CommonOptionsParser): suboptions, subargs = subcommand.parse_args(args) return subcommand, suboptions, subargs - @staticmethod - def _store_dict(option, opt_str, value, parser): - """Custom action callback to parse options which have ``key=value`` - pairs as values. All such pairs passed for this option are - aggregated into a dictionary. - """ - dest = option.dest - option_values = getattr(parser.values, dest, None) - - if option_values is None: - # This is the first supplied ``key=value`` pair of option. - # Initialize empty dictionary and get a reference to it. - setattr(parser.values, dest, dict()) - option_values = getattr(parser.values, dest) - - try: - key, value = map(lambda s: util.text_string(s), value.split('=')) - if not (key and value): - raise ValueError - except ValueError: - raise UserError( - "supplied argument `{0}' is not of the form `key=value'" - .format(value)) - - option_values[key] = value - optparse.Option.ALWAYS_TYPED_ACTIONS += ('callback',) diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 240c776ee..0003efc5d 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -40,7 +40,7 @@ from beets import config from beets import logging from beets.util.confit import _package_path import six -from . import SubcommandsOptionParser +from . import _store_dict VARIOUS_ARTISTS = u'Various Artists' PromptChoice = namedtuple('PromptChoice', ['short', 'long', 'callback']) @@ -1019,8 +1019,8 @@ import_cmd.parser.add_option( help=u'restrict matching to a specific metadata backend ID' ) import_cmd.parser.add_option( - u'--set-field', dest='set_fields', action='callback', - callback=SubcommandsOptionParser._store_dict, + u'--set', dest='set_fields', action='callback', + callback=_store_dict, metavar='FIELD=VALUE', help=u'set the given fields to the supplied values' ) diff --git a/docs/changelog.rst b/docs/changelog.rst index 0da327ac4..6fcdb5792 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -63,7 +63,7 @@ New features: :bug:`2519` :bug:`2529` * It is now possible to set fields to certain values during import, using either the `importer.set_fields` dictionary in the config file, or by - passing one or more `--set-field field=value` options on the command-line. + passing one or more `--set field=value` options on the command-line. :bug: `1881` diff --git a/docs/reference/cli.rst b/docs/reference/cli.rst index b9f29c9c9..38d4f40ca 100644 --- a/docs/reference/cli.rst +++ b/docs/reference/cli.rst @@ -138,13 +138,12 @@ Optional command flags: searching for other candidates by using the ``--search-id SEARCH_ID`` option. Multiple IDs can be specified by simply repeating the option several times. -* You can supply ``--set-field`` options with ``field=value`` pairs to assign to +* You can supply ``--set`` options with ``field=value`` pairs to assign to those fields the specified values on import, in addition to such field/value pairs defined in the ``importer.set_fields`` dictionary in the configuration - file. Make sure to use an option per field/value pair, like so: - :: + file. Make sure to use an option per field/value pair, like so:: - beet import --set-field genre="Alternative Rock" --set-field mood="emotional" + beet import --set genre="Alternative Rock" --set mood="emotional" Note that values for the fields specified on the command-line override the ones defined for those fields in the configuration file. diff --git a/docs/reference/config.rst b/docs/reference/config.rst index 1018b9bb4..bcd30169b 100644 --- a/docs/reference/config.rst +++ b/docs/reference/config.rst @@ -600,7 +600,7 @@ Example: :: genre: 'To Listen' collection: 'Unordered' -Note that field/value pairs supplied via ``--set-field`` options on the +Note that field/value pairs supplied via ``--set`` options on the command-line are processed in addition to those specified here. Those values override the ones defined here in the case of fields with the same name.