Merge pull request #638 from brilnius/ui_del_attr2

Modify command: ability to delete/reset an attribute (beet modify field!)
This commit is contained in:
brilnius 2014-04-01 22:47:03 +02:00
commit d02e02fe20
5 changed files with 39 additions and 20 deletions

View file

@ -1082,7 +1082,7 @@ default_commands.append(version_cmd)
# modify: Declaratively change metadata.
def modify_items(lib, mods, query, write, move, album, confirm):
def modify_items(lib, mods, dels, query, write, move, album, confirm):
"""Modifies matching items according to key=value assignments."""
# Parse key=value specifications into a dictionary.
model_cls = library.Album if album else library.Item
@ -1102,6 +1102,8 @@ def modify_items(lib, mods, query, write, move, album, confirm):
for obj in objs:
for field, value in fsets.iteritems():
obj[field] = value
for field in dels:
del obj[field]
if ui.show_model_changes(obj):
changed.add(obj)
@ -1155,13 +1157,22 @@ modify_cmd.parser.add_option('-f', '--format', action='store',
help='print with custom format', default=None)
def modify_func(lib, opts, args):
args = decargs(args)
mods = [a for a in args if '=' in a]
query = [a for a in args if '=' not in a]
if not mods:
mods = []
dels = []
query = []
for arg in args:
if arg.endswith('!') and '=' not in arg and ':' not in arg:
dels.append(arg[:-1])
elif '=' in arg:
mods.append(arg)
else:
query.append(arg)
if not mods and not dels:
raise ui.UserError('no modifications specified')
write = opts.write if opts.write is not None else \
config['import']['write'].get(bool)
modify_items(lib, mods, query, write, opts.move, opts.album, not opts.yes)
modify_items(lib, mods, dels, query, write, opts.move, opts.album,
not opts.yes)
modify_cmd.func = modify_func
default_commands.append(modify_cmd)

View file

@ -32,8 +32,10 @@ New stuff:
* We now only use "primary" aliases for artist names from MusicBrainz. This
eliminates some strange naming that could occur when the `languages` config
option was set. Thanks to Filipe Fortes.
* The :ref:`modify-cmd` command now allows removing flexible attribute. For
example: ``beet modify artist:beatles oldies!`` deletes the ``oldies``
flexible attribute from the database, for the matching items. Thanks to
brilnius.
Fixes:

View file

@ -111,9 +111,9 @@ Store any data you like
The beets database keeps track of a long list of :ref:`built-in fields
<itemfields>`, but you're not limited to just that list. Say, for example,
that you like to like to categorize your music by the setting where it should
be played. You can invent a new ``context`` attribute store this. Set the
field using the :ref:`modify-cmd` command::
that you like to categorize your music by the setting where it should be
played. You can invent a new ``context`` attribute store this. Set the field
using the :ref:`modify-cmd` command::
beet modify context=party artist:'beastie boys'
@ -125,6 +125,10 @@ other field::
You can even use these fields in your filenames (see
:ref:`path-format-config`).
And, unlike :ref:`built-in fields <itemfields>`, such fields can be removed::
beet modify context! artist:'beastie boys'
Read more than you ever wanted to know about the *flexible attributes*
feature `on the beets blog`_.

View file

@ -201,20 +201,21 @@ modify
``````
::
beet modify [-MWay] QUERY FIELD=VALUE...
beet modify [-MWay] QUERY [FIELD=VALUE...] [FIELD!...]
Change the metadata for items or albums in the database.
Supply a :doc:`query <query>` matching the things you want to change and a
series of ``field=value`` pairs. For example, ``beet modify genius of love
artist="Tom Tom Club"`` will change the artist for the track "Genius of Love."
The ``-a`` switch operates on albums instead of individual tracks. Items will
automatically be moved around when necessary if they're in your library
directory, but you can disable that with ``-M``. Tags will be written to the
files according to the settings you have for imports, but these can be
overridden with ``-w`` (write tags, the default) and ``-W`` (don't write tags).
Finally, this command politely asks for your permission before making any
changes, but you can skip that prompt with the ``-y`` switch.
For removing fields (only possible with flexible attributes), specify one or
more ``field!`` argument(s). The ``-a`` switch operates on albums instead of
individual tracks. Items will automatically be moved around when necessary if
they're in your library directory, but you can disable that with ``-M``. Tags
will be written to the files according to the settings you have for imports,
but these can be overridden with ``-w`` (write tags, the default) and ``-W``
(don't write tags). Finally, this command politely asks for your permission
before making any changes, but you can skip that prompt with the ``-y`` switch.
.. _move-cmd:

View file

@ -157,9 +157,10 @@ class ModifyTest(_common.TestCase):
self.i.move(True)
self.album = self.lib.add_album([self.i])
def _modify(self, mods, query=(), write=False, move=False, album=False):
def _modify(self, mods, dels=(), query=(), write=False, move=False,
album=False):
self.io.addinput('y')
commands.modify_items(self.lib, mods, query,
commands.modify_items(self.lib, mods, dels, query,
write, move, album, True)
def test_modify_item_dbdata(self):