1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| """python STRUCT like class for encoding / decoding structs with minimal boiler plate code """
from io import BytesIO
from functools import lru_cache
from collections.abc import Iterable
from dataclasses import Field, dataclass, fields
from typing import get_type_hints
from ctypes import sizeof
@dataclass
class Struct:
"""Struct class for encoding/decoding between python types"""
def __setattr__(self, name, value):
"""generic assignment converter for ctypes"""
_type = get_type_hints(type(self))[name] # dictionary lookup
super().__setattr__(name, value if isinstance(value, _type) else (_type(*value) if isinstance(value, Iterable) else _type(value)))
def to_bytes(self, endian=-1):
"""convert Struct to bytes"""
_values = []
for field in fields(self):
# don't reorder strings
order = 1 if "c_char_Array" in field.type.__name__ else endian
_values.append(bytearray(getattr(self, field.name))[::order])
return b''.join(_values)
@classmethod
def from_bytes(cls, data, endian=-1):
"""convert bytes to Struct"""
if len(data) != cls.size:
raise ValueError(f"invalid bytes size: class: {cls} input: {len(data)} expected: {cls.size}")
kwargs = {}
with BytesIO(data) as stream:
for field in fields(cls):
"""don't swap the order of character arrays"""
order = 1 if "c_char_Array" in field.type.__name__ else endian
kwargs[field.name] = field.type.from_buffer_copy(stream.read(sizeof(field.type))[::order])
return cls(**kwargs)
@classmethod
def default(cls):
"""assumes ctype default constructor exists"""
return cls(**{field.name: field.type() for field in fields(cls)})
@classmethod
@property
@lru_cache(maxsize=None)
def size(cls) -> int:
"""size in bytes"""
return sum([sizeof(field.type) for field in fields(cls)])
def fields(self) -> tuple[Field, ...]:
"""fields"""
return fields(self)
def __str__(self) -> str:
"""formated string representation"""
data = "\n ".join([f"{field.name} : {getattr(self, field.name)}" for field in fields(self)])
return f"{type(self).__name__}\n {data}\n"
def line_log(self) -> str:
return ", ".join([f"{getattr(self, field.name).value}" for field in fields(self)])
|