-
Notifications
You must be signed in to change notification settings - Fork 4
/
write.py
97 lines (69 loc) · 2.54 KB
/
write.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
"""Writes tags to track files."""
import logging
import mediafile
import pluggy
import moe
from moe import config
from moe.library import LibItem, Track
__all__ = ["write_tags"]
log = logging.getLogger("moe.write")
class Hooks:
"""Write plugin hook specifications."""
@staticmethod
@moe.hookspec
def write_custom_tags(track: Track):
"""Allow plugins to write tags to a Track.
How you write tags to the track is up to each individual plugin. Internally,
Moe uses the `mediafile <https://github.com/beetbox/mediafile>`_ library to
write tags.
Args:
track: Track to write tags to.
Example:
.. code:: python
audio_file = mediafile.MediaFile(track.path)
audio_file.album = track.album
audio_file.save()
See Also:
* :ref:`Album and track fields <fields:Fields>`
* `Mediafile docs <https://mediafile.readthedocs.io/en/latest/>`_
* The :meth:`~moe.library.track.Hooks.read_custom_tags` hook for reading
tags.
"""
@moe.hookimpl
def add_hooks(pm: pluggy.manager.PluginManager):
"""Registers `write` hookspecs to Moe."""
from moe.plugins.write import Hooks
pm.add_hookspecs(Hooks)
@moe.hookimpl
def process_new_items(items: list[LibItem]):
"""Writes tags to any new tracks in the library."""
for item in items:
if isinstance(item, Track):
write_tags(item)
@moe.hookimpl
def process_changed_items(items: list[LibItem]):
"""Writes tags to any altered tracks in the library."""
for item in items:
if isinstance(item, Track):
write_tags(item)
@moe.hookimpl(tryfirst=True)
def write_custom_tags(track: Track):
"""Writes all internally tracked tags to the track."""
audio_file = mediafile.MediaFile(track.path)
audio_file.album = track.album
audio_file.albumartist = track.albumartist
audio_file.artist = track.artist
audio_file.artists = track.artists
audio_file.date = track.album_obj.date
audio_file.disc = track.disc
audio_file.disctotal = track.album_obj.disc_total
audio_file.genres = track.genres
audio_file.media = track.album_obj.media
audio_file.title = track.title
audio_file.track = track.track_num
audio_file.save()
def write_tags(track: Track):
"""Write tags to a track's file."""
log.debug(f"Writing tags to track. [{track=!r}]")
config.CONFIG.pm.hook.write_custom_tags(track=track)
log.info(f"Wrote tags to track. [{track=!r}]")