2020-09-24 09:57:54 +10:00
|
|
|
# Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2019-12-19 22:46:16 +11:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2020-03-13 17:46:21 +11:00
|
|
|
# WARNING: make sure to not top-level import the media_cache module here,
|
|
|
|
# directly or indirectly via another module import (e.g. backend).
|
|
|
|
# See https://stackoverflow.com/a/55918049
|
|
|
|
|
2020-05-23 07:27:57 +10:00
|
|
|
"""Provide `BRIDGE`, main object accessed by QML to interact with Python.
|
|
|
|
|
|
|
|
PyOtherSide, the library that handles interaction between our Python backend
|
|
|
|
and QML UI, will access the `BRIDGE` object and call its methods directly.
|
|
|
|
|
|
|
|
The `BRIDGE` object should be the only existing instance of the `QMLBridge`
|
|
|
|
class.
|
|
|
|
"""
|
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
import asyncio
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
import logging as log
|
2020-03-19 10:12:38 +11:00
|
|
|
import os
|
2019-10-30 04:34:55 +11:00
|
|
|
import traceback
|
2019-06-27 16:31:03 +10:00
|
|
|
from concurrent.futures import Future
|
2019-07-19 10:24:59 +10:00
|
|
|
from operator import attrgetter
|
2019-06-27 16:31:03 +10:00
|
|
|
from threading import Thread
|
2020-10-29 19:40:15 +11:00
|
|
|
from typing import Coroutine, Dict, Sequence, Set
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2020-07-17 13:16:21 +10:00
|
|
|
import pyotherside
|
|
|
|
|
2019-12-27 01:05:01 +11:00
|
|
|
from .pyotherside_events import CoroutineDone, LoopException
|
2019-07-10 11:46:21 +10:00
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2019-12-18 23:49:03 +11:00
|
|
|
class QMLBridge:
|
2020-05-23 07:27:57 +10:00
|
|
|
"""Setup asyncio and provide methods to call coroutines from QML.
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
A thread is created to run the asyncio loop in, to ensure all calls from
|
|
|
|
QML return instantly.
|
2020-05-23 07:27:57 +10:00
|
|
|
Synchronous methods are provided for QML to call coroutines using
|
|
|
|
PyOtherSide, which doesn't have this ability out of the box.
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
Attributes:
|
2020-05-23 07:27:57 +10:00
|
|
|
backend: The `backend.Backend` object containing general coroutines
|
|
|
|
for QML and that manages `MatrixClient` objects.
|
2019-12-18 23:14:35 +11:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
2020-05-15 22:07:41 +10:00
|
|
|
try:
|
|
|
|
self._loop = asyncio.get_event_loop()
|
|
|
|
except RuntimeError:
|
|
|
|
self._loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(self._loop)
|
|
|
|
self._loop.set_exception_handler(self._loop_exception_handler)
|
|
|
|
|
2020-03-13 17:46:21 +11:00
|
|
|
from .backend import Backend
|
2019-12-18 23:14:35 +11:00
|
|
|
self.backend: Backend = Backend()
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2020-09-29 13:06:50 +10:00
|
|
|
self._running_futures: Dict[str, Future] = {}
|
2020-10-29 19:40:15 +11:00
|
|
|
self._cancelled_early: Set[str] = set()
|
2020-09-29 13:06:50 +10:00
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
Thread(target=self._start_asyncio_loop).start()
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
2019-12-27 01:05:01 +11:00
|
|
|
def _loop_exception_handler(
|
|
|
|
self, loop: asyncio.AbstractEventLoop, context: dict,
|
|
|
|
) -> None:
|
|
|
|
if "exception" in context:
|
|
|
|
err = context["exception"]
|
|
|
|
trace = "".join(
|
|
|
|
traceback.format_exception(type(err), err, err.__traceback__),
|
|
|
|
)
|
|
|
|
LoopException(context["message"], err, trace)
|
|
|
|
|
|
|
|
loop.default_exception_handler(context)
|
|
|
|
|
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
def _start_asyncio_loop(self) -> None:
|
|
|
|
asyncio.set_event_loop(self._loop)
|
|
|
|
self._loop.run_forever()
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
2020-09-29 13:06:50 +10:00
|
|
|
def _call_coro(self, coro: Coroutine, uuid: str) -> None:
|
2019-12-18 23:14:35 +11:00
|
|
|
"""Schedule a coroutine to run in our thread and return a `Future`."""
|
|
|
|
|
2020-10-29 19:40:15 +11:00
|
|
|
if uuid in self._cancelled_early:
|
|
|
|
self._cancelled_early.remove(uuid)
|
|
|
|
return
|
|
|
|
|
2019-10-28 21:26:02 +11:00
|
|
|
def on_done(future: Future) -> None:
|
2019-12-18 23:14:35 +11:00
|
|
|
"""Send a PyOtherSide event with the coro's result/exception."""
|
2019-10-30 04:34:55 +11:00
|
|
|
result = exception = trace = None
|
|
|
|
|
2019-10-28 21:26:02 +11:00
|
|
|
try:
|
2019-10-30 04:34:55 +11:00
|
|
|
result = future.result()
|
2020-11-16 05:57:00 +11:00
|
|
|
except Exception as err: # noqa
|
2019-10-28 21:26:02 +11:00
|
|
|
exception = err
|
2019-10-30 04:34:55 +11:00
|
|
|
trace = traceback.format_exc().rstrip()
|
2019-10-28 21:26:02 +11:00
|
|
|
|
2019-10-30 04:34:55 +11:00
|
|
|
CoroutineDone(uuid, result, exception, trace)
|
2020-09-29 13:06:50 +10:00
|
|
|
del self._running_futures[uuid]
|
2019-10-28 21:26:02 +11:00
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
future = asyncio.run_coroutine_threadsafe(coro, self._loop)
|
2020-09-29 13:06:50 +10:00
|
|
|
self._running_futures[uuid] = future
|
2019-12-08 09:33:33 +11:00
|
|
|
future.add_done_callback(on_done)
|
2019-06-29 08:12:45 +10:00
|
|
|
|
|
|
|
|
2019-12-18 21:44:18 +11:00
|
|
|
def call_backend_coro(
|
|
|
|
self, name: str, uuid: str, args: Sequence[str] = (),
|
2020-09-29 13:06:50 +10:00
|
|
|
) -> None:
|
|
|
|
"""Schedule a coroutine from the `QMLBridge.backend` object."""
|
2019-12-18 23:14:35 +11:00
|
|
|
|
2020-10-29 19:40:15 +11:00
|
|
|
if uuid in self._cancelled_early:
|
|
|
|
self._cancelled_early.remove(uuid)
|
|
|
|
else:
|
|
|
|
self._call_coro(attrgetter(name)(self.backend)(*args), uuid)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
2019-12-18 21:44:18 +11:00
|
|
|
def call_client_coro(
|
|
|
|
self, user_id: str, name: str, uuid: str, args: Sequence[str] = (),
|
2020-09-29 13:06:50 +10:00
|
|
|
) -> None:
|
|
|
|
"""Schedule a coroutine from a `QMLBridge.backend.clients` client."""
|
2019-12-18 21:44:18 +11:00
|
|
|
|
2020-10-29 19:40:15 +11:00
|
|
|
if uuid in self._cancelled_early:
|
|
|
|
self._cancelled_early.remove(uuid)
|
|
|
|
else:
|
|
|
|
client = self.backend.clients[user_id]
|
|
|
|
self._call_coro(attrgetter(name)(client)(*args), uuid)
|
2020-09-29 13:06:50 +10:00
|
|
|
|
|
|
|
|
|
|
|
def cancel_coro(self, uuid: str) -> None:
|
|
|
|
"""Cancel a couroutine scheduled by the `QMLBridge` methods."""
|
|
|
|
|
2020-10-29 19:40:15 +11:00
|
|
|
if uuid in self._running_futures:
|
2020-09-29 13:06:50 +10:00
|
|
|
self._running_futures[uuid].cancel()
|
2020-10-29 19:40:15 +11:00
|
|
|
else:
|
|
|
|
self._cancelled_early.add(uuid)
|
2019-06-27 16:31:03 +10:00
|
|
|
|
|
|
|
|
2021-01-16 23:15:49 +11:00
|
|
|
def pdb(self, extra_data: Sequence = (), remote: bool = False) -> None:
|
2021-01-16 23:37:21 +11:00
|
|
|
"""Call the python debugger, defining some conveniance variables."""
|
2019-12-18 23:14:35 +11:00
|
|
|
|
2021-01-16 23:15:49 +11:00
|
|
|
ad = extra_data # noqa
|
2019-08-18 12:46:54 +10:00
|
|
|
ba = self.backend # noqa
|
|
|
|
mo = self.backend.models # noqa
|
|
|
|
cl = self.backend.clients
|
2020-05-22 10:50:43 +10:00
|
|
|
gcl = lambda user: cl[f"@{user}"] # noqa
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
|
2019-12-18 23:14:35 +11:00
|
|
|
rc = lambda c: asyncio.run_coroutine_threadsafe(c, self._loop) # noqa
|
|
|
|
|
2019-12-18 21:44:18 +11:00
|
|
|
try:
|
2021-01-20 04:22:29 +11:00
|
|
|
from devtools import debug # noqa
|
2021-01-16 23:37:21 +11:00
|
|
|
d = debug # noqa
|
2019-12-18 21:44:18 +11:00
|
|
|
except ModuleNotFoundError:
|
2021-01-16 23:37:21 +11:00
|
|
|
log.warning("Module python-devtools not found, can't use debug()")
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2021-01-16 23:15:49 +11:00
|
|
|
if remote:
|
|
|
|
# Run `socat readline tcp:127.0.0.1:4444` in a terminal to connect
|
2020-05-22 10:50:43 +10:00
|
|
|
import remote_pdb
|
2021-01-16 23:15:49 +11:00
|
|
|
remote_pdb.RemotePdb("127.0.0.1", 4444).set_trace()
|
|
|
|
else:
|
2020-05-22 10:50:43 +10:00
|
|
|
import pdb
|
|
|
|
pdb.set_trace()
|
2019-07-03 03:59:52 +10:00
|
|
|
|
2019-06-27 16:31:03 +10:00
|
|
|
|
2020-07-17 13:34:35 +10:00
|
|
|
def exit(self) -> None:
|
2020-07-17 11:19:20 +10:00
|
|
|
try:
|
|
|
|
asyncio.run_coroutine_threadsafe(
|
2020-07-17 13:34:35 +10:00
|
|
|
self.backend.terminate_clients(), self._loop,
|
2020-07-17 11:19:20 +10:00
|
|
|
).result()
|
2020-11-16 05:57:00 +11:00
|
|
|
except Exception as e: # noqa
|
2020-07-17 11:19:20 +10:00
|
|
|
print(e)
|
|
|
|
|
|
|
|
|
2020-03-19 10:12:38 +11:00
|
|
|
# The AppImage AppRun script overwrites some environment path variables to
|
|
|
|
# correctly work, and sets RESTORE_<name> equivalents with the original values.
|
|
|
|
# If the app is launched from an AppImage, now restore the original values
|
|
|
|
# to prevent problems like QML Qt.openUrlExternally() failing because
|
|
|
|
# the external launched program is affected by our AppImage-specific variables.
|
|
|
|
for var in ("LD_LIBRARY_PATH", "PYTHONHOME", "PYTHONUSERBASE"):
|
|
|
|
if f"RESTORE_{var}" in os.environ:
|
|
|
|
os.environ[var] = os.environ[f"RESTORE_{var}"]
|
|
|
|
|
|
|
|
|
2019-12-18 23:49:03 +11:00
|
|
|
BRIDGE = QMLBridge()
|
2020-07-17 11:19:20 +10:00
|
|
|
|
2020-07-17 13:34:35 +10:00
|
|
|
pyotherside.atexit(BRIDGE.exit)
|