mirror of
https://github.com/hanxi/xiaomusic.git
synced 2026-03-15 08:13:16 +08:00
优化配置
This commit is contained in:
@@ -88,20 +88,10 @@ class ConfigManager:
|
||||
|
||||
Args:
|
||||
data: 配置数据字典
|
||||
|
||||
Returns:
|
||||
dict: 更新前的配置快照(用于检测变化)
|
||||
"""
|
||||
# 保存更新前的配置快照
|
||||
snapshot = {
|
||||
"enable_file_watch": self.config.enable_file_watch,
|
||||
}
|
||||
|
||||
# 自动赋值相同字段的配置
|
||||
self.config.update_config(data)
|
||||
|
||||
return snapshot
|
||||
|
||||
def get_config(self):
|
||||
"""获取当前配置
|
||||
|
||||
|
||||
@@ -107,11 +107,17 @@ class FileWatcherManager:
|
||||
self._file_watch_handler = None
|
||||
|
||||
def start(self, loop):
|
||||
"""启动文件监控
|
||||
"""启动文件监控(支持重入)
|
||||
|
||||
如果监控器已在运行,会先停止再重新启动。
|
||||
|
||||
Args:
|
||||
loop: asyncio 事件循环对象
|
||||
"""
|
||||
# 如果已经在运行,先停止
|
||||
if self._observer:
|
||||
self.stop()
|
||||
|
||||
if not self.config.enable_file_watch:
|
||||
self.log.info("目录监控功能已关闭")
|
||||
return
|
||||
@@ -136,7 +142,10 @@ class FileWatcherManager:
|
||||
self.log.info(f"已启动对 {self.music_path} 的目录监控。")
|
||||
|
||||
def stop(self):
|
||||
"""停止文件监控"""
|
||||
"""停止文件监控(支持重入)
|
||||
|
||||
如果监控器未运行,不做任何操作。
|
||||
"""
|
||||
if self._observer:
|
||||
self._observer.stop()
|
||||
self._observer.join()
|
||||
|
||||
@@ -952,9 +952,8 @@ class XiaoMusic:
|
||||
|
||||
def update_config_from_setting(self, data):
|
||||
"""从设置更新配置"""
|
||||
# 委托给 config_manager 更新配置,并获取更新前的快照
|
||||
snapshot = self._config_manager.update_config(data)
|
||||
pre_efw = snapshot["enable_file_watch"]
|
||||
# 委托给 config_manager 更新配置
|
||||
self._config_manager.update_config(data)
|
||||
|
||||
# 重新初始化配置相关的属性
|
||||
self.init_config()
|
||||
@@ -966,14 +965,13 @@ class XiaoMusic:
|
||||
self.log.info(f"语音控制已启动, 用【{joined_keywords}】开头来控制")
|
||||
self.log.debug(f"key_word_dict: {self.config.key_word_dict}")
|
||||
|
||||
# 检查 enable_file_watch 配置是否发生变化
|
||||
now_efw = self.config.enable_file_watch
|
||||
if pre_efw != now_efw:
|
||||
self.log.info("配置更新:{}目录监控".format("开启" if now_efw else "关闭"))
|
||||
if now_efw:
|
||||
self.start_file_watch()
|
||||
else:
|
||||
self.stop_file_watch()
|
||||
# 根据新配置控制文件监控
|
||||
if self.config.enable_file_watch:
|
||||
self.log.info("配置更新:开启目录监控")
|
||||
self.start_file_watch()
|
||||
else:
|
||||
self.log.info("配置更新:关闭目录监控")
|
||||
self.stop_file_watch()
|
||||
|
||||
# 重新加载计划任务
|
||||
self.crontab.reload_config(self)
|
||||
|
||||
Reference in New Issue
Block a user