/
alternatives.py
479 lines (401 loc) · 16.3 KB
/
alternatives.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# Copyright (c) 2014 Thomas Scholtes
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
import argparse
import os.path
import threading
import traceback
from concurrent import futures
from enum import Enum
from typing import Iterator, List, Optional, Tuple
import beets
from beets import art, util
from beets.library import Item, Library, parse_query_string
from beets.plugins import BeetsPlugin
from beets.ui import Subcommand, UserError, decargs, get_path_formats, input_yn, print_
from beets.util import FilesystemError, bytestring_path, displayable_path, syspath
from typing_extensions import override
import beetsplug.convert as convert
def _remove(path, soft=True):
"""Remove the file. If `soft`, then no error will be raised if the
file does not exist.
In contrast to beets' util.remove, this uses lexists such that it can
actually remove symlink links.
"""
path = syspath(path)
if soft and not os.path.lexists(path):
return
try:
os.remove(path)
except OSError as exc:
raise FilesystemError(exc, "delete", (path,), traceback.format_exc()) from exc
class AlternativesPlugin(BeetsPlugin):
def __init__(self):
super().__init__()
def commands(self):
return [AlternativesCommand(self)]
def update(self, lib: Library, options: argparse.Namespace):
if options.name is None:
if not options.all:
raise UserError("Please specify a collection name or the --all flag")
for name in self.config.keys(): # noqa: SIM118
self.alternative(name, lib).update(create=options.create)
else:
try:
alt = self.alternative(options.name, lib)
except KeyError as e:
raise UserError(
f"Alternative collection '{e.args[0]}' not found."
) from e
alt.update(create=options.create)
def list_tracks(self, lib, options):
if options.format is not None:
(fmt,) = decargs([options.format])
beets.config[Item._format_config_key].set(fmt)
alt = self.alternative(options.name, lib)
# This is slow but we cannot use a native SQL query since the
# path key is a flexible attribute
for item in lib.items():
if alt.path_key in item:
print_(format(item))
def alternative(self, name: str, lib: Library):
conf = self.config[name]
if not conf.exists():
raise KeyError(name)
if conf["formats"].exists():
fmt = conf["formats"].as_str()
assert isinstance(fmt, str)
if fmt == "link":
return SymlinkView(self._log, name, lib, conf)
else:
return ExternalConvert(self._log, name, fmt.split(), lib, conf)
else:
return External(self._log, name, lib, conf)
class AlternativesCommand(Subcommand):
name = "alt"
help = "manage alternative files"
def __init__(self, plugin):
parser = ArgumentParser()
subparsers = parser.add_subparsers(prog=parser.prog + " alt")
subparsers.required = True
update = subparsers.add_parser("update")
update.set_defaults(func=plugin.update)
update.add_argument(
"name",
metavar="NAME",
nargs="?",
help="Name of the collection. Must be provided unless --all is given",
)
update.add_argument("--create", action="store_const", dest="create", const=True)
update.add_argument(
"--no-create", action="store_const", dest="create", const=False
)
update.add_argument(
"--all",
action="store_true",
default=False,
help="Update all alternative collections that are defined in the configuration",
)
list_tracks = subparsers.add_parser(
"list-tracks",
description="""
List all tracks that are currently part of an alternative
collection""",
)
list_tracks.set_defaults(func=plugin.list_tracks)
list_tracks.add_argument(
"name",
metavar="NAME",
help="Name of the alternative",
)
list_tracks.add_argument(
"-f",
"--format",
metavar="FORMAT",
dest="format",
help="""Format string to print for each track. See beets’
Path Formats for more information.""",
)
super().__init__(self.name, parser, self.help)
def func(self, lib, opts, _):
opts.func(lib, opts)
def parse_args(self, args):
return self.parser.parse_args(args), []
class ArgumentParser(argparse.ArgumentParser):
"""
Facade for ``argparse.ArgumentParser`` so that beets can call
`_get_all_options()` to generate shell completion.
"""
def _get_all_options(self):
# FIXME return options like ``OptionParser._get_all_options``.
return []
class Action(Enum):
ADD = 1
REMOVE = 2
WRITE = 3
MOVE = 4
SYNC_ART = 5
class External:
def __init__(self, log, name, lib, config):
self._log = log
self.name = name
self.lib = lib
self.path_key = f"alt.{name}"
self.max_workers = int(str(beets.config["convert"]["threads"]))
self.parse_config(config)
def parse_config(self, config):
if "paths" in config:
path_config = config["paths"]
else:
path_config = beets.config["paths"]
self.path_formats = get_path_formats(path_config)
query = config["query"].as_str()
self.query, _ = parse_query_string(query, Item)
self.removable = config.get(dict).get("removable", True)
if "directory" in config:
dir = config["directory"].as_str()
else:
dir = self.name
dir = bytestring_path(dir)
if not os.path.isabs(syspath(dir)):
dir = os.path.join(self.lib.directory, dir)
self.directory = dir
def item_change_actions(self, item: Item, path: bytes, dest: bytes) -> List[Action]:
"""Returns the necessary actions for items that were previously in the
external collection, but might require metadata updates.
"""
actions = []
if not util.samefile(path, dest):
actions.append(Action.MOVE)
item_mtime_alt = os.path.getmtime(syspath(path))
if item_mtime_alt < os.path.getmtime(syspath(item.path)):
actions.append(Action.WRITE)
album = item.get_album()
if album and (
album.artpath
and os.path.isfile(syspath(album.artpath))
and (item_mtime_alt < os.path.getmtime(syspath(album.artpath)))
):
actions.append(Action.SYNC_ART)
return actions
def _matched_item_action(self, item: Item) -> List[Action]:
path = self._get_stored_path(item)
if path and os.path.lexists(syspath(path)):
dest = self.destination(item)
_, path_ext = os.path.splitext(path)
_, dest_ext = os.path.splitext(dest)
if path_ext != dest_ext:
# formats config option changed
return [Action.REMOVE, Action.ADD]
else:
return self.item_change_actions(item, path, dest)
else:
return [Action.ADD]
def _items_actions(self) -> Iterator[Tuple[Item, List[Action]]]:
matched_ids = set()
for album in self.lib.albums():
if self.query.match(album):
matched_items = album.items()
matched_ids.update(item.id for item in matched_items)
for item in self.lib.items():
if item.id in matched_ids or self.query.match(item):
yield (item, self._matched_item_action(item))
elif self._get_stored_path(item):
yield (item, [Action.REMOVE])
def ask_create(self, create: Optional[bool] = None) -> bool:
if not self.removable:
return True
if create is not None:
return create
msg = (
f"Collection at '{displayable_path(self.directory)}' does not exists. "
"Maybe you forgot to mount it.\n"
"Do you want to create the collection? (y/n)"
)
return input_yn(msg, require=True)
def update(self, create: Optional[bool] = None):
if not os.path.isdir(syspath(self.directory)) and not self.ask_create(create):
print_(f"Skipping creation of {displayable_path(self.directory)}")
return
converter = self._converter()
for item, actions in self._items_actions():
dest = self.destination(item)
path = self._get_stored_path(item)
for action in actions:
if action == Action.MOVE:
print_(f">{displayable_path(path)} -> {displayable_path(dest)}")
util.mkdirall(dest)
util.move(path, dest)
assert path is not None
util.prune_dirs(os.path.dirname(path), root=self.directory)
self._set_stored_path(item, dest)
item.store()
path = dest
elif action == Action.WRITE:
print_(f"*{displayable_path(path)}")
item.write(path=path)
elif action == Action.SYNC_ART:
print_(f"~{displayable_path(path)}")
assert path is not None
self._sync_art(item, path)
elif action == Action.ADD:
print_(f"+{displayable_path(dest)}")
converter.submit(item)
elif action == Action.REMOVE:
print_(f"-{displayable_path(path)}")
self._remove_file(item)
item.store()
for item, dest in converter.as_completed():
self._set_stored_path(item, dest)
item.store()
converter.shutdown()
def destination(self, item: Item) -> bytes:
"""Returns the path for `item` in the external collection."""
path = item.destination(basedir=self.directory, path_formats=self.path_formats)
assert isinstance(path, bytes)
return path
def _set_stored_path(self, item: Item, path: bytes):
item[self.path_key] = str(path, "utf8")
def _get_stored_path(self, item: Item) -> Optional[bytes]:
try:
path = item[self.path_key]
except KeyError:
return None
if path:
return path.encode("utf8")
else:
return None
def _remove_file(self, item: Item):
"""Remove the external file for `item`."""
path = self._get_stored_path(item)
_remove(path)
util.prune_dirs(path, root=self.directory)
del item[self.path_key]
def _converter(self) -> "Worker":
def _convert(item):
dest = self.destination(item)
util.mkdirall(dest)
util.copy(item.path, dest, replace=True)
return item, dest
return Worker(_convert, self.max_workers)
def _sync_art(self, item: Item, path: bytes):
"""Embed artwork in the file at `path`."""
album = item.get_album()
if album and album.artpath and os.path.isfile(syspath(album.artpath)):
self._log.debug(
f"Embedding art from {displayable_path(album.artpath)} into {displayable_path(path)}"
)
art.embed_item(self._log, item, album.artpath, itempath=path)
class ExternalConvert(External):
def __init__(self, log, name, formats, lib, config):
super().__init__(log, name, lib, config)
convert_plugin = convert.ConvertPlugin()
self._encode = convert_plugin.encode
self._embed = convert_plugin.config["embed"].get(bool)
formats = [f.lower() for f in formats]
self.formats = [convert.ALIASES.get(f, f) for f in formats]
self.convert_cmd, self.ext = convert.get_format(self.formats[0])
@override
def _converter(self) -> "Worker":
fs_lock = threading.Lock()
def _convert(item):
dest = self.destination(item)
with fs_lock:
util.mkdirall(dest)
if self._should_transcode(item):
self._encode(self.convert_cmd, item.path, dest)
# Don't rely on the converter to write correct/complete tags.
item.write(path=dest)
else:
self._log.debug(f"copying {displayable_path(dest)}")
util.copy(item.path, dest, replace=True)
if self._embed:
self._sync_art(item, dest)
return item, dest
return Worker(_convert, self.max_workers)
@override
def destination(self, item: Item) -> bytes:
dest = super().destination(item)
if self._should_transcode(item):
return os.path.splitext(dest)[0] + b"." + self.ext
else:
return dest
def _should_transcode(self, item):
return item.format.lower() not in self.formats
class SymlinkType(Enum):
ABSOLUTE = 0
RELATIVE = 1
class SymlinkView(External):
@override
def parse_config(self, config):
if "query" not in config:
config["query"] = "" # This is a TrueQuery()
if "link_type" not in config:
# Default as absolute so it doesn't break previous implementation
config["link_type"] = "absolute"
self.relativelinks = config["link_type"].as_choice({
"relative": SymlinkType.RELATIVE,
"absolute": SymlinkType.ABSOLUTE,
})
super().parse_config(config)
@override
def item_change_actions(self, item: Item, path: bytes, dest: bytes):
"""Returns the necessary actions for items that were previously in the
external collection, but might require metadata updates.
"""
if path != dest or not util.samefile(os.readlink(path), item.path):
return [Action.MOVE]
else:
return []
@override
def update(self, create=None):
for item, actions in self._items_actions():
dest = self.destination(item)
path = self._get_stored_path(item)
for action in actions:
if action == Action.MOVE:
print_(f">{displayable_path(path)} -> {displayable_path(dest)}")
self._remove_file(item)
self._create_symlink(item)
self._set_stored_path(item, dest)
elif action == Action.ADD:
print_(f"+{displayable_path(dest)}")
self._create_symlink(item)
self._set_stored_path(item, dest)
elif action == Action.REMOVE:
print_(f"-{displayable_path(path)}")
self._remove_file(item)
else:
continue
item.store()
def _create_symlink(self, item: Item):
dest = self.destination(item)
util.mkdirall(dest)
link = (
os.path.relpath(item.path, os.path.dirname(dest))
if self.relativelinks == SymlinkType.RELATIVE
else item.path
)
util.link(link, dest)
@override
def _sync_art(self, item: Item, path: bytes):
pass
class Worker(futures.ThreadPoolExecutor):
def __init__(self, fn, max_workers: Optional[int]):
super().__init__(max_workers)
self._tasks = set()
self._fn = fn
def submit(self, *args, **kwargs):
fut = super().submit(self._fn, *args, **kwargs)
self._tasks.add(fut)
return fut
def as_completed(self):
for f in futures.as_completed(self._tasks):
self._tasks.remove(f)
yield f.result()