refactorings

This commit is contained in:
Hellow 2023-03-09 22:14:39 +01:00
parent 38e7620fbd
commit c01dfd00d9
4 changed files with 48 additions and 52 deletions

View File

@ -280,8 +280,8 @@ song_object = Song(
If you just wanna start implementing, then just use the code example, I dont care. 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 reccomended, cuz I am only one person so there are defs bugs)* continue reading. 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 ## Appending and Merging data

View File

@ -44,7 +44,13 @@ class Collection:
self._attribute_to_object_map[name][value] = element self._attribute_to_object_map[name][value] = element
def append(self, element: DatabaseObject, merge_on_conflict: bool = True): 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): 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}") raise TypeError(f"{type(element)} is not the set type {self.element_type}")

View File

@ -20,7 +20,8 @@ from .source import (
Source, Source,
SourceTypes, SourceTypes,
SourcePages, SourcePages,
SourceAttribute SourceAttribute,
SourceCollection
) )
from .formatted_text import FormattedText from .formatted_text import FormattedText
from .collection import Collection from .collection import Collection
@ -35,7 +36,7 @@ All Objects dependent
CountryTyping = type(list(pycountry.countries)[0]) 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, 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. 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.tracksort: int = tracksort or 0
self.genre: str = genre self.genre: str = genre
self.target_collection: Collection = Collection( self.source_collection: SourceCollection = SourceCollection(source_list)
data=target_list,
element_type=Target
)
self.lyrics_collection: Collection = Collection( self.target_collection: Collection = Collection(data=target_list, element_type=Target)
data=lyrics_list,
element_type=Lyrics
)
self.album_collection: Collection = Collection( self.lyrics_collection: Collection = Collection(data=lyrics_list, element_type=Lyrics)
data=album_list,
element_type=Album
)
self.main_artist_collection = Collection( self.album_collection: Collection = Collection(data=album_list, element_type=Album)
data=main_artist_list or [],
map_attributes=["title"], self.main_artist_collection = Collection(data=main_artist_list, element_type=Artist)
element_type=Artist
) self.feature_artist_collection = Collection(data=feature_artist_list, element_type=Artist)
self.feature_artist_collection = Collection(
data=feature_artist_list or [],
map_attributes=["title"],
element_type=Artist
)
def __eq__(self, other): def __eq__(self, other):
if type(other) != type(self): 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()})" 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) 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): 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__( def __init__(
self, self,
_id: str = None, _id: str = None,
@ -230,11 +215,9 @@ class Album(MainObject, SourceAttribute, MetadataAttribute):
""" """
self.albumsort: Optional[int] = albumsort self.albumsort: Optional[int] = albumsort
self.song_collection: Collection = Collection( self.source_collection: SourceCollection = SourceCollection(source_list)
data=song_list or [],
map_attributes=["title"], self.song_collection: Collection = Collection(data=song_list, element_type=Song)
element_type=Song
)
self.artist_collection: Collection = Collection( self.artist_collection: Collection = Collection(
data=artist_list or [], data=artist_list or [],
@ -338,16 +321,10 @@ class Album(MainObject, SourceAttribute, MetadataAttribute):
def get_option_string(self) -> str: def get_option_string(self) -> str:
return f"Album: {self.title}; Artists {', '.join([i.name for i in self.artist_collection])}" 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()) tracklist: List[Song] = property(fget=lambda self: self.song_collection.copy())
copyright = property(fget=get_copyright) 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 All objects dependent on Artist

View File

@ -1,10 +1,10 @@
from collections import defaultdict
from enum import Enum from enum import Enum
from typing import List, Dict, Tuple from typing import List, Dict, Tuple
from .metadata import Mapping, MetadataAttribute from .metadata import Mapping, MetadataAttribute
from .parents import ( from .parents import DatabaseObject
DatabaseObject from .collection import Collection
)
class SourceTypes(Enum): class SourceTypes(Enum):
@ -133,6 +133,25 @@ class Source(DatabaseObject, MetadataAttribute):
homepage = property(fget=lambda self: SourcePages.get_homepage(self.page_enum)) 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): class SourceAttribute(DatabaseObject):
""" """
This is a class that is meant to be inherited from. 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 and the value is a List with all sources of according page
""" """
return self._source_dict 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_list: List[Source] = property(fget=get_source_list, fset=set_source_list)
source_dict: Dict[object, List[Source]] = property(fget=get_source_dict) source_dict: Dict[object, List[Source]] = property(fget=get_source_dict)