From 302d86056db96964011275c1d2f117b619307761 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Sat, 16 Mar 2024 15:41:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=89=80=E6=9C=89=E7=9A=84=20json?= =?UTF-8?q?=20=E5=8A=A0=E8=BD=BD=E7=BB=9F=E4=B8=80=E5=88=B0=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E9=98=B6=E6=AE=B5=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/core/app.py | 12 ++++++++++++ pkg/core/bootutils/files.py | 1 - pkg/core/stages/load_config.py | 10 ++++++++++ pkg/pipeline/cntfilter/filters/banwords.py | 15 +++++---------- pkg/platform/sources/qqbotpy.py | 5 +---- pkg/plugin/setting.py | 5 +---- templates/{ => metadata}/sensitive-words.json | 0 7 files changed, 29 insertions(+), 19 deletions(-) rename templates/{ => metadata}/sensitive-words.json (100%) diff --git a/pkg/core/app.py b/pkg/core/app.py index 0d726a44..e1df54ec 100644 --- a/pkg/core/app.py +++ b/pkg/core/app.py @@ -33,6 +33,8 @@ class Application: tool_mgr: llm_tool_mgr.ToolManager = None + # ======= 配置管理器 ======= + command_cfg: config_mgr.ConfigManager = None pipeline_cfg: config_mgr.ConfigManager = None @@ -43,6 +45,16 @@ class Application: system_cfg: config_mgr.ConfigManager = None + # ======= 元数据配置管理器 ======= + + sensitive_meta: config_mgr.ConfigManager = None + + adapter_qq_botpy_meta: config_mgr.ConfigManager = None + + plugin_setting_meta: config_mgr.ConfigManager = None + + # ========================= + ctr_mgr: center_mgr.V2CenterAPI = None plugin_mgr: plugin_mgr.PluginManager = None diff --git a/pkg/core/bootutils/files.py b/pkg/core/bootutils/files.py index ab4b15cf..9bab3826 100644 --- a/pkg/core/bootutils/files.py +++ b/pkg/core/bootutils/files.py @@ -13,7 +13,6 @@ required_files = { "data/config/platform.json": "templates/platform.json", "data/config/provider.json": "templates/provider.json", "data/config/system.json": "templates/system.json", - "data/config/sensitive-words.json": "templates/sensitive-words.json", "data/scenario/default.json": "templates/scenario-template.json", } diff --git a/pkg/core/stages/load_config.py b/pkg/core/stages/load_config.py index 0ee2c1e4..cdba5f8f 100644 --- a/pkg/core/stages/load_config.py +++ b/pkg/core/stages/load_config.py @@ -17,3 +17,13 @@ class LoadConfigStage(stage.BootingStage): ap.platform_cfg = await config.load_json_config("data/config/platform.json", "templates/platform.json") ap.provider_cfg = await config.load_json_config("data/config/provider.json", "templates/provider.json") ap.system_cfg = await config.load_json_config("data/config/system.json", "templates/system.json") + + + ap.plugin_setting_meta = await config.load_json_config("plugins/plugins.json", "templates/plugin-settings.json") + await ap.plugin_setting_meta.dump_config() + + ap.sensitive_meta = await config.load_json_config("data/metadata/sensitive-words.json", "templates/metadata/sensitive-words.json") + await ap.sensitive_meta.dump_config() + + ap.adapter_qq_botpy_meta = await config.load_json_config("data/metadata/adapter-qq-botpy.json", "templates/metadata/adapter-qq-botpy.json") + await ap.adapter_qq_botpy_meta.dump_config() \ No newline at end of file diff --git a/pkg/pipeline/cntfilter/filters/banwords.py b/pkg/pipeline/cntfilter/filters/banwords.py index 9391971c..5cd7dcfa 100644 --- a/pkg/pipeline/cntfilter/filters/banwords.py +++ b/pkg/pipeline/cntfilter/filters/banwords.py @@ -10,31 +10,26 @@ from ....config import manager as cfg_mgr class BanWordFilter(filter_model.ContentFilter): """根据内容禁言""" - sensitive: cfg_mgr.ConfigManager - async def initialize(self): - self.sensitive = await cfg_mgr.load_json_config( - "data/config/sensitive-words.json", - "templates/sensitive-words.json" - ) + pass async def process(self, message: str) -> entities.FilterResult: found = False - for word in self.sensitive.data['words']: + for word in self.ap.sensitive_meta.data['words']: match = re.findall(word, message) if len(match) > 0: found = True for i in range(len(match)): - if self.sensitive.data['mask_word'] == "": + if self.ap.sensitive_meta.data['mask_word'] == "": message = message.replace( - match[i], self.sensitive.data['mask'] * len(match[i]) + match[i], self.ap.sensitive_meta.data['mask'] * len(match[i]) ) else: message = message.replace( - match[i], self.sensitive.data['mask_word'] + match[i], self.ap.sensitive_meta.data['mask_word'] ) return entities.FilterResult( diff --git a/pkg/platform/sources/qqbotpy.py b/pkg/platform/sources/qqbotpy.py index ab981cfe..5d731d34 100644 --- a/pkg/platform/sources/qqbotpy.py +++ b/pkg/platform/sources/qqbotpy.py @@ -506,10 +506,7 @@ class OfficialAdapter(adapter_model.MessageSourceAdapter): async def run_async(self): - self.metadata = await cfg_mgr.load_json_config( - "data/metadata/adapter-qq-botpy.json", - "templates/metadata/adapter-qq-botpy.json", - ) + self.metadata = self.ap.adapter_qq_botpy_meta self.member_openid_mapping = OpenIDMapping( map=self.metadata.data["mapping"]["members"], diff --git a/pkg/plugin/setting.py b/pkg/plugin/setting.py index c1934937..7e715af1 100644 --- a/pkg/plugin/setting.py +++ b/pkg/plugin/setting.py @@ -16,10 +16,7 @@ class SettingManager: self.ap = ap async def initialize(self): - self.settings = await cfg_mgr.load_json_config( - 'plugins/plugins.json', - 'templates/plugin-settings.json' - ) + self.settings = self.ap.plugin_setting_meta async def sync_setting( self, diff --git a/templates/sensitive-words.json b/templates/metadata/sensitive-words.json similarity index 100% rename from templates/sensitive-words.json rename to templates/metadata/sensitive-words.json