From f81521b014672d9616a019c3a9056ec5716f1243 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Wed, 20 Dec 2023 09:55:09 +0100 Subject: [PATCH] feat: completed the default factories --- src/create_custom_objects.py | 6 ++ .../objects/lint_default_factories.py | 66 +++++++++++++++++++ src/music_kraken/objects/lyrics.py | 9 +++ src/music_kraken/objects/parents.py | 44 +------------ src/music_kraken/objects/song.py | 24 ++++++- src/music_kraken/objects/source.py | 11 ++-- src/music_kraken/objects/target.py | 8 +++ 7 files changed, 117 insertions(+), 51 deletions(-) create mode 100644 src/music_kraken/objects/lint_default_factories.py diff --git a/src/create_custom_objects.py b/src/create_custom_objects.py index 78adc96..c8f0052 100644 --- a/src/create_custom_objects.py +++ b/src/create_custom_objects.py @@ -9,6 +9,10 @@ from music_kraken.objects import ( from music_kraken.objects.collection import Collection from music_kraken.utils.enums import SourcePages +from music_kraken.objects.lint_default_factories import lint + +lint() + song = Song(title="Sad Story", isrc="testTest") other_song = Song(title="hihi", genre="dsbm") @@ -24,7 +28,9 @@ print(other_song.__dict__) print(song) +print(type(song).__dict__["__annotations__"]) +exit() only_smile = Artist( name="Only Smile", source_list=[Source(SourcePages.BANDCAMP, "https://onlysmile.bandcamp.com/")], diff --git a/src/music_kraken/objects/lint_default_factories.py b/src/music_kraken/objects/lint_default_factories.py new file mode 100644 index 0000000..9afad36 --- /dev/null +++ b/src/music_kraken/objects/lint_default_factories.py @@ -0,0 +1,66 @@ +from typing import List, TypeVar, Type + +from .country import Language +from .lyrics import Lyrics +from .parents import OuterProxy +from .song import Song, Album, Artist, Label +from .source import Source +from .target import Target + +T = TypeVar('T', bound=OuterProxy) +ALL_CLASSES: List[Type[T]] = [Song, Album, Artist, Label, Source, Target, Lyrics] + + +def print_lint_res(missing_values: dict): + print("_default_factories = {") + for key, value in missing_values.items(): + print(f'\t"{key}": {value},') + print("}") + + +def lint_type(cls: T): + missing_values: dict = {} + + for key, value in cls.__dict__["__annotations__"].items(): + if value is None: + continue + + if (not key.islower()) or key.startswith("_") or (key.startswith("__") and key.endswith("__")): + continue + + if key in cls._default_factories: + continue + + factory = "lambda: None" + if isinstance(value, str): + if value == "SourceCollection": + factory = "SourceCollection" + elif "collection" in value.lower(): + factory = "Collection" + elif value.istitle(): + factory = value + else: + if value is Language: + factory = 'Language.by_alpha_2("en")' + else: + try: + value() + factory = value.__name__ + except TypeError: + pass + + missing_values[key] = factory + + if len(missing_values) > 0: + print(f"{cls.__name__}:") + print_lint_res(missing_values) + print() + else: + print(f"Everything is fine at {cls.__name__}") + + +def lint(): + for i in ALL_CLASSES: + lint_type(i) + + print() diff --git a/src/music_kraken/objects/lyrics.py b/src/music_kraken/objects/lyrics.py index f35e186..e070bbf 100644 --- a/src/music_kraken/objects/lyrics.py +++ b/src/music_kraken/objects/lyrics.py @@ -5,6 +5,7 @@ import pycountry from .parents import OuterProxy from .source import Source, SourceCollection from .formatted_text import FormattedText +from .country import Language class Lyrics(OuterProxy): @@ -13,6 +14,14 @@ class Lyrics(OuterProxy): "text": FormattedText(), "language": None } + + text: FormattedText + language: Language + + _default_factories = { + "text": FormattedText, + "language": Language.by_alpha_2("en"), + } def __init__( self, diff --git a/src/music_kraken/objects/parents.py b/src/music_kraken/objects/parents.py index 1ed5379..e4334f2 100644 --- a/src/music_kraken/objects/parents.py +++ b/src/music_kraken/objects/parents.py @@ -54,44 +54,13 @@ class InnerData: self.__setattr__(key, value) -class Meta(type): - def __new__(meta, classname, bases, classDict): - for key, value in classDict.items(): - if (not key.islower()) or key.startswith("_") or (key.startswith("__") and key.endswith("__")): - continue - - if hasattr(value, "__call__") or isinstance(value, property) or isinstance(value, classmethod): - continue - - print("hi", type(value)) - print(key, value) - - new_instance = type.__new__(meta, classname, bases, classDict) - - return new_instance - - - -class OuterProxy(metaclass=Meta): +class OuterProxy: """ Wraps the inner data, and provides apis, to naturally access those values. """ _default_factories: dict = {} - def __new__(cls, *args, **kwargs): - for key, value in cls.__dict__["__annotations__"].items(): - if (not key.islower()) or key.startswith("_") or (key.startswith("__") and key.endswith("__")): - continue - - if key in cls._default_factories: - continue - - cls._default_factories[key] = lambda: None - - return super().__new__(cls) - - def __init__(self, _id: int = None, dynamic: bool = False, **kwargs): _automatic_id: bool = False @@ -107,17 +76,6 @@ class OuterProxy(metaclass=Meta): kwargs["id"] = _id kwargs["dynamic"] = dynamic - key: str - for key, value in super().__getattribute__("__dict__").items(): - if (not key.islower()) or key.startswith("_") or (key.startswith("__") and key.endswith("__")): - continue - - if hasattr(value, "__call__") or isinstance(value, property) or isinstance(value, classmethod): - continue - - print(type(value)) - print(key, value) - for name, factory in type(self)._default_factories.items(): if name not in kwargs: kwargs[name] = factory() diff --git a/src/music_kraken/objects/song.py b/src/music_kraken/objects/song.py index 2067648..c870150 100644 --- a/src/music_kraken/objects/song.py +++ b/src/music_kraken/objects/song.py @@ -60,6 +60,11 @@ class Song(Base): "main_artist_collection": Collection, "album_collection": Collection, "feature_artist_collection": Collection, + + "title": lambda: None, + "unified_title": lambda: None, + "isrc": lambda: None, + "genre": lambda: None, } """ @@ -199,7 +204,7 @@ class Album(Base): "date": ID3Timestamp(), "barcode": None, "albumsort": None, - "notes": FormattedText() + "notes": FormattedText(), } title: str @@ -227,6 +232,12 @@ class Album(Base): "artist_collection": Collection, "song_collection": Collection, "label_collection": Collection, + + "title": lambda: None, + "unified_title": lambda: None, + "album_status": lambda: None, + "barcode": lambda: None, + "albumsort": lambda: None, } DOWNWARDS_COLLECTION_STRING_ATTRIBUTES = ("song_collection", ) @@ -421,11 +432,17 @@ class Artist(Base): "notes": FormattedText, "lyrical_themes": list, "general_genre": lambda: "", + "source_collection": SourceCollection, "feature_song_collection": Collection, "main_album_collection": Collection, "contact_collection": Collection, "label_collection": Collection, + + "name": lambda: None, + "unified_name": lambda: None, + "country": Country, + "unformated_location": lambda: None, } DOWNWARDS_COLLECTION_STRING_ATTRIBUTES = ("feature_song_collection", "main_album_collection") @@ -599,7 +616,10 @@ class Label(Base): "album_collection": Collection, "current_artist_collection": Collection, "source_collection": SourceCollection, - "contact_collection": Collection + "contact_collection": Collection, + + "name": lambda: None, + "unified_name": lambda: None, } def __init__( diff --git a/src/music_kraken/objects/source.py b/src/music_kraken/objects/source.py index 58d4b53..2ba269a 100644 --- a/src/music_kraken/objects/source.py +++ b/src/music_kraken/objects/source.py @@ -26,12 +26,11 @@ class Source(OuterProxy): url: str audio_url: str - COLLECTION_STRING_ATTRIBUTES = tuple() - SIMPLE_STRING_ATTRIBUTES = { - "page_enum": None, - "url": None, - "referer_page": None, - "audio_url": None + _default_factories = { + "page_enum": lambda: None, + "referer_page": lambda: None, + "url": str, + "audio_url": str, } def __init__(self, page_enum: SourcePages, referer_page: SourcePages = None, **kwargs) -> None: diff --git a/src/music_kraken/objects/target.py b/src/music_kraken/objects/target.py index a5cc0dc..0d8bb25 100644 --- a/src/music_kraken/objects/target.py +++ b/src/music_kraken/objects/target.py @@ -22,6 +22,14 @@ class Target(OuterProxy): ``` """ + file: str + path: str + + _default_factories = { + "file": str, + "path": str, + } + SIMPLE_STRING_ATTRIBUTES = { "_file": None, "_path": None