def trigger(self, func: typing.Optional[typing.Callable[[], typing.Any]]=None, trigger_threads=True) -> typing.Any: func = func or (lambda: None) if utils.is_main_thread(): returned = func() if trigger_threads: self._trigger_both() return returned func_queue = queue.Queue(1) # type: queue.Queue[str] def _action(): try: returned = func() type = TriggerResult.Return except Exception as e: returned = e type = TriggerResult.Exception func_queue.put([type, returned]) event_item = TriggerEvent(TriggerEventType.Action, _action) self._event_queue.put(event_item) type, returned = func_queue.get(block=True) if trigger_threads: self._trigger_both() if type == TriggerResult.Exception: raise returned elif type == TriggerResult.Return: return returned
def _call(self, path: typing.List[str], kwargs: dict, safe: bool, context: typing.Optional[str], maximum: typing.Optional[int]) -> typing.List[typing.Any]: if not utils.is_main_thread(): raise RuntimeError("Can't call events outside of main thread") returns: typing.List[typing.Any] = [] path_str = self._path_str(path) if not path_str in self._hooks: self.log.trace("not calling non-hooked event \"%s\" (params: %s)", [path_str, str(kwargs)]) return returns self.log.trace("calling event: \"%s\" (params: %s)", [path_str, str(kwargs)]) start = time.monotonic() # .copy() hooks so we don't call new hooks in this loop mutable_hooks = self._hooks[path_str] hooks = mutable_hooks.copy() if maximum: hooks = hooks[:maximum] event = self._make_event(path, kwargs) for hook in hooks: if event.eaten: break if not hook in mutable_hooks: # this hook has been removed while handling this event continue try: returned = hook.call(event) except Exception as e: if safe: self.log.error("failed to call event \"%s\"", [path_str], exc_info=True) continue else: raise returns.append(returned) total_milliseconds = (time.monotonic() - start) * 1000 self.log.trace("event \"%s\" called in %fms", [path_str, total_milliseconds]) return returns
def _execute_fetch(self, query: str, fetch_func: typing.Callable[[sqlite3.Cursor], typing.Any], params: typing.List=[]): if not utils.is_main_thread(): raise RuntimeError("Can't access Database outside of main thread") printable_query = " ".join(query.split()) start = time.monotonic() cursor = self.cursor() with self._lock: cursor.execute(query, params) value = fetch_func(cursor) end = time.monotonic() total_milliseconds = (end - start) * 1000 self.log.trace("executed query in %fms: \"%s\" (params: %s)", [total_milliseconds, printable_query, params]) return value
def _execute_fetch(self, query: str, fetch_func: typing.Callable[[sqlite3.Cursor], typing.Any], params: typing.List = []): if not utils.is_main_thread(): raise RuntimeError("Can't access Database outside of main thread") printable_query = " ".join(query.split()) start = time.monotonic() cursor = self.cursor() with self._lock: cursor.execute(query, params) value = fetch_func(cursor) end = time.monotonic() total_milliseconds = (end - start) * 1000 self.log.trace("executed query in %fms: \"%s\" (params: %s)", [total_milliseconds, printable_query, params]) return value
def trigger(self, func: typing.Optional[typing.Callable[[], typing.Any]]=None ) -> typing.Any: func = func or (lambda: None) if utils.is_main_thread(): returned = func() self._trigger_client.send(b"TRIGGER") return returned self.lock.acquire() func_queue = queue.Queue(1) # type: queue.Queue[str] self._trigger_functions.append([func, func_queue]) self.lock.release() self._trigger_client.send(b"TRIGGER") type, returned = func_queue.get(block=True) if type == TRIGGER_EXCEPTION: raise returned elif type == TRIGGER_RETURN: return returned
def trigger(self, func: typing.Optional[typing.Callable[[], typing.Any]]=None ) -> typing.Any: func = func or (lambda: None) if utils.is_main_thread(): returned = func() self._trigger_client.send(b"TRIGGER") return returned self.lock.acquire() func_queue = queue.Queue(1) # type: queue.Queue[str] self._trigger_functions.append([func, func_queue]) self.lock.release() self._trigger_client.send(b"TRIGGER") type, returned = func_queue.get(block=True) if type == TriggerResult.Exception: raise returned elif type == TriggerResult.Return: return returned