From ba288ce604269d2a68304a27a4e68db83ac98863 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Wed, 8 May 2013 22:56:27 -0700 Subject: [PATCH] move type conversion to commands module (#265) This change also uses the existing str2bool utility function. --- beets/library.py | 23 ----------------------- beets/ui/commands.py | 26 ++++++++++++++++++++++++-- beets/util/__init__.py | 1 - 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/beets/library.py b/beets/library.py index 1ec08c7a1..319599e8e 100644 --- a/beets/library.py +++ b/beets/library.py @@ -159,29 +159,6 @@ ITEM_DEFAULT_FIELDS = ARTIST_DEFAULT_FIELDS + ALBUM_DEFAULT_FIELDS + \ # Special path format key. PF_KEY_DEFAULT = 'default' -# Convert 'true' and 'false' to correspondent boolean -def _str_to_bool(value): - if value.lower() == 'true': - return True - if value.lower() == 'false': - return False - raise ValueError('could not convert string to bool: {0}'.format(value)) - -# Mapping the correspondent Python constructor to SQLite field types -CONSTRUCTOR_MAPPING = { - 'int': int, - 'bool': _str_to_bool, - 'real': float, -} - -# Convert a string (from user input) to the correct Python type -def _convert_type(fields, key, value): - sqlite_type = [f[1] for f in fields if f[0] == key][0] - if value and sqlite_type in CONSTRUCTOR_MAPPING: - constructor = CONSTRUCTOR_MAPPING[sqlite_type] - return constructor(value) - return value - # Logger. log = logging.getLogger('beets') if not log.handlers: diff --git a/beets/ui/commands.py b/beets/ui/commands.py index e9e36d83e..be5cd811c 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -31,6 +31,7 @@ from beets import autotag from beets.autotag import recommendation from beets import plugins from beets import importer +from beets import util from beets.util import syspath, normpath, ancestry, displayable_path from beets.util.functemplate import Template from beets import library @@ -987,17 +988,38 @@ default_commands.append(version_cmd) # modify: Declaratively change metadata. +# Mapping from SQLite type names to conversion functions. +CONSTRUCTOR_MAPPING = { + 'int': int, + 'bool': util.str2bool, + 'real': float, +} + +# Convert a string (from user input) to the correct Python type +def _convert_type(key, value, album=False): + """Convert a string to the appropriate type for the given field. + `album` indicates whether to use album or item field definitions. + """ + fields = library.ALBUM_FIELDS if album else library.ITEM_FIELDS + sqlite_type = [f[1] for f in fields if f[0] == key][0] + if value and sqlite_type in CONSTRUCTOR_MAPPING: + constructor = CONSTRUCTOR_MAPPING[sqlite_type] + try: + return constructor(value) + except ValueError: + raise ui.UserError(u'wrong type for {0}: {1}'.format(key, value)) + return value + def modify_items(lib, mods, query, write, move, album, confirm): """Modifies matching items according to key=value assignments.""" # Parse key=value specifications into a dictionary. - fields = library.ALBUM_FIELDS if album else library.ITEM_FIELDS allowed_keys = library.ALBUM_KEYS if album else library.ITEM_KEYS_WRITABLE fsets = {} for mod in mods: key, value = mod.split('=', 1) if key not in allowed_keys: raise ui.UserError('"%s" is not a valid field' % key) - fsets[key] = library._convert_type(fields, key, value) + fsets[key] = _convert_type(key, value, album) # Get the items to modify. items, albums = _do_query(lib, query, album, False) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index e5f4eec09..cb1c12eaf 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -152,7 +152,6 @@ def sorted_walk(path, ignore=(), logger=None): try: contents = os.listdir(syspath(path)) except OSError as exc: - print('foo', logger, bool(logger)) if logger: logger.warn(u'could not list directory {0}: {1}'.format( displayable_path(path), exc.strerror