2022-01-30 18:09:00 -06:00
|
|
|
# Python imports
|
2022-11-28 22:34:13 -06:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import importlib
|
|
|
|
import traceback
|
|
|
|
from os.path import join
|
|
|
|
from os.path import isdir
|
2022-01-30 18:09:00 -06:00
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
2022-11-28 22:34:13 -06:00
|
|
|
from gi.repository import Gtk
|
2023-03-27 20:07:17 -05:00
|
|
|
from gi.repository import GLib
|
2022-11-28 22:34:13 -06:00
|
|
|
from gi.repository import Gio
|
2022-01-30 18:09:00 -06:00
|
|
|
|
|
|
|
# Application imports
|
2022-11-28 22:34:13 -06:00
|
|
|
from .manifest import PluginInfo
|
|
|
|
from .manifest import ManifestProcessor
|
2022-01-30 18:09:00 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-07-06 23:19:41 -05:00
|
|
|
|
2022-09-05 21:21:04 -05:00
|
|
|
class InvalidPluginException(Exception):
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2022-09-24 15:40:21 -05:00
|
|
|
class PluginsController:
|
|
|
|
"""PluginsController controller"""
|
2022-02-20 01:32:51 -06:00
|
|
|
|
2022-10-09 20:59:44 -05:00
|
|
|
def __init__(self):
|
2022-09-05 21:21:04 -05:00
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
sys.path.insert(0, path) # NOTE: I think I'm not using this correctly...
|
|
|
|
|
2023-07-29 23:42:59 -05:00
|
|
|
self._builder = settings_manager.get_builder()
|
|
|
|
self._plugins_path = settings_manager.get_plugins_path()
|
2022-07-07 12:51:51 -05:00
|
|
|
|
2022-01-30 18:09:00 -06:00
|
|
|
self._plugins_dir_watcher = None
|
|
|
|
self._plugin_collection = []
|
|
|
|
|
|
|
|
|
2022-03-24 22:15:08 -05:00
|
|
|
def launch_plugins(self) -> None:
|
2022-01-30 18:09:00 -06:00
|
|
|
self._set_plugins_watcher()
|
|
|
|
self.load_plugins()
|
|
|
|
|
2022-03-24 22:15:08 -05:00
|
|
|
def _set_plugins_watcher(self) -> None:
|
2022-01-30 18:09:00 -06:00
|
|
|
self._plugins_dir_watcher = Gio.File.new_for_path(self._plugins_path) \
|
|
|
|
.monitor_directory(Gio.FileMonitorFlags.WATCH_MOVES, Gio.Cancellable())
|
|
|
|
self._plugins_dir_watcher.connect("changed", self._on_plugins_changed, ())
|
|
|
|
|
|
|
|
def _on_plugins_changed(self, file_monitor, file, other_file=None, eve_type=None, data=None):
|
|
|
|
if eve_type in [Gio.FileMonitorEvent.CREATED, Gio.FileMonitorEvent.DELETED,
|
|
|
|
Gio.FileMonitorEvent.RENAMED, Gio.FileMonitorEvent.MOVED_IN,
|
|
|
|
Gio.FileMonitorEvent.MOVED_OUT]:
|
|
|
|
self.reload_plugins(file)
|
|
|
|
|
2023-03-27 20:07:17 -05:00
|
|
|
@daemon_threaded
|
2022-03-24 22:15:08 -05:00
|
|
|
def load_plugins(self, file: str = None) -> None:
|
2023-02-23 23:03:29 -06:00
|
|
|
logger.info(f"Loading plugins...")
|
2022-02-11 00:58:49 -06:00
|
|
|
parent_path = os.getcwd()
|
|
|
|
|
2022-07-16 02:33:25 -05:00
|
|
|
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)]:
|
2022-02-01 01:43:09 -06:00
|
|
|
try:
|
2022-08-12 22:54:16 -05:00
|
|
|
target = join(path, "plugin.py")
|
|
|
|
manifest = ManifestProcessor(path, self._builder)
|
|
|
|
|
2022-07-16 02:33:25 -05:00
|
|
|
if not os.path.exists(target):
|
2023-02-23 23:03:29 -06:00
|
|
|
raise FileNotFoundError("Invalid Plugin Structure: Plugin doesn't have 'plugin.py'. Aboarting load...")
|
2022-06-14 23:03:04 -05:00
|
|
|
|
2022-08-12 22:54:16 -05:00
|
|
|
plugin, loading_data = manifest.get_loading_data()
|
|
|
|
module = self.load_plugin_module(path, folder, target)
|
2023-03-27 20:07:17 -05:00
|
|
|
|
|
|
|
GLib.idle_add(self.execute_plugin, *(module, plugin, loading_data))
|
|
|
|
# self.execute_plugin(module, plugin, loading_data)
|
2023-02-23 23:03:29 -06:00
|
|
|
except InvalidPluginException as e:
|
|
|
|
logger.info(f"Malformed Plugin: Not loading -->: '{folder}' !")
|
|
|
|
logger.debug("Trace: ", traceback.print_exc())
|
2022-02-11 00:58:49 -06:00
|
|
|
|
|
|
|
os.chdir(parent_path)
|
2022-01-30 18:09:00 -06:00
|
|
|
|
|
|
|
|
2022-07-16 02:33:25 -05:00
|
|
|
def load_plugin_module(self, path, folder, target):
|
2022-07-06 23:19:41 -05:00
|
|
|
os.chdir(path)
|
2022-10-01 19:08:05 -05:00
|
|
|
|
|
|
|
locations = []
|
|
|
|
self.collect_search_locations(path, locations)
|
|
|
|
|
|
|
|
spec = importlib.util.spec_from_file_location(folder, target, submodule_search_locations = locations)
|
2022-07-06 23:19:41 -05:00
|
|
|
module = importlib.util.module_from_spec(spec)
|
2022-09-05 21:21:04 -05:00
|
|
|
sys.modules[folder] = module
|
2022-07-06 23:19:41 -05:00
|
|
|
spec.loader.exec_module(module)
|
|
|
|
|
|
|
|
return module
|
|
|
|
|
2022-10-01 19:08:05 -05:00
|
|
|
def collect_search_locations(self, path, locations):
|
|
|
|
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-07-06 23:19:41 -05:00
|
|
|
|
2022-09-05 21:21:04 -05:00
|
|
|
def execute_plugin(self, module: type, plugin: PluginInfo, loading_data: []):
|
2022-07-06 23:19:41 -05:00
|
|
|
plugin.reference = module.Plugin()
|
|
|
|
|
2024-06-12 00:32:14 -05:00
|
|
|
if "ui_target" in loading_data:
|
2022-10-09 20:59:44 -05:00
|
|
|
loading_data["ui_target"].add( plugin.reference.generate_reference_ui_element() )
|
2022-07-06 23:19:41 -05:00
|
|
|
loading_data["ui_target"].show_all()
|
|
|
|
|
2024-06-12 00:32:14 -05:00
|
|
|
if "pass_ui_objects" in loading_data:
|
2022-09-03 00:43:25 -05:00
|
|
|
plugin.reference.set_ui_object_collection( loading_data["pass_ui_objects"] )
|
|
|
|
|
2024-06-12 00:32:14 -05:00
|
|
|
if "pass_fm_events" in loading_data:
|
2022-07-06 23:19:41 -05:00
|
|
|
plugin.reference.set_fm_event_system(event_system)
|
2022-09-29 17:22:33 -05:00
|
|
|
plugin.reference.subscribe_to_events()
|
2022-07-06 23:19:41 -05:00
|
|
|
|
2024-06-12 00:32:14 -05:00
|
|
|
if "bind_keys" in loading_data:
|
2023-02-23 23:03:29 -06:00
|
|
|
keybindings.append_bindings( loading_data["bind_keys"] )
|
2022-07-07 12:51:51 -05:00
|
|
|
|
2022-07-06 23:19:41 -05:00
|
|
|
plugin.reference.run()
|
|
|
|
self._plugin_collection.append(plugin)
|
|
|
|
|
2023-12-31 22:35:33 -06:00
|
|
|
return False
|
|
|
|
|
2022-03-24 22:15:08 -05:00
|
|
|
def reload_plugins(self, file: str = None) -> None:
|
2023-02-23 23:03:29 -06:00
|
|
|
logger.info(f"Reloading plugins...")
|
2022-12-08 23:33:04 -06:00
|
|
|
parent_path = os.getcwd()
|
|
|
|
|
|
|
|
for plugin in self._plugin_collection:
|
|
|
|
os.chdir(plugin.path)
|
|
|
|
plugin.reference.reload_package(f"{plugin.path}/plugin.py")
|
|
|
|
|
2023-12-31 22:35:33 -06:00
|
|
|
os.chdir(parent_path)
|