From 314438b8fabb0ccf3549077c2e1269ac8a2e3f7b Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 25 Nov 2013 15:26:11 -0800 Subject: [PATCH] slightly simpler "write" command (#328, #448) --- beets/ui/commands.py | 55 +++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 7b05be5c5..a3d3b6ef8 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -1231,50 +1231,53 @@ def move_func(lib, opts, args): move_cmd.func = move_func default_commands.append(move_cmd) -# write: write tags into every item matching a query. + +# write: Write tags into files. def write_items(lib, query, pretend): - """Write tag information from the database to the respective - files in the filesystem.""" + """Write tag information from the database to the respective files + in the filesystem. + """ items, albums = _do_query(lib, query, False, False) for item in items: # Item deleted? if not os.path.exists(syspath(item.path)): + log.info(u'missing file: {0}'.format( + util.displayable_path(item.path) + )) continue - # Read new data. - new_data = dict(item) + # Get an Item object reflecting the "clean" (on-disk) state. try: - item.read() + clean_item = library.Item.from_path(item.path) except Exception as exc: log.error(u'error reading {0}: {1}'.format( - displayable_path(item.path), exc)) + displayable_path(item.path), exc + )) continue - # Get and save metadata changes. - changes = {} + # Get the keys that have changed between the version. + changed = set() for key in library.ITEM_KEYS_META: - if key in item._dirty: - changes[key] = getattr(item, key), new_data[key] - setattr(item, key, new_data[key]); + if item[key] != clean_item[key]: + changed.add(key) - if changes: - # Something changed. + # Did anything change? + if changed: ui.print_obj(item, lib) - for key, (oldval, newval) in changes.iteritems(): - _showdiff(key, oldval, newval) + for key in changed: + _showdiff(key, clean_item[key], item[key]) - # If we're just pretending, then don't move or save. - if pretend: - continue - - try: - item.write() - except Exception as exc: - log.error(u'could not write {0}: {1}'.format( - util.displayable_path(item.path), exc)) - continue + # Actually write the tags. + if not pretend: + try: + item.write() + except Exception as exc: + log.error(u'could not write {0}: {1}'.format( + util.displayable_path(item.path), exc + )) + continue write_cmd = ui.Subcommand('write', help='write tag information to files') write_cmd.parser.add_option('-p', '--pretend', action='store_true',