implemented options

This commit is contained in:
Hellow 2023-03-10 21:28:13 +01:00
parent 76a91f57cb
commit d8be349a40
4 changed files with 64 additions and 28 deletions

View File

@ -62,7 +62,7 @@ artist = EncyclopaediaMetallum.fetch_artist_details(artist, flat=False)
print_artist(artist) print_artist(artist)
""" """
results = EncyclopaediaMetallum.search_by_query("#a Ghost Bath") results = EncyclopaediaMetallum.search_by_query("#a Thy art is Murder")
artist = results[0] artist = results[0]
print(artist) print(artist)

View File

@ -0,0 +1,28 @@
from typing import TYPE_CHECKING, List
if TYPE_CHECKING:
from .parents import DatabaseObject
class Options:
def __init__(self, option_list: List[DatabaseObject] = None):
self._data: List[DatabaseObject] = option_list or list()
def __str__(self):
return "\n".join(f"{i:02d}: {database_object.option_string}" for i, database_object in enumerate(self._data))
def __iter__(self):
for database_object in self._data:
yield database_object
def get_next_options(self, index: int) -> 'Options':
if index >= len(self._data):
raise ValueError("Index out of bounds")
return self._data[index].options
def __getitem__(self, item: int) -> 'Options':
if type(item) != int:
raise TypeError("Key needs to be an Integer")
return self.get_next_options(item)

View File

@ -6,6 +6,7 @@ from ..utils.shared import (
SONG_LOGGER as LOGGER SONG_LOGGER as LOGGER
) )
from .metadata import Metadata from .metadata import Metadata
from .option import Options
class DatabaseObject: class DatabaseObject:
@ -75,8 +76,8 @@ class DatabaseObject:
return Metadata() return Metadata()
@property @property
def option_list(self) -> List[Type['DatabaseObject']]: def options(self) -> Options:
return [self] return Options([self])
@property @property
def option_string(self) -> str: def option_string(self) -> str:

View File

@ -27,6 +27,7 @@ from .collection import Collection
from .album import AlbumType, AlbumStatus from .album import AlbumType, AlbumStatus
from .lyrics import Lyrics from .lyrics import Lyrics
from .target import Target from .target import Target
from .option import Options
""" """
All Objects dependent All Objects dependent
@ -136,15 +137,7 @@ class Song(MainObject):
f"feat. Artist({OPTION_STRING_DELIMITER.join(artist.name for artist in self.feature_artist_collection)})" f"feat. Artist({OPTION_STRING_DELIMITER.join(artist.name for artist in self.feature_artist_collection)})"
@property @property
def tracksort_str(self) -> str: def options(self) -> Options:
"""
if the album tracklist is empty, it sets it length to 1, this song has to be in the Album
:returns id3_tracksort: {song_position}/{album.length_of_tracklist}
"""
return f"{self.tracksort}/{len(self.album.tracklist) or 1}"
@property
def option_list(self) -> list:
""" """
Return a list of related objects including the song object, album object, main artist objects, and feature artist objects. Return a list of related objects including the song object, album object, main artist objects, and feature artist objects.
@ -154,7 +147,15 @@ class Song(MainObject):
options.extend(self.feature_artist_collection) options.extend(self.feature_artist_collection)
options.extend(self.album_collection) options.extend(self.album_collection)
options.append(self) options.append(self)
return options return Options(options)
@property
def tracksort_str(self) -> str:
"""
if the album tracklist is empty, it sets it length to 1, this song has to be in the Album
:returns id3_tracksort: {song_position}/{album.length_of_tracklist}
"""
return f"{self.tracksort}/{len(self.album.tracklist) or 1}"
""" """
@ -240,6 +241,14 @@ class Album(MainObject):
f"by Artist({OPTION_STRING_DELIMITER.join([artist.name for artist in self.artist_collection])}) " \ f"by Artist({OPTION_STRING_DELIMITER.join([artist.name for artist in self.artist_collection])}) " \
f"under Label({OPTION_STRING_DELIMITER.join([label.name for label in self.label_collection])})" f"under Label({OPTION_STRING_DELIMITER.join([label.name for label in self.label_collection])})"
@property
def options(self) -> Options:
options = self.artist_collection.shallow_list
options.append(self)
options.extend(self.song_collection)
return Options(options)
def update_tracksort(self): def update_tracksort(self):
""" """
This updates the tracksort attributes, of the songs in This updates the tracksort attributes, of the songs in
@ -298,14 +307,6 @@ class Album(MainObject):
""" """
return len(self.artist_collection) > 1 return len(self.artist_collection) > 1
@property
def option_list(self) -> list:
options = self.artist_collection.shallow_list
options.append(self)
options.extend(self.song_collection)
return options
@ -395,6 +396,13 @@ class Artist(MainObject):
return f"{self.__repr__()} " \ return f"{self.__repr__()} " \
f"under Label({OPTION_STRING_DELIMITER.join([label.name for label in self.label_collection])})" f"under Label({OPTION_STRING_DELIMITER.join([label.name for label in self.label_collection])})"
@property
def options(self) -> Options:
options = [self]
options.extend(self.main_album_collection)
options.extend(self.feature_song_collection)
return Options(options)
@property @property
def country_string(self): def country_string(self):
return self.country.alpha_3 return self.country.alpha_3
@ -428,13 +436,6 @@ class Artist(MainObject):
song_list=self.feature_song_collection.copy() song_list=self.feature_song_collection.copy()
) )
@property
def option_list(self) -> list:
options = [self]
options.extend(self.main_album_collection)
options.extend(self.feature_song_collection)
return options
def get_all_songs(self) -> List[Song]: def get_all_songs(self) -> List[Song]:
""" """
returns a list of all Songs. returns a list of all Songs.
@ -490,3 +491,9 @@ class Label(MainObject):
('name', self.unified_name), ('name', self.unified_name),
*[('url', source.url) for source in self.source_collection] *[('url', source.url) for source in self.source_collection]
] ]
@property
def options(self) -> Options:
options = [self]
options.extend(self.current_artist_collection.shallow_list)
options.extend(self.album_collection.shallow_list)