This commit is contained in:
Hazel 2023-12-20 21:31:16 +01:00
parent f413fd4d77
commit b9e6820475

View File

@ -23,7 +23,9 @@ from ..utils.enums.source import SourcePages
from ..utils.enums.album import AlbumType from ..utils.enums.album import AlbumType
from ..audio import write_metadata_to_target, correct_codec from ..audio import write_metadata_to_target, correct_codec
from ..utils.config import main_settings from ..utils.config import main_settings
from ..utils.support_classes import Query, DownloadResult from ..utils.support_classes.query import Query
from ..utils.support_classes.download_result import DownloadResult
from ..utils.string_processing import fit_to_file_system
INDEPENDENT_DB_OBJECTS = Union[Label, Album, Artist, Song] INDEPENDENT_DB_OBJECTS = Union[Label, Album, Artist, Song]
@ -53,17 +55,12 @@ class NamingDict(dict):
return type(self)(super().copy(), self.object_mappings.copy()) return type(self)(super().copy(), self.object_mappings.copy())
def __getitem__(self, key: str) -> str: def __getitem__(self, key: str) -> str:
return super().__getitem__(key) return fit_to_file_system(super().__getitem__(key))
def default_value_for_name(self, name: str) -> str: def default_value_for_name(self, name: str) -> str:
return f'Various {name.replace("_", " ").title()}' return f'Various {name.replace("_", " ").title()}'
def __missing__(self, key: str) -> str: def __missing__(self, key: str) -> str:
"""
TODO
add proper logging
"""
if "." not in key: if "." not in key:
if key not in self.CUSTOM_KEYS: if key not in self.CUSTOM_KEYS:
return self.default_value_for_name(key) return self.default_value_for_name(key)
@ -144,6 +141,9 @@ def clean_object(dirty_object: DatabaseObject) -> DatabaseObject:
Song: Collection(element_type=Song) Song: Collection(element_type=Song)
} }
if isinstance(dirty_object, Song):
return dirty_object
_clean_music_object(dirty_object, collections) _clean_music_object(dirty_object, collections)
return dirty_object return dirty_object
@ -153,11 +153,12 @@ def build_new_object(new_object: DatabaseObject) -> DatabaseObject:
return new_object return new_object
def merge_together(old_object: DatabaseObject, new_object: DatabaseObject) -> DatabaseObject: def merge_together(old_object: DatabaseObject, new_object: DatabaseObject, do_compile: bool = True) -> DatabaseObject:
new_object = clean_object(new_object) new_object = clean_object(new_object)
old_object.merge(new_object) old_object.merge(new_object)
old_object.compile(merge_into=False) if do_compile and False:
old_object.compile(merge_into=False)
return old_object return old_object
@ -220,7 +221,7 @@ class Page:
if type(music_object) in search_functions: if type(music_object) in search_functions:
r = search_functions[type(music_object)](music_object) r = search_functions[type(music_object)](music_object)
if len(r) > 0: if r is not None and len(r) > 0:
return r return r
r = [] r = []
@ -246,7 +247,7 @@ class Page:
return [] return []
def fetch_details(self, music_object: DatabaseObject, stop_at_level: int = 1) -> DatabaseObject: def fetch_details(self, music_object: DatabaseObject, stop_at_level: int = 1, post_process: bool = True) -> DatabaseObject:
""" """
when a music object with lacking data is passed in, it returns when a music object with lacking data is passed in, it returns
the SAME object **(no copy)** with more detailed data. the SAME object **(no copy)** with more detailed data.
@ -270,22 +271,21 @@ class Page:
if isinstance(music_object, INDEPENDENT_DB_OBJECTS): if isinstance(music_object, INDEPENDENT_DB_OBJECTS):
source: Source source: Source
for source in music_object.source_collection.get_sources_from_page(self.SOURCE_TYPE): for source in music_object.source_collection.get_sources_from_page(self.SOURCE_TYPE):
new_music_object.merge( new_music_object.merge(self.fetch_object_from_source(
self.fetch_object_from_source( source=source,
source=source, enforce_type=type(music_object),
enforce_type=type(music_object), stop_at_level=stop_at_level,
stop_at_level=stop_at_level, post_process=False
post_process=False ))
)
)
return merge_together(music_object, new_music_object) return music_object.merge(new_music_object)
def fetch_object_from_source(self, source: Source, stop_at_level: int = 2, enforce_type: Type[DatabaseObject] = None, post_process: bool = True) -> Optional[DatabaseObject]: def fetch_object_from_source(self, source: Source, stop_at_level: int = 2, enforce_type: Type[DatabaseObject] = None, post_process: bool = True) -> Optional[DatabaseObject]:
obj_type = self.get_source_type(source) obj_type = self.get_source_type(source)
if obj_type is None: if obj_type is None:
return None return None
if enforce_type != obj_type and enforce_type is not None: if enforce_type != obj_type and enforce_type is not None:
self.LOGGER.warning(f"Object type isn't type to enforce: {enforce_type}, {obj_type}") self.LOGGER.warning(f"Object type isn't type to enforce: {enforce_type}, {obj_type}")
return None return None
@ -305,8 +305,13 @@ class Page:
self.LOGGER.warning(f"Can't fetch details of type: {obj_type}") self.LOGGER.warning(f"Can't fetch details of type: {obj_type}")
return None return None
if post_process and music_object: if stop_at_level > 1:
return build_new_object(music_object) collection: Collection
for collection_str in music_object.DOWNWARDS_COLLECTION_STRING_ATTRIBUTES:
collection = music_object.__getattribute__(collection_str)
for sub_element in collection:
sub_element.merge(self.fetch_details(sub_element, stop_at_level=stop_at_level-1, post_process=False))
return music_object return music_object
@ -328,7 +333,7 @@ class Page:
def fill_naming_objects(naming_music_object: DatabaseObject): def fill_naming_objects(naming_music_object: DatabaseObject):
nonlocal naming_dict nonlocal naming_dict
for collection_name in naming_music_object.UPWARDS_COLLECTION_ATTRIBUTES: for collection_name in naming_music_object.UPWARDS_COLLECTION_STRING_ATTRIBUTES:
collection: Collection = getattr(naming_music_object, collection_name) collection: Collection = getattr(naming_music_object, collection_name)
if collection.empty: if collection.empty:
@ -364,7 +369,7 @@ class Page:
download_result: DownloadResult = DownloadResult() download_result: DownloadResult = DownloadResult()
for collection_name in music_object.DOWNWARDS_COLLECTION_ATTRIBUTES: for collection_name in music_object.DOWNWARDS_COLLECTION_STRING_ATTRIBUTES:
collection: Collection = getattr(music_object, collection_name) collection: Collection = getattr(music_object, collection_name)
sub_ordered_music_object: DatabaseObject sub_ordered_music_object: DatabaseObject