Plugin rework/fixes

This commit is contained in:
2022-09-05 21:21:04 -05:00
parent 43fe513bb1
commit 51a565a79d
13 changed files with 151 additions and 311 deletions

View File

@@ -38,7 +38,7 @@ class Application(IPCServer):
message = f"FILE|{args.new_tab}"
self.send_ipc_message(message)
raise App_Launch_Exception("IPC Server Exists: Will send path(s) to it and close...\nNote: If no fm exists, remove /tmp/solarfm-ipc.sock")
raise App_Launch_Exception(f"IPC Server Exists: Will send path(s) to it and close...\nNote: If no fm exists, remove /tmp/{app_name}-ipc.sock")
settings = Settings()

View File

@@ -9,7 +9,11 @@ from os.path import join
class Plugin:
class ManifestProcessorException(Exception):
...
class PluginInfo:
path: str = None
name: str = None
author: str = None
@@ -23,7 +27,7 @@ class ManifestProcessor:
def __init__(self, path, builder):
manifest = join(path, "manifest.json")
if not os.path.exists(manifest):
raise Exception("Invalid Plugin Structure: Plugin doesn't have 'manifest.json'. Aboarting load...")
raise ManifestProcessorException("Invalid Plugin Structure: Plugin doesn't have 'manifest.json'. Aboarting load...")
self._path = path
self._builder = builder
@@ -32,8 +36,8 @@ class ManifestProcessor:
self._manifest = data["manifest"]
self._plugin = self.collect_info()
def collect_info(self) -> Plugin:
plugin = Plugin()
def collect_info(self) -> PluginInfo:
plugin = PluginInfo()
plugin.path = self._path
plugin.name = self._manifest["name"]
plugin.author = self._manifest["author"]
@@ -58,13 +62,13 @@ class ManifestProcessor:
if "ui_target_id" in keys:
loading_data["ui_target"] = self._builder.get_object(requests["ui_target_id"])
if loading_data["ui_target"] == None:
raise Exception('Invalid "ui_target_id" given in requests. Must have one if setting "ui_target" to "other"...')
raise ManifestProcessorException('Invalid "ui_target_id" given in requests. Must have one if setting "ui_target" to "other"...')
else:
raise Exception('Invalid "ui_target_id" given in requests. Must have one if setting "ui_target" to "other"...')
raise ManifestProcessorException('Invalid "ui_target_id" given in requests. Must have one if setting "ui_target" to "other"...')
else:
loading_data["ui_target"] = self._builder.get_object(requests["ui_target"])
else:
raise Exception('Unknown "ui_target" given in requests.')
raise ManifestProcessorException('Unknown "ui_target" given in requests.')
if "pass_fm_events" in keys:
if requests["pass_fm_events"] in ["true"]:

View File

@@ -0,0 +1,55 @@
# Python imports
import os, time
# Lib imports
# Application imports
class PluginBase:
def __init__(self):
self.name = "Example Plugin" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus
# where self.name should not be needed for message comms
self._builder = None
self._ui_objects = None
self._event_system = None
self._event_sleep_time = .5
self._event_message = None
def set_fm_event_system(self, fm_event_system):
self._event_system = fm_event_system
def set_ui_object_collection(self, ui_objects):
self._ui_objects = ui_objects
def wait_for_fm_message(self):
while not self._event_message:
pass
def clear_children(self, widget: type) -> None:
''' Clear children of a gtk widget. '''
for child in widget.get_children():
widget.remove(child)
@daemon_threaded
def _module_event_observer(self):
while True:
time.sleep(self._event_sleep_time)
event = self._event_system.read_module_event()
if event:
try:
if event[0] == self.name:
target_id, method_target, data = self._event_system.consume_module_event()
if not method_target:
self._event_message = data
else:
method = getattr(self.__class__, f"{method_target}")
if data:
data = method(*(self, *data))
else:
method(*(self,))
except Exception as e:
print(repr(e))

View File

@@ -8,15 +8,22 @@ gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio
# Application imports
from .manifest import Plugin, ManifestProcessor
from .manifest import PluginInfo, ManifestProcessor
class InvalidPluginException(Exception):
...
class Plugins:
"""Plugins controller"""
def __init__(self, settings: type):
path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, path) # NOTE: I think I'm not using this correctly...
self._settings = settings
self._builder = self._settings.get_builder()
self._plugins_path = self._settings.get_plugins_path()
@@ -51,7 +58,7 @@ class Plugins:
manifest = ManifestProcessor(path, self._builder)
if not os.path.exists(target):
raise Exception("Invalid Plugin Structure: Plugin doesn't have 'plugin.py'. Aboarting load...")
raise InvalidPluginException("Invalid Plugin Structure: Plugin doesn't have 'plugin.py'. Aboarting load...")
plugin, loading_data = manifest.get_loading_data()
module = self.load_plugin_module(path, folder, target)
@@ -65,17 +72,15 @@ class Plugins:
def load_plugin_module(self, path, folder, target):
os.chdir(path)
sys.path.insert(0, path) # NOTE: I think I'm not using this correctly...
# The folder and target aren't working to create parent package references, so using as stopgap.
# The above is probably polutling import logic and will cause unforseen import issues.
spec = importlib.util.spec_from_file_location(folder, target)
spec = importlib.util.spec_from_file_location(folder, target, submodule_search_locations=path)
module = importlib.util.module_from_spec(spec)
sys.modules[folder] = module
spec.loader.exec_module(module)
return module
def execute_plugin(self, module: type, plugin: Plugin, loading_data: []):
def execute_plugin(self, module: type, plugin: PluginInfo, loading_data: []):
plugin.reference = module.Plugin()
keys = loading_data.keys()

View File

@@ -7,6 +7,10 @@
class EventSystemPushException(Exception):
...
class EventSystem:
""" Inheret IPCServerMixin. Create an pub/sub systems. """
@@ -37,14 +41,14 @@ class EventSystem:
self._gui_events.append(event)
return None
raise Exception("Invald event format! Please do: ['sender_id': str, method_target: method, (data,): any]")
raise EventSystemPushException("Invald event format! Please do: ['sender_id': str, method_target: method, (data,): any]")
def push_module_event(self, event: list) -> None:
if len(event) == 3:
self._module_events.append(event)
return None
raise Exception("Invald event format! Please do: ['target_id': str, method_target: method, (data,): any]")
raise EventSystemPushException("Invald event format! Please do: ['target_id': str, method_target: method, (data,): any]")
def read_gui_event(self) -> list:
return self._gui_events[0] if self._gui_events else None

View File

@@ -16,11 +16,11 @@ class IPCServer:
self._ipc_port = 4848
self._ipc_address = ipc_address
self._conn_type = conn_type
self._ipc_authkey = b'solarfm-ipc'
self._ipc_authkey = b'' + bytes(f'{app_name}-ipc', 'utf-8')
self._ipc_timeout = 15.0
if conn_type == "socket":
self._ipc_address = '/tmp/solarfm-ipc.sock'
self._ipc_address = f'/tmp/{app_name}-ipc.sock'
elif conn_type == "full_network":
self._ipc_address = '0.0.0.0'
elif conn_type == "full_network_unsecured":