From 1da972f4bb28569b1bed4676cb4302aabe5baf8d Mon Sep 17 00:00:00 2001 From: Bart Kleijngeld Date: Tue, 30 May 2017 16:15:28 +0200 Subject: [PATCH] implemented `set_field` cli parsing --- beets/config_default.yaml | 3 ++- beets/ui/__init__.py | 18 ++++++++++++++++++ beets/ui/commands.py | 6 ++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/beets/config_default.yaml b/beets/config_default.yaml index b7e6b1e2b..439a93f55 100644 --- a/beets/config_default.yaml +++ b/beets/config_default.yaml @@ -25,7 +25,8 @@ import: pretend: false search_ids: [] duplicate_action: ask - bell: no + bell: no + set_fields: {} clutter: ["Thumbs.DB", ".DS_Store"] ignore: [".*", "*~", "System Volume Information", "lost+found"] diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 60652fa08..0a215535b 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -1060,6 +1060,24 @@ 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) + + key, value = value.split('=') + option_values[key] = value + optparse.Option.ALWAYS_TYPED_ACTIONS += ('callback',) diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 995ff87e9..39e2bcb45 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -40,6 +40,7 @@ from beets import config from beets import logging from beets.util.confit import _package_path import six +from . import SubcommandsOptionParser VARIOUS_ARTISTS = u'Various Artists' PromptChoice = namedtuple('PromptChoice', ['short', 'long', 'callback']) @@ -1017,6 +1018,11 @@ import_cmd.parser.add_option( metavar='ID', 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, + metavar='FIELD=VALUE', + help=u'set the given fields to the supplied values after importing' +) import_cmd.func = import_func default_commands.append(import_cmd)