mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-21 00:40:01 +02:00
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:
parent
473c6660e6
commit
ad0fd8e833
@ -5,7 +5,8 @@ from typing import TypedDict, cast
|
|||||||
|
|
||||||
import socketio # type: ignore
|
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.deserializer import loads
|
||||||
from pydase.utils.serialization.types import SerializedDataService, SerializedObject
|
from pydase.utils.serialization.types import SerializedDataService, SerializedObject
|
||||||
|
|
||||||
@ -26,6 +27,15 @@ def asyncio_loop_thread(loop: asyncio.AbstractEventLoop) -> None:
|
|||||||
loop.run_forever()
|
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:
|
class Client:
|
||||||
def __init__(self, hostname: str = "localhost", port: int = 8001):
|
def __init__(self, hostname: str = "localhost", port: int = 8001):
|
||||||
self._hostname = hostname
|
self._hostname = hostname
|
||||||
|
@ -5,9 +5,7 @@ from typing import TYPE_CHECKING, Any, cast
|
|||||||
|
|
||||||
import socketio # type: ignore
|
import socketio # type: ignore
|
||||||
|
|
||||||
import pydase.components
|
from pydase.utils.serialization.deserializer import Deserializer, loads
|
||||||
import pydase.data_service
|
|
||||||
from pydase.utils.serialization.deserializer import loads
|
|
||||||
from pydase.utils.serialization.serializer import dump
|
from pydase.utils.serialization.serializer import dump
|
||||||
from pydase.utils.serialization.types import SerializedObject
|
from pydase.utils.serialization.types import SerializedObject
|
||||||
|
|
||||||
@ -56,14 +54,18 @@ class ProxyList(list[Any]):
|
|||||||
|
|
||||||
|
|
||||||
class ProxyClassMixin:
|
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,
|
self,
|
||||||
sio_client: socketio.AsyncClient,
|
sio_client: socketio.AsyncClient,
|
||||||
loop: asyncio.AbstractEventLoop,
|
loop: asyncio.AbstractEventLoop,
|
||||||
) -> None:
|
) -> 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._loop = loop
|
||||||
self._sio = sio_client
|
self._sio = sio_client
|
||||||
|
|
||||||
@ -189,19 +191,6 @@ class ProxyClassMixin:
|
|||||||
self._proxy_getters[attr_name] = getter_proxy
|
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:
|
class ProxyLoader:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_list_proxy(
|
def load_list_proxy(
|
||||||
@ -267,11 +256,25 @@ class ProxyLoader:
|
|||||||
sio_client: socketio.AsyncClient,
|
sio_client: socketio.AsyncClient,
|
||||||
loop: asyncio.AbstractEventLoop,
|
loop: asyncio.AbstractEventLoop,
|
||||||
) -> Any:
|
) -> Any:
|
||||||
proxy_class = ProxyClass(sio_client=sio_client, loop=loop)
|
# Custom types like Components or DataService classes
|
||||||
ProxyLoader.update_data_service_proxy(
|
component_class = cast(
|
||||||
proxy_class=proxy_class, serialized_object=serialized_object
|
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
|
@staticmethod
|
||||||
def load_default(
|
def load_default(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user