2024-02-15 17:53:02 +01:00
|
|
|
from abc import ABC, abstractmethod
|
2024-02-23 09:50:43 +01:00
|
|
|
|
|
|
|
|
import moviepy.editor as mp
|
2024-02-20 14:55:58 +01:00
|
|
|
from sqlalchemy.future import select
|
2024-02-15 17:53:02 +01:00
|
|
|
|
|
|
|
|
from ..chore import GenerationContext
|
2024-02-22 15:15:52 +01:00
|
|
|
from ..models import SessionLocal, File, Setting
|
2024-02-13 14:15:27 +01:00
|
|
|
|
2024-02-14 17:49:51 +01:00
|
|
|
|
2024-02-13 14:15:27 +01:00
|
|
|
class BaseEngine(ABC):
|
2024-02-14 17:49:51 +01:00
|
|
|
num_options: int
|
2024-02-13 14:15:27 +01:00
|
|
|
name: str
|
|
|
|
|
description: str
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2024-02-15 17:53:02 +01:00
|
|
|
self.ctx: GenerationContext # This is for type hinting only
|
2024-02-14 17:49:51 +01:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
@abstractmethod
|
2024-02-21 09:06:36 +01:00
|
|
|
def get_options(cls):
|
2024-02-14 17:49:51 +01:00
|
|
|
...
|
2024-02-20 14:55:58 +01:00
|
|
|
|
|
|
|
|
def get_video_duration(self, path: str) -> float:
|
|
|
|
|
return mp.VideoFileClip(path).duration
|
|
|
|
|
|
|
|
|
|
def get_audio_duration(self, path: str) -> float:
|
|
|
|
|
return mp.AudioFileClip(path).duration
|
|
|
|
|
|
2024-02-23 11:07:50 +01:00
|
|
|
# noinspection PyShadowingBuiltins
|
2024-02-20 14:55:58 +01:00
|
|
|
@classmethod
|
2024-02-22 15:15:52 +01:00
|
|
|
def get_assets(cls, *, type: str = None, by_id: int = None) -> list[File] | File | None:
|
2024-02-20 14:55:58 +01:00
|
|
|
with SessionLocal() as db:
|
|
|
|
|
if type:
|
2024-02-23 13:12:48 +01:00
|
|
|
# noinspection PyTypeChecker
|
2024-02-20 14:55:58 +01:00
|
|
|
return (
|
|
|
|
|
db.execute(
|
|
|
|
|
select(File).filter(
|
|
|
|
|
File.type == type, File.provider == cls.name
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.scalars()
|
|
|
|
|
.all()
|
|
|
|
|
)
|
2024-02-22 15:15:52 +01:00
|
|
|
elif by_id:
|
2024-02-23 13:12:48 +01:00
|
|
|
# noinspection PyTypeChecker
|
2024-02-22 15:15:52 +01:00
|
|
|
return (
|
|
|
|
|
db.execute(
|
|
|
|
|
select(File).filter(
|
|
|
|
|
File.id == by_id, File.provider == cls.name
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.scalars()
|
|
|
|
|
.first()
|
|
|
|
|
)
|
2024-02-20 14:55:58 +01:00
|
|
|
else:
|
2024-02-23 13:12:48 +01:00
|
|
|
# noinspection PyTypeChecker
|
2024-02-20 14:55:58 +01:00
|
|
|
return (
|
|
|
|
|
db.execute(select(File).filter(File.provider == cls.name))
|
|
|
|
|
.scalars()
|
|
|
|
|
.all()
|
|
|
|
|
)
|
2024-02-20 16:23:15 +01:00
|
|
|
|
2024-02-23 11:07:50 +01:00
|
|
|
# noinspection PyShadowingBuiltins
|
2024-02-20 14:55:58 +01:00
|
|
|
@classmethod
|
|
|
|
|
def add_asset(cls, *, path: str, metadata: dict, type: str = None):
|
|
|
|
|
with SessionLocal() as db:
|
|
|
|
|
db.add(File(path=path, data=metadata, type=type, provider=cls.name))
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def remove_asset(cls, *, path: str):
|
|
|
|
|
with SessionLocal() as db:
|
2024-02-23 13:12:48 +01:00
|
|
|
# noinspection PyTypeChecker
|
2024-02-20 14:55:58 +01:00
|
|
|
db.execute(select(File).filter(File.path == path)).delete()
|
|
|
|
|
db.commit()
|
|
|
|
|
|
2024-02-23 11:07:50 +01:00
|
|
|
# noinspection PyShadowingBuiltins
|
2024-02-22 15:15:52 +01:00
|
|
|
@classmethod
|
|
|
|
|
def store_setting(cls, *, type: str = None, data: dict):
|
|
|
|
|
with SessionLocal() as db:
|
|
|
|
|
# check if setting exists
|
2024-02-23 13:12:48 +01:00
|
|
|
# noinspection PyTypeChecker
|
2024-02-22 15:15:52 +01:00
|
|
|
setting = db.execute(
|
|
|
|
|
select(Setting).filter(
|
|
|
|
|
Setting.provider == cls.name, Setting.type == type
|
|
|
|
|
)
|
|
|
|
|
).scalar()
|
|
|
|
|
if setting:
|
|
|
|
|
setting.data = data
|
|
|
|
|
else:
|
|
|
|
|
db.add(Setting(provider=cls.name, type=type, data=data))
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_setting(cls, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
This method is deprecated, use retrieve_setting instead
|
|
|
|
|
"""
|
|
|
|
|
return cls.retrieve_setting(*args, **kwargs)
|
|
|
|
|
|
2024-02-23 11:07:50 +01:00
|
|
|
# noinspection PyShadowingBuiltins
|
2024-02-22 15:15:52 +01:00
|
|
|
@classmethod
|
|
|
|
|
def retrieve_setting(cls, *, identifier: str = None, type: str = None) -> dict | list[dict] | None:
|
|
|
|
|
"""
|
|
|
|
|
Retrieve a setting from the database based on the provided identifier or type.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
identifier (str, optional): The identifier of the setting. Defaults to None.
|
|
|
|
|
type (str, optional): Deprecated. Now an alias for identifier, please use identifier instead. Defaults to None.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str | list[str] | None: The retrieved setting data, or None if not found.
|
|
|
|
|
"""
|
|
|
|
|
with SessionLocal() as db:
|
|
|
|
|
if not identifier and type:
|
|
|
|
|
identifier = type
|
|
|
|
|
if identifier:
|
2024-02-23 13:12:48 +01:00
|
|
|
# noinspection PyTypeChecker
|
2024-02-22 15:15:52 +01:00
|
|
|
result = db.execute(
|
|
|
|
|
select(Setting).filter(
|
|
|
|
|
Setting.provider == cls.name, Setting.type == identifier
|
|
|
|
|
)
|
|
|
|
|
).scalar()
|
|
|
|
|
|
|
|
|
|
if result:
|
|
|
|
|
return result.data
|
|
|
|
|
return None
|
|
|
|
|
else:
|
2024-02-23 13:12:48 +01:00
|
|
|
# noinspection PyTypeChecker
|
2024-02-22 15:15:52 +01:00
|
|
|
return [
|
|
|
|
|
s.data
|
|
|
|
|
for s in db.execute(
|
|
|
|
|
select(Setting).filter(Setting.provider == cls.name)
|
|
|
|
|
)
|
|
|
|
|
.scalars()
|
|
|
|
|
.all()
|
|
|
|
|
]
|
|
|
|
|
|
2024-02-23 11:07:50 +01:00
|
|
|
# noinspection PyShadowingBuiltins
|
2024-02-22 15:15:52 +01:00
|
|
|
@classmethod
|
|
|
|
|
def remove_setting(cls, *, identifier: str = None, type: str = None):
|
|
|
|
|
"""
|
|
|
|
|
Remove a setting from the database.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
identifier (str, optional): The identifier of the setting to be removed. If not provided, the type will be used as the identifier. Defaults to None.
|
|
|
|
|
type (str, optional): Deprecated. Now an alias for identifier, please use identifier instead. Defaults to None.
|
|
|
|
|
"""
|
|
|
|
|
with SessionLocal() as db:
|
|
|
|
|
if not identifier and type:
|
|
|
|
|
identifier = type
|
|
|
|
|
if identifier:
|
2024-02-23 13:12:48 +01:00
|
|
|
# noinspection PyTypeChecker
|
2024-02-22 15:15:52 +01:00
|
|
|
db.execute(
|
|
|
|
|
select(Setting).filter(
|
|
|
|
|
Setting.provider == cls.name, Setting.type == identifier
|
|
|
|
|
)
|
|
|
|
|
).delete()
|
|
|
|
|
else:
|
2024-02-23 13:12:48 +01:00
|
|
|
# noinspection PyTypeChecker
|
2024-02-22 15:15:52 +01:00
|
|
|
db.execute(
|
|
|
|
|
select(Setting).filter(Setting.provider == cls.name)
|
|
|
|
|
).delete()
|
|
|
|
|
db.commit()
|
|
|
|
|
|
2024-02-20 14:55:58 +01:00
|
|
|
@classmethod
|
|
|
|
|
def get_settings(cls):
|
|
|
|
|
...
|