refactored and cleaned up data objects

This commit is contained in:
Hellow 2023-02-23 23:52:41 +01:00
parent 04c5266f43
commit 819111c53a
5 changed files with 204 additions and 108 deletions

View File

@ -196,7 +196,7 @@ class Database:
for song in album.tracklist: for song in album.tracklist:
self.push_song(song, pushed=pushed) self.push_song(song, pushed=pushed)
for artist in album.artists: for artist in album.artist_collection:
self.push_artist_album(artist_ref=artist.reference, album_ref=album.reference) self.push_artist_album(artist_ref=artist.reference, album_ref=album.reference)
self.push_artist(artist, pushed=pushed) self.push_artist(artist, pushed=pushed)
@ -657,7 +657,7 @@ class Database:
artists = self.pull_artists(artist_ref, flat=flat_artist, exclude_relations=new_exclude_relations) artists = self.pull_artists(artist_ref, flat=flat_artist, exclude_relations=new_exclude_relations)
if len(artists) < 1: if len(artists) < 1:
continue continue
album_obj.artists.append(artists[0]) album_obj.artist_collection.append(artists[0])
return album_obj return album_obj

View File

@ -215,7 +215,7 @@ class WritingSession:
self.db_objects[data_models.AlbumSong].append(db_song_album) self.db_objects[data_models.AlbumSong].append(db_song_album)
for artist in album.artists: for artist in album.artist_collection:
db_album_artist = data_models.AlbumArtist( db_album_artist = data_models.AlbumArtist(
album = album, album = album,
artist = self.add_artist(artist) artist = self.add_artist(artist)

View File

@ -3,23 +3,22 @@ from typing import List
from .source import SourceAttribute from .source import SourceAttribute
from src.music_kraken.utils import string_processing from src.music_kraken.utils import string_processing
class Collection: class Collection:
""" """
This an class for the iterables This an class for the iterables
like tracklist or discography like tracklist or discography
""" """
_data: List[SourceAttribute] _data: List[SourceAttribute]
_by_url: dict _by_url: dict
_by_attribute: dict _by_attribute: dict
def __init__(self, data: list = None, map_attributes: list = None, element_type=None) -> None: def __init__(self, data: list = None, map_attributes: list = None, element_type=None) -> None:
""" """
Attribute needs to point to Attribute needs to point to
""" """
self._by_url = dict() self._by_url = dict()
self.map_attributes = map_attributes or [] self.map_attributes = map_attributes or []
self.element_type = element_type self.element_type = element_type
@ -30,6 +29,9 @@ class Collection:
for element in self._data: for element in self._data:
self.map_element(element=element) self.map_element(element=element)
def sort(self, reverse: bool = False, **kwargs):
self._data.sort(reverse=reverse, **kwargs)
def map_element(self, element: SourceAttribute): def map_element(self, element: SourceAttribute):
for source_url in element.source_url_map: for source_url in element.source_url_map:
self._by_url[source_url] = element self._by_url[source_url] = element
@ -42,7 +44,6 @@ class Collection:
self._by_attribute[attr][string_processing.unify(value)] = element self._by_attribute[attr][string_processing.unify(value)] = element
def get_object_with_source(self, url: str) -> any: def get_object_with_source(self, url: str) -> any:
""" """
Returns either None, or the object, that has a source Returns either None, or the object, that has a source
@ -61,7 +62,6 @@ class Collection:
def append(self, element: SourceAttribute): def append(self, element: SourceAttribute):
if type(element) is not self.element_type and self.element_type is not None: if type(element) is not self.element_type and self.element_type is not None:
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}")
self._data.append(element) self._data.append(element)
@ -77,6 +77,12 @@ class Collection:
def __len__(self) -> int: def __len__(self) -> int:
return len(self._data) return len(self._data)
def __getitem__(self, item):
if type(item) != int:
return ValueError("key needs to be an integer")
return self._data[item]
def copy(self) -> List: def copy(self) -> List:
""" """
returns a shallow copy of the data list returns a shallow copy of the data list

View File

@ -142,6 +142,21 @@ class ID3Timestamp:
second=second second=second
) )
def __lt__(self, other):
return self.date_obj < other.date_obj
def __le__(self, other):
return self.date_obj <= other.date_obj
def __gt__(self, other):
return self.date_obj > other.date_obj
def __ge__(self, other):
return self.date_obj >= other.date_obj
def __eq__(self, other):
return self.date_obj == other.date_obj
def get_time_format(self) -> str: def get_time_format(self) -> str:
""" """
https://mutagen-specs.readthedocs.io/en/latest/id3/id3v2.4.0-structure.html https://mutagen-specs.readthedocs.io/en/latest/id3/id3v2.4.0-structure.html

View File

@ -1,5 +1,5 @@
import os import os
from typing import List, Optional, Type from typing import List, Optional, Type, Dict
import pycountry import pycountry
import copy import copy
@ -121,7 +121,7 @@ class Song(DatabaseObject, SourceAttribute, MetadataAttribute):
tracksort: int = None, tracksort: int = None,
genre: str = None, genre: str = None,
source_list: List[Source] = None, source_list: List[Source] = None,
target: Target = None, target_list: Target = None,
lyrics_list: List[Lyrics] = None, lyrics_list: List[Lyrics] = None,
album_list: Type['Album'] = None, album_list: Type['Album'] = None,
main_artist_list: List[Type['Artist']] = None, main_artist_list: List[Type['Artist']] = None,
@ -133,7 +133,6 @@ class Song(DatabaseObject, SourceAttribute, MetadataAttribute):
""" """
super().__init__(id_=id_, **kwargs) super().__init__(id_=id_, **kwargs)
# attributes # attributes
# *private* attributes
self.title: str = title self.title: str = title
self.isrc: str = isrc self.isrc: str = isrc
self.length: int = length self.length: int = length
@ -142,18 +141,19 @@ class Song(DatabaseObject, SourceAttribute, MetadataAttribute):
self.source_list = source_list or [] self.source_list = source_list or []
self.target = target or Target() self.target_list = target_list or []
self.lyrics_list = lyrics_list or []
# initialize with either a passed in album, or an empty one, self.lyrics_collection: Collection = Collection(
# so it can at least properly generate dynamic attributes data=lyrics_list or [],
map_attributes=[],
element_type=Lyrics
)
""" self.album_collection: Collection = Collection(
TODO data=album_list or [],
put in collection map_attributes=["title"],
""" element_type=Album
self._album: List[Type['Album']] = album_list or [] )
self.album = album
self.main_artist_collection = Collection( self.main_artist_collection = Collection(
data=main_artist_list or [], data=main_artist_list or [],
@ -207,11 +207,11 @@ class Song(DatabaseObject, SourceAttribute, MetadataAttribute):
}) })
metadata.merge_many([s.get_song_metadata() for s in self.source_list]) metadata.merge_many([s.get_song_metadata() for s in self.source_list])
if not self.album.empty: metadata.merge_many([a.metadata for a in self.album_collection])
metadata.merge(self.album.metadata) metadata.merge_many([a.metadata for a in self.main_artist_collection])
metadata.merge_many([a.metadata for a in self.main_artist_list]) metadata.merge_many([a.metadata for a in self.feature_artist_collection])
metadata.merge_many([a.metadata for a in self.feature_artist_list]) metadata.merge_many([lyrics.metadata for lyrics in self.lyrics_collection])
metadata.merge_many([l.metadata for l in self.lyrics])
return metadata return metadata
def get_options(self) -> list: def get_options(self) -> list:
@ -221,9 +221,8 @@ class Song(DatabaseObject, SourceAttribute, MetadataAttribute):
:return: a list of objects that are related to the Song object :return: a list of objects that are related to the Song object
""" """
options = self.main_artist_list.copy() options = self.main_artist_list.copy()
options.extend(self.feature_artist_list.copy()) options.extend(self.feature_artist_collection)
if not self.album.empty: options.extend(self.album_collection)
options.append(self.album)
options.append(self) options.append(self)
return options return options
@ -234,6 +233,9 @@ class Song(DatabaseObject, SourceAttribute, MetadataAttribute):
main_artist_list: List[Type['Artist']] = property(fget=lambda self: self.main_artist_collection.copy()) 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()) 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())
""" """
All objects dependent on Album All objects dependent on Album
@ -245,7 +247,6 @@ class Album(DatabaseObject, SourceAttribute, MetadataAttribute):
self, self,
id_: str = None, id_: str = None,
title: str = None, title: str = None,
label: str = None,
language: pycountry.Languages = None, language: pycountry.Languages = None,
date: ID3Timestamp = None, date: ID3Timestamp = None,
barcode: str = None, barcode: str = None,
@ -254,24 +255,20 @@ class Album(DatabaseObject, SourceAttribute, MetadataAttribute):
dynamic: bool = False, dynamic: bool = False,
source_list: List[Source] = None, source_list: List[Source] = None,
artist_list: list = None, artist_list: list = None,
tracklist: List[Song] = None, song_list: List[Song] = None,
album_status: AlbumStatus = None, album_status: AlbumStatus = None,
album_type: AlbumType = None, album_type: AlbumType = None,
label_list: List[Type['Label']] = None,
**kwargs **kwargs
) -> None: ) -> None:
DatabaseObject.__init__(self, id_=id_, dynamic=dynamic, **kwargs) DatabaseObject.__init__(self, id_=id_, dynamic=dynamic, **kwargs)
"""
TODO
add to db
"""
self.album_type: AlbumType = album_type
self.title: str = title self.title: str = title
self.album_status: AlbumStatus = album_status self.album_status: AlbumStatus = album_status
self.label = label self.album_type: AlbumType = album_type
self.language: pycountry.Languages = language self.language: pycountry.Languages = language
self.date: ID3Timestamp = date or ID3Timestamp() self.date: ID3Timestamp = date or ID3Timestamp()
""" """
TODO TODO
find out the id3 tag for barcode and implement it find out the id3 tag for barcode and implement it
@ -286,56 +283,76 @@ class Album(DatabaseObject, SourceAttribute, MetadataAttribute):
""" """
self.albumsort: Optional[int] = albumsort self.albumsort: Optional[int] = albumsort
self._tracklist = Collection( self.song_collection: Collection = Collection(
data=tracklist or [], data=song_list or [],
map_attributes=["title"], map_attributes=["title"],
element_type=Song element_type=Song
) )
self.source_list = source_list or []
self.artists = Collection( self.artist_collection: Collection = Collection(
data=artist_list or [], data=artist_list or [],
map_attributes=["name"], map_attributes=["name"],
element_type=Artist element_type=Artist
) )
def __str__(self) -> str: self.label_collection: Collection = Collection(
return f"-----{self.title}-----\n{self.tracklist}" data=label_list,
map_attributes=["name"],
element_type=Label
)
self.source_list = source_list or []
def __repr__(self): def __repr__(self):
return f"Album(\"{self.title}\")" return f"Album(\"{self.title}\")"
def __len__(self) -> int: def update_tracksort(self):
return len(self.tracklist) """
This updates the tracksort attributes, of the songs in
`self.song_collection`, and sorts the songs, if possible.
def set_tracklist(self, tracklist): It is advised to only call this function, once all the tracks are
tracklist_list = [] added to the songs.
if type(tracklist) == Collection:
tracklist_list = tracklist_list
elif type(tracklist) == list:
tracklist_list = tracklist
self._tracklist = Collection( :return:
data=tracklist_list, """
map_attributes=["title"],
element_type=Song tracksort_map: Dict[int, Song] = {song.tracksort: song for song in self.song_collection if
) song.tracksort is not None}
# place the songs, with set tracksort attribute according to it
for tracksort, song in tracksort_map.items():
index = tracksort - 1
"""
I ONLY modify the `Collection._data` attribute directly,
to bypass the mapping of the attributes, because I will add the item in the next step
"""
self.song_collection._data.remove(song)
self.song_collection._data.insert(index, song)
# fill in the empty tracksort attributes
for i, song in enumerate(self.song_collection):
if song.tracksort is not None:
continue
song.tracksort = i + 1
def get_metadata(self) -> MetadataAttribute.Metadata: def get_metadata(self) -> MetadataAttribute.Metadata:
return MetadataAttribute.Metadata({ return MetadataAttribute.Metadata({
id3Mapping.ALBUM: [self.title], id3Mapping.ALBUM: [self.title],
id3Mapping.COPYRIGHT: [self.copyright], id3Mapping.COPYRIGHT: [self.copyright],
id3Mapping.LANGUAGE: [self.iso_639_2_language], id3Mapping.LANGUAGE: [self.iso_639_2_language],
id3Mapping.ALBUM_ARTIST: [a.name for a in self.artists], id3Mapping.ALBUM_ARTIST: [a.name for a in self.artist_collection],
id3Mapping.DATE: [self.date.timestamp] id3Mapping.DATE: [self.date.timestamp]
}) })
def get_copyright(self) -> str: def get_copyright(self) -> str:
if self.date is None: if self.date is None:
return "" return ""
if self.date.year == 1 or self.label is None: if self.date.has_year or len(self.label_collection) == 0:
return "" return ""
return f"{self.date.year} {self.label}" return f"{self.date.year} {self.label_collection[0].name}"
def get_iso_639_2_lang(self) -> Optional[str]: def get_iso_639_2_lang(self) -> Optional[str]:
if self.language is None: if self.language is None:
@ -344,21 +361,22 @@ class Album(DatabaseObject, SourceAttribute, MetadataAttribute):
return self.language.alpha_3 return self.language.alpha_3
def get_options(self) -> list: def get_options(self) -> list:
options = self.artists.copy() options = self.artist_collection.copy()
options.append(self) options.append(self)
for track in self.tracklist: options.extend(self.song_collection)
new_track: Song = copy.copy(track)
new_track.album = self
options.append(new_track)
return options return options
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.artists])}" 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) copyright = property(fget=get_copyright)
iso_639_2_language = property(fget=get_iso_639_2_lang) iso_639_2_language = property(fget=get_iso_639_2_lang)
tracklist: Collection = property(fget=lambda self: self._tracklist, fset=set_tracklist)
""" """
@ -367,25 +385,19 @@ All objects dependent on Artist
class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
"""
main_songs
feature_song
albums
"""
def __init__( def __init__(
self, self,
id_: str = None, id_: str = None,
name: str = None, name: str = None,
source_list: List[Source] = None, source_list: List[Source] = None,
feature_songs: List[Song] = None, feature_song_list: List[Song] = None,
main_albums: List[Album] = None, main_album_list: List[Album] = None,
notes: FormattedText = None, notes: FormattedText = None,
lyrical_themes: List[str] = None, lyrical_themes: List[str] = None,
general_genre: str = "", general_genre: str = "",
country: CountryTyping = None, country: CountryTyping = None,
formed_in: ID3Timestamp = None formed_in: ID3Timestamp = None,
label_list: List[Type['Label']] = None,
): ):
DatabaseObject.__init__(self, id_=id_) DatabaseObject.__init__(self, id_=id_)
@ -405,20 +417,25 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
self.name: str = name self.name: str = name
self.feature_songs = Collection( self.feature_song_collection: Collection = Collection(
data=feature_songs, data=feature_song_list,
map_attributes=["title"], map_attributes=["title"],
element_type=Song element_type=Song
) )
self.main_albums = Collection( self.main_album_collection: Collection = Collection(
data=main_albums, data=main_album_list,
map_attributes=["title"], map_attributes=["title"],
element_type=Album element_type=Album
) )
if source_list is not None: self.label_collection: Collection = Collection(
self.source_list = source_list data=label_list,
map_attributes=["name"],
element_type=Label
)
self.source_list = source_list or []
def __str__(self): def __str__(self):
string = self.name or "" string = self.name or ""
@ -428,10 +445,24 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
return string return string
def __repr__(self): def __repr__(self):
return self.__str__() return f"Artist(\"{self.name}\")"
def __eq__(self, __o: DatabaseObject) -> bool: def update_albumsort(self):
return self.id_ == __o.id_ """
This updates the albumsort attributes, of the albums in
`self.main_album_collection`, and sorts the albums, if possible.
It is advised to only call this function, once all the albums are
added to the artist.
:return:
"""
self.main_album_collection.sort(key=lambda _album: _album.date)
for i, album in enumerate(self.main_album_collection):
if album.albumsort is None:
continue
album.albumsort = i + 1
def get_features(self) -> Album: def get_features(self) -> Album:
feature_release = Album( feature_release = Album(
@ -440,28 +471,12 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
album_type=AlbumType.COMPILATION_ALBUM, album_type=AlbumType.COMPILATION_ALBUM,
is_split=True, is_split=True,
albumsort=666, albumsort=666,
dynamic=True dynamic=True,
song_list=self.feature_song_collection.copy()
) )
return feature_release return feature_release
def get_all_songs(self) -> List[Song]:
"""
returns a list of all Songs.
probably not that useful, because it is unsorted
"""
collection = []
for album in self.discography:
collection.extend(album.tracklist)
return collection
def get_discography(self) -> List[Album]:
flat_copy_discography = self.main_albums.copy()
flat_copy_discography.append(self.get_features())
return flat_copy_discography
def get_metadata(self) -> MetadataAttribute.Metadata: def get_metadata(self) -> MetadataAttribute.Metadata:
metadata = MetadataAttribute.Metadata({ metadata = MetadataAttribute.Metadata({
id3Mapping.ARTIST: [self.name] id3Mapping.ARTIST: [self.name]
@ -472,13 +487,73 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
def get_options(self) -> list: def get_options(self) -> list:
options = [self] options = [self]
options.extend(self.main_albums) options.extend(self.main_album_collection)
options.extend(self.feature_songs) options.extend(self.feature_song_collection)
return options return options
def get_option_string(self) -> str: def get_option_string(self) -> str:
return f"Artist: {self.name}" return f"Artist: {self.name}"
def get_all_songs(self) -> List[Song]:
"""
returns a list of all Songs.
probably not that useful, because it is unsorted
"""
collection = self.feature_song_collection.copy()
for album in self.discography:
collection.extend(album.song_collection)
return collection
def get_discography(self) -> List[Album]:
flat_copy_discography = self.main_album_collection.copy()
flat_copy_discography.append(self.get_features())
return flat_copy_discography
album_list: List[Album] = property(fget=lambda self: self.album_collection.copy())
complete_album_list: List[Album] = property(fget=get_discography)
discography: List[Album] = property(fget=get_discography) discography: List[Album] = property(fget=get_discography)
features: Album = property(fget=get_features)
all_songs: Album = property(fget=get_all_songs) feature_album: Album = property(fget=get_features)
song_list: List[Song] = property(fget=get_all_songs)
label_list: List[Type['Label']] = property(fget=lambda self: self.label_collection.copy())
"""
Label
"""
class Label(DatabaseObject, SourceAttribute, MetadataAttribute):
def __init__(
self,
id_: str = None,
name: str = None,
album_list: List[Album] = None,
current_artist_list: List[Artist] = None,
source_list: List[Source] = None,
**kwargs
):
DatabaseObject.__init__(self, id_=id_)
self.name: str = name
self.album_collection: Collection = Collection(
data=album_list,
map_attributes=["title"],
element_type=Album
)
self.current_artist_collection: Collection = Collection(
data=current_artist_list,
map_attributes=["name"],
element_type=Artist
)
self.source_list = source_list or []
self.additional_attributes = kwargs
album_list = property(fget=lambda self: self.album_collection.copy())
current_artist_list = property(fget=lambda self: self.current_artist_collection.copy())