diff --git a/src/create_custom_objects.py b/src/create_custom_objects.py index a6b178a..de950bb 100644 --- a/src/create_custom_objects.py +++ b/src/create_custom_objects.py @@ -52,7 +52,8 @@ only_smile = Artist( main_album_list=[ Album( title="Few words...", - source_list=[Source(SourcePages.BANDCAMP, "https://onlysmile.bandcamp.com/album/few-words")], + source_list=[ + Source(SourcePages.BANDCAMP, "https://onlysmile.bandcamp.com/album/few-words")], song_list=[ Song(title="Everything will be fine"), Song(title="Only Smile"), @@ -68,7 +69,8 @@ only_smile = Artist( ), Album( title="Your best friend", - source_list=[Source(SourcePages.BANDCAMP, "https://onlysmile.bandcamp.com/album/your-best-friend")] + source_list=[ + Source(SourcePages.BANDCAMP, "https://onlysmile.bandcamp.com/album/your-best-friend")] ) ] ), @@ -78,7 +80,8 @@ only_smile = Artist( main_album_list=[ Album( title="Few words...", - source_list=[Source(SourcePages.BANDCAMP, "https://onlysmile.bandcamp.com/album/few-words")], + source_list=[ + Source(SourcePages.BANDCAMP, "https://onlysmile.bandcamp.com/album/few-words")], song_list=[ Song(title="Everything will be fine"), Song(title="Only Smile"), @@ -94,7 +97,8 @@ only_smile = Artist( ), Album( title="Your best friend", - source_list=[Source(SourcePages.BANDCAMP, "https://onlysmile.bandcamp.com/album/your-best-friend")] + source_list=[ + Source(SourcePages.BANDCAMP, "https://onlysmile.bandcamp.com/album/your-best-friend")] ) ] ) @@ -107,9 +111,9 @@ only_smile = Artist( ] ) - objects_by_id = {} + def add_to_objects_dump(db_obj: DatabaseObject): objects_by_id[db_obj.id] = db_obj @@ -122,15 +126,10 @@ def add_to_objects_dump(db_obj: DatabaseObject): add_to_objects_dump(only_smile) for _id, _object in objects_by_id.items(): - try: - print(_id, _object.title, sep=": ") - except AttributeError: - try: - print(_id, _object.name, sep=": ") - except AttributeError: - print(_id, _object, sep=": ") + print(_id, _object, sep=": ") print(only_smile) + """ c = Collection([Song(title="hi"), Song(title="hi2"), Song(title="hi3")]) c1 = Collection([Song(title="he"), Song(title="hi5")]) @@ -180,4 +179,4 @@ print("b: ", b) print(c.data) print(c._data) -""" \ No newline at end of file +""" diff --git a/src/music_kraken/objects/collection.py b/src/music_kraken/objects/collection.py index a3bd8a4..ab7642b 100644 --- a/src/music_kraken/objects/collection.py +++ b/src/music_kraken/objects/collection.py @@ -8,6 +8,8 @@ T = TypeVar('T', bound=OuterProxy) class Collection(Generic[T]): + __is_collection__ = True + _data: List[T] _indexed_values: Dict[str, set] @@ -54,7 +56,6 @@ class Collection(Generic[T]): self._indexed_values[name].add(value) self._indexed_to_objects[value].append(__object) - print(from_map) if not from_map: for attribute, new_object in self.contain_given_in_attribute.items(): __object.__getattribute__(attribute).contain_collection_inside(new_object) @@ -181,14 +182,11 @@ class Collection(Generic[T]): self._data.append(__object) def append(self, __object: Optional[T], already_is_parent: bool = False, from_map: bool = False): - if __object is None: - return - - if __object.id in self._contains_ids: + if __object is None or __object.id in self._contains_ids: return exists_in_collection = self._contained_in_sub(__object) - if len(exists_in_collection) and self is exists_in_collection[0]: + if len(exists_in_collection) > 0 and self is exists_in_collection[0]: # assuming that the object already is contained in the correct collections if not already_is_parent: self.merge_into_self(__object, from_map=from_map) @@ -259,5 +257,5 @@ class Collection(Generic[T]): return self.__len__() == 0 def __iter__(self) -> Iterator[T]: - for element in self._data: + for element in self.data: yield element diff --git a/src/music_kraken/objects/parents.py b/src/music_kraken/objects/parents.py index e4334f2..2619d33 100644 --- a/src/music_kraken/objects/parents.py +++ b/src/music_kraken/objects/parents.py @@ -1,7 +1,8 @@ from __future__ import annotations import random -from collections import defaultdict +from functools import lru_cache + from typing import Optional, Dict, Tuple, List, Type, Generic, Any, TypeVar from .metadata import Metadata @@ -87,12 +88,11 @@ class OuterProxy: if isinstance(data_list, list) and name.endswith("_list"): collection_name = name.replace("_list", "_collection") - if collection_name not in self.__dict__: - continue - - collection = self.__getattribute__(collection_name) + collection = self._inner.__getattribute__(collection_name) collection.extend(data_list) + self._inner.__setattr__(collection_name, collection) + def __init_collections__(self): pass @@ -106,6 +106,9 @@ class OuterProxy: :return: """ + if __name.startswith("__"): + return super().__getattribute__(__name) + _inner: InnerData = super().__getattribute__("_inner") try: return _inner.__getattribute__(__name) @@ -174,3 +177,18 @@ class OuterProxy: """ return [] + + @property + @lru_cache() + def all_collections(self): + r = [] + + for key in self._default_factories: + val = self._inner.__getattribute__(key) + if hasattr(val, "__is_collection__"): + r.append(val) + + return r + + def __repr__(self): + return f"{type(self).__name__}({', '.join(key + ': ' + str(val) for key, val in self.indexing_values)})" diff --git a/src/music_kraken/objects/song.py b/src/music_kraken/objects/song.py index 348c70c..2d54aa3 100644 --- a/src/music_kraken/objects/song.py +++ b/src/music_kraken/objects/song.py @@ -148,6 +148,7 @@ class Song(Base): return main_artists return f"{main_artists} feat. {feature_artists}" + """ def __str__(self) -> str: artist_credit_str = "" artist_credits = self.get_artist_credits() @@ -155,6 +156,7 @@ class Song(Base): artist_credit_str = f" by {artist_credits}" return f"\"{self.title}\"{artist_credit_str}" + """ def __repr__(self) -> str: return f"Song(\"{self.title}\")" @@ -519,6 +521,7 @@ class Artist(Base): return metadata + """ def __str__(self, include_notes: bool = False): string = self.name or "" if include_notes: @@ -526,6 +529,7 @@ class Artist(Base): if plaintext_notes is not None: string += "\n" + plaintext_notes return string + """ def __repr__(self): return f"Artist(\"{self.name}\")" diff --git a/src/music_kraken/objects/source.py b/src/music_kraken/objects/source.py index 2ba269a..fff1bcc 100644 --- a/src/music_kraken/objects/source.py +++ b/src/music_kraken/objects/source.py @@ -33,11 +33,14 @@ class Source(OuterProxy): "audio_url": str, } - def __init__(self, page_enum: SourcePages, referer_page: SourcePages = None, **kwargs) -> None: + def __init__(self, page_enum: SourcePages, url: str, referer_page: SourcePages = None, audio_url: str = None, **kwargs) -> None: if referer_page is None: referer_page = page_enum - super().__init__(page_enum=page_enum, referer_page=referer_page, **kwargs) + if audio_url is None: + audio_url = url + + super().__init__(page_enum=page_enum, url=url, referer_page=referer_page, audio_url=audio_url, **kwargs) @classmethod def match_url(cls, url: str, referer_page: SourcePages) -> Optional["Source"]: