refs #0 The root commit.
This commit is contained in:
60
back/config/module/__init__.py
Normal file
60
back/config/module/__init__.py
Normal 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
|
||||
Reference in New Issue
Block a user