Commit 00c1148a authored by oroulet's avatar oroulet Committed by oroulet

instrospect dataclass instead of typing

parent 0d659b0e
...@@ -6,8 +6,8 @@ import struct ...@@ -6,8 +6,8 @@ import struct
import logging import logging
import uuid import uuid
from enum import IntEnum, Enum from enum import IntEnum, Enum
from typing import get_origin, Union, get_type_hints, get_args from typing import get_origin, Union, get_args
from dataclasses import is_dataclass from dataclasses import is_dataclass, fields
from .uaerrors import UaError from .uaerrors import UaError
from ..common.utils import Buffer from ..common.utils import Buffer
...@@ -261,24 +261,20 @@ def struct_to_binary(obj): ...@@ -261,24 +261,20 @@ def struct_to_binary(obj):
member = getattr(obj, name) member = getattr(obj, name)
container_name, idx = switch container_name, idx = switch
if member is not None: if member is not None:
print("container_val before", container_val, idx)
container_val = container_val | 1 << idx container_val = container_val | 1 << idx
print("container_val after", container_val, idx)
print("SETATTR", name, idx, bin(container_val))
setattr(obj, container_name, container_val) setattr(obj, container_name, container_val)
for name, uatype in get_type_hints(obj).items(): for field in fields(obj):
print("STRUCT LEMENT", obj, name, uatype) uatype = field.type
if name == "Encoding": if field.name == "Encoding":
packet.append(Primitives.Byte.pack(obj.Encoding)) packet.append(Primitives.Byte.pack(obj.Encoding))
continue continue
val = getattr(obj, name) val = getattr(obj, field.name)
if _is_union(uatype): if _is_union(uatype):
uatype = _from_union(uatype) uatype = _from_union(uatype)
if _is_list(uatype): if _is_list(uatype):
packet.append(list_to_binary(_from_list(uatype), val)) packet.append(list_to_binary(_from_list(uatype), val))
else: else:
if has_switch and val is None and name in obj.ua_switches: if has_switch and val is None and field.name in obj.ua_switches:
print("SWTICH SAYS TO NOT WRITE")
pass pass
else: else:
packet.append(to_binary(uatype, val)) packet.append(to_binary(uatype, val))
...@@ -289,7 +285,6 @@ def to_binary(uatype, val): ...@@ -289,7 +285,6 @@ def to_binary(uatype, val):
""" """
Pack a python object to binary given a type hint Pack a python object to binary given a type hint
""" """
print("TOBIN", uatype, val)
if _is_list(uatype): if _is_list(uatype):
return list_to_binary(_from_list(uatype), val) return list_to_binary(_from_list(uatype), val)
elif hasattr(Primitives, uatype.__name__): elif hasattr(Primitives, uatype.__name__):
...@@ -525,18 +520,15 @@ def struct_from_binary(objtype, data): ...@@ -525,18 +520,15 @@ def struct_from_binary(objtype, data):
if issubclass(objtype, Enum): if issubclass(objtype, Enum):
return objtype(Primitives.UInt32.unpack(data)) return objtype(Primitives.UInt32.unpack(data))
obj = objtype() obj = objtype()
for name, uatype in get_type_hints(obj).items(): for field in fields(obj):
# if our member has a switch and it is not set we skip it # if our member has a switch and it is not set we skip it
if hasattr(obj, "ua_switches") and name in obj.ua_switches: if hasattr(obj, "ua_switches") and field.name in obj.ua_switches:
container_name, idx = obj.ua_switches[name] container_name, idx = obj.ua_switches[field.name]
val = getattr(obj, container_name) val = getattr(obj, container_name)
print("BIN VAL", val, idx)
if not test_bit(val, idx): if not test_bit(val, idx):
print("NOT", name)
continue continue
val = from_binary(uatype, data) val = from_binary(field.type, data)
print("SETATR", obj, name, val) setattr(obj, field.name, val)
setattr(obj, name, val)
return obj return obj
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment