Prep client gen for header parsing - templates

This commit is contained in:
zvecr 2022-07-18 00:06:32 +01:00
parent 7bce3d7b25
commit 63c8e0c8a2
2 changed files with 14 additions and 26 deletions

View File

@ -2,35 +2,23 @@ from collections import namedtuple
from enum import IntFlag, IntEnum
from struct import Struct
class XAPRequest(namedtuple('XAPRequest', 'token length data')):
fmt = Struct('<HB61s')
{% set type_definitions = [{'name':'XAPRequest', 'members': 'token length data', 'fmt':'<HB61s'}, {'name':'XAPResponse', 'members': 'token flags length data', 'fmt':'<HBB60s'}] %}
{% for item in type_definitions -%}
class {{ item.name }}(namedtuple('{{ item.name }}', '{{ item.members }}')):
fmt = Struct('{{ item.fmt }}')
def __new__(cls, *args):
return super().__new__(cls, *args)
@staticmethod
def from_bytes(data):
return XAPRequest._make(XAPRequest.fmt.unpack(data))
def to_bytes(self):
return self.fmt.pack(*list(self))
class XAPResponse(namedtuple('XAPResponse', 'token flags length data')):
fmt = Struct('<HBB60s')
def __new__(cls, *args):
return super().__new__(cls, *args)
@staticmethod
def from_bytes(data):
return XAPResponse._make(XAPResponse.fmt.unpack(data))
@classmethod
def from_bytes(cls, data):
return cls._make(cls.fmt.unpack(data))
def to_bytes(self):
return self.fmt.pack(*list(self))
{% endfor -%}
class XAPSecureStatus(IntEnum):
LOCKED = 0x00
UNLOCKING = 0x01

View File

@ -37,9 +37,9 @@ class XAPRequest(namedtuple('XAPRequest', 'token length data')):
def __new__(cls, *args):
return super().__new__(cls, *args)
@staticmethod
def from_bytes(data):
return XAPRequest._make(XAPRequest.fmt.unpack(data))
@classmethod
def from_bytes(cls, data):
return cls._make(cls.fmt.unpack(data))
def to_bytes(self):
return self.fmt.pack(*list(self))
@ -51,9 +51,9 @@ class XAPResponse(namedtuple('XAPResponse', 'token flags length data')):
def __new__(cls, *args):
return super().__new__(cls, *args)
@staticmethod
def from_bytes(data):
return XAPResponse._make(XAPResponse.fmt.unpack(data))
@classmethod
def from_bytes(cls, data):
return cls._make(cls.fmt.unpack(data))
def to_bytes(self):
return self.fmt.pack(*list(self))