refs #0 The root commit.

This commit is contained in:
sbps-test user
2023-09-13 20:37:14 +03:00
commit 10eb607154
66 changed files with 1643 additions and 0 deletions

0
back/config/__init__.py Normal file
View File

View File

@@ -0,0 +1,19 @@
from core.config_factory import CreateSchemaConfig
from core.typing import CreateSchema
from pydantic import create_model
def create_create_schema(module_name: str) -> CreateSchema:
"""
Creates pydantic module for item creating.
Returns pydantic model.
:param module_name: The name of the module.
"""
config = CreateSchemaConfig(module_name)
return create_model(
config.module.get_name("create_schema"), **config.get_create_field_defs()
)

17
back/config/model.py Normal file
View File

@@ -0,0 +1,17 @@
from core.config_factory import ModelConfig
from core.database import Base
from core.typing import Model
def create_model(module_name: str) -> Model:
"""
Creates SQLAlchemy model.
Returns created SQLAlchemy model.
:param module_name: The name of the module.
"""
config = ModelConfig(module_name)
return type(config.module.get_name("model"), (Base,), config.get_model_attrs())

View File

@@ -0,0 +1,60 @@
from importlib import import_module
from os import listdir
from os.path import isdir, join
from typing import Literal
from core.helper import snake_to_camel
from yaml import safe_load
exclude = "__pycache__"
PATH = join(*__name__.split("."))
MODULES = tuple(
filter(
lambda item: isdir(join(PATH, item)) and item not in exclude,
listdir(PATH),
)
)
class Module:
"""
Represents a module of application.
"""
def __init__(self, title: str) -> None:
self.title = title
@property
def config_path(self) -> str:
return f"config/module/{self.title}/config.yaml"
@property
def create_schema_name(self) -> str:
return snake_to_camel(self.title) + "Create"
@property
def model_name(self) -> str:
return snake_to_camel(self.title)
@property
def schema_name(self) -> str:
return self.model_name
def get_name(self, _type: Literal["model", "schema", "create_schema"]) -> str:
match _type:
case "model" | "schema":
name = snake_to_camel(self.title)
case "create_schema":
name = snake_to_camel(self.title) + "Create"
return name
def get_item(self, _type: Literal["model", "schema", "create_schema"]):
return getattr(import_module(f"config.{_type}"), f"create_{_type}")(self.title)
def get_body(self) -> dict:
with open(self.config_path, "r") as stream:
project = safe_load(stream)
return project

View File

View File

@@ -0,0 +1,9 @@
fields:
author:
type: str
title:
type: str
year:
type: int
pdf:
type: file

View File

@@ -0,0 +1,3 @@
from core.util import update_module_locals
update_module_locals(__name__, locals())

View File

@@ -0,0 +1,3 @@
from core.util import update_module_locals
update_module_locals(__name__, locals())

View File

@@ -0,0 +1,3 @@
from core.util import update_module_locals
update_module_locals(__name__, locals())

21
back/config/schema.py Normal file
View File

@@ -0,0 +1,21 @@
from core.config_factory import SchemaConfig
from core.typing import Schema
from pydantic import create_model
def create_schema(module_name: str) -> Schema:
"""
Creates pydantic module for item returning.
Returns pydantic model.
:param module_name: The name of the module.
"""
config = SchemaConfig(module_name)
return create_model(
config.module.get_name("schema"),
__config__=config.Config,
**config.get_field_defs(),
)