Python-With-Gtk-Template/src/__builtins__.py

73 lines
2.2 KiB
Python
Raw Normal View History

2021-10-11 04:33:58 +00:00
import builtins
2022-01-23 22:56:27 +00:00
# Python imports
import builtins
# Lib imports
# Application imports
2022-03-08 00:32:22 +00:00
from utils.ipc_server import IPCServer
2022-01-23 22:56:27 +00:00
2022-02-25 23:53:58 +00:00
class EventSystem(IPCServer):
""" Inheret IPCServerMixin. Create an pub/sub systems. """
2022-01-23 22:56:27 +00:00
def __init__(self):
2022-02-25 23:53:58 +00:00
super(EventSystem, self).__init__()
# 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
2022-01-23 22:56:27 +00:00
# Where data may be any kind of data
self._gui_events = []
2022-02-01 06:03:04 +00:00
self._module_events = []
2022-02-25 23:53:58 +00:00
2022-01-23 22:56:27 +00:00
# Makeshift "events" system FIFO
2022-03-25 03:11:02 +00:00
def _pop_gui_event(self) -> None:
2022-01-23 22:56:27 +00:00
if len(self._gui_events) > 0:
return self._gui_events.pop(0)
2022-03-25 03:11:02 +00:00
def _pop_module_event(self) -> None:
2022-02-01 06:03:04 +00:00
if len(self._module_events) > 0:
return self._module_events.pop(0)
2022-01-23 22:56:27 +00:00
2022-03-25 03:11:02 +00:00
def push_gui_event(self, event: list) -> None:
2022-01-23 22:56:27 +00:00
if len(event) == 3:
self._gui_events.append(event)
return None
raise Exception("Invald event format! Please do: ['target_id': str, method_target: method, (data,): any]")
2022-01-23 22:56:27 +00:00
2022-03-25 03:11:02 +00:00
def push_module_event(self, event: list) -> None:
2022-01-23 22:56:27 +00:00
if len(event) == 3:
2022-02-01 06:03:04 +00:00
self._module_events.append(event)
2022-01-23 22:56:27 +00:00
return None
2022-02-25 23:53:58 +00:00
raise Exception("Invald event format! Please do: [type, target, (data,)]")
2022-01-23 22:56:27 +00:00
def read_gui_event(self) -> list:
return self._gui_events[0] if self._gui_events else None
2022-01-23 22:56:27 +00:00
def read_module_event(self) -> list:
return self._module_events[0] if self._module_events else None
2022-01-23 22:56:27 +00:00
2022-03-25 03:11:02 +00:00
def consume_gui_event(self) -> None:
2022-01-23 22:56:27 +00:00
return self._pop_gui_event()
2022-03-25 03:11:02 +00:00
def consume_module_event(self) -> None:
2022-02-01 06:03:04 +00:00
return self._pop_module_event()
2021-10-11 04:28:19 +00:00
2021-10-11 04:33:58 +00:00
2021-10-11 04:28:19 +00:00
2022-01-23 22:56:27 +00:00
# NOTE: Just reminding myself we can add to builtins two different ways...
# __builtins__.update({"event_system": Builtins()})
builtins.app_name = "<change_me>"
2022-02-25 23:53:58 +00:00
builtins.event_system = EventSystem()
builtins.event_sleep_time = 0.05
2022-01-23 22:56:27 +00:00
builtins.trace_debug = False
2022-03-25 03:11:02 +00:00
builtins.debug = False