diff --git a/.gitignore b/.gitignore index ae0963f..eec30e5 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,7 @@ pip-selfcheck.json # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 +.idea # User-specific stuff .idea/**/workspace.xml .idea/**/tasks.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index fdc85e3..de9c025 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,9 @@ - + + + diff --git a/.idea/myAssistant.iml b/.idea/myAssistant.iml index 6122bed..ef2940e 100644 --- a/.idea/myAssistant.iml +++ b/.idea/myAssistant.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/main.py b/main.py index e69de29..37b32a4 100644 --- a/main.py +++ b/main.py @@ -0,0 +1,10 @@ +from util.PluginManager import PluginManager + + +if __name__ == "__main__": + manager = PluginManager() + + # Load and execute plugins + manager.load_plugins() + plugin_names = manager.get_plugin_name_list() + print(plugin_names) \ No newline at end of file diff --git a/util/PluginManager.py b/util/PluginManager.py new file mode 100644 index 0000000..e9dcfd1 --- /dev/null +++ b/util/PluginManager.py @@ -0,0 +1,58 @@ +import importlib.util +import os + +from util.base_plugin import BasePlugin + + +class PluginManager: + PLUGIN_DIR = "custom_plugins" + + def __init__(self): + self.plugins = {} + self.plugin_dir = PluginManager.PLUGIN_DIR + + def load_plugins(self): + for root, dirs, files in os.walk(self.plugin_dir): + for file in files: + if file.endswith(".py"): + plugin_name = os.path.splitext(file)[0] + plugin_path = os.path.join(root, file) + print(plugin_name) + self.load_plugin(plugin_name, plugin_path) + + def load_plugin(self, plugin_name, plugin_path): + spec = importlib.util.spec_from_file_location(plugin_name, plugin_path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + + for member_name in dir(module): + member = getattr(module, member_name) + + if callable(member) and hasattr(member, "__bases__") and BasePlugin in member.__bases__: + print(member) + self.plugins[plugin_name] = member() + print(f"Plugin '{plugin_name}' loaded successfully.") + + def execute_plugins(self, *args, **kwargs): + for plugin_name, plugin_instance in self.plugins.items(): + print(f"Executing plugin '{plugin_name}':") + plugin_instance.run_plugin(self.callback, *args, **kwargs) + + def execute_plugin(self, plugin_name, *args, **kwargs): + if plugin_name in self.plugins: + print(f"Executing plugin '{plugin_name}':") + self.plugins[plugin_name].run_plugin(self.callback, *args, **kwargs) + else: + print(f"Plugin '{plugin_name}' not found.") + + def get_plugin_name_list(self): + + return [plugin[0] for plugin in self.plugins.items()] + + def callback(self, result): + print(f"Plugin '{result['plugin_name']}' returned:") + print(f"Success: {result['success']}") + if result['success']: + print(f"Result: {result['result']}") + else: + print(f"Error: {result['error']}") \ No newline at end of file diff --git a/util/base_plugin.py b/util/base_plugin.py new file mode 100644 index 0000000..5dea108 --- /dev/null +++ b/util/base_plugin.py @@ -0,0 +1,7 @@ +class BasePlugin: + def __init__(self, plugin_name, title): + self.plugin_name = plugin_name + self.title = title + + def run_plugin(self, callback): + raise NotImplementedError("Subclasses must implement run_plugin method") diff --git a/util/plugin.py b/util/plugin.py deleted file mode 100644 index 56cb42d..0000000 --- a/util/plugin.py +++ /dev/null @@ -1,33 +0,0 @@ - -class BasePlugin: - - def __init__(self): - self.__plugin_name = "" - - - def get_result(self): - """ - 用以返回插件的结果,所有插件都必须通过该方法返回结果 - 返回结果的类型必须是指定的类型 - :return: - """ - raise NotImplemented - - -class FinanceValue: - - def __init__(self): - self.__name = "" - self.__value = "" - - def set_name(self, name): - self.__name = name - - def get_name(self): - return self.__name - - def set_value(self, value): - self.__value = value - - def get_value(self): - return self.__value \ No newline at end of file