Event system updates, thread decorator changes
This commit is contained in:
parent
d74761344c
commit
df68ce7a5b
|
@ -16,16 +16,16 @@ class EventSystem(IPCServer):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(EventSystem, self).__init__()
|
super(EventSystem, self).__init__()
|
||||||
|
|
||||||
# NOTE: The format used is list of [type, target, (data,)] Where:
|
# NOTE: The format used is list of ['who', target, (data,)] Where:
|
||||||
# type is useful context for control flow,
|
# who is the sender or target ID and is used for context and control flow,
|
||||||
# target is the method to call,
|
# method_target is the method to call,
|
||||||
# data is the method parameters to give
|
# data is the method parameters OR message data to give
|
||||||
# Where data may be any kind of data
|
# Where data may be any kind of data
|
||||||
self._gui_events = []
|
self._gui_events = []
|
||||||
self._module_events = []
|
self._module_events = []
|
||||||
|
|
||||||
|
|
||||||
# Makeshift fake "events" type system FIFO
|
# Makeshift "events" system FIFO
|
||||||
def _pop_gui_event(self) -> None:
|
def _pop_gui_event(self) -> None:
|
||||||
if len(self._gui_events) > 0:
|
if len(self._gui_events) > 0:
|
||||||
return self._gui_events.pop(0)
|
return self._gui_events.pop(0)
|
||||||
|
@ -40,7 +40,7 @@ class EventSystem(IPCServer):
|
||||||
self._gui_events.append(event)
|
self._gui_events.append(event)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
raise Exception("Invald event format! Please do: [type, target, (data,)]")
|
raise Exception("Invald event format! Please do: ['target_id': str, method_target: method, (data,): any]")
|
||||||
|
|
||||||
def push_module_event(self, event: list) -> None:
|
def push_module_event(self, event: list) -> None:
|
||||||
if len(event) == 3:
|
if len(event) == 3:
|
||||||
|
@ -49,11 +49,11 @@ class EventSystem(IPCServer):
|
||||||
|
|
||||||
raise Exception("Invald event format! Please do: [type, target, (data,)]")
|
raise Exception("Invald event format! Please do: [type, target, (data,)]")
|
||||||
|
|
||||||
def read_gui_event(self) -> None:
|
def read_gui_event(self) -> list:
|
||||||
return self._gui_events[0]
|
return self._gui_events[0] if self._gui_events else None
|
||||||
|
|
||||||
def read_module_event(self) -> None:
|
def read_module_event(self) -> list:
|
||||||
return self._module_events[0]
|
return self._module_events[0] if self._module_events else None
|
||||||
|
|
||||||
def consume_gui_event(self) -> None:
|
def consume_gui_event(self) -> None:
|
||||||
return self._pop_gui_event()
|
return self._pop_gui_event()
|
||||||
|
@ -67,6 +67,6 @@ class EventSystem(IPCServer):
|
||||||
# __builtins__.update({"event_system": Builtins()})
|
# __builtins__.update({"event_system": Builtins()})
|
||||||
builtins.app_name = "<change_me>"
|
builtins.app_name = "<change_me>"
|
||||||
builtins.event_system = EventSystem()
|
builtins.event_system = EventSystem()
|
||||||
builtins.event_sleep_time = 0.2
|
builtins.event_sleep_time = 0.05
|
||||||
builtins.trace_debug = False
|
builtins.trace_debug = False
|
||||||
builtins.debug = False
|
builtins.debug = False
|
||||||
|
|
|
@ -13,13 +13,20 @@ from .mixins.dummy_mixin import DummyMixin
|
||||||
from .controller_data import Controller_Data
|
from .controller_data import Controller_Data
|
||||||
|
|
||||||
|
|
||||||
|
# NOTE: Threads will not die with parent's destruction
|
||||||
def threaded(fn):
|
def threaded(fn):
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
|
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=False).start()
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
# NOTE: Insure threads die with parent's destruction
|
||||||
|
def daemon_threaded(fn):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Controller(DummyMixin, Controller_Data):
|
class Controller(DummyMixin, Controller_Data):
|
||||||
def __init__(self, _settings, args, unknownargs):
|
def __init__(self, _settings, args, unknownargs):
|
||||||
|
@ -35,19 +42,28 @@ class Controller(DummyMixin, Controller_Data):
|
||||||
Gtk.main_quit()
|
Gtk.main_quit()
|
||||||
|
|
||||||
|
|
||||||
@threaded
|
@daemon_threaded
|
||||||
def gui_event_observer(self) -> None:
|
def gui_event_observer(self):
|
||||||
while True:
|
while True:
|
||||||
time.sleep(event_sleep_time)
|
time.sleep(event_sleep_time)
|
||||||
event = event_system.consume_gui_event()
|
event = event_system.consume_gui_event()
|
||||||
if event:
|
if event:
|
||||||
try:
|
try:
|
||||||
type, target, data = event
|
sender_id, method_target, parameters = event
|
||||||
method = getattr(self.__class__, target)
|
if sender_id:
|
||||||
GLib.idle_add(method, *(self, *data,))
|
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:
|
except Exception as e:
|
||||||
print(repr(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}")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue