Moved mirage2 to use newer GTK template structure patterns
This commit is contained in:
182
src/plugins/controller.py
Normal file
182
src/plugins/controller.py
Normal file
@@ -0,0 +1,182 @@
|
||||
# Python imports
|
||||
import os
|
||||
import sys
|
||||
import importlib
|
||||
import traceback
|
||||
|
||||
from os.path import join
|
||||
from os.path import isdir
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
from libs.event_factory import Event_Factory, App_Event_Types
|
||||
from libs.controllers.controller_base import ControllerBase
|
||||
from libs.dto.plugins.manifest_meta import ManifestMeta
|
||||
from libs.dto.base_event import BaseEvent
|
||||
|
||||
from .manifest_manager import ManifestManager
|
||||
from .plugins_controller_mixin import PluginsControllerMixin
|
||||
from .plugin_reload_mixin import PluginReloadMixin
|
||||
from .plugin_context import PluginContext
|
||||
from .plugins_ui import PluginsUI
|
||||
|
||||
|
||||
|
||||
class PluginsControllerException(Exception):
|
||||
...
|
||||
|
||||
|
||||
|
||||
class PluginsController(ControllerBase, PluginsControllerMixin, PluginReloadMixin):
|
||||
""" PluginsController controller """
|
||||
|
||||
def __init__(self):
|
||||
super(PluginsController, self).__init__()
|
||||
|
||||
# path = os.path.dirname(os.path.realpath(__file__))
|
||||
# sys.path.insert(0, path) # NOTE: I think I'm not using this correctly...
|
||||
|
||||
self.plugins_ui: PluginsUI = PluginsUI()
|
||||
self._manifest_manager: ManifestManager = ManifestManager()
|
||||
|
||||
self._plugin_collection: list = []
|
||||
self._plugins_path: str = settings_manager.path_manager.get_plugins_path()
|
||||
|
||||
self._set_plugins_watcher()
|
||||
|
||||
|
||||
def _controller_message(self, event: BaseEvent):
|
||||
for manifest_meta in self._plugin_collection:
|
||||
manifest_meta.instance._controller_message(event)
|
||||
|
||||
if isinstance(event, App_Event_Types.TogglePluginsUiEvent):
|
||||
self.toggle_plugins_ui()
|
||||
|
||||
def _collect_search_locations(self, path: str, locations: list):
|
||||
locations.append(path)
|
||||
for file in os.listdir(path):
|
||||
_path = os.path.join(path, file)
|
||||
if not os.path.isdir(_path): continue
|
||||
self._collect_search_locations(_path, locations)
|
||||
|
||||
def _load_plugins(
|
||||
self,
|
||||
manifest_metas: list = [],
|
||||
is_pre_launch: bool = False
|
||||
):
|
||||
parent_path = os.getcwd()
|
||||
|
||||
for manifest_meta in manifest_metas:
|
||||
try:
|
||||
path, \
|
||||
folder, \
|
||||
manifest = manifest_meta.path, manifest_meta.folder, manifest_meta.manifest
|
||||
target = join(path, "plugin.py")
|
||||
|
||||
if not os.path.exists(target):
|
||||
raise PluginsControllerException(
|
||||
"Invalid Plugin Structure: Plugin doesn't have 'plugin.py'. Aboarting load..."
|
||||
)
|
||||
|
||||
module = self._load_plugin_module(path, folder, target)
|
||||
|
||||
self._handle_plugin_execute(is_pre_launch, module, manifest_meta)
|
||||
except PluginsControllerException as e:
|
||||
logger.info(f"Malformed Plugin: Not loading -->: '{manifest_meta.folder}' !")
|
||||
logger.debug(f"Trace: {traceback.print_exc()}")
|
||||
|
||||
os.chdir(parent_path)
|
||||
|
||||
def _load_plugin_module(self, path, folder, target):
|
||||
os.chdir(path)
|
||||
|
||||
locations = []
|
||||
self._collect_search_locations(path, locations)
|
||||
|
||||
spec = importlib.util.spec_from_file_location(folder, target, submodule_search_locations = locations)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[folder] = module
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
return module
|
||||
|
||||
def _handle_plugin_execute(
|
||||
self, is_pre_launch: bool, module, manifest_meta
|
||||
):
|
||||
if not is_pre_launch:
|
||||
GLib.idle_add(
|
||||
self.execute_plugin, module, manifest_meta
|
||||
)
|
||||
return
|
||||
|
||||
self.execute_plugin(module, manifest_meta)
|
||||
|
||||
def pre_launch_plugins(self):
|
||||
logger.info(f"Loading pre-launch plugins...")
|
||||
manifest_metas: list = self._manifest_manager.get_pre_launch_plugins()
|
||||
self._load_plugins(manifest_metas, is_pre_launch = True)
|
||||
|
||||
for manifest_meta in manifest_metas:
|
||||
self.plugins_ui.add_row(manifest_meta, self.toggle_plugin_load_state)
|
||||
|
||||
def post_launch_plugins(self):
|
||||
logger.info(f"Loading post-launch plugins...")
|
||||
manifest_metas: list = self._manifest_manager.get_post_launch_plugins()
|
||||
self._load_plugins(manifest_metas)
|
||||
|
||||
for manifest_meta in manifest_metas:
|
||||
self.plugins_ui.add_row(manifest_meta, self.toggle_plugin_load_state)
|
||||
|
||||
def manual_launch_plugins(self):
|
||||
logger.info(f"Collecting manual-launch plugins...")
|
||||
manifest_metas: list = self._manifest_manager.get_manual_launch_plugins()
|
||||
|
||||
for manifest_meta in manifest_metas:
|
||||
self.plugins_ui.add_row(manifest_meta, self.toggle_plugin_load_state)
|
||||
|
||||
def toggle_plugin_load_state(self, widget, manifest_meta):
|
||||
if manifest_meta.instance:
|
||||
self._plugin_collection.remove(manifest_meta)
|
||||
manifest_meta.instance.unload()
|
||||
manifest_meta.instance = None
|
||||
widget.set_label("Load")
|
||||
return
|
||||
|
||||
self._load_plugins( [manifest_meta] )
|
||||
widget.set_label("Unload")
|
||||
|
||||
def execute_plugin(self, module: type, manifest_meta: ManifestMeta):
|
||||
plugin = module.Plugin()
|
||||
plugin.plugin_context: PluginContext = self.create_plugin_context()
|
||||
|
||||
manifest = manifest_meta.manifest
|
||||
manifest_meta.instance = plugin
|
||||
|
||||
if manifest.requests.bind_keys:
|
||||
keybindings.append_bindings( manifest.requests.bind_keys )
|
||||
|
||||
manifest_meta.instance.load()
|
||||
manifest_meta.instance.run()
|
||||
|
||||
self._plugin_collection.append(manifest_meta)
|
||||
|
||||
def create_plugin_context(self):
|
||||
plugin_context: PluginContext = PluginContext()
|
||||
|
||||
plugin_context.request_ui_element: callable = self.request_ui_element
|
||||
plugin_context.emit: callable = self.emit
|
||||
plugin_context.emit_to: callable = self.emit_to
|
||||
plugin_context.emit_to_selected: callable = self.emit_to_selected
|
||||
plugin_context.register_controller: callable = self.register_controller
|
||||
plugin_context.unregister_controller: callable = self.unregister_controller
|
||||
|
||||
return plugin_context
|
||||
|
||||
def toggle_plugins_ui(self, widget = None):
|
||||
self.plugins_ui.hide() if self.plugins_ui.is_visible() else self.plugins_ui.show()
|
||||
|
||||
plugins_controller = PluginsController()
|
||||
Reference in New Issue
Block a user