59 lines
1.1 KiB
Python
Raw Normal View History

2025-03-28 18:23:30 +08:00
from dataclasses import dataclass
import toml
@dataclass
class DatabaseConfig:
"""数据库配置"""
host: str
port: int
user: str
password: str
database: str
@dataclass
class ChromeConfig:
"""浏览器配置"""
proxy: str
2025-03-28 23:19:42 +08:00
browser_path: str
2025-03-28 18:23:30 +08:00
@dataclass
class AppConfig:
"""总配置,如果未来增加 Web 服务,添加 WebConfig 即可"""
debug: bool
2025-04-01 21:54:01 +08:00
wap_screenshot: bool
2025-03-28 18:23:30 +08:00
database: DatabaseConfig
chrome: ChromeConfig
def load_config(config_path: str) -> AppConfig:
"""加载配置"""
with open(config_path, "r", encoding="utf-8") as f:
config_dict = toml.load(f)
database_config = DatabaseConfig(**config_dict["database"])
chrome_config = ChromeConfig(**config_dict["chrome"])
2025-03-28 23:19:42 +08:00
AppCtx.g_app_config = AppConfig(
2025-04-01 21:56:22 +08:00
debug=config_dict["debug"],
wap_screenshot=config_dict["wap_screenshot"],
database=database_config,
chrome=chrome_config
2025-03-28 18:23:30 +08:00
)
2025-03-28 23:19:42 +08:00
return AppCtx.g_app_config
2025-04-01 21:54:01 +08:00
class AppCtx:
# 全局变量
# 配置信息
g_app_config: AppConfig = None
# 数据库连接
g_db_engine = None