Skip to content

Commit

Permalink
internal: fix several sonarcloud bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Dec 12, 2024
1 parent 1c71fcd commit d97b78e
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 108 deletions.
8 changes: 4 additions & 4 deletions beetsplug/bandcamp/metaguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@


class Metaguru(Helpers):
META = cached_patternprop(r'.*"@id".*')
META_PAT = cached_patternprop(r'.*"@id".*')
LABEL_IN_DESC = cached_patternprop(r"(?<=Label:) *\b[^/,\n]+")
ARTIST_IN_DESC = cached_patternprop(r"Artists?: *(\b[^\n]+)")
REMIX_IN_ARTIST = cached_patternprop(r"[(,+]+.+?re?mi?x", re.I)
Expand Down Expand Up @@ -88,11 +88,11 @@ def from_html(cls, html: str, config: JSONDict | None = None) -> Metaguru:
for char in cls.HTML_REMOVE_CHARS:
html = html.replace(char, "")
try:
meta = cls.META.search(html).group() # type: ignore[union-attr]
meta = cls.META_PAT.search(html).group() # type: ignore[union-attr]
except AttributeError as exc:
raise AttributeError("Could not find release metadata JSON") from exc
else:
return cls(json.loads(meta), config)

return cls(json.loads(meta), config)

@cached_property
def excluded_fields(self) -> set[str]:
Expand Down
8 changes: 4 additions & 4 deletions beetsplug/bandcamp/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,17 @@ def duration(self) -> int | None:
h, m, s = map(int, self.NUMBER_PAT.findall(self.json_item["duration"]))
except KeyError:
return None
else:
return h * 3600 + m * 60 + s

return h * 3600 + m * 60 + s

@cached_property
def lyrics(self) -> str:
try:
text: str = self.json_item["recordingOf"]["lyrics"]["text"]
except KeyError:
return ""
else:
return text.replace("\r", "")

return text.replace("\r", "")

@cached_property
def name_split(self) -> list[str]:
Expand Down
37 changes: 20 additions & 17 deletions beetsplug/bandcamp/tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ class Tracks:
"I": 5,
"J": 5,
}
tracks: list[Track]
tracklist: list[Track]
names: Names

def __iter__(self) -> Iterator[Track]:
return iter(self.tracks)
return iter(self.tracklist)

def __len__(self) -> int:
return len(self.tracks)
return len(self.tracklist)

def __getitem__(self, idx: int) -> Track:
return self.tracklist[idx]

@classmethod
def from_names(cls, names: Names) -> Tracks:
Expand All @@ -63,28 +66,28 @@ def from_names(cls, names: Names) -> Tracks:

@property
def tracks_without_artist(self) -> list[Track]:
return [t for t in self.tracks if not t.artist]
return [t for t in self if not t.artist]

@cached_property
def first(self) -> Track:
return self.tracks[0]
return self[0]

@cached_property
def raw_names(self) -> list[str]:
return [j.name for j in self.tracks]
return [j.name for j in self]

@property
def original_artists(self) -> list[str]:
"""Return all unique unsplit (original) main track artists."""
return list(dict.fromkeys(j.artist for j in self.tracks))
return list(dict.fromkeys(j.artist for j in self))

@property
def artists(self) -> list[str]:
"""Return all unique split main track artists.
"Artist1 x Artist2" -> ["Artist1", "Artist2"]
"""
return list(dict.fromkeys(a for t in self.tracks for a in t.artists))
return list(dict.fromkeys(a for t in self for a in t.artists))

@cached_property
def lead_artists(self) -> list[str]:
Expand All @@ -102,7 +105,7 @@ def lead_artists(self) -> list[str]:
[A & B] -> [A & B]
[A, B & C] -> [A, B & C]
"""
lead_artists = list(dict.fromkeys(t.lead_artist for t in self.tracks))
lead_artists = list(dict.fromkeys(t.lead_artist for t in self))
unique_artists = set(self.artists)
if not unique_artists:
return []
Expand All @@ -123,10 +126,10 @@ def collaborators(self) -> set[str]:
artists = set(self.artists)
remixers = {
r
for t in self.tracks
for t in self
if t.remix and (r := t.remix.artist) and all(a not in r for a in artists)
}
feat = {j.ft for j in self.tracks if j.ft}
feat = {j.ft for j in self if j.ft}
return remixers | feat

@cached_property
Expand Down Expand Up @@ -156,7 +159,7 @@ def fix_title_split(self) -> None:
# while a llegitimate artist was available in the JSON data.
not_json_artist_tracks = [
t
for t in self.tracks
for t in self
if t.artist
and "," not in t.json_artist
and not commonprefix([t.json_artist, t.artist])
Expand All @@ -177,12 +180,12 @@ def handle_wild_track_alt(self) -> None:
Clear the `track_alt` value after assignment.
"""
unique_track_alts = {t.track_alt for t in self.tracks if t.track_alt}
unique_track_alts = {t.track_alt for t in self if t.track_alt}
if len(unique_track_alts) == 1 and len(self) > 1:
track_alt_tracks = [t for t in self.tracks if t.track_alt]
track_alt_tracks = [t for t in self if t.track_alt]

same_track_alt_on_all = len(track_alt_tracks) == len(self)
parsed_any_artists = any(t for t in self.tracks if t.artist)
parsed_any_artists = any(t for t in self if t.artist)

may_set_artist = parsed_any_artists or same_track_alt_on_all
unique_track_alt = unique_track_alts.pop()
Expand Down Expand Up @@ -251,9 +254,9 @@ def for_media(
self, media: str, comments: str, include_digi: bool
) -> list[dict[str, Any]]:
if not include_digi and media != "Digital Media":
tracks_ = [t for t in self.tracks if not t.digi_only]
tracks_ = [t for t in self if not t.digi_only]
else:
tracks_ = self.tracks
tracks_ = list(self)

medium_total = {"medium_total": len(tracks_)}
tracks = [t.info | medium_total for t in tracks_]
Expand Down
38 changes: 31 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,22 @@ def digital_format() -> JSONDict:


@pytest.fixture
def vinyl_format() -> JSONDict:
def vinyl_desc() -> str:
return "Vinyl description"


@pytest.fixture
def vinyl_disctitle() -> str:
return "Vinyl disctitle"


@pytest.fixture
def vinyl_format(vinyl_desc: str, vinyl_disctitle: str) -> JSONDict:
return {
"@id": "https://bandcamp.com/album/hello",
"musicReleaseFormat": "VinylFormat",
"description": "Vinyl description",
"name": "Disctitle",
"description": vinyl_desc,
"name": vinyl_disctitle,
"additionalProperty": [
{"name": "some_id", "value": "some_value"},
{"name": "item_type", "value": "p"},
Expand Down Expand Up @@ -156,21 +166,35 @@ def json_track(track_name: str, track_artist: str | None) -> JSONDict:
}


@pytest.fixture
def release_desc() -> str:
return "Description"


@pytest.fixture
def release_name() -> str:
return "Album"


@pytest.fixture
def json_meta(
digital_format: JSONDict, vinyl_format: JSONDict, json_track: JSONDict
release_desc: str,
release_name: str,
digital_format: JSONDict,
vinyl_format: JSONDict,
json_track: JSONDict,
) -> JSONDict:
return {
"@id": "album_id",
"name": "Album",
"description": "Description",
"name": release_name,
"description": release_desc,
"publisher": {
"@id": "label_url",
"name": "Label",
"genre": "bandcamp.com/tag/folk",
},
"byArtist": {"name": "Albumartist"},
"albumRelease": [digital_format, vinyl_format],
"albumRelease": [vinyl_format, digital_format],
"track": {"itemListElement": [json_track]},
"keywords": ["London", "house"],
}
Expand Down
20 changes: 5 additions & 15 deletions tests/test_catalognum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


@pytest.mark.parametrize(
"album, disctitle, description, label, expected",
"release_name, vinyl_disctitle, release_desc, label, expected",
[
("Tracker-229 [PRH-002]", "", "", "", "PRH-002"),
("[PRH-002] Tracker-229", "", "", "", "PRH-002"),
Expand Down Expand Up @@ -77,20 +77,10 @@
("AE-MJ-001.1", "", "", "", "AE-MJ-001.1"),
],
)
def test_parse_catalognum(
json_meta,
vinyl_format,
album,
disctitle,
description,
label,
expected,
beets_config,
):
json_meta["name"] = album
json_meta["publisher"]["name"] = label or "Label"
json_meta["description"] = description
json_meta["albumRelease"] = [{**vinyl_format, "name": disctitle}]
def test_parse_catalognum(json_meta, vinyl_format, label, expected, beets_config):
if label:
json_meta["publisher"]["name"] = label
json_meta["albumRelease"] = [vinyl_format]

guru = Metaguru(json_meta, beets_config)

Expand Down
Loading

0 comments on commit d97b78e

Please sign in to comment.