From f7e8f6f166114b9ab9e05852f5cb80d3d36eab2f Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Tue, 17 Oct 2023 14:55:30 +0800 Subject: feat(status): add deprecated and new status --- tests/MyRule/core.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/MyRule/core.py (limited to 'tests/MyRule/core.py') diff --git a/tests/MyRule/core.py b/tests/MyRule/core.py new file mode 100644 index 00000000..acebe644 --- /dev/null +++ b/tests/MyRule/core.py @@ -0,0 +1,43 @@ +import os +import importlib +from typing import List +from pydantic import BaseModel +import asyncio + +class Rule(BaseModel): + name: str + config_name: str + + async def success(self): + # 处理成功事件的逻辑 + pass + + async def fail(self): + # 处理失败事件的逻辑 + pass + +async def load_rules_from_folder(folder_path: str) -> List[Rule]: + rules = [] + for file_name in os.listdir(folder_path): + if file_name.endswith(".py"): + module_name = file_name[:-3] + module = importlib.import_module(module_name) + for name, obj in module.__dict__.items(): + if isinstance(obj, type) and issubclass(obj, Rule) and obj != Rule: + rules.append(obj()) + return rules + +async def process_event(rules: List[Rule], event: str): + tasks = [] + for rule in rules: + if hasattr(rule, event): + task = getattr(rule, event)() + tasks.append(task) + await asyncio.gather(*tasks) + +async def main(): + folder_path = "tests/MyRule/Rule" + rules = await load_rules_from_folder(folder_path) + await process_event(rules, "success") + +asyncio.run(main()) -- cgit v1.2.3-70-g09d2