feat: completed the default factories

This commit is contained in:
Hazel 2023-12-20 09:55:09 +01:00
parent a2a2297354
commit f81521b014
7 changed files with 117 additions and 51 deletions

View File

@ -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/")],

View File

@ -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()

View File

@ -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,

View File

@ -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()

View File

@ -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__(

View File

@ -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:

View File

@ -22,6 +22,14 @@ class Target(OuterProxy):
```
"""
file: str
path: str
_default_factories = {
"file": str,
"path": str,
}
SIMPLE_STRING_ATTRIBUTES = {
"_file": None,
"_path": None