From 7b5f03ef3c6e0d65c5066bf72e276f2e043a77a8 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 28 Jul 2026 05:09:04 +0200 Subject: [PATCH] working enum --- src/pwp.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/pwp.py b/src/pwp.py index 3305468..e889321 100644 --- a/src/pwp.py +++ b/src/pwp.py @@ -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 \ No newline at end of file