From f91c9015bcbe34abf6996734de4bc9a37eaa7a90 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Sat, 16 Mar 2024 20:27:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=BF=81=E7=A7=BB=E9=98=B6=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/config/migration.py | 46 ++++++ pkg/config/migrations/__init__.py | 0 .../migrations/m1_sensitive_word_migration.py | 26 +++ pkg/core/boot.py | 3 +- pkg/core/stages/load_config.py | 2 +- pkg/core/stages/migrate.py | 28 ++++ templates/metadata/sensitive-words.json | 152 +++++++++--------- 7 files changed, 179 insertions(+), 78 deletions(-) create mode 100644 pkg/config/migration.py create mode 100644 pkg/config/migrations/__init__.py create mode 100644 pkg/config/migrations/m1_sensitive_word_migration.py create mode 100644 pkg/core/stages/migrate.py diff --git a/pkg/config/migration.py b/pkg/config/migration.py new file mode 100644 index 00000000..3a6650b2 --- /dev/null +++ b/pkg/config/migration.py @@ -0,0 +1,46 @@ +from __future__ import annotations + +import abc +import typing + +from ..core import app + + +preregistered_migrations: list[typing.Type[Migration]] = [] + +def migration_class(name: str, number: int): + """注册一个迁移 + """ + def decorator(cls: typing.Type[Migration]) -> typing.Type[Migration]: + cls.name = name + cls.number = number + preregistered_migrations.append(cls) + return cls + + return decorator + + +class Migration(abc.ABC): + """一个版本的迁移 + """ + + name: str + + number: int + + ap: app.Application + + def __init__(self, ap: app.Application): + self.ap = ap + + @abc.abstractmethod + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移 + """ + pass + + @abc.abstractmethod + async def run(self): + """执行迁移 + """ + pass diff --git a/pkg/config/migrations/__init__.py b/pkg/config/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pkg/config/migrations/m1_sensitive_word_migration.py b/pkg/config/migrations/m1_sensitive_word_migration.py new file mode 100644 index 00000000..cb8fc1c6 --- /dev/null +++ b/pkg/config/migrations/m1_sensitive_word_migration.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +import os +import sys + +from .. import migration + + +@migration.migration_class("sensitive-word-migration", 1) +class SensitiveWordMigration(migration.Migration): + """敏感词迁移 + """ + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移 + """ + return os.path.exists("data/config/sensitive-words.json") + + async def run(self): + """执行迁移 + """ + # 移动文件 + os.rename("data/config/sensitive-words.json", "data/metadata/sensitive-words.json") + + # 重新加载配置 + await self.ap.sensitive_meta.load_config() diff --git a/pkg/core/boot.py b/pkg/core/boot.py index 46f06760..081a2f84 100644 --- a/pkg/core/boot.py +++ b/pkg/core/boot.py @@ -5,11 +5,12 @@ from ..audit import identifier from . import stage # 引入启动阶段实现以便注册 -from .stages import load_config, setup_logger, build_app +from .stages import load_config, setup_logger, build_app, migrate stage_order = [ "LoadConfigStage", + "MigrationStage", "SetupLoggerStage", "BuildAppStage" ] diff --git a/pkg/core/stages/load_config.py b/pkg/core/stages/load_config.py index cdba5f8f..7f83dbad 100644 --- a/pkg/core/stages/load_config.py +++ b/pkg/core/stages/load_config.py @@ -26,4 +26,4 @@ class LoadConfigStage(stage.BootingStage): 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 + await ap.adapter_qq_botpy_meta.dump_config() diff --git a/pkg/core/stages/migrate.py b/pkg/core/stages/migrate.py new file mode 100644 index 00000000..5e84b182 --- /dev/null +++ b/pkg/core/stages/migrate.py @@ -0,0 +1,28 @@ +from __future__ import annotations + +import importlib + +from .. import stage, app +from ...config import migration +from ...config.migrations import m1_sensitive_word_migration + + +@stage.stage_class("MigrationStage") +class MigrationStage(stage.BootingStage): + """迁移阶段 + """ + + async def run(self, ap: app.Application): + """启动 + """ + + migrations = migration.preregistered_migrations + + # 按照迁移号排序 + migrations.sort(key=lambda x: x.number) + + for migration_cls in migrations: + migration_instance = migration_cls(ap) + + if await migration_instance.need_migrate(): + await migration_instance.run() diff --git a/templates/metadata/sensitive-words.json b/templates/metadata/sensitive-words.json index 61d15ff9..86820287 100644 --- a/templates/metadata/sensitive-words.json +++ b/templates/metadata/sensitive-words.json @@ -1,78 +1,78 @@ { - "说明": "mask将替换敏感词中的每一个字,若mask_word值不为空,则将敏感词整个替换为mask_word的值", - "mask": "*", - "mask_word": "", - "words": [ - "习近平", - "胡锦涛", - "江泽民", - "温家宝", - "李克强", - "李长春", - "毛泽东", - "邓小平", - "周恩来", - "马克思", - "社会主义", - "共产党", - "共产主义", - "大陆官方", - "北京政权", - "中华帝国", - "中国政府", - "共狗", - "六四事件", - "天安门", - "六四", - "政治局常委", - "两会", - "共青团", - "学潮", - "八九", - "二十大", - "民进党", - "台独", - "台湾独立", - "台湾国", - "国民党", - "台湾民国", - "中华民国", - "pornhub", - "Pornhub", - "[Yy]ou[Pp]orn", - "porn", - "Porn", - "[Xx][Vv]ideos", - "[Rr]ed[Tt]ube", - "[Xx][Hh]amster", - "[Ss]pank[Ww]ire", - "[Ss]pank[Bb]ang", - "[Tt]ube8", - "[Yy]ou[Jj]izz", - "[Bb]razzers", - "[Nn]aughty[ ]?[Aa]merica", - "作爱", - "做爱", - "性交", - "性爱", - "自慰", - "阴茎", - "淫妇", - "肛交", - "交配", - "性关系", - "性活动", - "色情", - "色图", - "涩图", - "裸体", - "小穴", - "淫荡", - "性爱", - "翻墙", - "VPN", - "科学上网", - "挂梯子", - "GFW" - ] + "说明": "mask将替换敏感词中的每一个字,若mask_word值不为空,则将敏感词整个替换为mask_word的值", + "mask": "*", + "mask_word": "", + "words": [ + "习近平", + "胡锦涛", + "江泽民", + "温家宝", + "李克强", + "李长春", + "毛泽东", + "邓小平", + "周恩来", + "马克思", + "社会主义", + "共产党", + "共产主义", + "大陆官方", + "北京政权", + "中华帝国", + "中国政府", + "共狗", + "六四事件", + "天安门", + "六四", + "政治局常委", + "两会", + "共青团", + "学潮", + "八九", + "二十大", + "民进党", + "台独", + "台湾独立", + "台湾国", + "国民党", + "台湾民国", + "中华民国", + "pornhub", + "Pornhub", + "[Yy]ou[Pp]orn", + "porn", + "Porn", + "[Xx][Vv]ideos", + "[Rr]ed[Tt]ube", + "[Xx][Hh]amster", + "[Ss]pank[Ww]ire", + "[Ss]pank[Bb]ang", + "[Tt]ube8", + "[Yy]ou[Jj]izz", + "[Bb]razzers", + "[Nn]aughty[ ]?[Aa]merica", + "作爱", + "做爱", + "性交", + "性爱", + "自慰", + "阴茎", + "淫妇", + "肛交", + "交配", + "性关系", + "性活动", + "色情", + "色图", + "涩图", + "裸体", + "小穴", + "淫荡", + "性爱", + "翻墙", + "VPN", + "科学上网", + "挂梯子", + "GFW" + ] } \ No newline at end of file