reworked event system

This commit is contained in:
itdominator 2022-09-30 23:35:03 -05:00
parent d41235882a
commit ee812c520f
5 changed files with 49 additions and 87 deletions

View File

@ -7,6 +7,7 @@ import builtins
# Application imports
from utils.event_system import EventSystem
from utils.endpoint_registry import EndpointRegistry
@ -24,21 +25,6 @@ def daemon_threaded_wrapper(fn):
return wrapper
class EndpointRegistry():
def __init__(self):
self._endpoints = {}
def register(self, rule, **options):
def decorator(f):
self._endpoints[rule] = f
return f
return decorator
def get_endpoints(self):
return self._endpoints
# NOTE: Just reminding myself we can add to builtins two different ways...

View File

@ -21,34 +21,16 @@ class Controller(DummyMixin, Controller_Data):
self.window.show()
self.print_hello_world() # A mixin method from the DummyMixin file
self._subscribe_to_events()
def _subscribe_to_events(self):
event_system.subscribe("handle_file_from_ipc", self.handle_file_from_ipc)
def tear_down(self, widget=None, eve=None):
time.sleep(event_sleep_time)
Gtk.main_quit()
@daemon_threaded
def gui_event_observer(self):
while True:
time.sleep(event_sleep_time)
event = event_system.consume_gui_event()
if event:
try:
sender_id, method_target, parameters = event
if sender_id:
method = getattr(self.__class__, "handle_gui_event_and_return_message")
GLib.idle_add(method, *(self, sender_id, method_target, parameters))
else:
method = getattr(self.__class__, method_target)
GLib.idle_add(method, *(self, *parameters,))
except Exception as e:
print(repr(e))
def handle_gui_event_and_return_message(self, sender, method_target, parameters):
method = getattr(self.__class__, f"{method_target}")
data = method(*(self, *parameters))
event_system.push_module_event([sender, None, data])
def handle_file_from_ipc(self, path: str) -> None:
print(f"Path From IPC: {path}")

View File

@ -0,0 +1,22 @@
# Python imports
# Lib imports
# Application imports
class EndpointRegistry():
def __init__(self):
self._endpoints = {}
def register(self, rule, **options):
def decorator(f):
self._endpoints[rule] = f
return f
return decorator
def get_endpoints(self):
return self._endpoints

View File

@ -1,4 +1,5 @@
# Python imports
from collections import defaultdict
# Lib imports
@ -7,57 +8,23 @@
class EventSystemPushException(Exception):
...
class EventSystem:
""" Inheret IPCServerMixin. Create an pub/sub systems. """
""" Create event system. """
def __init__(self):
# NOTE: The format used is list of ['who', target, (data,)] Where:
# who is the sender or target ID and is used for context and control flow,
# method_target is the method to call,
# data is the method parameters OR message data to give
# Where data may be any kind of data
self._gui_events = []
self._module_events = []
self.subscribers = defaultdict(list)
# Makeshift "events" system FIFO
def _pop_gui_event(self) -> None:
if len(self._gui_events) > 0:
return self._gui_events.pop(0)
return None
def subscribe(self, event_type, fn):
self.subscribers[event_type].append(fn)
def _pop_module_event(self) -> None:
if len(self._module_events) > 0:
return self._module_events.pop(0)
return None
def push_gui_event(self, event: list) -> None:
if len(event) == 3:
self._gui_events.append(event)
return None
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 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
def read_module_event(self) -> list:
return self._module_events[0] if self._module_events else None
def consume_gui_event(self) -> list:
return self._pop_gui_event()
def consume_module_event(self) -> list:
return self._pop_module_event()
def emit(self, event_type, data = None):
if event_type in self.subscribers:
for fn in self.subscribers[event_type]:
if data:
if hasattr(data, '__iter__') and not type(data) is str:
fn(*data)
else:
fn(data)
else:
fn()

View File

@ -29,6 +29,11 @@ class IPCServer:
elif conn_type == "local_network_unsecured":
self._ipc_authkey = None
self._subscribe_to_events()
def _subscribe_to_events(self):
event_system.subscribe("post_file_to_ipc", self.send_ipc_message)
@daemon_threaded
def create_ipc_listener(self) -> None:
@ -60,7 +65,7 @@ class IPCServer:
if "FILE|" in msg:
file = msg.split("FILE|")[1].strip()
if file:
event_system.push_gui_event([None, "handle_file_from_ipc", (file,)])
event_system.emit("handle_file_from_ipc", file)
conn.close()
break