fix: implemented artwork.add_data

This commit is contained in:
Hazel 2024-06-11 14:34:58 +02:00
parent 4ee6fd2137
commit b1a306f3f3
4 changed files with 27 additions and 19 deletions

View File

@ -1,15 +1,13 @@
import logging
import music_kraken
import logging
print("Setting logging-level to DEBUG")
logging.getLogger().setLevel(logging.DEBUG)
if __name__ == "__main__":
commands = [
"s: #a Crystal F",
"10",
"1",
"3",
"s: #a Ghost Bath",
]

View File

@ -20,7 +20,7 @@ class ArtworkVariant:
url: str
width: Optional[int] = None
height: Optional[int] = None
image_format: Optional[str] = ""
image_format: Optional[str] = None
def __hash__(self) -> int:
return custom_hash(self.url)
@ -31,6 +31,13 @@ class ArtworkVariant:
def __contains__(self, other: str) -> bool:
return custom_hash(other) == hash(self.url)
def __merge__(self, other: ArtworkVariant) -> None:
for key, value in other.__dict__.items():
if value is None:
continue
if getattr(self, key) is None:
setattr(self, key, value)
@dataclass
class Artwork:
@ -55,10 +62,9 @@ class Artwork:
variant = self.search_variant(kwargs.get("url"))
if variant is None:
variant, kwargs = create_dataclass_instance(ArtworkVariant, **kwargs)
variant, kwargs = create_dataclass_instance(ArtworkVariant, kwargs)
self.variants.append(variant)
variant.url = url
variant.__dict__.update(kwargs)
@property
@ -91,8 +97,6 @@ class ArtworkCollection:
self._data = []
self.extend(data)
def search_artwork(self, url: str) -> Optional[ArtworkVariant]:
for artwork in self._data:
if url in artwork:
@ -106,18 +110,19 @@ class ArtworkCollection:
def _create_new_artwork(self, **kwargs) -> Tuple[Artwork, dict]:
kwargs["artwork_type"] = kwargs.get("artwork_type", self.artwork_type)
return create_dataclass_instance(ArtworkVariant, dict(**kwargs))
return create_dataclass_instance(Artwork, dict(**kwargs))
def add_data(self, url: str, **kwargs) -> None:
def add_data(self, url: str, **kwargs) -> Artwork:
kwargs["url"] = url
artwork = self.search_artwork(url)
if artwork is None:
artwork, kwargs = self._create_new_artwork(url=url)
artwork, kwargs = self._create_new_artwork(**kwargs)
self._data.append(artwork)
artwork.add_data(url, **kwargs)
artwork.add_data(**kwargs)
return artwork
def append(self, value: Union[Artwork, ArtworkVariant, dict], **kwargs):
"""
@ -144,6 +149,8 @@ class ArtworkCollection:
"""
This will make the artworks ready for download
"""
from ..connection import Connection
for artwork in self._data:
for variants in artwork.variants:
pass

View File

@ -156,6 +156,7 @@ def create_dataclass_instance(t, data: dict):
Tuple[Type, dict]: The created instance and a dict, containing the data, which was not used in the creation
"""
data = {k: v for k, v in data.items() if hasattr(t, k)}
removed_data = {k: v for k, v in data.items() if not hasattr(t, k)}
return t(**data), removed_data
needed_data = {k: v for k, v in data.items() if k in t.__dataclass_fields__}
removed_data = {k: v for k, v in data.items() if k not in t.__dataclass_fields__}
return t(**needed_data), removed_data

View File

@ -1,3 +1,4 @@
import re
import string
from functools import lru_cache
from pathlib import Path
@ -237,5 +238,6 @@ def is_url(value: Any) -> bool:
if not isinstance(value, str):
return True
return re.match(URL_PATTERN, query) is not None
# value has to be a string
return re.match(URL_PATTERN, value) is not None