| 1 |
"""Populate an item's `added` and `mtime` fields by using the file |
| 2 |
modification time (mtime) of the item's source file before import. |
| 3 |
|
| 4 |
Reimported albums and items are skipped. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import os |
| 10 |
from typing import TYPE_CHECKING |
| 11 |
|
| 12 |
from beets import importer, util |
| 13 |
from beets.plugins import BeetsPlugin |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from beets.importer import ImportSession, ImportTask |
| 17 |
from beets.library import Album, Item, Library |
| 18 |
|
| 19 |
|
| 20 |
class ImportAddedPlugin(BeetsPlugin): |
| 21 |
item_mtime: dict[bytes, float] |
| 22 |
reimported_item_ids: set[int | None] |
| 23 |
replaced_album_paths: set[bytes] |
| 24 |
|
| 25 |
def __init__(self) -> None: |
| 26 |
super().__init__() |
| 27 |
self.config.add( |
| 28 |
{"preserve_mtimes": False, "preserve_write_mtimes": False} |
| 29 |
) |
| 30 |
|
| 31 |
# item.id for new items that were reimported |
| 32 |
self.reimported_item_ids = set() |
| 33 |
# album.path for old albums that were replaced by a reimported album |
| 34 |
self.replaced_album_paths = set() |
| 35 |
# item path in the library to the mtime of the source file |
| 36 |
self.item_mtime = {} |
| 37 |
|
| 38 |
register = self.register_listener |
| 39 |
register("import_task_created", self.check_config) |
| 40 |
register("import_task_created", self.record_if_inplace) |
| 41 |
register("import_task_files", self.record_reimported) |
| 42 |
register("before_item_moved", self.record_import_mtime) |
| 43 |
register("item_copied", self.record_import_mtime) |
| 44 |
register("item_linked", self.record_import_mtime) |
| 45 |
register("item_hardlinked", self.record_import_mtime) |
| 46 |
register("item_reflinked", self.record_import_mtime) |
| 47 |
register("album_imported", self.update_album_times) |
| 48 |
register("item_imported", self.update_item_times) |
| 49 |
register("after_write", self.update_after_write_time) |
| 50 |
|
| 51 |
def check_config( |
| 52 |
self, task: ImportTask, session: ImportSession |
| 53 |
) -> list[ImportTask] | None: |
| 54 |
self.config["preserve_mtimes"].get(bool) |
| 55 |
return None |
| 56 |
|
| 57 |
def reimported_item(self, item: Item) -> bool: |
| 58 |
return item.id in self.reimported_item_ids |
| 59 |
|
| 60 |
def reimported_album(self, album: Album) -> bool: |
| 61 |
return album.path in self.replaced_album_paths |
| 62 |
|
| 63 |
def record_if_inplace( |
| 64 |
self, task: ImportTask, session: ImportSession |
| 65 |
) -> list[ImportTask] | None: |
| 66 |
if not ( |
| 67 |
session.config["copy"] |
| 68 |
or session.config["move"] |
| 69 |
or session.config["link"] |
| 70 |
or session.config["hardlink"] |
| 71 |
or session.config["reflink"] |
| 72 |
): |
| 73 |
self._log.debug( |
| 74 |
"In place import detected, recording mtimes from source paths" |
| 75 |
) |
| 76 |
items = ( |
| 77 |
[task.item] |
| 78 |
if isinstance(task, importer.SingletonImportTask) |
| 79 |
else task.items |
| 80 |
) |
| 81 |
for item in items: |
| 82 |
self.record_import_mtime(item, item.path, item.path) |
| 83 |
|
| 84 |
return None |
| 85 |
|
| 86 |
def record_reimported( |
| 87 |
self, task: ImportTask, session: ImportSession |
| 88 |
) -> None: |
| 89 |
self.reimported_item_ids = { |
| 90 |
item.id |
| 91 |
for item, replaced_items in task.replaced_items.items() |
| 92 |
if replaced_items |
| 93 |
} |
| 94 |
self.replaced_album_paths = set(task.replaced_albums.keys()) |
| 95 |
|
| 96 |
def write_file_mtime(self, path: str, mtime: float) -> None: |
| 97 |
"""Write the given mtime to the destination path.""" |
| 98 |
stat = os.stat(util.syspath(path)) |
| 99 |
os.utime(util.syspath(path), (stat.st_atime, mtime)) |
| 100 |
|
| 101 |
def write_item_mtime(self, item: Item, mtime: float) -> None: |
| 102 |
"""Write the given mtime to an item's `mtime` field and to the mtime |
| 103 |
of the item's file. |
| 104 |
""" |
| 105 |
# The file's mtime on disk must be in sync with the item's mtime |
| 106 |
self.write_file_mtime(util.syspath(item.path), mtime) |
| 107 |
item.mtime = int(mtime) |
| 108 |
|
| 109 |
def record_import_mtime( |
| 110 |
self, item: Item, source: bytes, destination: bytes |
| 111 |
) -> None: |
| 112 |
"""Record the file mtime of an item's path before its import.""" |
| 113 |
mtime = os.stat(util.syspath(source)).st_mtime |
| 114 |
self.item_mtime[destination] = mtime |
| 115 |
self._log.debug( |
| 116 |
"Recorded mtime {} for item '{}' imported from '{}'", |
| 117 |
mtime, |
| 118 |
util.displayable_path(destination), |
| 119 |
util.displayable_path(source), |
| 120 |
) |
| 121 |
|
| 122 |
def update_album_times(self, lib: Library, album: Album) -> None: |
| 123 |
if self.reimported_album(album): |
| 124 |
self._log.debug( |
| 125 |
"Album '{.filepath}' is reimported, skipping import of " |
| 126 |
"added dates for the album and its items.", |
| 127 |
album, |
| 128 |
) |
| 129 |
return |
| 130 |
|
| 131 |
album_mtimes = [] |
| 132 |
for item in album.items(): |
| 133 |
mtime = self.item_mtime.pop(item.path, None) |
| 134 |
if mtime: |
| 135 |
album_mtimes.append(mtime) |
| 136 |
if self.config["preserve_mtimes"].get(bool): |
| 137 |
self.write_item_mtime(item, mtime) |
| 138 |
item.store() |
| 139 |
album.added = min(album_mtimes) |
| 140 |
self._log.debug( |
| 141 |
"Import of album '{0.album}', selected album.added={0.added} " |
| 142 |
"from item file mtimes.", |
| 143 |
album, |
| 144 |
) |
| 145 |
album.store() |
| 146 |
|
| 147 |
def update_item_times(self, lib: Library, item: Item) -> None: |
| 148 |
if self.reimported_item(item): |
| 149 |
self._log.debug( |
| 150 |
"Item '{.filepath}' is reimported, skipping import of added date.", |
| 151 |
item, |
| 152 |
) |
| 153 |
return |
| 154 |
mtime = self.item_mtime.pop(item.path, None) |
| 155 |
if mtime: |
| 156 |
item.added = mtime |
| 157 |
if self.config["preserve_mtimes"].get(bool): |
| 158 |
self.write_item_mtime(item, mtime) |
| 159 |
self._log.debug( |
| 160 |
"Import of item '{0.filepath}', selected item.added={0.added}", |
| 161 |
item, |
| 162 |
) |
| 163 |
item.store() |
| 164 |
|
| 165 |
def update_after_write_time(self, item: Item, path: bytes) -> None: |
| 166 |
"""Update the mtime of the item's file with the item.added value |
| 167 |
after each write of the item if `preserve_write_mtimes` is enabled. |
| 168 |
""" |
| 169 |
if item.added: |
| 170 |
if self.config["preserve_write_mtimes"].get(bool): |
| 171 |
self.write_item_mtime(item, item.added) |
| 172 |
self._log.debug( |
| 173 |
"Write of item '{0.filepath}', selected item.added={0.added}", |
| 174 |
item, |
| 175 |
) |