更新配置文件库
parent
74e8f9c7b0
commit
8ba74fa952
@ -0,0 +1,43 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
import sqlalchemy
|
||||||
|
from api.model.base import BaseHelper, BaseModel
|
||||||
|
|
||||||
|
import sqlalchemy
|
||||||
|
from sqlalchemy import select, update
|
||||||
|
from sqlalchemy.orm import mapped_column, relationship, Mapped
|
||||||
|
|
||||||
|
class BotPersonaCategoryModel(BaseModel):
|
||||||
|
__tablename__ = "chat_complete_bot_persona_category"
|
||||||
|
|
||||||
|
id: Mapped[int] = mapped_column(sqlalchemy.Integer, primary_key=True, autoincrement=True)
|
||||||
|
name: Mapped[str] = mapped_column(sqlalchemy.String(60), index=True)
|
||||||
|
description: Mapped[str] = mapped_column(sqlalchemy.String, nullable=True)
|
||||||
|
icon: Mapped[str] = mapped_column(sqlalchemy.String, nullable=True)
|
||||||
|
font_icon: Mapped[str] = mapped_column(sqlalchemy.String, nullable=True)
|
||||||
|
color: Mapped[str] = mapped_column(sqlalchemy.String(10), nullable=True)
|
||||||
|
order: Mapped[int] = mapped_column(sqlalchemy.Integer, nullable=True)
|
||||||
|
|
||||||
|
class BotPersonaHelper(BaseHelper):
|
||||||
|
async def add(self, obj: BotPersonaCategoryModel):
|
||||||
|
self.session.add(obj)
|
||||||
|
await self.session.commit()
|
||||||
|
await self.session.refresh(obj)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
async def update(self, obj: BotPersonaCategoryModel):
|
||||||
|
obj = await self.session.merge(obj)
|
||||||
|
await self.session.commit()
|
||||||
|
return obj
|
||||||
|
|
||||||
|
async def remove(self, id: int):
|
||||||
|
stmt = sqlalchemy.delete(BotPersonaCategoryModel).where(BotPersonaCategoryModel.id == id)
|
||||||
|
await self.session.execute(stmt)
|
||||||
|
await self.session.commit()
|
||||||
|
|
||||||
|
async def get_list(self):
|
||||||
|
stmt = select(BotPersonaCategoryModel)
|
||||||
|
return await self.session.scalars(stmt)
|
||||||
|
|
||||||
|
async def find_by_id(self, id: int):
|
||||||
|
stmt = select(BotPersonaCategoryModel).where(BotPersonaCategoryModel.id == id)
|
||||||
|
return await self.session.scalar(stmt)
|
@ -0,0 +1,56 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
from typing import TypeVar
|
||||||
|
import toml
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
values: dict = {}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def load_config(file):
|
||||||
|
with open(file, "r") as f:
|
||||||
|
Config.values = toml.load(f)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get(key: str, default=None, type=None, empty_is_none=False):
|
||||||
|
key_path = key.split(".")
|
||||||
|
value = Config.values
|
||||||
|
for k in key_path:
|
||||||
|
if k in value:
|
||||||
|
value = value[k]
|
||||||
|
else:
|
||||||
|
return default
|
||||||
|
|
||||||
|
if empty_is_none and value == "":
|
||||||
|
return None
|
||||||
|
|
||||||
|
if type == bool:
|
||||||
|
if isinstance(value, bool):
|
||||||
|
return value
|
||||||
|
elif isinstance(value, int) or isinstance(value, float):
|
||||||
|
return value != 0
|
||||||
|
else:
|
||||||
|
return str(value).lower() in ("yes", "true", "1")
|
||||||
|
elif type == int:
|
||||||
|
return int(value)
|
||||||
|
elif type == float:
|
||||||
|
return float(value)
|
||||||
|
elif type == str:
|
||||||
|
return str(value)
|
||||||
|
elif type == list:
|
||||||
|
if not isinstance(value, list):
|
||||||
|
return []
|
||||||
|
elif type == dict:
|
||||||
|
if not isinstance(value, dict):
|
||||||
|
return {}
|
||||||
|
else:
|
||||||
|
return value
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def set(key: str, value):
|
||||||
|
key_path = key.split(".")
|
||||||
|
obj = Config.values
|
||||||
|
for k in key_path[:-1]:
|
||||||
|
if k not in obj:
|
||||||
|
obj[k] = {}
|
||||||
|
obj = obj[k]
|
||||||
|
obj[key_path[-1]] = value
|
@ -1,7 +1,10 @@
|
|||||||
import sys
|
import sys
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
sys.path.append(str(pathlib.Path(__file__).parent.parent))
|
root_path = pathlib.Path(__file__).parent.parent
|
||||||
|
sys.path.append(root_path)
|
||||||
|
|
||||||
import config
|
from config import Config
|
||||||
config.DEBUG = True
|
|
||||||
|
Config.load_config(root_path + "/config.toml")
|
||||||
|
Config.set("debug", True)
|
Loading…
Reference in New Issue