mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-18 15:33:03 +08:00
refactor(config): 新增 ConfigUtils 工具类并优化配置管理逻辑
- 新增 ConfigUtils 工具类,提供配置路径获取等工具方法 - 将 ConfigManager.getValidateAutomationConfigPaths() 重构为 ConfigUtils.getAutomationConfigPaths() - 优化 MsgBase 中 LogManager 的导入方式,使用模块导入替代函数导入 - 规范化 TimerUtils.py 中 calculate_next_repeat_time() 的文档字符串格式
This commit is contained in:
@@ -176,49 +176,7 @@ class ConfigManager:
|
||||
|
||||
|
||||
# ConfigManager singleton instance.
|
||||
_config_manager_instance = None
|
||||
|
||||
# Utility functions.
|
||||
#
|
||||
# Utility function to get validated automation config paths.
|
||||
def getValidateAutomationConfigPaths(
|
||||
) -> dict:
|
||||
"""
|
||||
Get validated automation config paths from ConfigManager instance.
|
||||
These function will validate the config paths and return the validated paths in a dict.
|
||||
|
||||
Returns:
|
||||
dict: Validated automation config paths.
|
||||
"""
|
||||
config_paths = {"run": "", "user": ""}
|
||||
auto_config = _config_manager_instance.get(ConfigType.GLOBAL, "automation", {})
|
||||
for cfg_type in ["run", "user"]:
|
||||
paths = auto_config.get(f"{cfg_type}_path", {}).get("paths", [])
|
||||
index = auto_config.get(f"{cfg_type}_path", {}).get("current", 0)
|
||||
if paths == []:
|
||||
paths.append(os.path.join(_config_manager_instance.configDir(), f"{cfg_type}.json"))
|
||||
if index < 0:
|
||||
index = 0
|
||||
if index >= len(paths):
|
||||
index = len(paths) - 1
|
||||
config_paths[cfg_type] = paths[index]
|
||||
data = {"current": index, "paths": paths}
|
||||
auto_config[f"{cfg_type}_path"] = data
|
||||
_config_manager_instance.set(ConfigType.GLOBAL, "automation", auto_config)
|
||||
return config_paths
|
||||
|
||||
# Utility function to get base config directory.
|
||||
def getBaseConfigDir(
|
||||
) -> str:
|
||||
"""
|
||||
Get base config directory, on Windows, it is usually at :
|
||||
'C:\\Users\\<username>\\AppData\\Local\\AutoLibrary\\config'.
|
||||
|
||||
Returns:
|
||||
str: Base config directory.
|
||||
"""
|
||||
|
||||
return _config_manager_instance.configDir()
|
||||
_config_manager_instance : ConfigManager | None = None
|
||||
|
||||
# Singleton instance of ConfigManager.
|
||||
_instance_lock = threading.Lock()
|
||||
@@ -240,6 +198,6 @@ def instance(
|
||||
else:
|
||||
if config_dir == "":
|
||||
return _config_manager_instance
|
||||
if getBaseConfigDir() != config_dir:
|
||||
if _config_manager_instance.configDir() != config_dir:
|
||||
raise ValueError("ConfigManager 的实例已初始化,不能使用不同的配置目录。")
|
||||
return _config_manager_instance
|
||||
|
||||
Reference in New Issue
Block a user