feat: 改进配置加载逻辑并更新项目配置
当配置文件不存在时自动生成示例配置 添加pyproject.toml作为项目构建配置 更新.gitignore忽略更多文件类型 删除不再使用的反向WebSocket示例文件
This commit is contained in:
@@ -38,10 +38,10 @@ class Config:
|
||||
:raises ConfigError: 如果加载配置时发生其他错误
|
||||
"""
|
||||
if not self.path.exists():
|
||||
error = ConfigNotFoundError(message=f"配置文件 {self.path} 未找到!")
|
||||
self.logger.error(f"配置加载失败: {error.message}")
|
||||
self.logger.log_custom_exception(error)
|
||||
raise error
|
||||
self.logger.warning(f"配置文件 {self.path} 未找到,正在生成示例配置...")
|
||||
self._generate_example_config()
|
||||
self.logger.success(f"示例配置已生成: {self.path}")
|
||||
self.logger.info("请编辑配置文件后重新启动程序")
|
||||
|
||||
try:
|
||||
self.logger.info(f"正在从 {self.path} 加载配置...")
|
||||
@@ -86,6 +86,19 @@ class Config:
|
||||
self.logger.log_custom_exception(error)
|
||||
raise error
|
||||
|
||||
def _generate_example_config(self):
|
||||
"""
|
||||
生成示例配置文件
|
||||
"""
|
||||
example_path = Path("config.example.toml")
|
||||
|
||||
if not example_path.exists():
|
||||
self.logger.error(f"示例配置文件 {example_path} 不存在,无法生成配置")
|
||||
raise ConfigNotFoundError(message=f"示例配置文件 {example_path} 不存在")
|
||||
|
||||
content = example_path.read_text()
|
||||
self.path.write_text(content)
|
||||
|
||||
# 通过属性访问配置
|
||||
@property
|
||||
def napcat_ws(self) -> NapCatWSModel:
|
||||
|
||||
Reference in New Issue
Block a user