From c01dfd00d9c75ad57e63cdaeaa8df7bd2ffa2f85 Mon Sep 17 00:00:00 2001 From: Hellow Date: Thu, 9 Mar 2023 22:14:39 +0100 Subject: [PATCH] refactorings --- README.md | 4 +- src/music_kraken/objects/collection.py | 8 +++- src/music_kraken/objects/song.py | 57 ++++++++------------------ src/music_kraken/objects/source.py | 31 ++++++++++---- 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index f7171b4..e4c1446 100644 --- a/README.md +++ b/README.md @@ -280,8 +280,8 @@ song_object = Song( -If you just wanna start implementing, then just use the code example, I dont care. -For those who don't want any bugs and use it as intended *(which is reccomended, cuz I am only one person so there are defs bugs)* continue reading. +If you just want to start implementing, then just use the code example, I don't care. +For those who don't want any bugs and use it as intended *(which is recommended, cuz I am only one person so there are defs bugs)* continue reading. ## Appending and Merging data diff --git a/src/music_kraken/objects/collection.py b/src/music_kraken/objects/collection.py index 846499f..405b12c 100644 --- a/src/music_kraken/objects/collection.py +++ b/src/music_kraken/objects/collection.py @@ -44,7 +44,13 @@ class Collection: self._attribute_to_object_map[name][value] = element def append(self, element: DatabaseObject, merge_on_conflict: bool = True): - # if the element type has ben defide in the initializer it checks if the type maches + """ + :param element: + :param merge_on_conflict: + :return: + """ + + # if the element type has been defined in the initializer it checks if the type matches if self.element_type is not None and isinstance(element, self.element_type): raise TypeError(f"{type(element)} is not the set type {self.element_type}") diff --git a/src/music_kraken/objects/song.py b/src/music_kraken/objects/song.py index bb5ede5..317c125 100644 --- a/src/music_kraken/objects/song.py +++ b/src/music_kraken/objects/song.py @@ -20,7 +20,8 @@ from .source import ( Source, SourceTypes, SourcePages, - SourceAttribute + SourceAttribute, + SourceCollection ) from .formatted_text import FormattedText from .collection import Collection @@ -35,7 +36,7 @@ All Objects dependent CountryTyping = type(list(pycountry.countries)[0]) -class Song(MainObject, SourceAttribute, MetadataAttribute): +class Song(MainObject, MetadataAttribute): """ Class representing a song object, with attributes id, mb_id, title, album_name, isrc, length, tracksort, genre, source_list, target, lyrics_list, album, main_artist_list, and feature_artist_list. @@ -77,31 +78,17 @@ class Song(MainObject, SourceAttribute, MetadataAttribute): self.tracksort: int = tracksort or 0 self.genre: str = genre - self.target_collection: Collection = Collection( - data=target_list, - element_type=Target - ) + self.source_collection: SourceCollection = SourceCollection(source_list) - self.lyrics_collection: Collection = Collection( - data=lyrics_list, - element_type=Lyrics - ) + self.target_collection: Collection = Collection(data=target_list, element_type=Target) - self.album_collection: Collection = Collection( - data=album_list, - element_type=Album - ) + self.lyrics_collection: Collection = Collection(data=lyrics_list, element_type=Lyrics) - self.main_artist_collection = Collection( - data=main_artist_list or [], - map_attributes=["title"], - element_type=Artist - ) - self.feature_artist_collection = Collection( - data=feature_artist_list or [], - map_attributes=["title"], - element_type=Artist - ) + self.album_collection: Collection = Collection(data=album_list, element_type=Album) + + self.main_artist_collection = Collection(data=main_artist_list, element_type=Artist) + + self.feature_artist_collection = Collection(data=feature_artist_list, element_type=Artist) def __eq__(self, other): if type(other) != type(self): @@ -176,11 +163,6 @@ class Song(MainObject, SourceAttribute, MetadataAttribute): return f"Song({self.title}) of Album({self.album.title}) from Artists({self.get_artist_credits()})" tracksort_str: List[Type['Album']] = property(fget=get_tracksort_str) - main_artist_list: List[Type['Artist']] = property(fget=lambda self: self.main_artist_collection.copy()) - feature_artist_list: List[Type['Artist']] = property(fget=lambda self: self.feature_artist_collection.copy()) - - album_list: List[Type['Album']] = property(fget=lambda self: self.album_collection.copy()) - lyrics_list: List[Type[Lyrics]] = property(fget=lambda self: self.lyrics_collection.copy()) """ @@ -189,6 +171,9 @@ All objects dependent on Album class Album(MainObject, SourceAttribute, MetadataAttribute): + COLLECTION_ATTRIBUTES = ("label_collection", "artist_collection", "song_collection") + SIMPLE_ATTRIBUTES = ("title", "album_status", "album_type", "language", "date", "barcode", "albumsort") + def __init__( self, _id: str = None, @@ -230,11 +215,9 @@ class Album(MainObject, SourceAttribute, MetadataAttribute): """ self.albumsort: Optional[int] = albumsort - self.song_collection: Collection = Collection( - data=song_list or [], - map_attributes=["title"], - element_type=Song - ) + self.source_collection: SourceCollection = SourceCollection(source_list) + + self.song_collection: Collection = Collection(data=song_list, element_type=Song) self.artist_collection: Collection = Collection( data=artist_list or [], @@ -338,16 +321,10 @@ class Album(MainObject, SourceAttribute, MetadataAttribute): def get_option_string(self) -> str: return f"Album: {self.title}; Artists {', '.join([i.name for i in self.artist_collection])}" - label_list: List[Type['Label']] = property(fget=lambda self: self.label_collection.copy()) - artist_list: List[Type['Artist']] = property(fget=lambda self: self.artist_collection.copy()) - song_list: List[Song] = property(fget=lambda self: self.song_collection.copy()) tracklist: List[Song] = property(fget=lambda self: self.song_collection.copy()) copyright = property(fget=get_copyright) - COLLECTION_ATTRIBUTES = ("label_collection", "artist_collection", "song_collection") - SIMPLE_ATTRIBUTES = ("title", "album_status", "album_type", "language", "date", "barcode", "albumsort") - """ All objects dependent on Artist diff --git a/src/music_kraken/objects/source.py b/src/music_kraken/objects/source.py index 67232f7..9a92ed8 100644 --- a/src/music_kraken/objects/source.py +++ b/src/music_kraken/objects/source.py @@ -1,10 +1,10 @@ +from collections import defaultdict from enum import Enum from typing import List, Dict, Tuple from .metadata import Mapping, MetadataAttribute -from .parents import ( - DatabaseObject -) +from .parents import DatabaseObject +from .collection import Collection class SourceTypes(Enum): @@ -133,6 +133,25 @@ class Source(DatabaseObject, MetadataAttribute): homepage = property(fget=lambda self: SourcePages.get_homepage(self.page_enum)) +class SourceCollection(Collection): + def __init__(self, source_list: List[Source]): + super().__init__(data=source_list, element_type=Source) + + self._page_to_source_list: Dict[SourcePages, List[Source]] = defaultdict(list) + + def map_element(self, source: Source): + super().map_element(source) + + self._page_to_source_list[source.page_enum].append(source) + + def get_sources_from_page(self, source_page: SourcePages) -> List[Source]: + """ + getting the sources for a specific page like + YouTube or musify + """ + return self._page_to_source_list[source_page] + + class SourceAttribute(DatabaseObject): """ This is a class that is meant to be inherited from. @@ -194,12 +213,6 @@ class SourceAttribute(DatabaseObject): and the value is a List with all sources of according page """ return self._source_dict - - def merge(self, other, override: bool = False): - super().merge(other, override) - - for source in other.source_list: - self.add_source(source=source) source_list: List[Source] = property(fget=get_source_list, fset=set_source_list) source_dict: Dict[object, List[Source]] = property(fget=get_source_dict)