️ 对于启动config进行懒写入

This commit is contained in:
KimigaiiWuyi 2025-04-02 05:19:23 +08:00
parent 0814748eb6
commit d0075562f0
5 changed files with 21 additions and 10 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 MiB

View File

@ -50,6 +50,7 @@ plugins_sample = {
class CoreConfig: class CoreConfig:
def __init__(self) -> None: def __init__(self) -> None:
self.lock = False
if OLD_CONFIG_PATH.exists(): if OLD_CONFIG_PATH.exists():
if not CONFIG_PATH.exists(): if not CONFIG_PATH.exists():
shutil.copy2(OLD_CONFIG_PATH, CONFIG_PATH) shutil.copy2(OLD_CONFIG_PATH, CONFIG_PATH)
@ -143,5 +144,15 @@ class CoreConfig:
else: else:
return False return False
def lazy_write_config(self):
self.write_config()
def lazy_set_config(
self, key: str, value: Union[str, List, Dict, int, bool]
):
if key in CONFIG_DEFAULT:
# 设置值
self.config[key] = value
core_config = CoreConfig() core_config = CoreConfig()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

View File

@ -13,6 +13,7 @@ from fastapi import WebSocket
from gsuid_core.bot import _Bot from gsuid_core.bot import _Bot
from gsuid_core.logger import logger from gsuid_core.logger import logger
from gsuid_core.config import core_config
from gsuid_core.utils.plugins_update.utils import check_start_tool from gsuid_core.utils.plugins_update.utils import check_start_tool
from gsuid_core.utils.plugins_config.gs_config import core_plugins_config from gsuid_core.utils.plugins_config.gs_config import core_plugins_config
@ -74,10 +75,10 @@ class GsServer:
return f'插件{plugin.name}包含"_", 跳过加载!' return f'插件{plugin.name}包含"_", 跳过加载!'
# 如果发现文件夹,则视为插件包 # 如果发现文件夹,则视为插件包
logger.trace('===============')
logger.debug(f'🔹 导入{plugin.stem}中...') logger.debug(f'🔹 导入{plugin.stem}中...')
logger.trace('===============') logger.trace('===============')
try: try:
module_list = []
if plugin.is_dir(): if plugin.is_dir():
plugin_path = plugin / '__init__.py' plugin_path = plugin / '__init__.py'
plugins_path = plugin / '__full__.py' plugins_path = plugin / '__full__.py'
@ -87,7 +88,6 @@ class GsServer:
sys.path.append(str(plugin_path.parents)) sys.path.append(str(plugin_path.parents))
if plugins_path.exists(): if plugins_path.exists():
module_list = self.load_dir_plugins(plugin, plugin_parent) module_list = self.load_dir_plugins(plugin, plugin_parent)
return module_list
elif nest_path.exists() or src_path.exists(): elif nest_path.exists() or src_path.exists():
path = nest_path.parent / plugin.name path = nest_path.parent / plugin.name
pyproject = plugin / 'pyproject.toml' pyproject = plugin / 'pyproject.toml'
@ -97,7 +97,6 @@ class GsServer:
module_list = self.load_dir_plugins( module_list = self.load_dir_plugins(
path, plugin_parent, True path, plugin_parent, True
) )
return module_list
# 如果文件夹内有__init_.py则视为单个插件包 # 如果文件夹内有__init_.py则视为单个插件包
elif plugin_path.exists(): elif plugin_path.exists():
module_list = [ module_list = [
@ -105,7 +104,6 @@ class GsServer:
f'{plugin_parent}.{plugin.name}.__init__' f'{plugin_parent}.{plugin.name}.__init__'
) )
] ]
return module_list
# 如果发现单文件,则视为单文件插件 # 如果发现单文件,则视为单文件插件
elif plugin.suffix == '.py': elif plugin.suffix == '.py':
module_list = [ module_list = [
@ -113,9 +111,9 @@ class GsServer:
f'{plugin_parent}.{plugin.name[:-3]}' f'{plugin_parent}.{plugin.name[:-3]}'
) )
] ]
return module_list
'''导入成功''' '''导入成功'''
logger.success(f'✅ 插件{plugin.stem}导入成功!') logger.success(f'✅ 插件{plugin.stem}导入成功!')
return module_list
except Exception as e: # noqa except Exception as e: # noqa
exception = sys.exc_info() exception = sys.exc_info()
logger.opt(exception=exception).error(f'加载插件时发生错误: {e}') logger.opt(exception=exception).error(f'加载插件时发生错误: {e}')
@ -134,7 +132,9 @@ class GsServer:
# 遍历插件文件夹内所有文件 # 遍历插件文件夹内所有文件
for plugin in plug_path_list: for plugin in plug_path_list:
self.load_plugin(plugin) self.load_plugin(plugin)
logger.info('[GsCore] 插件加载完成!')
core_config.lazy_write_config()
logger.success('[GsCore] 插件加载完成!')
def load_dir_plugins( def load_dir_plugins(
self, plugin: Path, plugin_parent: str, nest: bool = False self, plugin: Path, plugin_parent: str, nest: bool = False

View File

@ -195,7 +195,7 @@ class SV:
config_plugins[plugins_name] = _plugins_config config_plugins[plugins_name] = _plugins_config
plugins = Plugins(**_plugins_config) plugins = Plugins(**_plugins_config)
core_config.set_config('plugins', config_plugins) core_config.lazy_set_config('plugins', config_plugins)
else: else:
if 'prefix' not in config_plugins[plugins_name]: if 'prefix' not in config_plugins[plugins_name]:
if plugins_name in SL.plugins: if plugins_name in SL.plugins:
@ -236,7 +236,7 @@ class SV:
**config_plugins[plugins_name], **config_plugins[plugins_name],
) )
core_config.set_config('plugins', config_plugins) core_config.lazy_set_config('plugins', config_plugins)
# SV指向唯一Plugins实例 # SV指向唯一Plugins实例
self.plugins = plugins self.plugins = plugins
@ -261,7 +261,7 @@ class SV:
self.area = config_sv[name]['area'] self.area = config_sv[name]['area']
self.white_list = config_sv[name]['white_list'] self.white_list = config_sv[name]['white_list']
del config_sv[name] del config_sv[name]
core_config.set_config('sv', config_sv) core_config.lazy_set_config('sv', config_sv)
elif name in plugin_sv_config: elif name in plugin_sv_config:
self.priority = plugin_sv_config[name]['priority'] self.priority = plugin_sv_config[name]['priority']
self.enabled = plugin_sv_config[name]['enabled'] self.enabled = plugin_sv_config[name]['enabled']
@ -303,7 +303,7 @@ class SV:
for var in kwargs: for var in kwargs:
setattr(self, var, kwargs[var]) setattr(self, var, kwargs[var])
plugin_sv_config[self.name][var] = kwargs[var] plugin_sv_config[self.name][var] = kwargs[var]
core_config.set_config('plugins', config_plugins) core_config.lazy_set_config('plugins', config_plugins)
def enable(self): def enable(self):
self.set(enabled=True) self.set(enabled=True)