48 lines
967 B
Python
48 lines
967 B
Python
from dataclasses import dataclass
|
|
import toml
|
|
|
|
|
|
@dataclass
|
|
class DatabaseConfig:
|
|
"""数据库配置"""
|
|
|
|
host: str
|
|
port: int
|
|
user: str
|
|
password: str
|
|
database: str
|
|
|
|
|
|
@dataclass
|
|
class ChromeConfig:
|
|
"""浏览器配置"""
|
|
|
|
proxy: str
|
|
|
|
|
|
@dataclass
|
|
class AppConfig:
|
|
"""总配置,如果未来增加 Web 服务,添加 WebConfig 即可"""
|
|
|
|
debug: bool
|
|
database: DatabaseConfig
|
|
chrome: ChromeConfig
|
|
|
|
|
|
# 全局变量,存储配置信息,not best practice, but it's ok for now
|
|
gAppConfig = AppConfig()
|
|
|
|
|
|
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"])
|
|
|
|
gAppConfig = AppConfig(
|
|
debug=config_dict["debug"], database=database_config, chrome=chrome_config
|
|
)
|
|
return gAppConfig
|