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:
@@ -0,0 +1,44 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Copyright (c) 2025 - 2026 KenanZhu.
|
||||
All rights reserved.
|
||||
|
||||
This software is provided "as is", without any warranty of any kind.
|
||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||
See the LICENSE file for details.
|
||||
"""
|
||||
import managers.config.ConfigManager as ConfigManager
|
||||
|
||||
class ConfigUtils:
|
||||
"""
|
||||
Config utilities class.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def getAutomationConfigPaths(
|
||||
) -> dict[str]:
|
||||
"""
|
||||
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[str]: Validated automation config paths (include user and run config paths).
|
||||
"""
|
||||
cfg_mgr = ConfigManager.instance() # config manager instance
|
||||
|
||||
config_paths = {"run": "", "user": ""}
|
||||
auto_config = cfg_mgr.get(ConfigManager.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(cfg_mgr.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
|
||||
cfg_mgr.set(ConfigManager.ConfigType.GLOBAL, "automation", auto_config)
|
||||
return config_paths
|
||||
+41
-35
@@ -10,41 +10,47 @@ See the LICENSE file for details.
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
|
||||
def calculateNextRepeatTime(
|
||||
repeat_days: list,
|
||||
hour: int,
|
||||
minute: int,
|
||||
second: int
|
||||
) -> datetime:
|
||||
class TimerUtils:
|
||||
"""
|
||||
Calculate the next repeat time based on repeat days and target time.
|
||||
|
||||
This function calculates the next execution time for a repeatable task.
|
||||
If the current day is in repeat_days and the target time has not passed,
|
||||
it returns today's target time. Otherwise, it finds the next matching day.
|
||||
|
||||
Args:
|
||||
repeat_days (list): List of weekdays to repeat (0=Monday, 6=Sunday).
|
||||
hour (int): Target hour (0-23).
|
||||
minute (int): Target minute (0-59).
|
||||
second (int): Target second (0-59).
|
||||
|
||||
Returns:
|
||||
datetime: The next repeat execution time.
|
||||
Timer utilities class.
|
||||
"""
|
||||
|
||||
current_time = datetime.now()
|
||||
current_weekday = current_time.weekday()
|
||||
target_time = current_time.replace(hour=hour, minute=minute, second=second, microsecond=0)
|
||||
if current_weekday in repeat_days:
|
||||
if target_time > current_time:
|
||||
return target_time
|
||||
repeat_days_sorted = sorted(repeat_days)
|
||||
for day in repeat_days_sorted:
|
||||
if day > current_weekday:
|
||||
days_until = day - current_weekday
|
||||
next_time = target_time + timedelta(days=days_until)
|
||||
return next_time
|
||||
days_until = 7 - current_weekday + repeat_days_sorted[0]
|
||||
next_time = target_time + timedelta(days=days_until)
|
||||
return next_time
|
||||
@staticmethod
|
||||
def getNextTimerRepeatTime(
|
||||
repeat_days: list[int],
|
||||
hour: int,
|
||||
minute: int,
|
||||
second: int
|
||||
) -> datetime:
|
||||
"""
|
||||
Calculate the next repeat time based on repeat days and target time.
|
||||
|
||||
This function calculates the next execution time for a repeatable task.
|
||||
If the current day is in repeat_days and the target time has not passed,
|
||||
it returns today's target time. Otherwise, it finds the next matching day.
|
||||
|
||||
Args:
|
||||
repeat_days (list[int]): List of weekdays to repeat (0=Monday, 6=Sunday).
|
||||
hour (int): Target hour (0-23).
|
||||
minute (int): Target minute (0-59).
|
||||
second (int): Target second (0-59).
|
||||
|
||||
Returns:
|
||||
datetime: The next repeat execution time.
|
||||
"""
|
||||
|
||||
current_time = datetime.now()
|
||||
current_weekday = current_time.weekday()
|
||||
target_time = current_time.replace(hour=hour, minute=minute, second=second, microsecond=0)
|
||||
if current_weekday in repeat_days:
|
||||
if target_time > current_time:
|
||||
return target_time
|
||||
repeat_days_sorted = sorted(repeat_days)
|
||||
for day in repeat_days_sorted:
|
||||
if day > current_weekday:
|
||||
days_until = day - current_weekday
|
||||
next_time = target_time + timedelta(days=days_until)
|
||||
return next_time
|
||||
days_until = 7 - current_weekday + repeat_days_sorted[0]
|
||||
next_time = target_time + timedelta(days=days_until)
|
||||
return next_time
|
||||
|
||||
Reference in New Issue
Block a user