2022-01-31 00:09:00 +00:00
|
|
|
# Python imports
|
2022-02-11 06:58:49 +00:00
|
|
|
import os, sys, importlib, traceback
|
2022-01-31 00:09:00 +00:00
|
|
|
from os.path import join, isdir
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk, Gio
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
2022-02-01 07:43:09 +00:00
|
|
|
class Plugin:
|
2022-07-07 04:19:41 +00:00
|
|
|
path: str = None
|
2022-03-25 03:15:08 +00:00
|
|
|
name: str = None
|
2022-06-15 04:03:04 +00:00
|
|
|
author: str = None
|
|
|
|
version: str = None
|
2022-07-07 17:51:51 +00:00
|
|
|
support: str = None
|
2022-07-07 04:19:41 +00:00
|
|
|
permissions:{} = None
|
2022-03-25 03:15:08 +00:00
|
|
|
reference: type = None
|
2022-01-31 00:09:00 +00:00
|
|
|
|
|
|
|
|
2022-07-07 04:19:41 +00:00
|
|
|
|
2022-01-31 00:09:00 +00:00
|
|
|
class Plugins:
|
2022-02-20 07:32:51 +00:00
|
|
|
"""Plugins controller"""
|
|
|
|
|
2022-03-25 03:15:08 +00:00
|
|
|
def __init__(self, settings: type):
|
2022-01-31 00:09:00 +00:00
|
|
|
self._settings = settings
|
2022-02-25 07:29:37 +00:00
|
|
|
self._builder = self._settings.get_builder()
|
2022-01-31 00:09:00 +00:00
|
|
|
self._plugins_path = self._settings.get_plugins_path()
|
2022-07-07 17:51:51 +00:00
|
|
|
self._keybindings = self._settings.get_keybindings()
|
|
|
|
|
2022-01-31 00:09:00 +00:00
|
|
|
self._plugins_dir_watcher = None
|
|
|
|
self._plugin_collection = []
|
|
|
|
|
|
|
|
|
2022-03-25 03:15:08 +00:00
|
|
|
def launch_plugins(self) -> None:
|
2022-01-31 00:09:00 +00:00
|
|
|
self._set_plugins_watcher()
|
|
|
|
self.load_plugins()
|
|
|
|
|
2022-03-25 03:15:08 +00:00
|
|
|
def _set_plugins_watcher(self) -> None:
|
2022-01-31 00:09:00 +00: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)
|
|
|
|
|
2022-03-25 03:15:08 +00:00
|
|
|
def load_plugins(self, file: str = None) -> None:
|
2022-01-31 00:09:00 +00:00
|
|
|
print(f"Loading plugins...")
|
2022-02-11 06:58:49 +00:00
|
|
|
parent_path = os.getcwd()
|
|
|
|
|
2022-01-31 00:09:00 +00:00
|
|
|
for file in os.listdir(self._plugins_path):
|
2022-02-01 07:43:09 +00:00
|
|
|
try:
|
|
|
|
path = join(self._plugins_path, file)
|
|
|
|
if isdir(path):
|
2022-07-07 04:19:41 +00:00
|
|
|
module = self.load_plugin_module(path, file)
|
|
|
|
plugin = self.collect_info(module, path)
|
|
|
|
loading_data = self.parse_permissions(plugin)
|
2022-06-15 04:03:04 +00:00
|
|
|
|
2022-07-07 04:19:41 +00:00
|
|
|
self.execute_plugin(module, plugin, loading_data)
|
2022-02-01 07:43:09 +00:00
|
|
|
except Exception as e:
|
|
|
|
print("Malformed plugin! Not loading!")
|
2022-02-11 06:58:49 +00:00
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
os.chdir(parent_path)
|
2022-01-31 00:09:00 +00:00
|
|
|
|
|
|
|
|
2022-07-07 04:19:41 +00:00
|
|
|
def load_plugin_module(self, path, file):
|
|
|
|
os.chdir(path)
|
|
|
|
sys.path.insert(0, path)
|
|
|
|
spec = importlib.util.spec_from_file_location(file, join(path, "plugin.py"))
|
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(module)
|
|
|
|
|
|
|
|
return module
|
|
|
|
|
|
|
|
def collect_info(self, module, path) -> Plugin:
|
|
|
|
plugin = Plugin()
|
|
|
|
plugin.path = module.Manifest.path
|
|
|
|
plugin.name = module.Manifest.name
|
|
|
|
plugin.author = module.Manifest.author
|
|
|
|
plugin.version = module.Manifest.version
|
|
|
|
plugin.support = module.Manifest.support
|
|
|
|
plugin.permissions = module.Manifest.permissions
|
|
|
|
|
|
|
|
return plugin
|
|
|
|
|
|
|
|
def parse_permissions(self, plugin):
|
|
|
|
loading_data = {}
|
|
|
|
permissions = plugin.permissions
|
|
|
|
keys = permissions.keys()
|
|
|
|
|
|
|
|
if "ui_target" in keys:
|
|
|
|
if permissions["ui_target"] in [
|
|
|
|
"none", "other", "main_Window", "main_menu_bar", "path_menu_bar", "plugin_control_list",
|
|
|
|
"context_menu", "window_1", "window_2", "window_3", "window_4"
|
|
|
|
]:
|
|
|
|
if permissions["ui_target"] == "other":
|
|
|
|
if "ui_target_id" in keys:
|
|
|
|
loading_data["ui_target"] = self._builder.get_object(permissions["ui_target_id"])
|
|
|
|
if loading_data["ui_target"] == None:
|
|
|
|
raise Exception('Invalid "ui_target_id" given in permissions. Must have one if setting "ui_target" to "other"...')
|
|
|
|
else:
|
|
|
|
raise Exception('Invalid "ui_target_id" given in permissions. Must have one if setting "ui_target" to "other"...')
|
|
|
|
else:
|
|
|
|
loading_data["ui_target"] = self._builder.get_object(permissions["ui_target"])
|
|
|
|
else:
|
|
|
|
raise Exception('Unknown "ui_target" given in permissions.')
|
|
|
|
|
|
|
|
|
|
|
|
if "pass_fm_events" in keys:
|
|
|
|
if permissions["pass_fm_events"] in ["true"]:
|
|
|
|
loading_data["pass_fm_events"] = True
|
|
|
|
|
2022-07-07 17:51:51 +00:00
|
|
|
if "bind_keys" in keys:
|
|
|
|
if isinstance(permissions["bind_keys"], list):
|
|
|
|
loading_data["bind_keys"] = permissions["bind_keys"]
|
|
|
|
|
2022-07-07 04:19:41 +00:00
|
|
|
return loading_data
|
|
|
|
|
|
|
|
def execute_plugin(self, module: type, plugin: Plugin, loading_data: []):
|
|
|
|
plugin.reference = module.Plugin()
|
|
|
|
keys = loading_data.keys()
|
|
|
|
|
|
|
|
if "ui_target" in keys:
|
|
|
|
loading_data["ui_target"].add(plugin.reference.get_ui_element())
|
|
|
|
loading_data["ui_target"].show_all()
|
|
|
|
|
|
|
|
if "pass_fm_events" in keys:
|
|
|
|
plugin.reference.set_fm_event_system(event_system)
|
|
|
|
|
2022-07-07 17:51:51 +00:00
|
|
|
if "bind_keys" in keys:
|
|
|
|
self._keybindings.append_bindings(loading_data["bind_keys"])
|
|
|
|
|
2022-07-07 04:19:41 +00:00
|
|
|
plugin.reference.run()
|
|
|
|
self._plugin_collection.append(plugin)
|
|
|
|
|
2022-03-25 03:15:08 +00:00
|
|
|
def reload_plugins(self, file: str = None) -> None:
|
2022-02-26 08:49:08 +00:00
|
|
|
print(f"Reloading plugins... stub.")
|