Change name of TLVItem to TLV
This commit is contained in:
parent
1f3bc5fcb2
commit
234147f4a8
6 changed files with 46 additions and 46 deletions
|
@ -17,7 +17,7 @@ from ecrterm.packets.base_packets import (
|
|||
Authorisation, Completion, DisplayText, EndOfDay, Packet, PrintLine,
|
||||
Registration, ResetTerminal, StatusEnquiry, StatusInformation)
|
||||
from ecrterm.transmission._transmission import Transmission
|
||||
from ecrterm.packets.tlv import TLVItem
|
||||
from ecrterm.packets.tlv import TLV
|
||||
from ecrterm.packets.types import ConfigByte
|
||||
from ecrterm.transmission.signals import ACK, DLE, ETX, NAK, STX, TRANSMIT_OK
|
||||
from ecrterm.transmission.transport_serial import SerialTransport
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from enum import Enum
|
||||
from typing import Any, Union, List, Optional, Tuple
|
||||
|
||||
from .tlv import TLVItem, TLVDictionary
|
||||
from .tlv import TLV, TLVDictionary
|
||||
from .types import CharacterSet, VendorQuirks
|
||||
from .context import CurrentContext
|
||||
from .text_encoding import encode, decode
|
||||
|
@ -270,25 +270,25 @@ class LLLStringField(LLLVARField, StringField):
|
|||
|
||||
|
||||
class TLVField(Field):
|
||||
DATA_TYPE = TLVItem
|
||||
DATA_TYPE = TLV
|
||||
|
||||
def from_bytes(self, v: Union[bytes, List[int]]) -> TLVItem:
|
||||
return TLVItem(bytes(v))
|
||||
def from_bytes(self, v: Union[bytes, List[int]]) -> TLV:
|
||||
return TLV(bytes(v))
|
||||
|
||||
def to_bytes(self, v: TLVItem, length: Optional[int] = None) -> bytes:
|
||||
def to_bytes(self, v: TLV, length: Optional[int] = None) -> bytes:
|
||||
if length is not None:
|
||||
raise ValueError("Must not give length for TLV container")
|
||||
return v.serialize()
|
||||
|
||||
def parse(self, data: Union[bytes, List[int]]) -> Tuple[TLVItem, bytes]:
|
||||
return TLVItem.parse(data, empty_tag=True, dictionary='feig_zvt' if VendorQuirks.FEIG_CVEND in CurrentContext.get('vendor_quirks', set()) else 'zvt')
|
||||
def parse(self, data: Union[bytes, List[int]]) -> Tuple[TLV, bytes]:
|
||||
return TLV.parse(data, empty_tag=True, dictionary='feig_zvt' if VendorQuirks.FEIG_CVEND in CurrentContext.get('vendor_quirks', set()) else 'zvt')
|
||||
|
||||
def serialize(self, data: TLVItem) -> bytes:
|
||||
def serialize(self, data: TLV) -> bytes:
|
||||
return data.serialize()
|
||||
|
||||
def __get__(self, instance, objtype=None) -> TLVItem:
|
||||
def __get__(self, instance, objtype=None) -> TLV:
|
||||
if not self in instance._values:
|
||||
instance._values[self] = TLVItem()
|
||||
instance._values[self] = TLV()
|
||||
instance._values[self].pending = True
|
||||
return super().__get__(instance, objtype)
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ class TLVClass(IntEnum):
|
|||
PRIVATE = 3
|
||||
|
||||
|
||||
TLVItemType = TypeVar('TLVItemType', bound='TLVItem')
|
||||
TLVType = TypeVar('TLVType', bound='TLV')
|
||||
|
||||
|
||||
class NotProvided:
|
||||
|
@ -23,10 +23,10 @@ class NotProvided:
|
|||
NOT_PROVIDED = NotProvided()
|
||||
|
||||
|
||||
_FIRST_PARAM_TYPE = Union[NotProvided, TLVItemType, List[TLVItemType], Dict[Union[int, str], Any]]
|
||||
_FIRST_PARAM_TYPE = Union[NotProvided, TLVType, List[TLVType], Dict[Union[int, str], Any]]
|
||||
|
||||
|
||||
class TLVItem:
|
||||
class TLV:
|
||||
# <editor-fold desc="static T/L helpers">
|
||||
@staticmethod
|
||||
def _read_tlv_tag(data: bytes, pos: int) -> Tuple[int, int]:
|
||||
|
@ -76,13 +76,13 @@ class TLVItem:
|
|||
return bytes(reversed(retval))
|
||||
# </editor-fold>
|
||||
|
||||
def __new__(cls: Type[TLVItemType], constructed_value_: _FIRST_PARAM_TYPE = NOT_PROVIDED, *args, **kwargs):
|
||||
if isinstance(constructed_value_, TLVItem):
|
||||
def __new__(cls: Type[TLVType], constructed_value_: _FIRST_PARAM_TYPE = NOT_PROVIDED, *args, **kwargs):
|
||||
if isinstance(constructed_value_, TLV):
|
||||
return constructed_value_
|
||||
return super().__new__(cls)
|
||||
|
||||
def __init__(self, constructed_value_: _FIRST_PARAM_TYPE =NOT_PROVIDED, tag_=None, value_=NOT_PROVIDED, implicit_=False, **kwargs):
|
||||
if isinstance(constructed_value_, TLVItem):
|
||||
if isinstance(constructed_value_, TLV):
|
||||
return # __new__ handled this
|
||||
|
||||
self._constructed = False
|
||||
|
@ -168,7 +168,7 @@ class TLVItem:
|
|||
if isinstance(value, (tuple,list)):
|
||||
self._value = []
|
||||
for item in value:
|
||||
if isinstance(item, TLVItem):
|
||||
if isinstance(item, TLV):
|
||||
self._value.append(item)
|
||||
elif isinstance(item, (tuple, list)) and len(item) == 2:
|
||||
k, v = item
|
||||
|
@ -186,7 +186,7 @@ class TLVItem:
|
|||
elif isinstance(value, bytes):
|
||||
self._value = []
|
||||
while len(value):
|
||||
item, value = TLVItem.parse(value)
|
||||
item, value = TLV.parse(value)
|
||||
self._value.append(item)
|
||||
else:
|
||||
if self._type:
|
||||
|
@ -206,7 +206,7 @@ class TLVItem:
|
|||
return item.value_
|
||||
|
||||
# Generate an implicit empty tag
|
||||
target = TLVItem(tag_=tag, implicit_=True)
|
||||
target = TLV(tag_=tag, implicit_=True)
|
||||
self.value_.append(target)
|
||||
|
||||
return self.__getattr__(key)
|
||||
|
@ -234,7 +234,7 @@ class TLVItem:
|
|||
target.value_ = value
|
||||
break
|
||||
else:
|
||||
target = TLVItem(tag_=tag, value_=value)
|
||||
target = TLV(tag_=tag, value_=value)
|
||||
self.value_.append(target)
|
||||
|
||||
def __repr__(self):
|
||||
|
@ -262,7 +262,7 @@ class TLVItem:
|
|||
@property
|
||||
def items_(self):
|
||||
if not self._constructed:
|
||||
raise TypeError("Cannot access items_ of primitive TLVItem")
|
||||
raise TypeError("Cannot access items_ of primitive TLV")
|
||||
retval = []
|
||||
for v in self.value_:
|
||||
if v._type and v._type.name:
|
||||
|
@ -287,7 +287,7 @@ class TLVItem:
|
|||
return bytes(retval)
|
||||
|
||||
@classmethod
|
||||
def parse(cls: Type[TLVItemType], data: bytes, empty_tag: bool = False, dictionary: Optional[str]=None) -> Tuple[TLVItemType, bytes]:
|
||||
def parse(cls: Type[TLVType], data: bytes, empty_tag: bool = False, dictionary: Optional[str]=None) -> Tuple[TLVType, bytes]:
|
||||
pos = 0
|
||||
|
||||
if empty_tag:
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from ecrterm.packets.apdu import CommandAPDU, ParseError
|
||||
from ecrterm.packets.tlv import TLVItem
|
||||
from ecrterm.packets.tlv import TLV
|
||||
from ecrterm.packets.fields import ByteField, BytesField, BCDIntField
|
||||
from ecrterm.packets.base_packets import LogOff, Initialisation, Registration, DisplayText, Completion, PrintLine, Authorisation
|
||||
from unittest import TestCase, main
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from unittest import TestCase, main
|
||||
|
||||
from ecrterm.packets.fields import *
|
||||
from ecrterm.packets.tlv import TLVItem
|
||||
from ecrterm.packets.tlv import TLV
|
||||
|
||||
|
||||
class TestFields(TestCase):
|
||||
|
@ -113,16 +113,16 @@ class TestFields(TestCase):
|
|||
tf = TLVField()
|
||||
|
||||
c, d = tf.parse(b'\x00')
|
||||
self.assertIsInstance(c, TLVItem)
|
||||
self.assertIsInstance(c, TLV)
|
||||
self.assertEqual(d, b'')
|
||||
|
||||
self.assertEqual(tf.serialize(TLVItem(tag_=None, value_=[])), b'\x00')
|
||||
self.assertEqual(tf.serialize(TLV(tag_=None, value_=[])), b'\x00')
|
||||
|
||||
# FIXME With more tags
|
||||
|
||||
def test_coercion(self):
|
||||
self.assertIsInstance(TLVField().coerce([]), TLVItem)
|
||||
self.assertIsInstance(TLVField().coerce(TLVItem(tag_=None, value_=[])).value_, list)
|
||||
self.assertIsInstance(TLVField().coerce([]), TLV)
|
||||
self.assertIsInstance(TLVField().coerce(TLV(tag_=None, value_=[])).value_, list)
|
||||
self.assertIsInstance(IntField().coerce('3'), int)
|
||||
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ from ecrterm.packets.tlv import *
|
|||
|
||||
class TestTLV(TestCase):
|
||||
def test_simple(self):
|
||||
t, r = TLVItem.parse(b'\x01\x02\x03\x04')
|
||||
t, r = TLV.parse(b'\x01\x02\x03\x04')
|
||||
|
||||
self.assertEqual(b'', r)
|
||||
|
||||
|
@ -16,14 +16,14 @@ class TestTLV(TestCase):
|
|||
self.assertEqual(bytearray.fromhex('01020304'), t.serialize())
|
||||
|
||||
def test_container(self):
|
||||
c, r = TLVItem.parse(b'\x04\x01\x02\x03\x04', empty_tag=True)
|
||||
c, r = TLV.parse(b'\x04\x01\x02\x03\x04', empty_tag=True)
|
||||
|
||||
self.assertEqual(b'', r)
|
||||
self.assertEqual(b'\x03\x04', c.x1)
|
||||
|
||||
def test_construction(self):
|
||||
t = TLVItem(tag_=0x20, value_=[
|
||||
TLVItem(tag_=0x1e, value_=b'012')
|
||||
t = TLV(tag_=0x20, value_=[
|
||||
TLV(tag_=0x1e, value_=b'012')
|
||||
])
|
||||
|
||||
self.assertEqual(t.x1e, b'012')
|
||||
|
@ -31,39 +31,39 @@ class TestTLV(TestCase):
|
|||
self.assertEqual(t.serialize(), bytearray.fromhex('20051e03303132'))
|
||||
|
||||
def test_container_easy_1(self):
|
||||
t1 = TLVItem({'xde': b'012'})
|
||||
t1 = TLV({'xde': b'012'})
|
||||
|
||||
self.assertEqual(1, len(t1.value_))
|
||||
self.assertEqual(b'012', t1.xde)
|
||||
|
||||
t2 = TLVItem({0xde: b'012'})
|
||||
t2 = TLV({0xde: b'012'})
|
||||
|
||||
self.assertEqual(1, len(t2.value_))
|
||||
self.assertEqual(b'012', t2.xde)
|
||||
|
||||
def test_container_easy_2(self):
|
||||
t1 = TLVItem(xde=b'012')
|
||||
t1 = TLV(xde=b'012')
|
||||
|
||||
self.assertEqual(1, len(t1.value_))
|
||||
self.assertEqual(b'012', t1.xde)
|
||||
|
||||
def test_container_easy_3(self):
|
||||
t1 = TLVItem(xfe={'xde': b'23'})
|
||||
t1 = TLV(xfe={'xde': b'23'})
|
||||
|
||||
self.assertIsInstance(t1.xfe, TLVItem)
|
||||
self.assertIsInstance(t1.xfe, TLV)
|
||||
self.assertEqual(b'23', t1.xfe.xde)
|
||||
|
||||
def test_container_easy_4(self):
|
||||
t1 = TLVItem()
|
||||
t1 = TLV()
|
||||
t1.xfe.xde = b'34'
|
||||
|
||||
self.assertIsInstance(t1.xfe, TLVItem)
|
||||
self.assertIsInstance(t1.xfe, TLV)
|
||||
self.assertEqual(b'34', t1.xfe.xde)
|
||||
|
||||
def test_container_implicit_creation(self):
|
||||
t1 = TLVItem()
|
||||
t1 = TLV()
|
||||
|
||||
self.assertIsInstance(t1.xfe, TLVItem)
|
||||
self.assertIsInstance(t1.xfe, TLV)
|
||||
# An implicit item is created, but since we are not accessing its value, it is not fully realized
|
||||
self.assertEqual(b'\x00', t1.serialize())
|
||||
|
||||
|
@ -72,18 +72,18 @@ class TestTLV(TestCase):
|
|||
self.assertEqual(b'\x02\xfe\x00', t1.serialize())
|
||||
|
||||
# This works deep down
|
||||
t2 = TLVItem()
|
||||
t2 = TLV()
|
||||
|
||||
self.assertIsNone(t2.xfe.xfe.xfe.xfe.xfe.xfe.xde)
|
||||
self.assertIsInstance(t2.xfe.xfe.xfe.xfe.xfe.xfe, TLVItem)
|
||||
self.assertIsInstance(t2.xfe.xfe.xfe.xfe.xfe.xfe, TLV)
|
||||
self.assertEqual(b'\x00', t2.serialize())
|
||||
|
||||
t2.xfe.xfe.xfe.xfe.xfe.xfe.xde = b''
|
||||
self.assertEqual(b'\x0e\xfe\x0c\xfe\x0a\xfe\x08\xfe\x06\xfe\x04\xfe\x02\xde\x00', t2.serialize())
|
||||
|
||||
def test_null_coercion(self):
|
||||
a = TLVItem()
|
||||
b = TLVItem(a)
|
||||
a = TLV()
|
||||
b = TLV(a)
|
||||
|
||||
self.assertIs(a, b)
|
||||
|
||||
|
|
Loading…
Reference in a new issue