mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-17 18:06:06 +00:00
feat: 添加配置文件迁移阶段
This commit is contained in:
@@ -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
|
||||||
@@ -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()
|
||||||
+2
-1
@@ -5,11 +5,12 @@ from ..audit import identifier
|
|||||||
from . import stage
|
from . import stage
|
||||||
|
|
||||||
# 引入启动阶段实现以便注册
|
# 引入启动阶段实现以便注册
|
||||||
from .stages import load_config, setup_logger, build_app
|
from .stages import load_config, setup_logger, build_app, migrate
|
||||||
|
|
||||||
|
|
||||||
stage_order = [
|
stage_order = [
|
||||||
"LoadConfigStage",
|
"LoadConfigStage",
|
||||||
|
"MigrationStage",
|
||||||
"SetupLoggerStage",
|
"SetupLoggerStage",
|
||||||
"BuildAppStage"
|
"BuildAppStage"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -26,4 +26,4 @@ class LoadConfigStage(stage.BootingStage):
|
|||||||
await ap.sensitive_meta.dump_config()
|
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")
|
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()
|
await ap.adapter_qq_botpy_meta.dump_config()
|
||||||
|
|||||||
@@ -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()
|
||||||
@@ -1,78 +1,78 @@
|
|||||||
{
|
{
|
||||||
"说明": "mask将替换敏感词中的每一个字,若mask_word值不为空,则将敏感词整个替换为mask_word的值",
|
"说明": "mask将替换敏感词中的每一个字,若mask_word值不为空,则将敏感词整个替换为mask_word的值",
|
||||||
"mask": "*",
|
"mask": "*",
|
||||||
"mask_word": "",
|
"mask_word": "",
|
||||||
"words": [
|
"words": [
|
||||||
"习近平",
|
"习近平",
|
||||||
"胡锦涛",
|
"胡锦涛",
|
||||||
"江泽民",
|
"江泽民",
|
||||||
"温家宝",
|
"温家宝",
|
||||||
"李克强",
|
"李克强",
|
||||||
"李长春",
|
"李长春",
|
||||||
"毛泽东",
|
"毛泽东",
|
||||||
"邓小平",
|
"邓小平",
|
||||||
"周恩来",
|
"周恩来",
|
||||||
"马克思",
|
"马克思",
|
||||||
"社会主义",
|
"社会主义",
|
||||||
"共产党",
|
"共产党",
|
||||||
"共产主义",
|
"共产主义",
|
||||||
"大陆官方",
|
"大陆官方",
|
||||||
"北京政权",
|
"北京政权",
|
||||||
"中华帝国",
|
"中华帝国",
|
||||||
"中国政府",
|
"中国政府",
|
||||||
"共狗",
|
"共狗",
|
||||||
"六四事件",
|
"六四事件",
|
||||||
"天安门",
|
"天安门",
|
||||||
"六四",
|
"六四",
|
||||||
"政治局常委",
|
"政治局常委",
|
||||||
"两会",
|
"两会",
|
||||||
"共青团",
|
"共青团",
|
||||||
"学潮",
|
"学潮",
|
||||||
"八九",
|
"八九",
|
||||||
"二十大",
|
"二十大",
|
||||||
"民进党",
|
"民进党",
|
||||||
"台独",
|
"台独",
|
||||||
"台湾独立",
|
"台湾独立",
|
||||||
"台湾国",
|
"台湾国",
|
||||||
"国民党",
|
"国民党",
|
||||||
"台湾民国",
|
"台湾民国",
|
||||||
"中华民国",
|
"中华民国",
|
||||||
"pornhub",
|
"pornhub",
|
||||||
"Pornhub",
|
"Pornhub",
|
||||||
"[Yy]ou[Pp]orn",
|
"[Yy]ou[Pp]orn",
|
||||||
"porn",
|
"porn",
|
||||||
"Porn",
|
"Porn",
|
||||||
"[Xx][Vv]ideos",
|
"[Xx][Vv]ideos",
|
||||||
"[Rr]ed[Tt]ube",
|
"[Rr]ed[Tt]ube",
|
||||||
"[Xx][Hh]amster",
|
"[Xx][Hh]amster",
|
||||||
"[Ss]pank[Ww]ire",
|
"[Ss]pank[Ww]ire",
|
||||||
"[Ss]pank[Bb]ang",
|
"[Ss]pank[Bb]ang",
|
||||||
"[Tt]ube8",
|
"[Tt]ube8",
|
||||||
"[Yy]ou[Jj]izz",
|
"[Yy]ou[Jj]izz",
|
||||||
"[Bb]razzers",
|
"[Bb]razzers",
|
||||||
"[Nn]aughty[ ]?[Aa]merica",
|
"[Nn]aughty[ ]?[Aa]merica",
|
||||||
"作爱",
|
"作爱",
|
||||||
"做爱",
|
"做爱",
|
||||||
"性交",
|
"性交",
|
||||||
"性爱",
|
"性爱",
|
||||||
"自慰",
|
"自慰",
|
||||||
"阴茎",
|
"阴茎",
|
||||||
"淫妇",
|
"淫妇",
|
||||||
"肛交",
|
"肛交",
|
||||||
"交配",
|
"交配",
|
||||||
"性关系",
|
"性关系",
|
||||||
"性活动",
|
"性活动",
|
||||||
"色情",
|
"色情",
|
||||||
"色图",
|
"色图",
|
||||||
"涩图",
|
"涩图",
|
||||||
"裸体",
|
"裸体",
|
||||||
"小穴",
|
"小穴",
|
||||||
"淫荡",
|
"淫荡",
|
||||||
"性爱",
|
"性爱",
|
||||||
"翻墙",
|
"翻墙",
|
||||||
"VPN",
|
"VPN",
|
||||||
"科学上网",
|
"科学上网",
|
||||||
"挂梯子",
|
"挂梯子",
|
||||||
"GFW"
|
"GFW"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user