refactored

This commit is contained in:
Hellow2 2023-02-09 15:49:22 +01:00
parent c3db4df70b
commit 7b3f4a2b81
2 changed files with 56 additions and 58 deletions

View File

@ -101,23 +101,27 @@ class Database:
return return
if type(db_object) == Song: if type(db_object) == Song:
return self.push_song(song=db_object) return self.push_song(song=db_object, pushed=set())
"""
if type(db_object) == Lyrics: if type(db_object) == Lyrics:
return self.push_lyrics(lyrics=db_object) return self.push_lyrics(lyrics=db_object)
if type(db_object) == Target: if type(db_object) == Target:
return self.push_target(target=db_object) return self.push_target(target=db_object)
"""
if type(db_object) == Artist: if type(db_object) == Artist:
return self.push_artist(artist=db_object) return self.push_artist(artist=db_object, pushed=set())
"""
if type(db_object) == Source: if type(db_object) == Source:
# needs to have the property type_enum or type_str set # needs to have the property type_enum or type_str set
return self.push_source(source=db_object) return self.push_source(source=db_object)
"""
if type(db_object) == Album: if type(db_object) == Album:
return self.push_album(album=db_object) return self.push_album(album=db_object, pushed=set())
logger.warning(f"type {type(db_object)} isn't yet supported by the db") logger.warning(f"type {type(db_object)} isn't yet supported by the db")
@ -134,10 +138,14 @@ class Database:
for db_object in db_object_list: for db_object in db_object_list:
self.push_one(db_object) self.push_one(db_object)
def push_album(self, album: Album): def push_album(self, album: Album, pushed: set):
table = "Album" table = "Album"
query = f"INSERT OR REPLACE INTO {table} (id, title, label, album_status, language, date, date_format, country, barcode, albumsort, is_split) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" query = f"INSERT OR REPLACE INTO {table} (id, title, label, album_status, language, date, date_format, country, barcode, albumsort, is_split) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
if album.id in pushed:
return
pushed.add(album.id)
date_format, date = album.date.get_timestamp_w_format() date_format, date = album.date.get_timestamp_w_format()
values = ( values = (
@ -157,19 +165,24 @@ class Database:
self.connection.commit() self.connection.commit()
for song in album.tracklist: for song in album.tracklist:
self.push_song(song) self.push_song(song, pushed=pushed)
for artist in album.artists: for artist in album.artists:
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) self.push_artist(artist, pushed=pushed)
for source in album.source_list: for source in album.source_list:
source.type_enum = SourceTypes.ALBUM source.type_enum = SourceTypes.ALBUM
source.add_song(album) source.add_song(album)
self.push_source(source=source) self.push_source(source=source)
def push_song(self, song: Song): def push_song(self, song: Song, pushed: set):
if song.dynamic: if song.dynamic:
return return
if song.id in pushed:
return
pushed.add(song.id)
# ADDING THE DATA FOR THE SONG OBJECT # ADDING THE DATA FOR THE SONG OBJECT
""" """
db_field - object attribute db_field - object attribute
@ -210,18 +223,18 @@ class Database:
for main_artist in song.main_artist_list: for main_artist in song.main_artist_list:
self.push_artist_song(artist_ref=Reference(main_artist.id), song_ref=Reference(song.id), is_feature=False) self.push_artist_song(artist_ref=Reference(main_artist.id), song_ref=Reference(song.id), is_feature=False)
self.push_artist(artist=main_artist) self.push_artist(artist=main_artist, pushed=pushed)
for feature_artist in song.feature_artist_list: for feature_artist in song.feature_artist_list:
self.push_artist_song(artist_ref=Reference(feature_artist.id), song_ref=Reference(song.id), is_feature=True) self.push_artist_song(artist_ref=Reference(feature_artist.id), song_ref=Reference(song.id), is_feature=True)
self.push_artist(artist=feature_artist) self.push_artist(artist=feature_artist, pushed=pushed)
if song.album is not None: if song.album is not None:
self.push_album(song.album) self.push_album(song.album, pushed=pushed)
def push_lyrics(self, lyrics: Lyrics, ): def push_lyrics(self, lyrics: Lyrics):
if lyrics.song_ref_id is None: if lyrics.dynamic:
logger.warning("the Lyrics don't refer to a song") return
table = "Lyrics" table = "Lyrics"
query = f"INSERT OR REPLACE INTO {table} (id, song_id, text, language) VALUES (?, ?, ?, ?);" query = f"INSERT OR REPLACE INTO {table} (id, song_id, text, language) VALUES (?, ?, ?, ?);"
@ -236,8 +249,8 @@ class Database:
self.connection.commit() self.connection.commit()
def push_source(self, source: Source): def push_source(self, source: Source):
if source.song_ref_id is None: if source.dynamic:
logger.warning(f"the Source {source} don't refer to a song") return
table = "Source" table = "Source"
query = f"INSERT OR REPLACE INTO {table} (id, type, song_id, src, url) VALUES (?, ?, ?, ?, ?);" query = f"INSERT OR REPLACE INTO {table} (id, type, song_id, src, url) VALUES (?, ?, ?, ?, ?);"
@ -253,8 +266,8 @@ class Database:
self.connection.commit() self.connection.commit()
def push_target(self, target: Target): def push_target(self, target: Target):
if target.song_ref_id is None: if target.dynamic:
logger.warning("the Target doesn't refer to a song") return
table = "Target" table = "Target"
query = f"INSERT OR REPLACE INTO {table} (id, song_id, file, path) VALUES (?, ?, ?, ?);" query = f"INSERT OR REPLACE INTO {table} (id, song_id, file, path) VALUES (?, ?, ?, ?);"
@ -305,7 +318,13 @@ class Database:
self.cursor.execute(query, values) self.cursor.execute(query, values)
self.connection.commit() self.connection.commit()
def push_artist(self, artist: Artist): def push_artist(self, artist: Artist, pushed: set):
if artist.dynamic:
return
if artist.id in pushed:
return
pushed.add(artist.id)
table = "Artist" table = "Artist"
query = f"INSERT OR REPLACE INTO {table} (id, name) VALUES (?, ?);" query = f"INSERT OR REPLACE INTO {table} (id, name) VALUES (?, ?);"
values = ( values = (
@ -318,11 +337,11 @@ class Database:
for song in artist.feature_songs: for song in artist.feature_songs:
self.push_artist_song(artist_ref=artist.reference, song_ref=song.reference, is_feature=True) self.push_artist_song(artist_ref=artist.reference, song_ref=song.reference, is_feature=True)
self.push_song(song=song) self.push_song(song=song, pushed=pushed)
for song in artist.main_songs: for song in artist.main_songs:
self.push_artist_song(artist_ref=artist.reference, song_ref=song.reference, is_feature=False) self.push_artist_song(artist_ref=artist.reference, song_ref=song.reference, is_feature=False)
self.push_song(song=song) self.push_song(song=song, pushed=pushed)
for album in artist.main_albums: for album in artist.main_albums:
self.push_artist_album(artist_ref=artist.reference, album_ref=album.reference) self.push_artist_album(artist_ref=artist.reference, album_ref=album.reference)

View File

@ -395,10 +395,8 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
id_: str = None, id_: str = None,
name: str = None, name: str = None,
source_list: List[Source] = None, source_list: List[Source] = None,
main_songs: List[Song] = None,
feature_songs: List[Song] = None, feature_songs: List[Song] = None,
main_albums: List[Album] = None, main_albums: List[Album] = None,
album_type: str = None,
notes: FormattedText = None, notes: FormattedText = None,
lyrical_themes: List[str] = None, lyrical_themes: List[str] = None,
general_genre: str = "", general_genre: str = "",
@ -410,7 +408,6 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
""" """
TODO implement album type and notes TODO implement album type and notes
""" """
self.album_type = album_type
self.country: pycountry.Country = country self.country: pycountry.Country = country
self.formed_in: ID3Timestamp = formed_in self.formed_in: ID3Timestamp = formed_in
""" """
@ -418,24 +415,11 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
which are meant to only use in outputs to describe the object which are meant to only use in outputs to describe the object
i mean do as you want but there aint no strict rule about em so good luck i mean do as you want but there aint no strict rule about em so good luck
""" """
self.notes: FormattedText = notes self.notes: FormattedText = notes or FormattedText()
if self.notes is None: self.lyrical_themes: List[str] = lyrical_themes or []
self.notes = FormattedText()
self.lyrical_themes: List[str] = lyrical_themes
if self.lyrical_themes is None:
self.lyrical_themes = []
self.general_genre = general_genre self.general_genre = general_genre
if main_albums is None: self.name: str = name
main_albums = []
if feature_songs is None:
feature_songs = []
if main_songs is None:
main_songs = []
self.name: str | None = name
self.main_songs = main_songs
self.feature_songs = Collection( self.feature_songs = Collection(
data=feature_songs, data=feature_songs,
@ -478,19 +462,16 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
return feature_release return feature_release
def get_songs(self) -> Album: def get_all_songs(self) -> List[Song]:
song_release = Album( """
title="song collection", returns a list of all Songs.
album_status="dynamic", probaply not that usefull, because it is unsorted
is_split=False, """
albumsort=666, collection = []
dynamic=True for album in self.discography:
) collection.extend(album)
for song in self.main_songs:
song_release.add_song(song) return collection
for song in self.feature_songs:
song_release.add_song(song)
return song_release
def get_discography(self) -> List[Album]: def get_discography(self) -> List[Album]:
flat_copy_discography = self.main_albums.copy() flat_copy_discography = self.main_albums.copy()
@ -508,10 +489,8 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
def get_options(self) -> list: def get_options(self) -> list:
options = [self] options = [self]
for album in self.main_albums: options.extend(self.main_albums)
new_album: Album = copy.copy(album) options.extend(self.feature_songs)
new_album.artists.append(self)
options.append(new_album)
return options return options
def get_option_string(self) -> str: def get_option_string(self) -> str:
@ -519,4 +498,4 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
discography: List[Album] = property(fget=get_discography) discography: List[Album] = property(fget=get_discography)
features: Album = property(fget=get_features) features: Album = property(fget=get_features)
songs: Album = property(fget=get_songs) all_songs: Album = property(fget=get_all_songs)