2019-06-27 16:31:03 +10:00
|
|
|
import asyncio
|
2019-07-03 03:59:52 +10:00
|
|
|
import signal
|
2019-06-27 16:31:03 +10:00
|
|
|
from concurrent.futures import Future
|
|
|
|
from pathlib import Path
|
|
|
|
from threading import Thread
|
|
|
|
from typing import Any, Coroutine, Dict, List, Optional, Sequence
|
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
from appdirs import AppDirs
|
|
|
|
|
|
|
|
from . import __about__
|
2019-06-27 16:36:31 +10:00
|
|
|
from .events.app import CoroutineDone, ExitRequested
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
|
|
|
class App:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.appdirs = AppDirs(appname=__about__.__pkg_name__, roaming=True)
|
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
from .backend import Backend
|
|
|
|
self.backend = Backend(app=self)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
self.loop = asyncio.get_event_loop()
|
|
|
|
self.loop_thread = Thread(target=self._loop_starter)
|
|
|
|
self.loop_thread.start()
|
|
|
|
|
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
def is_debug_on(self, cli_flags: Sequence[str] = ()) -> bool:
|
|
|
|
return "-d" in cli_flags or "--debug" in cli_flags
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
|
|
|
def _loop_starter(self) -> None:
|
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
self.loop.run_forever()
|
|
|
|
|
|
|
|
|
2019-06-29 08:12:45 +10:00
|
|
|
def run_in_loop(self, coro: Coroutine) -> Future:
|
2019-06-27 16:31:03 +10:00
|
|
|
return asyncio.run_coroutine_threadsafe(coro, self.loop)
|
|
|
|
|
|
|
|
|
2019-07-07 15:37:13 +10:00
|
|
|
def _call_coro(self, coro: Coroutine, uuid: str) -> None:
|
2019-06-29 08:12:45 +10:00
|
|
|
self.run_in_loop(coro).add_done_callback(
|
|
|
|
lambda future: CoroutineDone(uuid=uuid, result=future.result())
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
def call_backend_coro(self,
|
2019-07-08 13:38:37 +10:00
|
|
|
name: str,
|
|
|
|
uuid: str,
|
|
|
|
args: Optional[List[str]] = None) -> None:
|
2019-07-07 15:37:13 +10:00
|
|
|
self._call_coro(
|
2019-07-08 13:38:37 +10:00
|
|
|
getattr(self.backend, name)(*args or []), uuid
|
2019-06-29 08:12:45 +10:00
|
|
|
)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
2019-06-29 08:12:45 +10:00
|
|
|
def call_client_coro(self,
|
|
|
|
account_id: str,
|
|
|
|
name: str,
|
2019-07-07 15:37:13 +10:00
|
|
|
uuid: str,
|
2019-07-08 13:38:37 +10:00
|
|
|
args: Optional[List[str]] = None) -> None:
|
2019-07-03 03:59:52 +10:00
|
|
|
client = self.backend.clients[account_id]
|
2019-07-07 15:37:13 +10:00
|
|
|
self._call_coro(
|
2019-07-08 13:38:37 +10:00
|
|
|
getattr(client, name)(*args or []), uuid
|
2019-06-27 16:31:03 +10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def pdb(self, additional_data: Sequence = ()) -> None:
|
|
|
|
# pylint: disable=all
|
|
|
|
ad = additional_data
|
2019-06-29 08:12:45 +10:00
|
|
|
rl = self.run_in_loop
|
2019-06-27 16:31:03 +10:00
|
|
|
ba = self.backend
|
2019-07-03 03:59:52 +10:00
|
|
|
cl = self.backend.clients
|
2019-06-27 16:31:03 +10:00
|
|
|
tcl = lambda user: cl[f"@test_{user}:matrix.org"]
|
|
|
|
|
|
|
|
import json
|
|
|
|
jd = lambda obj: print(json.dumps(obj, indent=4, ensure_ascii=False))
|
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
print("\n=> Run `socat readline tcp:127.0.0.1:4444` in a terminal "
|
|
|
|
"to connect to pdb.")
|
|
|
|
import remote_pdb
|
|
|
|
remote_pdb.RemotePdb("127.0.0.1", 4444).set_trace()
|
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2019-07-03 03:59:52 +10:00
|
|
|
# Make CTRL-C work again
|
|
|
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
APP = App()
|