Skip to content

Commit

Permalink
<jx3>[feat]config use pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
HornCopper committed Aug 15, 2024
1 parent 8eb1846 commit 543ca25
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions src/tools/config/config.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
from pydantic import BaseModel
from typing import List, Dict, Optional
import yaml
import os

class config:
def __init__(self, **kwargs):
for key, value in kwargs.items():
if isinstance(value, dict):
value = config(**value)
setattr(self, key, value)
class BotBasic(BaseModel):
bot_name: str = ""
bot_name_argument: str = ""
bot_owner: List[str] = []
bot_repo: str = ""
bot_notice: Dict[str, str] = {} # 键是 Bot 的 QQ 号,值是通知的群号
proxy: Optional[str] = None

def yaml_to_class(yaml_str, cls):
config_data = yaml.safe_load(yaml_str)
return cls(**config_data)
class GithubConfig(BaseModel):
web_path: str = ""
github_personal_token: str = ""

class Jx3APIConfig(BaseModel):
token: str = ""
token_v2: str = ""
ticket: str = ""
url: str = ""

class Jx3WSConfig(BaseModel):
url: str = ""
token: str = ""

class Jx3Config(BaseModel):
api: Jx3APIConfig = Jx3APIConfig()
ws: Jx3WSConfig = Jx3WSConfig()

class HiddenConfig(BaseModel):
offcial_token: Optional[str] = None

class Config(BaseModel):
bot_basic: BotBasic = BotBasic()
github: GithubConfig = GithubConfig()
jx3: Jx3Config = Jx3Config()
hidden: HiddenConfig = HiddenConfig()

def load_config(yaml_file_path: str) -> Config:
with open(yaml_file_path, "r", encoding="utf8") as f:
config_data = yaml.safe_load(f)
return Config(**config_data)

script_dir = os.path.dirname(os.path.abspath(__file__))
yaml_file_path = os.path.join(script_dir, "config.yml")

with open(yaml_file_path, "r", encoding="utf8") as f:
Config = yaml_to_class(f.read(), config)
config = load_config(yaml_file_path)

0 comments on commit 543ca25

Please sign in to comment.