Added example, further plugin work

This commit is contained in:
itdominator 2022-01-30 20:29:57 -06:00
parent 3bedd83793
commit ed2a27ed9a
5 changed files with 54 additions and 20 deletions

2
plugins/README.txt Normal file
View File

@ -0,0 +1,2 @@
### Note
Copy the example and rename it to your desired name. The Main class and passed in arguments are required. You don't necessarily need to use the passed in socket_id or event_system.

View File

@ -0,0 +1,37 @@
# Python imports
import sys, traceback, threading, inspect, os, time
# Gtk imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
return wrapper
class Main:
def __init__(self, socket_id, event_system):
self._socket_id = socket_id
self._event_system = event_system
self._gtk_plug = Gtk.Plug.new(self._socket_id)
self.start_loop()
@threaded
def start_loop(self):
i = 0
cycles = 5
alive = True
while alive:
if i == cycles:
alive = False
self._event_system.push_gui_event(["some_type", "display_message", ("warning", str(i), None)])
i += 1
time.sleep(1)

View File

@ -4,7 +4,7 @@ import builtins
# Lib imports # Lib imports
# Application imports # Application imports
from signal_classes import IPCServerMixin from controller import IPCServerMixin

View File

@ -22,7 +22,7 @@ def threaded(fn):
class Controller(UIMixin, KeyboardSignalsMixin, IPCSignalsMixin, Controller_Data): class Controller(UIMixin, KeyboardSignalsMixin, IPCSignalsMixin, Controller_Data):
def __init__(self, args, unknownargs, _settings): def __init__(self, args, unknownargs, _settings):
# sys.excepthook = self.custom_except_hook sys.excepthook = self.custom_except_hook
self.setup_controller_data(_settings) self.setup_controller_data(_settings)
self.window.show() self.window.show()
self.generate_windows(self.state) self.generate_windows(self.state)
@ -58,7 +58,7 @@ class Controller(UIMixin, KeyboardSignalsMixin, IPCSignalsMixin, Controller_Data
try: try:
type, target, data = event type, target, data = event
method = getattr(self.__class__, target) method = getattr(self.__class__, target)
GLib.idle_add(method, *(self, data,)) GLib.idle_add(method, *(self, *data,))
except Exception as e: except Exception as e:
print(repr(e)) print(repr(e))

View File

@ -17,14 +17,14 @@ class Plugins:
def __init__(self, settings): def __init__(self, settings):
self._settings = settings self._settings = settings
self._plugins_path = self._settings.get_plugins_path() self._plugins_path = self._settings.get_plugins_path()
self._socket = Gtk.Socket().new() self.gtk_socket = Gtk.Socket().new()
self._plugins_dir_watcher = None self._plugins_dir_watcher = None
self._socket_id = None self.gtk_socket_id = None
self._plugin_collection = [] self._plugin_collection = []
self._settings.get_main_window().add(self._socket) self._settings.get_main_window().add(self.gtk_socket)
self._socket.show() self.gtk_socket.show()
self._socket_id = self._socket.get_id() self.gtk_socket_id = self.gtk_socket.get_id()
def launch_plugins(self): def launch_plugins(self):
@ -49,20 +49,15 @@ class Plugins:
if isdir(path): if isdir(path):
spec = importlib.util.spec_from_file_location(file, join(path, "__main__.py")) spec = importlib.util.spec_from_file_location(file, join(path, "__main__.py"))
module = importlib.util.module_from_spec(spec) module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
self._plugin_collection.append([file, module]) self._plugin_collection.append([file, module])
# module.set_socket_id(self._socket_id)
# print(f"\n\n\n {event_system} \n\n\n")
# module.set_event_system(event_system)
# module.main()
# module.start_loop(event_system)
print(self._plugin_collection) spec.loader.exec_module(module)
module.Main(self.gtk_socket_id, event_system)
def reload_plugins(self, file=None): def reload_plugins(self, file=None):
print(f"Reloading plugins...") print(f"Reloading plugins...")
if self._plugin_collection: # if self._plugin_collection:
to_unload = [] # to_unload = []
for dir in self._plugin_collection: # for dir in self._plugin_collection:
if not os.path.isdir(os.path.join(self._plugins_path, dir)): # if not os.path.isdir(os.path.join(self._plugins_path, dir)):
to_unload.append(dir) # to_unload.append(dir)