Skip to content

Commit

Permalink
Apply a decorator on the ParameterWithPath object to make it serializ…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
seb5g committed Nov 5, 2024
1 parent 12e66c4 commit d3e374b
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/pymodaq_gui/parameter/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from __future__ import annotations
from typing import TYPE_CHECKING, List
from typing import TYPE_CHECKING, List, Tuple, Any
from dataclasses import Field, fields
import numpy as np
from collections import OrderedDict
from dataclasses import dataclass
from pymodaq_utils.utils import find_keys_from_val

from pymodaq_data.serialize.factory import SerializableFactory, SerializableBase
from pymodaq_gui.parameter import ioxml
if TYPE_CHECKING:
from pymodaq_gui.parameter import Parameter

ser_factory = SerializableFactory()


class ParameterWithPath:
@SerializableFactory.register_decorator()
class ParameterWithPath(SerializableBase):
""" holds together a Parameter object and its full path
To be used when communicating between TCPIP to reconstruct properly the Parameter
Expand All @@ -22,6 +26,7 @@ class ParameterWithPath:
path: full path of the parameter, if None it is constructed from the parameter parents
"""
def __init__(self, parameter: Parameter, path: List[str] = None):
super().__init__()
self._parameter = parameter
if path is None:
path = get_param_path(parameter)
Expand All @@ -35,6 +40,32 @@ def parameter(self) -> Parameter:
def path(self) -> List[str]:
return self._path

@staticmethod
def serialize(param: 'ParameterWithPath') -> bytes:
"""
"""
bytes_string = b''
path = param.path
param_as_xml = ioxml.parameter_to_xml_string(param.parameter)
bytes_string += ser_factory.get_apply_serializer(path)
bytes_string += ser_factory.get_apply_serializer(param_as_xml)

@classmethod
def deserialize(cls, bytes_str: bytes) -> Tuple[Any, bytes]:
"""Convert bytes into a ParameterWithPath object
Returns
-------
ParameterWithPath: the decoded object
bytes: the remaining bytes string if any
"""
path, remaining_bytes = ser_factory.get_apply_deserializer(bytes_str)
param_as_xml, remaining_bytes = ser_factory.get_apply_deserializer(remaining_bytes)
param_dict = ioxml.XML_string_to_parameter(param_as_xml)
param_obj = Parameter(**param_dict[0])
return ParameterWithPath(param_obj, path), remaining_bytes


def get_widget_from_tree(parameter_tree, widget_instance):
widgets = []
Expand Down

0 comments on commit d3e374b

Please sign in to comment.