fix: implemented artwork.add_data
This commit is contained in:
parent
4ee6fd2137
commit
b1a306f3f3
@ -1,15 +1,13 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
import music_kraken
|
import music_kraken
|
||||||
|
|
||||||
import logging
|
|
||||||
print("Setting logging-level to DEBUG")
|
print("Setting logging-level to DEBUG")
|
||||||
logging.getLogger().setLevel(logging.DEBUG)
|
logging.getLogger().setLevel(logging.DEBUG)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
commands = [
|
commands = [
|
||||||
"s: #a Crystal F",
|
"s: #a Ghost Bath",
|
||||||
"10",
|
|
||||||
"1",
|
|
||||||
"3",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ class ArtworkVariant:
|
|||||||
url: str
|
url: str
|
||||||
width: Optional[int] = None
|
width: Optional[int] = None
|
||||||
height: Optional[int] = None
|
height: Optional[int] = None
|
||||||
image_format: Optional[str] = ""
|
image_format: Optional[str] = None
|
||||||
|
|
||||||
def __hash__(self) -> int:
|
def __hash__(self) -> int:
|
||||||
return custom_hash(self.url)
|
return custom_hash(self.url)
|
||||||
@ -31,6 +31,13 @@ class ArtworkVariant:
|
|||||||
def __contains__(self, other: str) -> bool:
|
def __contains__(self, other: str) -> bool:
|
||||||
return custom_hash(other) == hash(self.url)
|
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
|
@dataclass
|
||||||
class Artwork:
|
class Artwork:
|
||||||
@ -55,10 +62,9 @@ class Artwork:
|
|||||||
variant = self.search_variant(kwargs.get("url"))
|
variant = self.search_variant(kwargs.get("url"))
|
||||||
|
|
||||||
if variant is None:
|
if variant is None:
|
||||||
variant, kwargs = create_dataclass_instance(ArtworkVariant, **kwargs)
|
variant, kwargs = create_dataclass_instance(ArtworkVariant, kwargs)
|
||||||
self.variants.append(variant)
|
self.variants.append(variant)
|
||||||
|
|
||||||
variant.url = url
|
|
||||||
variant.__dict__.update(kwargs)
|
variant.__dict__.update(kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -91,8 +97,6 @@ class ArtworkCollection:
|
|||||||
self._data = []
|
self._data = []
|
||||||
self.extend(data)
|
self.extend(data)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def search_artwork(self, url: str) -> Optional[ArtworkVariant]:
|
def search_artwork(self, url: str) -> Optional[ArtworkVariant]:
|
||||||
for artwork in self._data:
|
for artwork in self._data:
|
||||||
if url in artwork:
|
if url in artwork:
|
||||||
@ -106,18 +110,19 @@ class ArtworkCollection:
|
|||||||
def _create_new_artwork(self, **kwargs) -> Tuple[Artwork, dict]:
|
def _create_new_artwork(self, **kwargs) -> Tuple[Artwork, dict]:
|
||||||
kwargs["artwork_type"] = kwargs.get("artwork_type", self.artwork_type)
|
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
|
kwargs["url"] = url
|
||||||
|
|
||||||
artwork = self.search_artwork(url)
|
artwork = self.search_artwork(url)
|
||||||
|
|
||||||
if artwork is None:
|
if artwork is None:
|
||||||
artwork, kwargs = self._create_new_artwork(url=url)
|
artwork, kwargs = self._create_new_artwork(**kwargs)
|
||||||
self._data.append(artwork)
|
self._data.append(artwork)
|
||||||
|
|
||||||
artwork.add_data(url, **kwargs)
|
artwork.add_data(**kwargs)
|
||||||
|
return artwork
|
||||||
|
|
||||||
def append(self, value: Union[Artwork, ArtworkVariant, dict], **kwargs):
|
def append(self, value: Union[Artwork, ArtworkVariant, dict], **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -144,6 +149,8 @@ class ArtworkCollection:
|
|||||||
"""
|
"""
|
||||||
This will make the artworks ready for download
|
This will make the artworks ready for download
|
||||||
"""
|
"""
|
||||||
|
from ..connection import Connection
|
||||||
|
|
||||||
for artwork in self._data:
|
for artwork in self._data:
|
||||||
for variants in artwork.variants:
|
for variants in artwork.variants:
|
||||||
pass
|
pass
|
||||||
|
@ -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
|
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)}
|
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 not hasattr(t, k)}
|
removed_data = {k: v for k, v in data.items() if k not in t.__dataclass_fields__}
|
||||||
return t(**data), removed_data
|
|
||||||
|
return t(**needed_data), removed_data
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import re
|
||||||
import string
|
import string
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@ -238,4 +239,5 @@ def is_url(value: Any) -> bool:
|
|||||||
if not isinstance(value, str):
|
if not isinstance(value, str):
|
||||||
return True
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user