From dfa64d16cb61f7820b2ce021ba0925a20de7e157 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Fri, 30 Sep 2016 12:25:48 -0400 Subject: [PATCH] Moved tags into their own module --- serial/serial/binary.py | 85 +++++++++++++++++------------------------ serial/serial/tag.py | 16 ++++++++ serial/test_binary.py | 54 +++++++++++++------------- 3 files changed, 78 insertions(+), 77 deletions(-) create mode 100644 serial/serial/tag.py diff --git a/serial/serial/binary.py b/serial/serial/binary.py index fabcd47..55fe8c3 100644 --- a/serial/serial/binary.py +++ b/serial/serial/binary.py @@ -3,22 +3,7 @@ import functools import io import struct -TAG_NULL = 0x00 -TAG_TRUE = 0x01 -TAG_FALSE = 0x02 -TAG_UINT8 = 0x03 -TAG_UINT16 = 0x04 -TAG_UINT32 = 0x05 -TAG_UINT64 = 0x06 -TAG_INT8 = 0x10 -TAG_INT16 = 0x11 -TAG_INT32 = 0x12 -TAG_INT64 = 0x13 -TAG_BINARY = 0x20 -TAG_UTF8 = 0x21 -TAG_UTF16 = 0x22 -TAG_UTF32 = 0x23 -TAG_TUPLE = 0x30 +from . import tag TaggedObject = collections.namedtuple( 'TaggedObject', @@ -62,26 +47,26 @@ def _serialize_tuple(to): fmt = '!BI' - return struct.pack('!BI', TAG_TUPLE, len(payload)) + payload + return struct.pack('!BI', tag.TUPLE, len(payload)) + payload _TAGS_TO_SERIALIZERS = { - TAG_NULL: _make_tag_only_serializer(TAG_NULL, None), - TAG_TRUE: _make_tag_only_serializer(TAG_TRUE, True), - TAG_FALSE: _make_tag_only_serializer(TAG_FALSE, False), - TAG_UINT8: _make_struct_serializer('B'), - TAG_UINT16: _make_struct_serializer('H'), - TAG_UINT32: _make_struct_serializer('I'), - TAG_UINT64: _make_struct_serializer('Q'), - TAG_INT8: _make_struct_serializer('b'), - TAG_INT16: _make_struct_serializer('h'), - TAG_INT32: _make_struct_serializer('i'), - TAG_INT64: _make_struct_serializer('q'), - TAG_BINARY: _make_string_serializer(lambda s: s), - TAG_UTF8: _make_string_serializer(lambda s: s.encode('utf-8')), - TAG_UTF16: _make_string_serializer(lambda s: s.encode('utf-16')), - TAG_UTF32: _make_string_serializer(lambda s: s.encode('utf-32')), - TAG_TUPLE: _serialize_tuple, + tag.NULL: _make_tag_only_serializer(tag.NULL, None), + tag.TRUE: _make_tag_only_serializer(tag.TRUE, True), + tag.FALSE: _make_tag_only_serializer(tag.FALSE, False), + tag.UINT8: _make_struct_serializer('B'), + tag.UINT16: _make_struct_serializer('H'), + tag.UINT32: _make_struct_serializer('I'), + tag.UINT64: _make_struct_serializer('Q'), + tag.INT8: _make_struct_serializer('b'), + tag.INT16: _make_struct_serializer('h'), + tag.INT32: _make_struct_serializer('i'), + tag.INT64: _make_struct_serializer('q'), + tag.BINARY: _make_string_serializer(lambda s: s), + tag.UTF8: _make_string_serializer(lambda s: s.encode('utf-8')), + tag.UTF16: _make_string_serializer(lambda s: s.encode('utf-16')), + tag.UTF32: _make_string_serializer(lambda s: s.encode('utf-32')), + tag.TUPLE: _serialize_tuple, } def serialize(to): @@ -141,25 +126,25 @@ def _deserialize_tuple(b): total_bytes_read += partial_bytes_read instance.append(item) - return bytes_read, TaggedObject(tag = TAG_TUPLE, instance = tuple(instance)) + return bytes_read, TaggedObject(tag = tag.TUPLE, instance = tuple(instance)) _TAGS_TO_PARSERS = { - TAG_NULL: _make_tag_only_parser(TAG_NULL, None), - TAG_TRUE: _make_tag_only_parser(TAG_TRUE, True), - TAG_FALSE: _make_tag_only_parser(TAG_FALSE, False), - TAG_UINT8: _make_struct_deserializer(TAG_UINT8, 'B'), - TAG_UINT16: _make_struct_deserializer(TAG_UINT16, 'H'), - TAG_UINT32: _make_struct_deserializer(TAG_UINT32, 'I'), - TAG_UINT64: _make_struct_deserializer(TAG_UINT64, 'Q'), - TAG_INT8: _make_struct_deserializer(TAG_INT8, 'b'), - TAG_INT16: _make_struct_deserializer(TAG_INT16, 'h'), - TAG_INT32: _make_struct_deserializer(TAG_INT32, 'i'), - TAG_INT64: _make_struct_deserializer(TAG_INT64, 'q'), - TAG_BINARY: _make_string_deserializer(TAG_BINARY, lambda b: b), - TAG_UTF8: _make_string_deserializer(TAG_UTF8, lambda b: b.decode('utf-8')), - TAG_UTF16: _make_string_deserializer(TAG_UTF16, lambda b: b.decode('utf-16')), - TAG_UTF32: _make_string_deserializer(TAG_UTF32, lambda b: b.decode('utf-32')), - TAG_TUPLE: _deserialize_tuple, + tag.NULL: _make_tag_only_parser(tag.NULL, None), + tag.TRUE: _make_tag_only_parser(tag.TRUE, True), + tag.FALSE: _make_tag_only_parser(tag.FALSE, False), + tag.UINT8: _make_struct_deserializer(tag.UINT8, 'B'), + tag.UINT16: _make_struct_deserializer(tag.UINT16, 'H'), + tag.UINT32: _make_struct_deserializer(tag.UINT32, 'I'), + tag.UINT64: _make_struct_deserializer(tag.UINT64, 'Q'), + tag.INT8: _make_struct_deserializer(tag.INT8, 'b'), + tag.INT16: _make_struct_deserializer(tag.INT16, 'h'), + tag.INT32: _make_struct_deserializer(tag.INT32, 'i'), + tag.INT64: _make_struct_deserializer(tag.INT64, 'q'), + tag.BINARY: _make_string_deserializer(tag.BINARY, lambda b: b), + tag.UTF8: _make_string_deserializer(tag.UTF8, lambda b: b.decode('utf-8')), + tag.UTF16: _make_string_deserializer(tag.UTF16, lambda b: b.decode('utf-16')), + tag.UTF32: _make_string_deserializer(tag.UTF32, lambda b: b.decode('utf-32')), + tag.TUPLE: _deserialize_tuple, } def _deserialize_partial(b): diff --git a/serial/serial/tag.py b/serial/serial/tag.py new file mode 100644 index 0000000..0c67029 --- /dev/null +++ b/serial/serial/tag.py @@ -0,0 +1,16 @@ +NULL = 0x00 +TRUE = 0x01 +FALSE = 0x02 +UINT8 = 0x03 +UINT16 = 0x04 +UINT32 = 0x05 +UINT64 = 0x06 +INT8 = 0x10 +INT16 = 0x11 +INT32 = 0x12 +INT64 = 0x13 +BINARY = 0x20 +UTF8 = 0x21 +UTF16 = 0x22 +UTF32 = 0x23 +TUPLE = 0x30 diff --git a/serial/test_binary.py b/serial/test_binary.py index 504f9c2..0c7b65d 100644 --- a/serial/test_binary.py +++ b/serial/test_binary.py @@ -1,41 +1,41 @@ import unittest -from serial import binary +from serial import binary, tag EXAMPLE_REPRESENTATIONS = [ - (binary.TaggedObject(binary.TAG_NULL, None), b'\x00'), - (binary.TaggedObject(binary.TAG_TRUE, True), b'\x01'), - (binary.TaggedObject(binary.TAG_FALSE, False), b'\x02'), - (binary.TaggedObject(binary.TAG_UINT8, 7), b'\x03\x07'), - (binary.TaggedObject(binary.TAG_UINT16, 7), b'\x04\x00\x07'), - (binary.TaggedObject(binary.TAG_UINT32, 7), b'\x05\x00\x00\x00\x07'), - (binary.TaggedObject(binary.TAG_UINT64, 7), b'\x06\x00\x00\x00\x00\x00\x00\x00\x07'), - (binary.TaggedObject(binary.TAG_INT8, 7), b'\x10\x07'), - (binary.TaggedObject(binary.TAG_INT16, 7), b'\x11\x00\x07'), - (binary.TaggedObject(binary.TAG_INT32, 7), b'\x12\x00\x00\x00\x07'), - (binary.TaggedObject(binary.TAG_INT64, 7), b'\x13\x00\x00\x00\x00\x00\x00\x00\x07'), - (binary.TaggedObject(binary.TAG_UINT8, 254), b'\x03\xfe'), - (binary.TaggedObject(binary.TAG_UINT16, 65534), b'\x04\xff\xfe'), - (binary.TaggedObject(binary.TAG_UINT32, 4294967294), b'\x05\xff\xff\xff\xfe'), - (binary.TaggedObject(binary.TAG_UINT64, 18446744073709551614), b'\x06\xff\xff\xff\xff\xff\xff\xff\xfe'), - (binary.TaggedObject(binary.TAG_INT8, -2), b'\x10\xfe'), - (binary.TaggedObject(binary.TAG_INT16, -2), b'\x11\xff\xfe'), - (binary.TaggedObject(binary.TAG_INT32, -2), b'\x12\xff\xff\xff\xfe'), - (binary.TaggedObject(binary.TAG_INT64, -2), b'\x13\xff\xff\xff\xff\xff\xff\xff\xfe'), - (binary.TaggedObject(binary.TAG_BINARY, b'\xde\xad\xbe\xef'), b'\x20\x00\x00\x00\x04\xde\xad\xbe\xef'), - (binary.TaggedObject(binary.TAG_UTF8, 'Lol!'), b'\x21\x00\x00\x00\x04Lol!'), - (binary.TaggedObject(binary.TAG_UTF16, 'かわ'), b'\x22\x00\x00\x00\x06\xff\xfeK0\x8f0'), - (binary.TaggedObject(binary.TAG_UTF32, '漢'), b'\x23\x00\x00\x00\x08\xff\xfe\x00\x00"o\x00\x00'), + (binary.TaggedObject(tag.NULL, None), b'\x00'), + (binary.TaggedObject(tag.TRUE, True), b'\x01'), + (binary.TaggedObject(tag.FALSE, False), b'\x02'), + (binary.TaggedObject(tag.UINT8, 7), b'\x03\x07'), + (binary.TaggedObject(tag.UINT16, 7), b'\x04\x00\x07'), + (binary.TaggedObject(tag.UINT32, 7), b'\x05\x00\x00\x00\x07'), + (binary.TaggedObject(tag.UINT64, 7), b'\x06\x00\x00\x00\x00\x00\x00\x00\x07'), + (binary.TaggedObject(tag.INT8, 7), b'\x10\x07'), + (binary.TaggedObject(tag.INT16, 7), b'\x11\x00\x07'), + (binary.TaggedObject(tag.INT32, 7), b'\x12\x00\x00\x00\x07'), + (binary.TaggedObject(tag.INT64, 7), b'\x13\x00\x00\x00\x00\x00\x00\x00\x07'), + (binary.TaggedObject(tag.UINT8, 254), b'\x03\xfe'), + (binary.TaggedObject(tag.UINT16, 65534), b'\x04\xff\xfe'), + (binary.TaggedObject(tag.UINT32, 4294967294), b'\x05\xff\xff\xff\xfe'), + (binary.TaggedObject(tag.UINT64, 18446744073709551614), b'\x06\xff\xff\xff\xff\xff\xff\xff\xfe'), + (binary.TaggedObject(tag.INT8, -2), b'\x10\xfe'), + (binary.TaggedObject(tag.INT16, -2), b'\x11\xff\xfe'), + (binary.TaggedObject(tag.INT32, -2), b'\x12\xff\xff\xff\xfe'), + (binary.TaggedObject(tag.INT64, -2), b'\x13\xff\xff\xff\xff\xff\xff\xff\xfe'), + (binary.TaggedObject(tag.BINARY, b'\xde\xad\xbe\xef'), b'\x20\x00\x00\x00\x04\xde\xad\xbe\xef'), + (binary.TaggedObject(tag.UTF8, 'Lol!'), b'\x21\x00\x00\x00\x04Lol!'), + (binary.TaggedObject(tag.UTF16, 'かわ'), b'\x22\x00\x00\x00\x06\xff\xfeK0\x8f0'), + (binary.TaggedObject(tag.UTF32, '漢'), b'\x23\x00\x00\x00\x08\xff\xfe\x00\x00"o\x00\x00'), ( binary.TaggedObject( - binary.TAG_TUPLE, + tag.TUPLE, ( binary.TaggedObject( - binary.TAG_TRUE, + tag.TRUE, True, ), binary.TaggedObject( - binary.TAG_UINT8, + tag.UINT8, 7, ), ), -- 2.20.1