From c7f4b8a0958fdc19443d5732efa31d10aa3eb903 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Sun, 5 May 2013 20:22:38 -0300 Subject: [PATCH 1/2] "Modify" for non-string values Added the possibility to modify float, integer e bool values (using explicitly true/false) --- beets/library.py | 24 +++++++++++++++++++++++- beets/ui/commands.py | 3 ++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/beets/library.py b/beets/library.py index 65c27a0c9..8ec588451 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 +TYPE_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 TYPE_MAPPING: + python_type = TYPE_MAPPING[sqlite_type] + return python_type(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 f13232808..a91215666 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -989,13 +989,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) From e72fcc4b68541a4baa30c3825797610c9dc76ff7 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Sun, 5 May 2013 20:32:33 -0300 Subject: [PATCH 2/2] Changing to some more meaningful names --- beets/library.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/beets/library.py b/beets/library.py index 8ec588451..1ec08c7a1 100644 --- a/beets/library.py +++ b/beets/library.py @@ -168,7 +168,7 @@ def _str_to_bool(value): raise ValueError('could not convert string to bool: {0}'.format(value)) # Mapping the correspondent Python constructor to SQLite field types -TYPE_MAPPING = { +CONSTRUCTOR_MAPPING = { 'int': int, 'bool': _str_to_bool, 'real': float, @@ -177,9 +177,9 @@ TYPE_MAPPING = { # 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 TYPE_MAPPING: - python_type = TYPE_MAPPING[sqlite_type] - return python_type(value) + if value and sqlite_type in CONSTRUCTOR_MAPPING: + constructor = CONSTRUCTOR_MAPPING[sqlite_type] + return constructor(value) return value # Logger.