Pligins refactor with new context and controller integration
This commit is contained in:
@@ -11,47 +11,56 @@ import gi
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
from .dto.manifest_meta import ManifestMeta
|
||||
from .plugin_reload_mixin import PluginReloadMixin
|
||||
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
|
||||
|
||||
|
||||
|
||||
class InvalidPluginException(Exception):
|
||||
class PluginsControllerException(Exception):
|
||||
...
|
||||
|
||||
|
||||
|
||||
class PluginsController(PluginReloadMixin):
|
||||
"""PluginsController controller"""
|
||||
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._plugin_collection = []
|
||||
self._plugin_collection: list = []
|
||||
|
||||
self._plugins_path = settings_manager.path_manager.get_plugins_path()
|
||||
self._manifest_manager = ManifestManager()
|
||||
self._plugins_path: str = settings_manager.path_manager.get_plugins_path()
|
||||
self._manifest_manager: ManifestManager = ManifestManager()
|
||||
|
||||
self._set_plugins_watcher()
|
||||
|
||||
|
||||
def pre_launch_plugins(self) -> None:
|
||||
logger.info(f"Loading pre-launch plugins...")
|
||||
manifest_metas: [] = self._manifest_manager.get_pre_launch_manifests()
|
||||
self._load_plugins(manifest_metas, is_pre_launch = True)
|
||||
def _controller_message(self, event: BaseEvent):
|
||||
...
|
||||
|
||||
def post_launch_plugins(self) -> None:
|
||||
logger.info(f"Loading post-launch plugins...")
|
||||
manifest_metas: [] = self._manifest_manager.get_post_launch_plugins()
|
||||
self._load_plugins(manifest_metas)
|
||||
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 os.path.isdir(_path):
|
||||
self.collect_search_locations(_path, locations)
|
||||
|
||||
def _load_plugins(
|
||||
self,
|
||||
manifest_metas: [] = [],
|
||||
is_pre_launch: bool = False
|
||||
) -> None:
|
||||
manifest_metas: list = [],
|
||||
is_pre_launch: bool = False
|
||||
):
|
||||
parent_path = os.getcwd()
|
||||
|
||||
for manifest_meta in manifest_metas:
|
||||
@@ -62,7 +71,7 @@ class PluginsController(PluginReloadMixin):
|
||||
if not os.path.exists(target):
|
||||
raise InvalidPluginException("Invalid Plugin Structure: Plugin doesn't have 'plugin.py'. Aboarting load...")
|
||||
|
||||
module = self.load_plugin_module(path, folder, target)
|
||||
module = self._load_plugin_module(path, folder, target)
|
||||
|
||||
if is_pre_launch:
|
||||
self.execute_plugin(module, manifest_meta)
|
||||
@@ -70,15 +79,15 @@ class PluginsController(PluginReloadMixin):
|
||||
GLib.idle_add(self.execute_plugin, module, manifest_meta)
|
||||
except Exception as e:
|
||||
logger.info(f"Malformed Plugin: Not loading -->: '{folder}' !")
|
||||
logger.debug("Trace: ", traceback.print_exc())
|
||||
logger.debug(f"Trace: {traceback.print_exc()}")
|
||||
|
||||
os.chdir(parent_path)
|
||||
|
||||
def load_plugin_module(self, path, folder, target):
|
||||
def _load_plugin_module(self, path, folder, target):
|
||||
os.chdir(path)
|
||||
|
||||
locations = []
|
||||
self.collect_search_locations(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)
|
||||
@@ -87,48 +96,44 @@ class PluginsController(PluginReloadMixin):
|
||||
|
||||
return module
|
||||
|
||||
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 os.path.isdir(_path):
|
||||
self.collect_search_locations(_path, locations)
|
||||
def create_plugin_context(self):
|
||||
plugin_context: PluginContext = PluginContext()
|
||||
|
||||
plugin_context.requests_ui_element: callable = self.requests_ui_element
|
||||
plugin_context.message: callable = self.message
|
||||
plugin_context.message_to: callable = self.message_to
|
||||
plugin_context.message_to_selected: callable = self.message_to_selected
|
||||
plugin_context.emit: callable = event_system.emit
|
||||
plugin_context.emit_and_await: callable = event_system.emit_and_await
|
||||
|
||||
return plugin_context
|
||||
|
||||
def pre_launch_plugins(self) -> None:
|
||||
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)
|
||||
|
||||
def post_launch_plugins(self) -> None:
|
||||
logger.info(f"Loading post-launch plugins...")
|
||||
manifest_metas: list = self._manifest_manager.get_post_launch_plugins()
|
||||
self._load_plugins(manifest_metas)
|
||||
|
||||
def execute_plugin(self, module: type, manifest_meta: ManifestMeta):
|
||||
plugin = module.Plugin()
|
||||
plugin.plugin_context: PluginContext = self.create_plugin_context()
|
||||
plugin._controller_message: callable = self._controller_message
|
||||
|
||||
manifest = manifest_meta.manifest
|
||||
manifest_meta.instance = module.Plugin()
|
||||
|
||||
if manifest.requests.ui_target:
|
||||
builder = settings_manager.get_builder()
|
||||
ui_target = manifest.requests.ui_target
|
||||
ui_target_id = manifest.requests.ui_target_id
|
||||
|
||||
if not ui_target == "other":
|
||||
ui_target = builder.get_object(ui_target)
|
||||
else:
|
||||
if not ui_target_id:
|
||||
raise InvalidPluginException('Invalid "ui_target_id" given in requests. Must have one if setting "ui_target" to "other"...')
|
||||
|
||||
ui_target = builder.get_object(ui_target_id)
|
||||
|
||||
if not ui_target:
|
||||
raise InvalidPluginException('Unknown "ui_target" given in requests.')
|
||||
|
||||
ui_element = manifest_meta.instance.generate_reference_ui_element()
|
||||
|
||||
ui_target.add(ui_element)
|
||||
|
||||
if manifest.requests.pass_ui_objects:
|
||||
manifest_meta.instance.set_ui_object_collection(
|
||||
[ builder.get_object(obj) for obj in manifest.requests.pass_ui_objects ]
|
||||
)
|
||||
|
||||
if manifest.requests.pass_events:
|
||||
manifest_meta.instance.set_event_system(event_system)
|
||||
manifest_meta.instance.subscribe_to_events()
|
||||
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)
|
||||
|
||||
|
||||
|
||||
plugins_controller = PluginsController()
|
||||
|
||||
Reference in New Issue
Block a user