Browse Source

Prep client gen for header parsing - templates

zvecr 4 years ago
parent
commit
63c8e0c8a2
2 changed files with 14 additions and 26 deletions
  1. 8 20
      data/templates/xap/client/python/types.py.j2
  2. 6 6
      lib/python/xap_client/types.py

+ 8 - 20
data/templates/xap/client/python/types.py.j2

@@ -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')
-
-    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')
+{% 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 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

+ 6 - 6
lib/python/xap_client/types.py

@@ -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))