genshin-wiki/model/_base.py

79 lines
1.6 KiB
Python
Raw Permalink Normal View History

2023-04-29 14:59:16 +00:00
from datetime import datetime
2023-04-15 05:09:56 +00:00
2023-04-29 14:59:16 +00:00
# noinspection PyPep8Naming
from enum import Enum as E
from typing import Any, TYPE_CHECKING, Type
2023-04-15 05:09:56 +00:00
2023-04-29 14:59:16 +00:00
from databases import Database
2023-04-15 05:09:56 +00:00
2023-04-29 14:59:16 +00:00
# noinspection PyPep8Naming
from ormar import (
BaseField,
Enum as ENUM,
Model as _Model,
ModelMeta as _ModelMeta,
String as STRING,
)
from sqlalchemy import MetaData
from utils import json
try:
import regex as re
except ImportError:
import re
if TYPE_CHECKING:
pass
__all__ = ("Model", "String", "Enum")
DATABASE_URL = "sqlite:///data/db.sqlite"
database = Database(DATABASE_URL)
metadata = MetaData()
class ModelMeta(_ModelMeta):
database = database
metadata = metadata
class Model(_Model):
class Config(_Model.Config):
json_dumps = json.dumps
json_loads = json.loads
json_encoders = {datetime: lambda v: v.timestamp()}
class String(STRING):
def __new__(
cls,
*,
max_length: int = 255,
min_length: int = None,
regex: str = None,
**kwargs: Any,
) -> BaseField:
return super().__new__(
cls=STRING,
max_length=max_length,
min_length=min_length,
regex=regex,
**kwargs,
)
class Enum(ENUM):
def __new__(cls, enum_class: Type[E], **kwargs: Any) -> BaseField:
return super().__new__(
cls,
**{
**kwargs,
**{
k: v
for k, v in locals().items()
if k not in ["cls", "__class__", "kwargs"]
},
},
)