Skip to content

Commit

Permalink
type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Apr 26, 2024
1 parent 99f121a commit ea78c66
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 84 deletions.
42 changes: 22 additions & 20 deletions rose/audiotags.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ class RoseDate:
month: int | None = None
day: int | None = None

@classmethod
def parse(cls, value: str | None) -> RoseDate | None:
if not value:
return None
if YEAR_REGEX.match(value):
return RoseDate(year=int(value), month=None, day=None)
# There may be a time value after the date... allow that and other crap.
if m := DATE_REGEX.match(value):
return RoseDate(year=int(m[1]), month=int(m[2]), day=int(m[3]))
return None

def __str__(self) -> str:
if self.month is None and self.day is None:
return f"{self.year:04}"
Expand Down Expand Up @@ -161,9 +172,11 @@ def _get_paired_frame(x: str) -> str | None:
id=_get_tag(m.tags, ["TXXX:ROSEID"], first=True),
release_id=_get_tag(m.tags, ["TXXX:ROSERELEASEID"], first=True),
tracktitle=_get_tag(m.tags, ["TIT2"]),
releasedate=_parse_date(_get_tag(m.tags, ["TDRC", "TYER", "TDAT"])),
originaldate=_parse_date(_get_tag(m.tags, ["TDOR", "TORY"])),
compositiondate=_parse_date(_get_tag(m.tags, ["TXXX:COMPOSITIONDATE"], first=True)),
releasedate=RoseDate.parse(_get_tag(m.tags, ["TDRC", "TYER", "TDAT"])),
originaldate=RoseDate.parse(_get_tag(m.tags, ["TDOR", "TORY"])),
compositiondate=RoseDate.parse(
_get_tag(m.tags, ["TXXX:COMPOSITIONDATE"], first=True)
),
tracknumber=tracknumber,
tracktotal=tracktotal,
discnumber=discnumber,
Expand Down Expand Up @@ -205,8 +218,8 @@ def _get_paired_frame(x: str) -> str | None:
id=_get_tag(m.tags, ["----:net.sunsetglow.rose:ID"]),
release_id=_get_tag(m.tags, ["----:net.sunsetglow.rose:RELEASEID"]),
tracktitle=_get_tag(m.tags, ["\xa9nam"]),
releasedate=_parse_date(_get_tag(m.tags, ["\xa9day"])),
originaldate=_parse_date(
releasedate=RoseDate.parse(_get_tag(m.tags, ["\xa9day"])),
originaldate=RoseDate.parse(
_get_tag(
m.tags,
[
Expand All @@ -216,7 +229,7 @@ def _get_paired_frame(x: str) -> str | None:
],
)
),
compositiondate=_parse_date(
compositiondate=RoseDate.parse(
_get_tag(m.tags, ["----:net.sunsetglow.rose:COMPOSITIONDATE"])
),
tracknumber=str(tracknumber),
Expand Down Expand Up @@ -261,9 +274,9 @@ def _get_paired_frame(x: str) -> str | None:
id=_get_tag(m.tags, ["roseid"]),
release_id=_get_tag(m.tags, ["rosereleaseid"]),
tracktitle=_get_tag(m.tags, ["title"]),
releasedate=_parse_date(_get_tag(m.tags, ["date", "year"])),
originaldate=_parse_date(_get_tag(m.tags, ["originaldate", "originalyear"])),
compositiondate=_parse_date(_get_tag(m.tags, ["compositiondate"])),
releasedate=RoseDate.parse(_get_tag(m.tags, ["date", "year"])),
originaldate=RoseDate.parse(_get_tag(m.tags, ["originaldate", "originalyear"])),
compositiondate=RoseDate.parse(_get_tag(m.tags, ["compositiondate"])),
tracknumber=_get_tag(m.tags, ["tracknumber"], first=True),
tracktotal=_parse_int(_get_tag(m.tags, ["tracktotal"], first=True)),
discnumber=_get_tag(m.tags, ["discnumber"], first=True),
Expand Down Expand Up @@ -516,17 +529,6 @@ def _parse_int(x: str | None) -> int | None:
return None


def _parse_date(value: str | None) -> RoseDate | None:
if not value:
return None
if YEAR_REGEX.match(value):
return RoseDate(year=int(value), month=None, day=None)
# There may be a time value after the date... allow that and other crap.
if m := DATE_REGEX.match(value):
return RoseDate(year=int(m[1]), month=int(m[2]), day=int(m[3]))
return None


TAG_SPLITTER_REGEX = re.compile(r" \\\\ | / |; ?| vs\. ")


Expand Down
24 changes: 14 additions & 10 deletions rose/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ def dump(self) -> dict[str, Any]:
"added_at": self.added_at,
"releasetitle": self.releasetitle,
"releasetype": self.releasetype,
"releasedate": self.releasedate,
"originaldate": self.originaldate,
"compositiondate": self.compositiondate,
"releasedate": str(self.releasedate),
"originaldate": str(self.originaldate),
"compositiondate": str(self.compositiondate),
"catalognumber": self.catalognumber,
"edition": self.edition,
"new": self.new,
Expand Down Expand Up @@ -344,9 +344,9 @@ def dump(self, with_release_info: bool = True) -> dict[str, Any]:
"releasetitle": self.release.releasetitle,
"releasetype": self.release.releasetype,
"disctotal": self.release.disctotal,
"releasedate": self.release.releasedate,
"originaldate": self.release.originaldate,
"compositiondate": self.release.compositiondate,
"releasedate": str(self.release.releasedate),
"originaldate": str(self.release.originaldate),
"compositiondate": str(self.release.compositiondate),
"catalognumber": self.release.catalognumber,
"edition": self.release.edition,
"new": self.release.new,
Expand Down Expand Up @@ -1910,12 +1910,12 @@ def get_release_logtext(c: Config, release_id: str) -> str | None:

def calculate_release_logtext(
title: str,
releasedate: int | None,
releasedate: RoseDate | None,
artists: ArtistMapping,
) -> str:
logtext = f"{artistsfmt(artists)} - "
if releasedate:
logtext += f"{releasedate}. "
logtext += f"{releasedate.year}. "
logtext += title
return logtext

Expand Down Expand Up @@ -2088,10 +2088,14 @@ def get_track_logtext(c: Config, track_id: str) -> str | None:
def calculate_track_logtext(
title: str,
artists: ArtistMapping,
releasedate: int | None,
releasedate: RoseDate | None,
suffix: str,
) -> str:
return f"{artistsfmt(artists)} - {title or 'Unknown Title'} [{releasedate}]{suffix}"
rval = f"{artistsfmt(artists)} - {title or 'Unknown Title'}"
if releasedate:
rval += f" [{releasedate.year}]"
rval += suffix
return rval


def list_playlists(c: Config) -> list[str]:
Expand Down
40 changes: 20 additions & 20 deletions rose/cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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.audiotags import AudioTags, RoseDate
from rose.cache import (
CACHE_SCHEMA_PATH,
STORED_DATA_FILE_REGEX,
Expand Down Expand Up @@ -1091,7 +1091,7 @@ def test_list_releases(config: Config) -> None:
releasetype="album",
compositiondate=None,
catalognumber=None,
releasedate=2023,
releasedate=RoseDate(2023),
disctotal=1,
new=False,
genres=["Techno", "Deep House"],
Expand Down Expand Up @@ -1124,15 +1124,15 @@ def test_list_releases(config: Config) -> None:
added_at="0000-01-01T00:00:00+00:00",
releasetitle="Release 2",
releasetype="album",
releasedate=2021,
releasedate=RoseDate(2021),
compositiondate=None,
catalognumber="DG-001",
disctotal=1,
new=False,
genres=["Classical"],
parent_genres=[],
labels=["Native State"],
originaldate=2019,
originaldate=RoseDate(2019),
edition="Deluxe",
secondary_genres=["Orchestral"],
parent_secondary_genres=[
Expand All @@ -1153,8 +1153,8 @@ def test_list_releases(config: Config) -> None:
added_at="0000-01-01T00:00:00+00:00",
releasetitle="Release 3",
releasetype="album",
releasedate=2021,
compositiondate=1780,
releasedate=RoseDate(2021),
compositiondate=RoseDate(1780),
catalognumber="DG-002",
disctotal=1,
new=True,
Expand Down Expand Up @@ -1187,7 +1187,7 @@ def test_get_release_and_associated_tracks(config: Config) -> None:
added_at="0000-01-01T00:00:00+00:00",
releasetitle="Release 1",
releasetype="album",
releasedate=2023,
releasedate=RoseDate(2023),
compositiondate=None,
catalognumber=None,
disctotal=1,
Expand Down Expand Up @@ -1304,7 +1304,7 @@ def test_list_tracks(config: Config) -> None:
added_at="0000-01-01T00:00:00+00:00",
releasetitle="Release 1",
releasetype="album",
releasedate=2023,
releasedate=RoseDate(2023),
compositiondate=None,
catalognumber=None,
disctotal=1,
Expand Down Expand Up @@ -1351,7 +1351,7 @@ def test_list_tracks(config: Config) -> None:
added_at="0000-01-01T00:00:00+00:00",
releasetitle="Release 1",
releasetype="album",
releasedate=2023,
releasedate=RoseDate(2023),
compositiondate=None,
catalognumber=None,
disctotal=1,
Expand Down Expand Up @@ -1400,15 +1400,15 @@ def test_list_tracks(config: Config) -> None:
datafile_mtime="999",
releasetitle="Release 2",
releasetype="album",
releasedate=2021,
releasedate=RoseDate(2021),
compositiondate=None,
catalognumber="DG-001",
new=False,
disctotal=1,
genres=["Classical"],
parent_genres=[],
labels=["Native State"],
originaldate=2019,
originaldate=RoseDate(2019),
edition="Deluxe",
secondary_genres=["Orchestral"],
parent_secondary_genres=[
Expand Down Expand Up @@ -1441,8 +1441,8 @@ def test_list_tracks(config: Config) -> None:
datafile_mtime="999",
releasetitle="Release 3",
releasetype="album",
releasedate=2021,
compositiondate=1780,
releasedate=RoseDate(2021),
compositiondate=RoseDate(1780),
catalognumber="DG-002",
new=True,
disctotal=1,
Expand Down Expand Up @@ -1485,7 +1485,7 @@ def test_get_track(config: Config) -> None:
added_at="0000-01-01T00:00:00+00:00",
releasetitle="Release 1",
releasetype="album",
releasedate=2023,
releasedate=RoseDate(2023),
compositiondate=None,
catalognumber=None,
disctotal=1,
Expand Down Expand Up @@ -1598,7 +1598,7 @@ def test_get_collage(config: Config) -> None:
datafile_mtime="999",
releasetitle="Release 1",
releasetype="album",
releasedate=2023,
releasedate=RoseDate(2023),
compositiondate=None,
catalognumber=None,
new=False,
Expand Down Expand Up @@ -1633,15 +1633,15 @@ def test_get_collage(config: Config) -> None:
datafile_mtime="999",
releasetitle="Release 2",
releasetype="album",
releasedate=2021,
releasedate=RoseDate(2021),
compositiondate=None,
catalognumber="DG-001",
new=False,
disctotal=1,
genres=["Classical"],
parent_genres=[],
labels=["Native State"],
originaldate=2019,
originaldate=RoseDate(2019),
edition="Deluxe",
secondary_genres=["Orchestral"],
parent_secondary_genres=[
Expand Down Expand Up @@ -1710,7 +1710,7 @@ def test_get_playlist(config: Config) -> None:
added_at="0000-01-01T00:00:00+00:00",
releasetitle="Release 1",
releasetype="album",
releasedate=2023,
releasedate=RoseDate(2023),
compositiondate=None,
catalognumber=None,
disctotal=1,
Expand Down Expand Up @@ -1759,15 +1759,15 @@ def test_get_playlist(config: Config) -> None:
datafile_mtime="999",
releasetitle="Release 2",
releasetype="album",
releasedate=2021,
releasedate=RoseDate(2021),
compositiondate=None,
catalognumber="DG-001",
new=False,
disctotal=1,
genres=["Classical"],
parent_genres=[],
labels=["Native State"],
originaldate=2019,
originaldate=RoseDate(2019),
edition="Deluxe",
secondary_genres=["Orchestral"],
parent_secondary_genres=[
Expand Down
14 changes: 7 additions & 7 deletions rose/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import tomllib
from send2trash import send2trash

from rose.audiotags import AudioTags
from rose.audiotags import AudioTags, RoseDate
from rose.cache import (
STORED_DATA_FILE_REGEX,
CachedRelease,
Expand Down Expand Up @@ -238,9 +238,9 @@ class MetadataRelease:
title: str
new: bool
releasetype: str
releasedate: int | None
originaldate: int | None
compositiondate: int | None
releasedate: RoseDate | None
originaldate: RoseDate | None
compositiondate: RoseDate | None
artists: list[MetadataArtist]
labels: list[str]
edition: str | None
Expand Down Expand Up @@ -295,9 +295,9 @@ def from_toml(cls, toml: str) -> MetadataRelease:
title=d["title"],
new=d["new"],
releasetype=d["releasetype"],
originaldate=d["originaldate"] if d["originaldate"] != -9999 else None,
releasedate=d["releasedate"] if d["releasedate"] != -9999 else None,
compositiondate=d["compositiondate"] if d["compositiondate"] != -9999 else None,
originaldate=RoseDate.parse(d["originaldate"]),
releasedate=RoseDate.parse(d["releasedate"]),
compositiondate=RoseDate.parse(d["compositiondate"]),
genres=d["genres"],
secondary_genres=d["secondary_genres"],
descriptors=d["descriptors"],
Expand Down
Loading

0 comments on commit ea78c66

Please sign in to comment.