refactoring according to feedback in pull request

This commit is contained in:
Bart Kleijngeld 2017-06-12 16:46:09 +02:00
parent c64878179e
commit 52d5d2310b
5 changed files with 35 additions and 35 deletions

View file

@ -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',)

View file

@ -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'
)

View file

@ -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`

View file

@ -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.

View file

@ -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.