Combine try_write() and store() as try_sync()

This makes sure we store the updated file `mtime`. By providing the
same interface on `Album` and `item` we can also reduce some code
duplication in the `modify` command.
This commit is contained in:
Thomas Scholtes 2014-09-14 14:44:32 +02:00
parent 84acf17855
commit 1613a199fa
2 changed files with 30 additions and 13 deletions

View file

@ -470,6 +470,21 @@ class Item(LibModel):
log.error(exc)
return False
def try_sync(self, write=None):
"""Synchronizes the current state with the database and the
media file tags.
If `write` is `None` or `True` the method tries to write the
tags to `self.path`. If `write` is `False` it does not write
tags. Otherwise it interprets `write` as a path and tries to
write the tags to that file.
"""
if write is True:
write = None
if write is not False:
self.try_write(path=write)
self.store()
# Files themselves.
def move_file(self, dest, copy=False):
@ -882,6 +897,18 @@ class Album(LibModel):
item[key] = value
item.store()
def try_sync(self, write=True):
"""Synchronizes the current state with the database, propagates
it to the items and synchronizes them with the database and
their files.
The `write` parameter is a boolean indicating whether to write
tags to the item files.
"""
self.store()
for item in self.items():
item.try_sync(bool(write))
# Query construction helper.

View file

@ -20,7 +20,6 @@ from __future__ import print_function
import logging
import os
import time
import itertools
import codecs
import platform
import re
@ -1298,7 +1297,7 @@ def modify_items(lib, mods, dels, query, write, move, album, confirm):
if not ui.input_yn('Really modify%s (Y/n)?' % extra):
return
# Apply changes to database.
# Apply changes to database and files
with lib.transaction():
for obj in changed:
if move:
@ -1307,16 +1306,7 @@ def modify_items(lib, mods, dels, query, write, move, album, confirm):
log.debug('moving object %s' % cur_path)
obj.move()
obj.store()
# Apply tags if requested.
if write:
if album:
changed_items = itertools.chain(*(a.items() for a in changed))
else:
changed_items = changed
for item in changed_items:
item.try_write()
obj.try_sync(write)
def modify_parse_args(args):
@ -1457,7 +1447,7 @@ def write_items(lib, query, pretend, force):
changed = ui.show_model_changes(item, clean_item,
library.Item._media_fields, force)
if (changed or force) and not pretend:
item.try_write()
item.try_sync()
def write_func(lib, opts, args):