music-kraken-core/src/music_kraken/database/objects/parents.py

67 lines
1.7 KiB
Python
Raw Normal View History

2022-12-01 12:15:30 +00:00
import uuid
2022-12-06 13:45:18 +00:00
from ...utils.shared import (
2022-12-01 12:15:30 +00:00
SONG_LOGGER as logger
)
2022-12-06 22:44:42 +00:00
2022-12-06 13:45:18 +00:00
class Reference:
def __init__(self, id_: str) -> None:
self.id = id_
2022-12-06 22:44:42 +00:00
def __str__(self):
return f"references to an object with the id: {self.id}"
2022-12-07 14:51:38 +00:00
def __eq__(self, __o: object) -> bool:
if type(__o) != type(self):
return False
return self.id == __o.id
2022-12-06 13:45:18 +00:00
2022-12-01 12:15:30 +00:00
class DatabaseObject:
2022-12-12 18:30:18 +00:00
def __init__(self, id_: str = None, dynamic: bool = False) -> None:
2022-12-01 12:15:30 +00:00
self.id_: str | None = id_
2022-12-12 18:30:18 +00:00
self.dynamic = dynamic
2022-12-06 22:44:42 +00:00
2022-12-06 13:45:18 +00:00
def get_id(self) -> str:
2022-12-01 12:15:30 +00:00
"""
returns the id if it is set, else
it returns a randomly generated UUID
https://docs.python.org/3/library/uuid.html
2022-12-12 18:30:18 +00:00
if the object is dynamic, it raises an error
2022-12-01 12:15:30 +00:00
"""
2022-12-12 18:30:18 +00:00
if self.dynamic:
raise ValueError("Dynamic objects have no idea, because they are not in the database")
2022-12-01 12:15:30 +00:00
if self.id_ is None:
self.id_ = str(uuid.uuid4())
logger.info(f"id for {self.__str__()} isn't set. Setting to {self.id_}")
return self.id_
2022-12-06 13:45:18 +00:00
def get_reference(self) -> Reference:
return Reference(self.id)
2022-12-01 12:15:30 +00:00
id = property(fget=get_id)
2022-12-06 13:45:18 +00:00
reference = property(fget=get_reference)
2023-01-12 15:25:50 +00:00
class SongAttribute:
def __init__(self, song=None):
# the reference to the song the lyrics belong to
self.song = song
def add_song(self, song):
self.song = song
def get_ref_song_id(self):
if self.song is None:
return None
return self.song.reference.id
def set_ref_song_id(self, song_id):
self.song_ref = Reference(song_id)
song_ref_id = property(fget=get_ref_song_id, fset=set_ref_song_id)