diff options
| author | 2023-10-17 14:55:30 +0800 | |
|---|---|---|
| committer | 2023-10-17 14:55:30 +0800 | |
| commit | f7e8f6f166114b9ab9e05852f5cb80d3d36eab2f (patch) | |
| tree | c72f096792ff7343f9744d20ae9c5afd2d340884 /tests/MyRule/core.py | |
| parent | 7967a1317b54a17d8039ad7e25a03681bf6aacb2 (diff) | |
| download | infini-f7e8f6f166114b9ab9e05852f5cb80d3d36eab2f.tar.gz infini-f7e8f6f166114b9ab9e05852f5cb80d3d36eab2f.zip | |
feat(status): add deprecated and new status
Diffstat (limited to 'tests/MyRule/core.py')
| -rw-r--r-- | tests/MyRule/core.py | 43 |
1 files changed, 43 insertions, 0 deletions
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()) |
