From 43d0881a3633350f5e28db7671d48b538bcf6232 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Tue, 10 Oct 2023 22:33:28 -0500 Subject: [PATCH] Plugin inferastructure work/fixes --- plugins/template/manifest.json | 2 +- src/plugins/manifest.py | 4 +++ src/plugins/plugin_base.py | 47 +++++++++++++++++++++++++------ src/plugins/plugins_controller.py | 11 ++++---- src/utils/keybindings.py | 11 ++++++++ 5 files changed, 61 insertions(+), 14 deletions(-) diff --git a/plugins/template/manifest.json b/plugins/template/manifest.json index 4dcbf47..1f8c8a5 100644 --- a/plugins/template/manifest.json +++ b/plugins/template/manifest.json @@ -6,7 +6,7 @@ "support": "", "requests": { "ui_target": "plugin_control_list", - "pass_fm_events": "true", + "pass_events": "true", "bind_keys": ["Example Plugin||send_message:f"] } } diff --git a/src/plugins/manifest.py b/src/plugins/manifest.py index 4088eed..1b93f34 100644 --- a/src/plugins/manifest.py +++ b/src/plugins/manifest.py @@ -61,4 +61,8 @@ class ManifestProcessor: if isinstance(requests["bind_keys"], list): loading_data["bind_keys"] = requests["bind_keys"] + if "pass_ui_objects" in keys: + if isinstance(requests["pass_ui_objects"], list): + loading_data["pass_ui_objects"] = [ self._builder.get_object(obj) for obj in requests["pass_ui_objects"] ] + return self._plugin, loading_data diff --git a/src/plugins/plugin_base.py b/src/plugins/plugin_base.py index 3130bb4..3650495 100644 --- a/src/plugins/plugin_base.py +++ b/src/plugins/plugin_base.py @@ -1,6 +1,7 @@ # Python imports import os import time +import inspect # Lib imports @@ -12,7 +13,8 @@ class PluginBaseException(Exception): class PluginBase: - def __init__(self): + def __init__(self, **kwargs): + super().__init__(**kwargs) self.name = "Example Plugin" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus # where self.name should not be needed for message comms @@ -36,13 +38,6 @@ class PluginBase: """ raise PluginBaseException("Method hasn't been overriden...") - def set_event_system(self, event_system): - """ - Requests Key: 'pass_events': "true" - Must define in plugin if "pass_events" is set to "true" string. - """ - self._event_system = event_system - def set_ui_object_collection(self, ui_objects): """ Requests Key: "pass_ui_objects": [""] @@ -51,9 +46,45 @@ class PluginBase: """ self._ui_objects = ui_objects + def set_event_system(self, event_system): + """ + Requests Key: 'pass_events': "true" + Must define in plugin if "pass_events" is set to "true" string. + """ + self._event_system = event_system + def subscribe_to_events(self): ... + def _connect_builder_signals(self, caller_class, builder): + classes = [caller_class] + handlers = {} + for c in classes: + methods = None + try: + methods = inspect.getmembers(c, predicate=inspect.ismethod) + handlers.update(methods) + except Exception as e: + logger.debug(repr(e)) + + builder.connect_signals(handlers) + + def reload_package(self, plugin_path, module_dict_main=locals()): + import importlib + from pathlib import Path + + def reload_package_recursive(current_dir, module_dict): + for path in current_dir.iterdir(): + if "__init__" in str(path) or path.stem not in module_dict: + continue + + if path.is_file() and path.suffix == ".py": + importlib.reload(module_dict[path.stem]) + elif path.is_dir(): + reload_package_recursive(path, module_dict[path.stem].__dict__) + + reload_package_recursive(Path(plugin_path).parent, module_dict_main["module_dict_main"]) + def clear_children(self, widget: type) -> None: """ Clear children of a gtk widget. """ diff --git a/src/plugins/plugins_controller.py b/src/plugins/plugins_controller.py index 564d49f..a77186c 100644 --- a/src/plugins/plugins_controller.py +++ b/src/plugins/plugins_controller.py @@ -53,7 +53,7 @@ class PluginsController: self.reload_plugins(file) def load_plugins(self, file: str = None) -> None: - logger.debug(f"Loading plugins...") + logger.info(f"Loading plugins...") parent_path = os.getcwd() for path, folder in [[join(self._plugins_path, item), item] if os.path.isdir(join(self._plugins_path, item)) else None for item in os.listdir(self._plugins_path)]: @@ -68,7 +68,8 @@ class PluginsController: module = self.load_plugin_module(path, folder, target) self.execute_plugin(module, plugin, loading_data) except Exception as e: - logger.debug(f"Malformed Plugin: Not loading -->: '{folder}' !\n{traceback.print_exc()}") + logger.info(f"Malformed Plugin: Not loading -->: '{folder}' !") + logger.debug("Trace: ", traceback.print_exc()) os.chdir(parent_path) @@ -99,13 +100,13 @@ class PluginsController: if "ui_target" in keys: loading_data["ui_target"].add( plugin.reference.generate_reference_ui_element() ) - loading_data["ui_target"].show_all() + loading_data["ui_target"].show() if "pass_ui_objects" in keys: plugin.reference.set_ui_object_collection( loading_data["pass_ui_objects"] ) if "pass_events" in keys: - plugin.reference.set_fm_event_system(event_system) + plugin.reference.set_event_system(event_system) plugin.reference.subscribe_to_events() if "bind_keys" in keys: @@ -115,4 +116,4 @@ class PluginsController: self._plugin_collection.append(plugin) def reload_plugins(self, file: str = None) -> None: - logger.debug(f"Reloading plugins... stub.") + logger.info(f"Reloading plugins... stub.") diff --git a/src/utils/keybindings.py b/src/utils/keybindings.py index 50e7b71..0437b72 100644 --- a/src/utils/keybindings.py +++ b/src/utils/keybindings.py @@ -42,6 +42,17 @@ class Keybindings(Singleton): self.keymap = Gdk.Keymap.get_default() self.configure({}) + def print_keys(self): + print(self.keys) + + def append_bindings(self, combos): + """ Accept new binding(s) and reload """ + for item in combos: + method, keys = item.split(":") + self.keys[method] = keys + + self.reload() + def configure(self, bindings): """ Accept new bindings and reconfigure with them """ self.keys = bindings