From 3737e0dc81880e80208f72c7a2b65283424d5255 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Mon, 29 Apr 2024 18:18:57 +0200 Subject: [PATCH] feat: added id possibility to output --- development/actual_donwload.py | 2 +- music_kraken/objects/collection.py | 14 +++++++------- music_kraken/objects/parents.py | 3 ++- music_kraken/objects/song.py | 28 +++++++++++++++++++--------- music_kraken/objects/source.py | 6 +++++- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/development/actual_donwload.py b/development/actual_donwload.py index 4788eb5..8161548 100644 --- a/development/actual_donwload.py +++ b/development/actual_donwload.py @@ -7,7 +7,7 @@ logging.getLogger().setLevel(logging.DEBUG) if __name__ == "__main__": commands = [ "s: #a Ghost Bath", - "d: 4", + "d: 14", ] diff --git a/music_kraken/objects/collection.py b/music_kraken/objects/collection.py index 02bff19..0842f58 100644 --- a/music_kraken/objects/collection.py +++ b/music_kraken/objects/collection.py @@ -13,8 +13,8 @@ class Collection(Generic[T]): _data: List[T] - _indexed_values: Dict[str, set] - _indexed_to_objects: Dict[any, list] + _indexed_from_id: Dict[int, Dict[str, Any]] + _indexed_values: Dict[str, Dict[Any, T]] shallow_list = property(fget=lambda self: self.data) @@ -74,6 +74,10 @@ class Collection(Generic[T]): del self._indexed_from_id[obj_id] + def _remap(self): + for e in self: + self._map_element(e) + def _find_object(self, __object: T) -> Optional[T]: for name, value in __object.indexing_values: if value in self._indexed_values[name]: @@ -94,6 +98,7 @@ class Collection(Generic[T]): if __object is None: return + self._remap() existing_object = self._find_object(__object) if existing_object is None: @@ -112,15 +117,10 @@ class Collection(Generic[T]): b = __object.__getattribute__(attribute) object_trace(f"Syncing [{a}{id(a)}] = [{b}{id(b)}]") - data_to_extend = b.data - a._collection_for.update(b._collection_for) for synced_with, key in b._collection_for.items(): synced_with.__setattr__(key, a) - a.extend(data_to_extend) - - else: # merge only if the two objects are not the same if existing_object.id == __object.id: diff --git a/music_kraken/objects/parents.py b/music_kraken/objects/parents.py index 2f04b45..4db562f 100644 --- a/music_kraken/objects/parents.py +++ b/music_kraken/objects/parents.py @@ -32,7 +32,6 @@ class InnerData: """ Attribute versions keep track, of if the attribute has been changed. """ - _attribute_versions: Dict[str, int] = None def __init__(self, object_type, **kwargs): self._refers_to_instances = set() @@ -249,6 +248,8 @@ class OuterProxy: return r + INDEX_DEPENDS_ON: List[str] = [] + @property def indexing_values(self) -> List[Tuple[str, object]]: """ diff --git a/music_kraken/objects/song.py b/music_kraken/objects/song.py index 37f1a6e..8da5e16 100644 --- a/music_kraken/objects/song.py +++ b/music_kraken/objects/song.py @@ -43,7 +43,8 @@ def get_collection_string( template: str, ignore_titles: Set[str] = None, background: BColors = OPTION_BACKGROUND, - foreground: BColors = OPTION_FOREGROUND + foreground: BColors = OPTION_FOREGROUND, + add_id: bool = False, ) -> str: if collection.empty: return "" @@ -55,8 +56,15 @@ def get_collection_string( r = background + def get_element_str(element) -> str: + nonlocal add_id + r = element.title_string.strip() + if add_id: + r += " " + str(element.id) + return r + element: Base - titel_list: List[str] = [element.title_string.strip() for element in collection if element.title_string not in ignore_titles] + titel_list: List[str] = [get_element_str(element) for element in collection if element.title_string not in ignore_titles] for i, titel in enumerate(titel_list): delimiter = ", " @@ -151,13 +159,14 @@ class Song(Base): self.album_collection.extend(object_list) return + INDEX_DEPENDS_ON = ("title", "isrc", "source_collection") + @property def indexing_values(self) -> List[Tuple[str, object]]: return [ - ('id', self.id), ('title', unify(self.title)), ('isrc', self.isrc), - *[('url', source.url) for source in self.source_collection] + *self.source_collection.indexing_values(), ] @property @@ -304,13 +313,14 @@ class Album(Base): self.label_collection.extend(object_list) return + INDEX_DEPENDS_ON = ("title", "barcode", "source_collection") + @property def indexing_values(self) -> List[Tuple[str, object]]: return [ - ('id', self.id), ('title', unify(self.title)), ('barcode', self.barcode), - *[('url', source.url) for source in self.source_collection] + *self.source_collection.indexing_values(), ] @property @@ -545,13 +555,13 @@ class Artist(Base): # replace the old collection with the new one self.main_album_collection: Collection = Collection(data=album_list, element_type=Album) + INDEX_DEPENDS_ON = ("name", "source_collection", "contact_collection") @property def indexing_values(self) -> List[Tuple[str, object]]: return [ - ('id', self.id), ('name', unify(self.name)), - *[('url', source.url) for source in self.source_collection], - *[('contact', contact.value) for contact in self.contact_collection] + *[('contact', contact.value) for contact in self.contact_collection], + *self.source_collection.indexing_values(), ] @property diff --git a/music_kraken/objects/source.py b/music_kraken/objects/source.py index 26425a8..b605aec 100644 --- a/music_kraken/objects/source.py +++ b/music_kraken/objects/source.py @@ -167,4 +167,8 @@ class SourceCollection: @property def homepage_list(self) -> List[str]: - return [source.homepage for source in self.source_pages] \ No newline at end of file + return [source.homepage for source in self.source_pages] + + def indexing_values(self) -> Generator[Tuple[str, str], None, None]: + for index in self._indexed_sources: + yield "url", index \ No newline at end of file