reworked event system
This commit is contained in:
parent
d41235882a
commit
ee812c520f
|
@ -7,6 +7,7 @@ import builtins
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
from utils.event_system import EventSystem
|
from utils.event_system import EventSystem
|
||||||
|
from utils.endpoint_registry import EndpointRegistry
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,21 +25,6 @@ def daemon_threaded_wrapper(fn):
|
||||||
return wrapper
|
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...
|
# NOTE: Just reminding myself we can add to builtins two different ways...
|
||||||
|
|
|
@ -21,34 +21,16 @@ class Controller(DummyMixin, Controller_Data):
|
||||||
self.window.show()
|
self.window.show()
|
||||||
self.print_hello_world() # A mixin method from the DummyMixin file
|
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):
|
def tear_down(self, widget=None, eve=None):
|
||||||
time.sleep(event_sleep_time)
|
time.sleep(event_sleep_time)
|
||||||
Gtk.main_quit()
|
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:
|
def handle_file_from_ipc(self, path: str) -> None:
|
||||||
print(f"Path From IPC: {path}")
|
print(f"Path From IPC: {path}")
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
@ -1,4 +1,5 @@
|
||||||
# Python imports
|
# Python imports
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
# Lib imports
|
# Lib imports
|
||||||
|
|
||||||
|
@ -7,57 +8,23 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class EventSystemPushException(Exception):
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
class EventSystem:
|
class EventSystem:
|
||||||
""" Inheret IPCServerMixin. Create an pub/sub systems. """
|
""" Create event system. """
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# NOTE: The format used is list of ['who', target, (data,)] Where:
|
self.subscribers = defaultdict(list)
|
||||||
# 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 = []
|
|
||||||
|
|
||||||
|
|
||||||
# Makeshift "events" system FIFO
|
def subscribe(self, event_type, fn):
|
||||||
def _pop_gui_event(self) -> None:
|
self.subscribers[event_type].append(fn)
|
||||||
if len(self._gui_events) > 0:
|
|
||||||
return self._gui_events.pop(0)
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _pop_module_event(self) -> None:
|
def emit(self, event_type, data = None):
|
||||||
if len(self._module_events) > 0:
|
if event_type in self.subscribers:
|
||||||
return self._module_events.pop(0)
|
for fn in self.subscribers[event_type]:
|
||||||
return None
|
if data:
|
||||||
|
if hasattr(data, '__iter__') and not type(data) is str:
|
||||||
|
fn(*data)
|
||||||
def push_gui_event(self, event: list) -> None:
|
else:
|
||||||
if len(event) == 3:
|
fn(data)
|
||||||
self._gui_events.append(event)
|
else:
|
||||||
return None
|
fn()
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
|
@ -29,6 +29,11 @@ class IPCServer:
|
||||||
elif conn_type == "local_network_unsecured":
|
elif conn_type == "local_network_unsecured":
|
||||||
self._ipc_authkey = None
|
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
|
@daemon_threaded
|
||||||
def create_ipc_listener(self) -> None:
|
def create_ipc_listener(self) -> None:
|
||||||
|
@ -60,7 +65,7 @@ class IPCServer:
|
||||||
if "FILE|" in msg:
|
if "FILE|" in msg:
|
||||||
file = msg.split("FILE|")[1].strip()
|
file = msg.split("FILE|")[1].strip()
|
||||||
if file:
|
if file:
|
||||||
event_system.push_gui_event([None, "handle_file_from_ipc", (file,)])
|
event_system.emit("handle_file_from_ipc", file)
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
break
|
break
|
||||||
|
|
Loading…
Reference in New Issue