参考mower的conf配置
This commit is contained in:
parent
01c4bee95e
commit
3aad1fe8ff
11 changed files with 199 additions and 67 deletions
40
launcher/config/__init__.py
Normal file
40
launcher/config/__init__.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
import os
|
||||
|
||||
import yaml
|
||||
from yamlcore import CoreDumper, CoreLoader
|
||||
|
||||
from launcher.config.conf import Conf
|
||||
from python.Lib.pathlib import Path
|
||||
|
||||
conf_path = Path(os.path.join(os.getcwd(), "conf.yml"))
|
||||
|
||||
|
||||
def save_conf():
|
||||
with conf_path.open("w", encoding="utf8") as f:
|
||||
# json.dump(conf.model_dump(), f, ensure_ascii=False, indent=4) # Use json.dump
|
||||
yaml.dump(
|
||||
conf.model_dump(),
|
||||
f,
|
||||
Dumper=CoreDumper,
|
||||
encoding="utf-8",
|
||||
default_flow_style=False,
|
||||
allow_unicode=True,
|
||||
)
|
||||
|
||||
|
||||
def load_conf():
|
||||
global conf
|
||||
if not conf_path.is_file():
|
||||
conf_path.parent.mkdir(exist_ok=True)
|
||||
conf = Conf()
|
||||
save_conf()
|
||||
return
|
||||
with conf_path.open("r", encoding="utf-8") as f:
|
||||
data = yaml.load(f, Loader=CoreLoader)
|
||||
if data is None:
|
||||
data = {}
|
||||
conf = Conf(**data)
|
||||
|
||||
|
||||
conf: Conf
|
||||
load_conf()
|
36
launcher/config/conf.py
Normal file
36
launcher/config/conf.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
from pydantic import BaseModel, model_validator
|
||||
from pydantic_core import PydanticUndefined
|
||||
|
||||
|
||||
class ConfModel(BaseModel):
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def nested_defaults(cls, data):
|
||||
for name, field in cls.model_fields.items():
|
||||
if name not in data:
|
||||
if field.default is PydanticUndefined:
|
||||
data[name] = field.annotation()
|
||||
else:
|
||||
data[name] = field.default
|
||||
return data
|
||||
|
||||
|
||||
class Total(ConfModel):
|
||||
"""整体"""
|
||||
# 所在页面
|
||||
page: str = "init"
|
||||
|
||||
|
||||
class UpdatePart(ConfModel):
|
||||
"""更新代码"""
|
||||
# mower-ng 代码分支
|
||||
branch: str = "slow"
|
||||
# PyPI 仓库镜像
|
||||
mirror: str = "aliyun"
|
||||
|
||||
|
||||
class Conf(
|
||||
Total,
|
||||
UpdatePart,
|
||||
):
|
||||
pass
|
Loading…
Add table
Add a link
Reference in a new issue