2022-02-25 17:53:58 -06:00
|
|
|
# Python imports
|
2022-12-11 14:52:09 -06:00
|
|
|
import os
|
|
|
|
|
import sys
|
2023-02-23 18:48:37 -06:00
|
|
|
import importlib
|
2022-12-11 14:52:09 -06:00
|
|
|
import traceback
|
|
|
|
|
from os.path import join
|
|
|
|
|
from os.path import isdir
|
2022-02-25 17:53:58 -06:00
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
import gi
|
2024-06-29 21:24:57 -05:00
|
|
|
from gi.repository import GLib
|
2022-02-25 17:53:58 -06:00
|
|
|
|
|
|
|
|
# Application imports
|
2026-01-03 21:57:37 -06:00
|
|
|
from .dto.manifest_meta import ManifestMeta
|
|
|
|
|
from .plugin_reload_mixin import PluginReloadMixin
|
|
|
|
|
from .manifest_manager import ManifestManager
|
2022-02-25 17:53:58 -06:00
|
|
|
|
2022-09-05 18:01:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidPluginException(Exception):
|
|
|
|
|
...
|
2022-02-25 17:53:58 -06:00
|
|
|
|
|
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
|
|
|
|
|
class PluginsController(PluginReloadMixin):
|
2022-10-13 20:58:44 -05:00
|
|
|
"""PluginsController controller"""
|
2022-02-25 17:53:58 -06:00
|
|
|
|
2022-10-13 20:58:44 -05:00
|
|
|
def __init__(self):
|
2026-01-03 21:57:37 -06:00
|
|
|
# path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
# sys.path.insert(0, path) # NOTE: I think I'm not using this correctly...
|
2022-09-05 20:15:32 -05:00
|
|
|
|
2022-02-25 17:53:58 -06:00
|
|
|
self._plugin_collection = []
|
2024-06-29 21:24:57 -05:00
|
|
|
|
2026-01-03 23:28:14 -06:00
|
|
|
self._plugins_path = settings_manager.path_manager.get_plugins_path()
|
2026-01-03 21:57:37 -06:00
|
|
|
self._manifest_manager = ManifestManager()
|
2022-02-25 17:53:58 -06:00
|
|
|
|
|
|
|
|
self._set_plugins_watcher()
|
|
|
|
|
|
|
|
|
|
|
2024-06-29 21:24:57 -05:00
|
|
|
def pre_launch_plugins(self) -> None:
|
|
|
|
|
logger.info(f"Loading pre-launch plugins...")
|
2026-01-03 21:57:37 -06:00
|
|
|
manifest_metas: [] = self._manifest_manager.get_pre_launch_manifests()
|
|
|
|
|
self._load_plugins(manifest_metas, is_pre_launch = True)
|
2024-06-29 21:24:57 -05:00
|
|
|
|
|
|
|
|
def post_launch_plugins(self) -> None:
|
|
|
|
|
logger.info(f"Loading post-launch plugins...")
|
2026-01-03 21:57:37 -06:00
|
|
|
manifest_metas: [] = self._manifest_manager.get_post_launch_plugins()
|
|
|
|
|
self._load_plugins(manifest_metas)
|
|
|
|
|
|
|
|
|
|
def _load_plugins(
|
|
|
|
|
self,
|
|
|
|
|
manifest_metas: [] = [],
|
|
|
|
|
is_pre_launch: bool = False
|
|
|
|
|
) -> None:
|
2022-02-25 17:53:58 -06:00
|
|
|
parent_path = os.getcwd()
|
|
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
for manifest_meta in manifest_metas:
|
|
|
|
|
path, folder, manifest = manifest_meta.path, manifest_meta.folder, manifest_meta.manifest
|
2022-02-25 17:53:58 -06:00
|
|
|
|
2024-06-29 21:24:57 -05:00
|
|
|
try:
|
|
|
|
|
target = join(path, "plugin.py")
|
2022-09-05 18:01:39 -05:00
|
|
|
if not os.path.exists(target):
|
|
|
|
|
raise InvalidPluginException("Invalid Plugin Structure: Plugin doesn't have 'plugin.py'. Aboarting load...")
|
2022-02-25 17:53:58 -06:00
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
module = self.load_plugin_module(path, folder, target)
|
2024-06-29 21:24:57 -05:00
|
|
|
|
|
|
|
|
if is_pre_launch:
|
2026-01-03 21:57:37 -06:00
|
|
|
self.execute_plugin(module, manifest_meta)
|
2024-06-29 21:24:57 -05:00
|
|
|
else:
|
2026-01-03 21:57:37 -06:00
|
|
|
GLib.idle_add(self.execute_plugin, module, manifest_meta)
|
2022-02-25 17:53:58 -06:00
|
|
|
except Exception as e:
|
2023-10-10 22:33:28 -05:00
|
|
|
logger.info(f"Malformed Plugin: Not loading -->: '{folder}' !")
|
|
|
|
|
logger.debug("Trace: ", traceback.print_exc())
|
2022-02-25 17:53:58 -06:00
|
|
|
|
|
|
|
|
os.chdir(parent_path)
|
|
|
|
|
|
2022-09-05 18:01:39 -05:00
|
|
|
def load_plugin_module(self, path, folder, target):
|
|
|
|
|
os.chdir(path)
|
2022-10-13 20:58:44 -05:00
|
|
|
|
|
|
|
|
locations = []
|
|
|
|
|
self.collect_search_locations(path, locations)
|
|
|
|
|
|
|
|
|
|
spec = importlib.util.spec_from_file_location(folder, target, submodule_search_locations = locations)
|
2022-09-05 18:01:39 -05:00
|
|
|
module = importlib.util.module_from_spec(spec)
|
2022-09-05 20:15:32 -05:00
|
|
|
sys.modules[folder] = module
|
2022-09-05 18:01:39 -05:00
|
|
|
spec.loader.exec_module(module)
|
|
|
|
|
|
|
|
|
|
return module
|
|
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
def collect_search_locations(self, path: str, locations: list):
|
2022-10-13 20:58:44 -05:00
|
|
|
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)
|
2022-09-05 18:01:39 -05:00
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
def execute_plugin(self, module: type, manifest_meta: ManifestMeta):
|
|
|
|
|
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.')
|
2022-09-05 18:01:39 -05:00
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
ui_element = manifest_meta.instance.generate_reference_ui_element()
|
2022-10-13 20:58:44 -05:00
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
ui_target.add(ui_element)
|
2022-10-13 20:58:44 -05:00
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
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 ]
|
|
|
|
|
)
|
2022-09-05 18:01:39 -05:00
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
if manifest.requests.pass_events:
|
|
|
|
|
manifest_meta.instance.set_event_system(event_system)
|
|
|
|
|
manifest_meta.instance.subscribe_to_events()
|
2022-09-05 18:01:39 -05:00
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
if manifest.requests.bind_keys:
|
|
|
|
|
keybindings.append_bindings( manifest.requests.bind_keys )
|
2022-09-05 18:01:39 -05:00
|
|
|
|
2026-01-03 21:57:37 -06:00
|
|
|
manifest_meta.instance.run()
|
|
|
|
|
self._plugin_collection.append(manifest_meta)
|