From 424f82a443a6818b5066eddc5dbde35d3047be45 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Fri, 23 Jun 2023 12:42:16 +0800 Subject: 添加Rule基类 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hydrorollcore/main.py | 2 -- hydrorollcore/rule.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) delete mode 100644 hydrorollcore/main.py create mode 100644 hydrorollcore/rule.py (limited to 'hydrorollcore') diff --git a/hydrorollcore/main.py b/hydrorollcore/main.py deleted file mode 100644 index 319cefc9..00000000 --- a/hydrorollcore/main.py +++ /dev/null @@ -1,2 +0,0 @@ -def main(): - return None diff --git a/hydrorollcore/rule.py b/hydrorollcore/rule.py new file mode 100644 index 00000000..bdc24369 --- /dev/null +++ b/hydrorollcore/rule.py @@ -0,0 +1,30 @@ +import os +import importlib.util + +__all__ = ["Rule"] + +class Rule: + def __init__(self, folder_path): + self.folder_path = folder_path + + def load_modules(self): + # 获取指定文件夹下的所有文件和文件夹 + file_list = os.listdir(self.folder_path) + module_list = [] + + # 遍历文件列表 + for file_name in file_list: + file_path = os.path.join(self.folder_path, file_name) + + # 判断是否为Python文件或者包 + if file_name.startswith('_') or not file_name.endswith(".py"): + continue + + # 尝试加载模块 + module_name = os.path.splitext(file_name)[0] + spec = importlib.util.spec_from_file_location(module_name, file_path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module_list.append(module) + + return module_list -- cgit v1.2.3-70-g09d2