working enum

This commit is contained in:
2026-07-28 05:09:04 +02:00
parent 9ff3ef4521
commit 7b5f03ef3c
+35 -3
View File
@@ -169,15 +169,17 @@ def pwp(cls):
return output return output
@classmethod @classmethod
def from_com(data): def from_com(cls, data):
values = {} values = {}
offset = 0 offset = 0
obj = cls.__new__(cls)
for name, typ in fields.items(): for name, typ in fields.items():
value, offset = decode_value(data, offset, typ) 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.to_com = to_com
cls.from_com = from_com cls.from_com = from_com
@@ -185,6 +187,8 @@ def pwp(cls):
return cls return cls
def pwp_enum(cls): def pwp_enum(cls):
variants = {}
for index, (name, value) in enumerate(cls.__dict__.items()): for index, (name, value) in enumerate(cls.__dict__.items()):
if isinstance(value, type) and value.__module__ == cls.__module__: if isinstance(value, type) and value.__module__ == cls.__module__:
value.__annotations__ = { value.__annotations__ = {
@@ -196,6 +200,34 @@ def pwp_enum(cls):
value = pwp(value) value = pwp(value)
variants[index] = value
setattr(cls, name, 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 return cls