diff --git a/beets/library.py b/beets/library.py index 65c27a0c9..1ec08c7a1 100644 --- a/beets/library.py +++ b/beets/library.py @@ -159,6 +159,28 @@ 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') @@ -642,7 +664,7 @@ class AnyFieldQuery(CollectionQuery): if subq.match(item): return True return False - + class MutableCollectionQuery(CollectionQuery): """A collection query whose subqueries may be modified after the query is initialized. diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 5560e1d79..e9e36d83e 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -990,13 +990,14 @@ default_commands.append(version_cmd) 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] = value + fsets[key] = library._convert_type(fields, key, value) # Get the items to modify. items, albums = _do_query(lib, query, album, False)