Skip to content

Commit

Permalink
rename tagger.py to audiotags.py to avoid ambiguity with metadata man…
Browse files Browse the repository at this point in the history
…agement logic
  • Loading branch information
azuline committed Oct 30, 2023
1 parent 9735618 commit 3fc6b45
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
16 changes: 8 additions & 8 deletions rose/tagger.py → rose/audiotags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
The tagger module abstracts over tag reading and writing for five different audio formats, exposing
a single standard interface for all audio files.
The audiotags module abstracts over tag reading and writing for five different audio formats,
exposing a single standard interface for all audio files.
The tagger module also handles Rose-specific tagging semantics, such as multi-valued tags,
The audiotags module also handles Rose-specific tagging semantics, such as multi-valued tags,
normalization, and enum validation.
"""

Expand Down Expand Up @@ -74,7 +74,7 @@ class UnsupportedTagValueTypeError(RoseError):


@dataclass
class AudioFile:
class AudioTags:
id: str | None
release_id: str | None
title: str | None
Expand All @@ -94,7 +94,7 @@ class AudioFile:
_m: Any

@classmethod
def from_file(cls, p: Path) -> AudioFile:
def from_file(cls, p: Path) -> AudioTags:
"""Read the tags of an audio file on disk."""
if not any(p.suffix.lower() == ext for ext in SUPPORTED_AUDIO_EXTENSIONS):
raise UnsupportedFiletypeError(f"{p.suffix} not a supported filetype")
Expand All @@ -115,7 +115,7 @@ def _get_paired_frame(x: str) -> str | None:
return r" \\ ".join([p[1] for p in frame.people if p[0].lower() == x.lower()])
return None

return AudioFile(
return AudioTags(
id=_get_tag(m.tags, ["TXXX:ROSEID"]),
release_id=_get_tag(m.tags, ["TXXX:ROSERELEASEID"]),
title=_get_tag(m.tags, ["TIT2"]),
Expand All @@ -139,7 +139,7 @@ def _get_paired_frame(x: str) -> str | None:
_m=m,
)
if isinstance(m, mutagen.mp4.MP4):
return AudioFile(
return AudioTags(
id=_get_tag(m.tags, ["----:net.sunsetglow.rose:ID"]),
release_id=_get_tag(m.tags, ["----:net.sunsetglow.rose:RELEASEID"]),
title=_get_tag(m.tags, ["\xa9nam"]),
Expand All @@ -165,7 +165,7 @@ def _get_paired_frame(x: str) -> str | None:
_m=m,
)
if isinstance(m, (mutagen.flac.FLAC, mutagen.oggvorbis.OggVorbis, mutagen.oggopus.OggOpus)):
return AudioFile(
return AudioTags(
id=_get_tag(m.tags, ["roseid"]),
release_id=_get_tag(m.tags, ["rosereleaseid"]),
title=_get_tag(m.tags, ["title"]),
Expand Down
20 changes: 10 additions & 10 deletions rose/tagger_test.py → rose/audiotags_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from conftest import TEST_TAGGER
from rose.artiststr import ArtistMapping
from rose.tagger import (
AudioFile,
from rose.audiotags import (
AudioTags,
UnsupportedTagValueTypeError,
_split_tag,
)
Expand All @@ -23,7 +23,7 @@
],
)
def test_getters(filename: str, track_num: str, duration: int) -> None:
af = AudioFile.from_file(TEST_TAGGER / filename)
af = AudioTags.from_file(TEST_TAGGER / filename)
assert af.track_number == track_num
assert af.title == f"Track {track_num}"

Expand Down Expand Up @@ -60,12 +60,12 @@ def test_flush(isolated_dir: Path, filename: str, track_num: str, duration: int)
"""Test the flush by flushing the file, then asserting that all the tags still read properly."""
fpath = isolated_dir / filename
shutil.copyfile(TEST_TAGGER / filename, fpath)
af = AudioFile.from_file(fpath)
af = AudioTags.from_file(fpath)
# Inject one special case into here: modify the djmixer artist. This checks that we also clear
# the original djmixer tag, so that the next read does not contain Artist EF and Artist FG.
af.artists.djmixer = ["New"]
af.flush()
af = AudioFile.from_file(fpath)
af = AudioTags.from_file(fpath)

assert af.track_number == track_num
assert af.title == f"Track {track_num}"
Expand Down Expand Up @@ -104,12 +104,12 @@ def test_id_assignment(isolated_dir: Path, filename: str) -> None:
fpath = isolated_dir / filename
shutil.copyfile(TEST_TAGGER / filename, fpath)

af = AudioFile.from_file(fpath)
af = AudioTags.from_file(fpath)
af.id = "ahaha"
af.release_id = "bahaha"
af.flush()

af = AudioFile.from_file(fpath)
af = AudioTags.from_file(fpath)
assert af.id == "ahaha"
assert af.release_id == "bahaha"

Expand All @@ -124,7 +124,7 @@ def test_release_type_normalization(isolated_dir: Path, filename: str) -> None:
shutil.copyfile(TEST_TAGGER / filename, fpath)

# Check that release type is read correctly.
af = AudioFile.from_file(fpath)
af = AudioTags.from_file(fpath)
assert af.release_type == "album"
# Assert that attempting to flush a stupid value fails.
af.release_type = "lalala"
Expand All @@ -133,12 +133,12 @@ def test_release_type_normalization(isolated_dir: Path, filename: str) -> None:
# Flush it anyways...
af.flush(validate=False)
# Check that stupid release type is normalized as unknown.
af = AudioFile.from_file(fpath)
af = AudioTags.from_file(fpath)
assert af.release_type == "unknown"
# And now assert that the read is case insensitive.
af.release_type = "ALBUM"
af.flush(validate=False)
af = AudioFile.from_file(fpath)
af = AudioTags.from_file(fpath)
assert af.release_type == "album"


Expand Down
8 changes: 4 additions & 4 deletions rose/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
import uuid6

from rose.artiststr import format_artist_string
from rose.audiotags import SUPPORTED_AUDIO_EXTENSIONS, AudioTags
from rose.common import VERSION
from rose.config import Config
from rose.tagger import SUPPORTED_AUDIO_EXTENSIONS, AudioFile

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -634,7 +634,7 @@ def _update_cache_for_releases_executor(
# process the directory at this time.
release_id_from_first_file = None
with contextlib.suppress(Exception):
release_id_from_first_file = AudioFile.from_file(first_audio_file).release_id
release_id_from_first_file = AudioTags.from_file(first_audio_file).release_id
directory_mtime = os.stat(source_path).st_mtime
if release_id_from_first_file is not None and time.time() - directory_mtime < 3.0:
logger.info(
Expand Down Expand Up @@ -725,7 +725,7 @@ def _update_cache_for_releases_executor(
# in a follow-up loop.
tracks: list[CachedTrack] = []
track_ids_to_insert: set[str] = set()
# This value is set to true if we read an AudioFile and used it to confirm the release
# This value is set to true if we read an AudioTags and used it to confirm the release
# tags.
pulled_release_tags = False
for f in files:
Expand All @@ -749,7 +749,7 @@ def _update_cache_for_releases_executor(

# Otherwise, read tags from disk and construct a new cached_track.
logger.debug(f"Track cache miss for {os.path.basename(f)}, reading tags from disk")
tags = AudioFile.from_file(Path(f))
tags = AudioTags.from_file(Path(f))

# Now that we're here, pull the release tags. We also need them to compute the
# formatted artist string.
Expand Down
10 changes: 5 additions & 5 deletions rose/cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tomllib

from conftest import TEST_COLLAGE_1, TEST_PLAYLIST_1, TEST_RELEASE_1, TEST_RELEASE_2, TEST_RELEASE_3
from rose.audiotags import AudioTags
from rose.cache import (
CACHE_SCHEMA_PATH,
STORED_DATA_FILE_REGEX,
Expand Down Expand Up @@ -46,7 +47,6 @@
)
from rose.common import VERSION
from rose.config import Config
from rose.tagger import AudioFile


def test_schema(config: Config) -> None:
Expand Down Expand Up @@ -298,19 +298,19 @@ def test_update_cache_releases_writes_ids_to_tags(config: Config) -> None:
release_dir = config.music_source_dir / TEST_RELEASE_3.name
shutil.copytree(TEST_RELEASE_3, release_dir)

af = AudioFile.from_file(release_dir / "01.m4a")
af = AudioTags.from_file(release_dir / "01.m4a")
assert af.id is None
assert af.release_id is None
af = AudioFile.from_file(release_dir / "02.m4a")
af = AudioTags.from_file(release_dir / "02.m4a")
assert af.id is None
assert af.release_id is None

update_cache_for_releases(config, [release_dir])

af = AudioFile.from_file(release_dir / "01.m4a")
af = AudioTags.from_file(release_dir / "01.m4a")
assert af.id is not None
assert af.release_id is not None
af = AudioFile.from_file(release_dir / "02.m4a")
af = AudioTags.from_file(release_dir / "02.m4a")
assert af.id is not None
assert af.release_id is not None

Expand Down
4 changes: 2 additions & 2 deletions rose/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from send2trash import send2trash

from rose.artiststr import ArtistMapping
from rose.audiotags import AudioTags
from rose.cache import (
STORED_DATA_FILE_REGEX,
CachedRelease,
Expand All @@ -34,7 +35,6 @@
)
from rose.common import InvalidCoverArtFileError, RoseError, valid_uuid
from rose.config import Config
from rose.tagger import AudioFile

logger = logging.getLogger()

Expand Down Expand Up @@ -243,7 +243,7 @@ def edit_release(c: Config, release_id_or_virtual_dirname: str) -> None:

for t in tracks:
track_meta = release_meta.tracks[t.id]
tags = AudioFile.from_file(t.source_path)
tags = AudioTags.from_file(t.source_path)

dirty = False

Expand Down
4 changes: 2 additions & 2 deletions rose/virtualfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import cachetools
import llfuse

from rose.audiotags import SUPPORTED_AUDIO_EXTENSIONS, AudioTags
from rose.cache import (
artist_exists,
collage_exists,
Expand Down Expand Up @@ -89,7 +90,6 @@
set_release_cover_art,
toggle_release_new,
)
from rose.tagger import SUPPORTED_AUDIO_EXTENSIONS, AudioFile

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -894,7 +894,7 @@ def release(self, fh: int) -> None:
audiopath = Path(tmpdir) / f"f{ext}"
with audiopath.open("wb") as fp:
fp.write(b)
audiofile = AudioFile.from_file(audiopath)
audiofile = AudioTags.from_file(audiopath)
track_id = audiofile.id
if not track_id:
logger.warning(
Expand Down
6 changes: 3 additions & 3 deletions rose/virtualfs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import pytest

from rose.audiotags import AudioTags
from rose.config import Config
from rose.tagger import AudioFile
from rose.virtualfs import mount_virtualfs, unmount_virtualfs


Expand Down Expand Up @@ -124,15 +124,15 @@ def test_virtual_filesystem_write_files(
)
with start_virtual_fs(config):
# Write!
af = AudioFile.from_file(path)
af = AudioTags.from_file(path)
assert af.title == "Track 1"
af.title = "Hahahaha!!"
af.flush()
# Read! File should have been renamed post-cache update.
assert not path.exists()
path = path.with_name("01. BLACKPINK - Hahahaha!!.m4a")
assert path.is_file()
af = AudioFile.from_file(path)
af = AudioTags.from_file(path)
assert af.title == "Hahahaha!!"


Expand Down

0 comments on commit 3fc6b45

Please sign in to comment.