mirror of
https://github.com/beetbox/beets.git
synced 2025-12-25 10:05:13 +01:00
Convert importadded plugin
Many listeners. Everything is now a method
This commit is contained in:
parent
63041736e3
commit
11d5f93db1
1 changed files with 86 additions and 103 deletions
|
|
@ -8,13 +8,10 @@ from __future__ import unicode_literals, absolute_import, print_function
|
|||
|
||||
import os
|
||||
|
||||
from beets import logging
|
||||
from beets import config
|
||||
from beets import util
|
||||
from beets.plugins import BeetsPlugin
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ImportAddedPlugin(BeetsPlugin):
|
||||
def __init__(self):
|
||||
|
|
@ -23,118 +20,104 @@ class ImportAddedPlugin(BeetsPlugin):
|
|||
'preserve_mtimes': False,
|
||||
})
|
||||
|
||||
# item.id for new items that were reimported
|
||||
self.reimported_item_ids = None
|
||||
# album.path for old albums that were replaced by a reimported album
|
||||
self.replaced_album_paths = None
|
||||
# item path in the library to the mtime of the source file
|
||||
self.item_mtime = dict()
|
||||
|
||||
@ImportAddedPlugin.listen('import_task_start')
|
||||
def check_config(task, session):
|
||||
config['importadded']['preserve_mtimes'].get(bool)
|
||||
register = self.register_listener
|
||||
register('import_task_start', self.check_config)
|
||||
register('import_task_start', self.record_if_inplace)
|
||||
register('import_task_files', self.record_reimported)
|
||||
register('before_item_moved', self.record_import_mtime)
|
||||
register('item_copied', self.record_import_mtime)
|
||||
register('item_linked', self.record_import_mtime)
|
||||
register('album_imported', self.update_album_times)
|
||||
register('item_imported', self.update_item_times)
|
||||
|
||||
# item.id for new items that were reimported
|
||||
reimported_item_ids = None
|
||||
def check_config(self, task, session):
|
||||
self.config['preserve_mtimes'].get(bool)
|
||||
|
||||
# album.path for old albums that were replaced by a new reimported album
|
||||
replaced_album_paths = None
|
||||
def reimported_item(self, item):
|
||||
return item.id in self.reimported_item_ids
|
||||
|
||||
def reimported_album(self, album):
|
||||
return album.path in self.replaced_album_paths
|
||||
|
||||
def reimported_item(item):
|
||||
return item.id in reimported_item_ids
|
||||
def record_if_inplace(self, task, session):
|
||||
if not (session.config['copy'] or session.config['move'] or
|
||||
session.config['link']):
|
||||
self._log.debug(u"In place import detected, recording mtimes from "
|
||||
u"source paths")
|
||||
for item in task.items:
|
||||
self.record_import_mtime(item, item.path, item.path)
|
||||
|
||||
def record_reimported(self, task, session):
|
||||
self.reimported_item_ids = set(item.id for item, replaced_items
|
||||
in task.replaced_items.iteritems()
|
||||
if replaced_items)
|
||||
self.replaced_album_paths = set(task.replaced_albums.keys())
|
||||
|
||||
def reimported_album(album):
|
||||
return album.path in replaced_album_paths
|
||||
def write_file_mtime(self, path, mtime):
|
||||
"""Write the given mtime to the destination path.
|
||||
"""
|
||||
stat = os.stat(util.syspath(path))
|
||||
os.utime(util.syspath(path), (stat.st_atime, mtime))
|
||||
|
||||
def write_item_mtime(self, item, mtime):
|
||||
"""Write the given mtime to an item's `mtime` field and to the mtime
|
||||
of the item's file.
|
||||
"""
|
||||
if mtime is None:
|
||||
self._log.warn(u"No mtime to be preserved for item '{0}'",
|
||||
util.displayable_path(item.path))
|
||||
return
|
||||
|
||||
@ImportAddedPlugin.listen('import_task_start')
|
||||
def record_if_inplace(task, session):
|
||||
if not (session.config['copy'] or session.config['move'] or
|
||||
session.config['link']):
|
||||
log.debug(u"In place import detected, recording mtimes from source"
|
||||
u" paths")
|
||||
for item in task.items:
|
||||
record_import_mtime(item, item.path, item.path)
|
||||
# The file's mtime on disk must be in sync with the item's mtime
|
||||
self.write_file_mtime(util.syspath(item.path), mtime)
|
||||
item.mtime = mtime
|
||||
|
||||
def record_import_mtime(self, item, source, destination):
|
||||
"""Record the file mtime of an item's path before its import.
|
||||
"""
|
||||
mtime = os.stat(util.syspath(source)).st_mtime
|
||||
self.item_mtime[destination] = mtime
|
||||
self._log.debug(u"Recorded mtime {0} for item '{1}' imported from "
|
||||
u"'{2}'", mtime, util.displayable_path(destination),
|
||||
util.displayable_path(source))
|
||||
|
||||
@ImportAddedPlugin.listen('import_task_files')
|
||||
def record_reimported(task, session):
|
||||
global reimported_item_ids, replaced_album_paths
|
||||
reimported_item_ids = set([item.id for item, replaced_items
|
||||
in task.replaced_items.iteritems()
|
||||
if replaced_items])
|
||||
replaced_album_paths = set(task.replaced_albums.keys())
|
||||
def update_album_times(self, lib, album):
|
||||
if self.reimported_album(album):
|
||||
self._log.debug(u"Album '{0}' is reimported, skipping import of "
|
||||
u"added dates for the album and its items.",
|
||||
util.displayable_path(album.path))
|
||||
return
|
||||
|
||||
album_mtimes = []
|
||||
for item in album.items():
|
||||
mtime = self.item_mtime.pop(item.path, None)
|
||||
if mtime:
|
||||
album_mtimes.append(mtime)
|
||||
if config['importadded']['preserve_mtimes'].get(bool):
|
||||
self.write_item_mtime(item, mtime)
|
||||
item.store()
|
||||
album.added = min(album_mtimes)
|
||||
self._log.debug(u"Import of album '{0}', selected album.added={1} "
|
||||
u"from item file mtimes.", album.album, album.added)
|
||||
album.store()
|
||||
|
||||
def write_file_mtime(path, mtime):
|
||||
"""Write the given mtime to the destination path.
|
||||
"""
|
||||
stat = os.stat(util.syspath(path))
|
||||
os.utime(util.syspath(path),
|
||||
(stat.st_atime, mtime))
|
||||
|
||||
|
||||
def write_item_mtime(item, mtime):
|
||||
"""Write the given mtime to an item's `mtime` field and to the mtime of the
|
||||
item's file.
|
||||
"""
|
||||
if mtime is None:
|
||||
log.warn(u"No mtime to be preserved for item '{0}'",
|
||||
util.displayable_path(item.path))
|
||||
return
|
||||
|
||||
# The file's mtime on disk must be in sync with the item's mtime
|
||||
write_file_mtime(util.syspath(item.path), mtime)
|
||||
item.mtime = mtime
|
||||
|
||||
|
||||
# key: item path in the library
|
||||
# value: the file mtime of the file the item was imported from
|
||||
item_mtime = dict()
|
||||
|
||||
|
||||
@ImportAddedPlugin.listen('before_item_moved')
|
||||
@ImportAddedPlugin.listen('item_copied')
|
||||
@ImportAddedPlugin.listen('item_linked')
|
||||
def record_import_mtime(item, source, destination):
|
||||
"""Record the file mtime of an item's path before its import.
|
||||
"""
|
||||
mtime = os.stat(util.syspath(source)).st_mtime
|
||||
item_mtime[destination] = mtime
|
||||
log.debug(u"Recorded mtime {0} for item '{1}' imported from '{2}'",
|
||||
mtime, util.displayable_path(destination),
|
||||
util.displayable_path(source))
|
||||
|
||||
|
||||
@ImportAddedPlugin.listen('album_imported')
|
||||
def update_album_times(lib, album):
|
||||
if reimported_album(album):
|
||||
log.debug(u"Album '{0}' is reimported, skipping import of added dates"
|
||||
u" for the album and its items.",
|
||||
util.displayable_path(album.path))
|
||||
return
|
||||
|
||||
album_mtimes = []
|
||||
for item in album.items():
|
||||
mtime = item_mtime.pop(item.path, None)
|
||||
def update_item_times(self, lib, item):
|
||||
if self.reimported_item(item):
|
||||
self._log.debug(u"Item '{0}' is reimported, skipping import of "
|
||||
u"added date.", util.displayable_path(item.path))
|
||||
return
|
||||
mtime = self.item_mtime.pop(item.path, None)
|
||||
if mtime:
|
||||
album_mtimes.append(mtime)
|
||||
item.added = mtime
|
||||
if config['importadded']['preserve_mtimes'].get(bool):
|
||||
write_item_mtime(item, mtime)
|
||||
item.store()
|
||||
album.added = min(album_mtimes)
|
||||
log.debug(u"Import of album '{0}', selected album.added={1} from item"
|
||||
u" file mtimes.", album.album, album.added)
|
||||
album.store()
|
||||
|
||||
|
||||
@ImportAddedPlugin.listen('item_imported')
|
||||
def update_item_times(lib, item):
|
||||
if reimported_item(item):
|
||||
log.debug(u"Item '{0}' is reimported, skipping import of added "
|
||||
u"date.", util.displayable_path(item.path))
|
||||
return
|
||||
mtime = item_mtime.pop(item.path, None)
|
||||
if mtime:
|
||||
item.added = mtime
|
||||
if config['importadded']['preserve_mtimes'].get(bool):
|
||||
write_item_mtime(item, mtime)
|
||||
log.debug(u"Import of item '{0}', selected item.added={1}",
|
||||
util.displayable_path(item.path), item.added)
|
||||
item.store()
|
||||
self.write_item_mtime(item, mtime)
|
||||
self._log.debug(u"Import of item '{0}', selected item.added={1}",
|
||||
util.displayable_path(item.path), item.added)
|
||||
item.store()
|
||||
|
|
|
|||
Loading…
Reference in a new issue