diff --git a/src/python/app.py b/src/python/app.py index 600a3e9d..0770a928 100644 --- a/src/python/app.py +++ b/src/python/app.py @@ -80,11 +80,12 @@ class App: def pdb(self, additional_data: Sequence = ()) -> None: - ad = additional_data # noqa - rl = self.run_in_loop # noqa - ba = self.backend # noqa - mo = self.backend.models # noqa - cl = self.backend.clients + ad = additional_data # noqa + rl = self.run_in_loop # noqa + ba = self.backend # noqa + mo = self.backend.models # noqa + smo = self.backend.sidepane_model # noqa + cl = self.backend.clients tcl = lambda user: cl[f"@test_{user}:matrix.org"] # noqa from .models.items import Account, Room, Member, Event, Device # noqa diff --git a/src/python/backend.py b/src/python/backend.py index 02b336dd..a164c1d7 100644 --- a/src/python/backend.py +++ b/src/python/backend.py @@ -1,7 +1,7 @@ import asyncio import logging as log import random -from typing import DefaultDict, Dict, List, Optional, Tuple, Union +from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union import hsluv @@ -122,6 +122,13 @@ class Backend: # General functions + + @staticmethod + def hsluv(hue: int, saturation: int, lightness: int) -> List[float]: + # (0-360, 0-100, 0-100) -> [0-1, 0-1, 0-1] + return hsluv.hsluv_to_rgb([hue, saturation, lightness]) + + async def load_settings(self) -> tuple: from .config_files import Theme settings = await self.ui_settings.read() @@ -153,7 +160,21 @@ class Backend: return response - @staticmethod - def hsluv(hue: int, saturation: int, lightness: int) -> List[float]: - # (0-360, 0-100, 0-100) -> [0-1, 0-1, 0-1] - return hsluv.hsluv_to_rgb([hue, saturation, lightness]) + async def get_flat_sidepane_data(self) -> List[Dict[str, Any]]: + data = [] + + for account in sorted(self.models[Account].values()): + data.append({ + "type": "Account", + "id": account.user_id, + "data": account.__dict__, + }) + + for room in sorted(self.models[Room, account.user_id].values()): + data.append({ + "type": "Room", + "id": (account.user_id, room.room_id), + "data": room.__dict__, + }) + + return data diff --git a/src/python/models/__init__.py b/src/python/models/__init__.py index 11c88095..6ebb92fb 100644 --- a/src/python/models/__init__.py +++ b/src/python/models/__init__.py @@ -2,5 +2,5 @@ from typing import Tuple, Type, Union from .model_item import ModelItem -# Type[ModelItem] or Tuple[Type[ModelItem], str...] -SyncId = Union[Type[ModelItem], Tuple[Union[Type[ModelItem], str], ...]] +# last one: Tuple[Union[Type[ModelItem], Tuple[Type[ModelItem]]], str...] +SyncId = Union[Type[ModelItem], Tuple[Type[ModelItem]], tuple] diff --git a/src/python/models/items.py b/src/python/models/items.py index 92493a72..e659426b 100644 --- a/src/python/models/items.py +++ b/src/python/models/items.py @@ -9,8 +9,6 @@ from .model_item import ModelItem @dataclass class Account(ModelItem): - main_key = "user_id" - user_id: str = field() display_name: str = "" avatar_url: str = "" @@ -24,8 +22,6 @@ class Account(ModelItem): @dataclass class Room(ModelItem): - main_key = "room_id" - room_id: str = field() display_name: str = "" avatar_url: str = "" @@ -66,8 +62,6 @@ class Room(ModelItem): @dataclass class Member(ModelItem): - main_key = "user_id" - user_id: str = field() display_name: str = "" avatar_url: str = "" @@ -89,8 +83,6 @@ class TypeSpecifier(AutoStrEnum): @dataclass class Event(ModelItem): - main_key = "event_id" - client_id: str = field() event_id: str = field() event_type: str = field() # Type[nio.Event] @@ -127,8 +119,6 @@ class Event(ModelItem): @dataclass class Device(ModelItem): - main_key = "device_id" - device_id: str = field() ed25519_key: str = field() trusted: bool = False diff --git a/src/python/models/model.py b/src/python/models/model.py index afc644bd..e5e8d90e 100644 --- a/src/python/models/model.py +++ b/src/python/models/model.py @@ -1,7 +1,7 @@ import logging as log import time from threading import Lock, Thread -from typing import Any, Dict, Iterator, List, MutableMapping, Optional +from typing import Any, Dict, Iterator, List, MutableMapping from . import SyncId from ..pyotherside_events import ModelUpdated @@ -11,7 +11,6 @@ from .model_item import ModelItem class Model(MutableMapping): def __init__(self, sync_id: SyncId) -> None: self.sync_id: SyncId = sync_id - self.sortable: Optional[bool] = None self._data: Dict[Any, ModelItem] = {} self._changed: bool = False @@ -27,22 +26,22 @@ class Model(MutableMapping): from pprint import pformat # type: ignore if isinstance(self.sync_id, tuple): - sid = (self.sync_id[0].__name__, *self.sync_id[1:]) # type: ignore + sid = (self.sync_id[0].__name__, *self.sync_id[1:]) else: sid = self.sync_id.__name__ # type: ignore - return "%s(sync_id=%s, sortable=%r, %s)" % ( - type(self).__name__, sid, self.sortable, pformat(self._data), + return "%s(sync_id=%s, %s)" % ( + type(self).__name__, sid, pformat(self._data), ) def __str__(self) -> str: if isinstance(self.sync_id, tuple): reprs = tuple(repr(s) for s in self.sync_id[1:]) - sid = ", ".join((self.sync_id[0].__name__, *reprs)) # type: ignore + sid = ", ".join((self.sync_id[0].__name__, *reprs)) sid = f"({sid})" else: - sid = self.sync_id.__name__ # type: ignore + sid = self.sync_id.__name__ return f"{sid!s}: {len(self)} items" @@ -64,7 +63,7 @@ class Model(MutableMapping): if merged == existing: return - new = type(value)(**merged) # type: ignore + new = type(value)(**merged) new.parent_model = self @@ -99,20 +98,7 @@ class Model(MutableMapping): def serialized(self) -> List[Dict[str, Any]]: - if self.sortable is True: - return [item.__dict__ for item in sorted(self._data.values())] - - if self.sortable is False or len(self) < 2: - return [item.__dict__ for item in self._data.values()] - - try: - return [item.__dict__ for item in sorted(self._data.values())] - except TypeError: - self.sortable = False - else: - self.sortable = True - - return self.serialized() + return [item.__dict__ for item in sorted(self._data.values())] def __lt__(self, other: "Model") -> bool: diff --git a/src/python/models/model_item.py b/src/python/models/model_item.py index 002c9bb5..75c64aa7 100644 --- a/src/python/models/model_item.py +++ b/src/python/models/model_item.py @@ -2,15 +2,6 @@ from typing import ClassVar, Optional class ModelItem: - main_key: ClassVar[str] = "" - - def __init_subclass__(cls) -> None: - if not cls.main_key: - raise ValueError("Must specify main_key str class attribute.") - - super().__init_subclass__() - - def __new__(cls, *_args, **_kwargs) -> "ModelItem": from .model import Model cls.parent_model: Optional[Model] = None diff --git a/src/python/models/model_store.py b/src/python/models/model_store.py index 7e702a44..f40721f1 100644 --- a/src/python/models/model_store.py +++ b/src/python/models/model_store.py @@ -25,7 +25,7 @@ class ModelStore(MutableMapping): raise ValueError(f"Empty string in key: {key!r}") key_type = (key[0],) + \ - tuple(type(el) for el in key[1:]) # type: ignore + tuple(type(el) for el in key[1:]) else: key_type = key # type: ignore diff --git a/src/qml/event_handlers.js b/src/qml/event_handlers.js index d2645848..ed9495c0 100644 --- a/src/qml/event_handlers.js +++ b/src/qml/event_handlers.js @@ -20,6 +20,12 @@ function onCoroutineDone(uuid, result) { function onModelUpdated(syncId, data, serializedSyncId) { + if (serializedSyncId == ["Account"] || serializedSyncId[0] == "Room") { + py.callCoro("get_flat_sidepane_data", [], data => { + print( JSON.stringify( data, null, 4)) + }) + } + window.modelSources[serializedSyncId] = data window.modelSourcesChanged() }