添加了config读取,写入,重置功能

This commit is contained in:
Nanaloveyuki
2025-07-27 14:52:15 +08:00
parent 8193cd42a7
commit f2833cb719
3 changed files with 172 additions and 0 deletions

22
config.json Normal file
View File

@ -0,0 +1,22 @@
{
"file_level": "DEBUG",
"file_name": "pliblog.log",
"file_path": "./",
"file_format": "{asctime} | {levelname} | {prefix} {message}",
"file_encoding": "utf-8",
"enable_console": true,
"enable_file": true,
"console_color": true,
"console_level": "INFO",
"console_format": "{asctime} | {levelname} | {prefix} {message}",
"console_prefix": "Auto",
"console_encoding": "utf-8",
"date_format": "%Y-%m-%d %H:%M:%S",
"level_name": {
"DEBUG": "DEBUG",
"INFO": "INFO",
"WARN": "WARN",
"ERRO": "ERRO",
"CRIT": "CRIT"
}
}

13
src/utils/__init__.py Normal file
View File

@ -0,0 +1,13 @@
"""
工具函数 / Utility functions
"""
# encoding = utf-8
# python 3.13.5
from .configs import get_config, set_config, reset_config
__all__ = [
"get_config",
"set_config",
"reset_config"
]

137
src/utils/configs.py Normal file
View File

@ -0,0 +1,137 @@
"""
pliblog的配置设置,用于设置pliblog的全局配置
pliblog's config settings, used to set pliblog's global config
"""
# encoding = utf-8
# python 3.13.5
# Default Config Path
DEFAULT_CONFIG_PATH = "config.json"
DEFAULT_CONFIG = {
"file_level": "DEBUG",
"file_name": "pliblog.log",
"file_path": "./logs",
"file_format": "{asctime} | {levelname} | {prefix} {message}",
"file_encoding": "utf-8",
"enable_console": True,
"enable_file": True,
"console_color": True,
"console_level": "INFO",
"console_format": "{asctime} | {levelname} | {prefix} {message}",
"console_prefix": "Auto",
"console_encoding": "utf-8",
"date_format": "%Y-%m-%d %H:%M:%S",
"level_name": {"DEBUG": "DEBUG", "INFO": "INFO", "WARN": "WARN", "ERRO": "ERRO", "CRIT": "CRIT"}
}
from typing import Union, Optional
import logging
import json
def get_config(select: str = None) -> Union[dict, str, bool, int, None]:
"""
获取配置信息 Get config info
:param select: 配置项名称 Config item name
:return: 配置项值 Config item value
"""
try:
with open(DEFAULT_CONFIG_PATH, "r", encoding="utf-8") as f:
config = json.load(f)
if select:
return config[select]
return config
except (FileNotFoundError, json.JSONDecodeError):
with open(DEFAULT_CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(DEFAULT_CONFIG, f, indent=4)
if select:
return DEFAULT_CONFIG[select]
return DEFAULT_CONFIG
except KeyError as e:
logging.error(f"配置项 '{select}' 不存在")
return None
except Exception as e:
logging.error(f"读取配置文件失败: {e}")
return None
import shutil
from pathlib import Path
def set_config(select: str, value: Union[dict, str, bool, int, None]) -> tuple[bool, Optional[str]]:
"""
设置配置信息 Set config info
:param select: 配置项名称 Config item name
:param value: 配置项值 Config item value
:return: (设置是否成功, 消息) (Set success or not, message)
"""
try:
config_path = Path(DEFAULT_CONFIG_PATH)
backup_path = Path(__file__).parent / "configs.backup.json"
if not config_path.exists():
with open(config_path, "w", encoding="utf-8") as f:
json.dump(DEFAULT_CONFIG, f, indent=4)
shutil.copy2(config_path, backup_path)
with open(DEFAULT_CONFIG_PATH, "r+", encoding="utf-8") as f:
config = json.load(f)
config[select] = value
f.seek(0)
json.dump(config, f, indent=4)
f.truncate()
with open(DEFAULT_CONFIG_PATH, "r", encoding="utf-8") as f:
verify_config = json.load(f)
if verify_config.get(select) != value:
shutil.move(backup_path, DEFAULT_CONFIG_PATH)
return False, f"配置项 '{select}' 设置失败,已恢复备份"
Path(backup_path).unlink(missing_ok=True)
return True, f"配置项 '{select}' 设置成功"
except json.JSONDecodeError as e:
logging.error(f"配置文件格式错误: {e}")
return False, f"配置文件格式错误: {str(e)}"
except PermissionError:
logging.error(f"没有权限操作配置文件: {DEFAULT_CONFIG_PATH}")
return False, "没有权限操作配置文件"
except Exception as e:
logging.error(f"设置配置文件失败: {e}")
if Path(backup_path).exists():
shutil.move(backup_path, DEFAULT_CONFIG_PATH)
return False, f"设置失败,已恢复备份: {str(e)}"
return False, f"设置配置文件失败: {str(e)}"
@staticmethod
def reset_config() -> tuple[bool, Optional[str]]:
"""
重置配置信息 Reset config info
:return: (重置是否成功, 消息) (Reset success or not, message)
"""
try:
config_path = Path(DEFAULT_CONFIG_PATH)
if not config_path.exists():
with open(config_path, "w", encoding="utf-8") as f:
json.dump(DEFAULT_CONFIG, f, indent=4)
return True, "配置文件不存在,已创建默认配置"
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_path = f"{DEFAULT_CONFIG_PATH}.backup.{timestamp}"
shutil.copy2(DEFAULT_CONFIG_PATH, backup_path)
with open(DEFAULT_CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(DEFAULT_CONFIG, f, indent=4)
return True
except PermissionError:
logging.error(f"权限不足,无法操作配置文件: {DEFAULT_CONFIG_PATH}")
return False, "权限不足,无法重置配置"
except json.JSONDecodeError:
logging.error("配置文件格式错误,无法解析")
return False, "配置文件格式错误,无法重置"
except Exception as e:
logging.error(f"重置配置文件失败: {e}")
return False, f"重置配置失败: {str(e)}"