mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-01-01 03:21:19 +01:00
fix(serializer): remove deprecated serializer
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
from typing import Type
|
||||
|
||||
from bec_lib.codecs import BECCodec
|
||||
from bec_lib.serialization import msgpack
|
||||
from qtpy.QtCore import QPointF
|
||||
|
||||
@@ -6,39 +9,26 @@ def register_serializer_extension():
|
||||
"""
|
||||
Register the serializer extension for the BECConnector.
|
||||
"""
|
||||
if not module_is_registered("bec_widgets.utils.serialization"):
|
||||
msgpack.register_object_hook(encode_qpointf, decode_qpointf)
|
||||
if not msgpack.is_registered(QPointF):
|
||||
msgpack.register_codec(QPointFEncoder)
|
||||
|
||||
|
||||
def module_is_registered(module_name: str) -> bool:
|
||||
"""
|
||||
Check if the module is registered in the encoder.
|
||||
class QPointFEncoder(BECCodec):
|
||||
obj_type: Type = QPointF
|
||||
|
||||
Args:
|
||||
module_name (str): The name of the module to check.
|
||||
@staticmethod
|
||||
def encode(obj: QPointF) -> str:
|
||||
"""
|
||||
Encode a QPointF object to a list of floats. As this is mostly used for sending
|
||||
data to the client, it is not necessary to convert it back to a QPointF object.
|
||||
"""
|
||||
if isinstance(obj, QPointF):
|
||||
return [obj.x(), obj.y()]
|
||||
return obj
|
||||
|
||||
Returns:
|
||||
bool: True if the module is registered, False otherwise.
|
||||
"""
|
||||
# pylint: disable=protected-access
|
||||
for enc in msgpack._encoder:
|
||||
if enc[0].__module__ == module_name:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def encode_qpointf(obj):
|
||||
"""
|
||||
Encode a QPointF object to a list of floats. As this is mostly used for sending
|
||||
data to the client, it is not necessary to convert it back to a QPointF object.
|
||||
"""
|
||||
if isinstance(obj, QPointF):
|
||||
return [obj.x(), obj.y()]
|
||||
return obj
|
||||
|
||||
|
||||
def decode_qpointf(obj):
|
||||
"""
|
||||
no-op function since QPointF is encoded as a list of floats.
|
||||
"""
|
||||
return obj
|
||||
@staticmethod
|
||||
def decode(type_name: str, data: list[float]) -> list[float]:
|
||||
"""
|
||||
no-op function since QPointF is encoded as a list of floats.
|
||||
"""
|
||||
return data
|
||||
|
||||
@@ -21,7 +21,6 @@ def test_multiple_extension_registration():
|
||||
"""
|
||||
Test that multiple extension registrations do not cause issues.
|
||||
"""
|
||||
assert serialization.module_is_registered("bec_widgets.utils.serialization")
|
||||
assert msgpack.is_registered(QPointF)
|
||||
serialization.register_serializer_extension()
|
||||
assert serialization.module_is_registered("bec_widgets.utils.serialization")
|
||||
assert len(msgpack._encoder) == len(set(msgpack._encoder))
|
||||
assert msgpack.is_registered(QPointF)
|
||||
|
||||
Reference in New Issue
Block a user