updates proxy class usage

- ProxyClass class is inheriting from DeviceConnection and is only used for topmost proxy
- classes of nested proxy objects are dynamically created to keep their component types
- adds _initialise method to ProxyClassMixin as I cannot pass sio_client and loop to each
component_class (initialising a class with multiple base classes will pass the arguments passed to
the constructor to each initialiser function)
This commit is contained in:
Mose Müller 2024-04-04 16:19:29 +02:00
parent 473c6660e6
commit ad0fd8e833
2 changed files with 38 additions and 25 deletions

View File

@ -5,7 +5,8 @@ from typing import TypedDict, cast
import socketio # type: ignore
from pydase.client.proxy_loader import ProxyClass, ProxyLoader
import pydase.components
from pydase.client.proxy_loader import ProxyClassMixin, ProxyLoader
from pydase.utils.serialization.deserializer import loads
from pydase.utils.serialization.types import SerializedDataService, SerializedObject
@ -26,6 +27,15 @@ def asyncio_loop_thread(loop: asyncio.AbstractEventLoop) -> None:
loop.run_forever()
class ProxyClass(ProxyClassMixin, pydase.components.DeviceConnection):
def __init__(
self, sio_client: socketio.AsyncClient, loop: asyncio.AbstractEventLoop
) -> None:
ProxyClassMixin.__init__(self)
pydase.components.DeviceConnection.__init__(self)
self._initialise(sio_client=sio_client, loop=loop)
class Client:
def __init__(self, hostname: str = "localhost", port: int = 8001):
self._hostname = hostname

View File

@ -5,9 +5,7 @@ from typing import TYPE_CHECKING, Any, cast
import socketio # type: ignore
import pydase.components
import pydase.data_service
from pydase.utils.serialization.deserializer import loads
from pydase.utils.serialization.deserializer import Deserializer, loads
from pydase.utils.serialization.serializer import dump
from pydase.utils.serialization.types import SerializedObject
@ -56,14 +54,18 @@ class ProxyList(list[Any]):
class ProxyClassMixin:
def __init__(
def __init__(self) -> None:
self._proxy_getters: dict[str, Callable[..., Any]] = {}
self._proxy_setters: dict[str, Callable[..., Any]] = {}
self._proxy_methods: dict[str, Callable[..., Any]] = {}
# declare before DataService init to avoid warning messaged
self._observers: dict[str, Any] = {}
def _initialise(
self,
sio_client: socketio.AsyncClient,
loop: asyncio.AbstractEventLoop,
) -> None:
self._proxy_getters: dict[str, Callable[..., Any]] = {}
self._proxy_setters: dict[str, Callable[..., Any]] = {}
self._proxy_methods: dict[str, Callable[..., Any]] = {}
self._loop = loop
self._sio = sio_client
@ -189,19 +191,6 @@ class ProxyClassMixin:
self._proxy_getters[attr_name] = getter_proxy
class ProxyClass(pydase.data_service.DataService, ProxyClassMixin):
def __init__(
self,
sio_client: socketio.AsyncClient,
loop: asyncio.AbstractEventLoop,
) -> None:
# declare before ProxyClassMixin init to avoid warning messaged
self._observers = {}
ProxyClassMixin.__init__(self, sio_client=sio_client, loop=loop)
pydase.DataService.__init__(self)
class ProxyLoader:
@staticmethod
def load_list_proxy(
@ -267,11 +256,25 @@ class ProxyLoader:
sio_client: socketio.AsyncClient,
loop: asyncio.AbstractEventLoop,
) -> Any:
proxy_class = ProxyClass(sio_client=sio_client, loop=loop)
ProxyLoader.update_data_service_proxy(
proxy_class=proxy_class, serialized_object=serialized_object
# Custom types like Components or DataService classes
component_class = cast(
type, Deserializer.get_component_class(serialized_object["type"])
)
return proxy_class
class_bases = (
ProxyClassMixin,
component_class,
)
proxy_base_class: type[ProxyClassMixin] = type(
serialized_object["name"], # type: ignore
class_bases,
{},
)
proxy_class_instance = proxy_base_class()
proxy_class_instance._initialise(sio_client=sio_client, loop=loop)
ProxyLoader.update_data_service_proxy(
proxy_class=proxy_class_instance, serialized_object=serialized_object
)
return proxy_class_instance
@staticmethod
def load_default(