|
4 | 4 | import difflib |
5 | 5 | import chardet |
6 | 6 | import html |
| 7 | +import importlib.util |
7 | 8 |
|
8 | 9 | from PyQt5.QtCore import Qt, QRect, QSize, QThread, pyqtSignal |
9 | 10 | from PyQt5.QtGui import QFont, QColor, QTextCharFormat, QPainter |
|
22 | 23 |
|
23 | 24 |
|
24 | 25 |
|
| 26 | +""" Utility function to load plugins """ |
| 27 | +def load_plugins(app_context): |
| 28 | + user_home = os.path.expanduser("~") |
| 29 | + plugins_dir = os.path.join(user_home, "fdplugins") |
| 30 | + os.makedirs(plugins_dir, exist_ok=True) |
| 31 | + loaded_plugins = [] |
| 32 | + for filename in os.listdir(plugins_dir): |
| 33 | + if filename.endswith(".py") and not filename.startswith("_"): |
| 34 | + plugin_path = os.path.join(plugins_dir, filename) |
| 35 | + mod_name = os.path.splitext(filename)[0] |
| 36 | + spec = importlib.util.spec_from_file_location(mod_name, plugin_path) |
| 37 | + module = importlib.util.module_from_spec(spec) |
| 38 | + try: |
| 39 | + spec.loader.exec_module(module) |
| 40 | + if hasattr(module, "register_plugin"): |
| 41 | + module.register_plugin(app_context) |
| 42 | + loaded_plugins.append(mod_name) |
| 43 | + print(f"Plugin '{mod_name}' loaded successfully from {plugins_dir}") |
| 44 | + except Exception as e: |
| 45 | + print(f"Failed to load plugin '{filename}' from {plugins_dir}: {e}") |
| 46 | + return loaded_plugins |
| 47 | + |
| 48 | + |
| 49 | + |
25 | 50 | """ Utility function to load the CSS stylesheet """ |
26 | 51 | def loadStyle(): |
27 | 52 | user_css_path = os.path.join(os.path.expanduser("~"), "fdstyle.css") |
@@ -196,6 +221,8 @@ def __init__(self): |
196 | 221 | self.binary_worker_left = None |
197 | 222 | self.binary_worker_right = None |
198 | 223 | self.init_ui() |
| 224 | + app_context = {"main_window": self} |
| 225 | + self.plugins = load_plugins(app_context) |
199 | 226 |
|
200 | 227 | def init_ui(self): |
201 | 228 | main_widget = QWidget(self) |
|
0 commit comments