working enum
This commit is contained in:
+35
-3
@@ -169,15 +169,17 @@ def pwp(cls):
|
||||
return output
|
||||
|
||||
@classmethod
|
||||
def from_com(data):
|
||||
def from_com(cls, data):
|
||||
values = {}
|
||||
offset = 0
|
||||
|
||||
obj = cls.__new__(cls)
|
||||
|
||||
for name, typ in fields.items():
|
||||
value, offset = decode_value(data, offset, typ)
|
||||
values[name] = value
|
||||
setattr(obj, name, value)
|
||||
|
||||
return cls(**values)
|
||||
return obj
|
||||
|
||||
cls.to_com = to_com
|
||||
cls.from_com = from_com
|
||||
@@ -185,6 +187,8 @@ def pwp(cls):
|
||||
return cls
|
||||
|
||||
def pwp_enum(cls):
|
||||
variants = {}
|
||||
|
||||
for index, (name, value) in enumerate(cls.__dict__.items()):
|
||||
if isinstance(value, type) and value.__module__ == cls.__module__:
|
||||
value.__annotations__ = {
|
||||
@@ -196,6 +200,34 @@ def pwp_enum(cls):
|
||||
|
||||
value = pwp(value)
|
||||
|
||||
variants[index] = value
|
||||
setattr(cls, name, value)
|
||||
|
||||
@classmethod
|
||||
def from_com(enum_cls, data):
|
||||
enum_id = data[0]
|
||||
|
||||
if enum_id not in variants:
|
||||
raise PWPError(f"Unknown enum id: {enum_id}")
|
||||
|
||||
variant = variants[enum_id]
|
||||
|
||||
# Decode the rest of the data
|
||||
obj = variant.__new__(variant)
|
||||
|
||||
offset = 1 # skip enum id byte
|
||||
|
||||
fields = get_type_hints(variant)
|
||||
|
||||
for name, typ in fields.items():
|
||||
if name == "_id":
|
||||
continue
|
||||
|
||||
value, offset = decode_value(data, offset, typ)
|
||||
setattr(obj, name, value)
|
||||
|
||||
return obj
|
||||
|
||||
cls.from_com = from_com
|
||||
|
||||
return cls
|
||||
Reference in New Issue
Block a user