當前位置: 首頁>>代碼示例>>Python>>正文


Python containers.Protocol類代碼示例

本文整理匯總了Python中Rammbock.templates.containers.Protocol的典型用法代碼示例。如果您正苦於以下問題:Python Protocol類的具體用法?Python Protocol怎麽用?Python Protocol使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Protocol類的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: setUp

 def setUp(self):
     self._protocol = Protocol('TestProtocol', little_endian=True)
     self._protocol.add(UInt(2, 'msgId', 5))
     self._protocol.add(UInt(2, 'length', None))
     self._protocol.add(PDU('length-4'))
     self.tmp = MessageTemplate('FooRequest', self._protocol, {})
     self.tmp.add(UInt(2, 'field_1', '0xcafe'))
     self.tmp.add(UInt(2, 'field_2', '0xbabe'))
開發者ID:vivektatipamula,項目名稱:Rammbock,代碼行數:8,代碼來源:test_endianness.py

示例2: setUp

 def setUp(self):
     self._protocol = Protocol('TestProtocol')
     self._protocol.add(UInt(2, 'msgId', 5))
     self._protocol.add(UInt(2, 'length', None))
     self._protocol.add(PDU('length-4'))
     self.tmp = MessageTemplate('FooRequest', self._protocol, {})
     self.tmp.add(UInt(2, 'field_1', 1))
     self.tmp.add(UInt(2, 'field_2', 2))
開發者ID:caseyding,項目名稱:Rammbock,代碼行數:8,代碼來源:test_message_templates.py

示例3: test_access_struct

 def test_access_struct(self):
     self._protocol = Protocol('TestProtocol')
     self._protocol.add(UInt(2, 'msgId', 5))
     self._protocol.add(UInt(2, 'length', None))
     self._protocol.add(PDU('length-4'))
     self.tmp = MessageTemplate('StructuredRequest', self._protocol, {})
     struct = get_pair()
     self.tmp.add(struct)
     msg = self.tmp.encode({}, {})
     self.assertEquals(msg.pair.first.int, 1)
開發者ID:ballacky13,項目名稱:Rammbock,代碼行數:10,代碼來源:test_struct.py

示例4: TestStructs

class TestStructs(TestCase):

    def test_access_struct(self):
        self._protocol = Protocol('TestProtocol')
        self._protocol.add(UInt(2, 'msgId', 5))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length-4'))
        self.tmp = MessageTemplate('StructuredRequest', self._protocol, {})
        struct = get_pair()
        self.tmp.add(struct)
        msg = self.tmp.encode({}, {})
        self.assertEquals(msg.pair.first.int, 1)

    def test_create_struct(self):
        struct = get_pair()
        self.assertEquals(struct.name, 'pair')

    def test_add_fields_to_struct(self):
        struct = get_pair()
        encoded = struct.encode({}, {})
        self.assertEquals(encoded.first.int, 1)

    def test_add_fields_to_struct_and_override_values(self):
        struct = get_pair()
        encoded = struct.encode({'pair.first': 42}, {})
        self.assertEquals(encoded.first.int, 42)

    def test_yo_dawg_i_heard(self):
        str_str = get_recursive_struct()
        encoded = str_str.encode({}, {})
        self.assertEquals(encoded.pair.first.int, 1)

    def test_get_recursive_names(self):
        pair = get_pair()
        names = pair._get_params_sub_tree({'pair.foo': 0, 'pairnotyourname.ploo': 2, 'pair.goo.doo': 3})
        self.assertEquals(len(names), 2)
        self.assertEquals(names['foo'], 0)
        self.assertEquals(names['goo.doo'], 3)

    def test_set_recursive(self):
        str_str = get_recursive_struct()
        encoded = str_str.encode({'str_str.pair.first': 42}, {})
        self.assertEquals(encoded.pair.first.int, 42)

    def test_decode_several_structs(self):
        str_list = get_struct_list()
        decoded = str_list.decode(to_bin('0xcafebabe d00df00d'), {})
        self.assertEquals(decoded[0].first.hex, '0xcafe')
        self.assertEquals(decoded[1].second.hex, '0xf00d')

    def test_length_of_struct(self):
        pair = get_pair()
        encoded = pair.encode({}, {})
        self.assertEquals(len(encoded), 4)

    def test_decode_struct(self):
        pair = get_pair()
        decoded = pair.decode(to_bin('0xcafebabe'), {})
        self.assertEquals(decoded.first.hex, '0xcafe')
        self.assertEquals(decoded.second.hex, '0xbabe')
開發者ID:ballacky13,項目名稱:Rammbock,代碼行數:60,代碼來源:test_struct.py

示例5: TestLittleEndianProtocol

class TestLittleEndianProtocol(TestCase):

    def setUp(self):
        self._protocol = Protocol('TestProtocol', little_endian=True)
        self._protocol.add(UInt(2, 'msgId', 5))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length-4'))
        self.tmp = MessageTemplate('FooRequest', self._protocol, {})
        self.tmp.add(UInt(2, 'field_1', '0xcafe'))
        self.tmp.add(UInt(2, 'field_2', '0xbabe'))

    def test_encode_little_endian_header(self):
        encoded = self.tmp.encode({}, {})
        self.assertEquals(encoded._header.msgId.hex, '0x0005')
        self.assertEquals(encoded._header.length.hex, '0x0008')
        self.assertEquals(encoded._header.msgId._raw, to_bin('0x0500'))
        self.assertEquals(encoded._header.length._raw, to_bin('0x0800'))

    def test_decode_little_endian_header(self):
        byte_stream = MockStream(to_bin('0x0500 0800 cafe babe'))
        self._msg_stream = MessageStream(byte_stream, self._protocol, threading.RLock())
        decoded = self._msg_stream.get(self.tmp)
        self.assertEquals(decoded._header.msgId.hex, '0x0005')
        self.assertEquals(decoded._header.msgId._raw, to_bin('0x0500'))
        self.assertEquals(decoded.field_1.hex, '0xcafe')
        self.assertEquals(decoded.field_1._raw, to_bin('0xcafe'))
        self.assertEquals(decoded.field_2.hex, '0xbabe')
        self.assertEquals(decoded.field_2._raw, to_bin('0xbabe'))
開發者ID:vivektatipamula,項目名稱:Rammbock,代碼行數:28,代碼來源:test_endianness.py

示例6: TestMessageTemplate

class TestMessageTemplate(TestCase):

    def setUp(self):
        self._protocol = Protocol('TestProtocol')
        self._protocol.add(UInt(2, 'msgId', 5))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length-4'))
        self.tmp = MessageTemplate('FooRequest', self._protocol, {})
        self.tmp.add(UInt(2, 'field_1', 1))
        self.tmp.add(UInt(2, 'field_2', 2))

    def test_create_template(self):
        self.assertEquals(len(self.tmp._fields), 2)

    def test_encode_template(self):
        msg = self.tmp.encode({}, {})
        self.assertEquals(msg.field_1.int, 1)
        self.assertEquals(msg.field_2.int, 2)

    def test_message_field_type_conversions(self):
        msg = self.tmp.encode({'field_1': 1024}, {})
        self.assertEquals(msg.field_1.int, 1024)
        self.assertEquals(msg.field_1.hex, '0x0400')
        self.assertEquals(msg.field_1.bytes, '\x04\x00')

    def test_encode_template_with_params(self):
        msg = self.tmp.encode({'field_1': 111, 'field_2': 222}, {})
        self.assertEquals(msg.field_1.int, 111)
        self.assertEquals(msg.field_2.int, 222)

    def test_encode_template_header(self):
        msg = self.tmp.encode({}, {})
        self.assertEquals(msg._header.msgId.int, 5)
        self.assertEquals(msg._header.length.int, 8)

    def test_encode_to_bytes(self):
        msg = self.tmp.encode({}, {})
        self.assertEquals(msg._header.msgId.int, 5)
        self.assertEquals(msg._raw, to_bin_of_length(8, '0x0005 0008 0001 0002'))

    def test_pretty_print(self):
        msg = self.tmp.encode({}, {})
        self.assertEquals(msg._header.msgId.int, 5)
        self.assertEquals(str(msg), 'Message FooRequest')
        self.assertEquals(repr(msg),
                          """Message FooRequest
  Header TestProtocol
    msgId = 5 (0x0005)
    length = 8 (0x0008)
  field_1 = 1 (0x0001)
  field_2 = 2 (0x0002)
""")

    def test_unknown_params_cause_exception(self):
        self.assertRaises(AssertionError, self.tmp.encode, {'unknown': 111}, {})

    def test_decode_message(self):
        msg = self.tmp.decode(to_bin('0xcafebabe'))
        self.assertEquals(msg.field_1.hex, '0xcafe')
開發者ID:caseyding,項目名稱:Rammbock,代碼行數:59,代碼來源:test_message_templates.py

示例7: TestMessageTemplateValidation

class TestMessageTemplateValidation(TestCase):

    def setUp(self):
        self._protocol = Protocol('TestProtocol')
        self._protocol.add(UInt(2, 'msgId', 5))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length-4'))
        self.tmp = MessageTemplate('FooRequest', self._protocol, {})
        self.tmp.add(UInt(2, 'field_1', '0xcafe'))
        self.tmp.add(UInt(2, 'field_2', '0xbabe'))
        self.example = self.tmp.encode({}, {})

    def test_validate_passing_hex(self):
        msg = self._decode_and_set_fake_header('0xcafebabe')
        errors = self.tmp.validate(msg, {}, {})
        self.assertEquals(errors, [])

    def test_validate_error_default_value(self):
        msg = self._decode_and_set_fake_header('0xcafedead')
        errors = self.tmp.validate(msg, {}, {})
        self.assertEquals(errors, ['Value of field field_2 does not match 0xdead!=0xbabe'])

    def test_validate_error_override(self):
        msg = self._decode_and_set_fake_header('0xcafebabe')
        errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {})
        self.assertEquals(errors, ['Value of field field_2 does not match 0xbabe!=0xdead'])

    def test_validate_two_errors(self):
        msg = self._decode_and_set_fake_header('0xbeefbabe')
        errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {})
        self.assertEquals(len(errors), 2)

    def test_validate_pattern_pass(self):
        msg = self._decode_and_set_fake_header('0xcafe0002')
        errors = self.tmp.validate(msg, {'field_2': '(0|2)'}, {})
        self.assertEquals(len(errors), 0)

    def test_validate_pattern_failure(self):
        msg = self._decode_and_set_fake_header('0xcafe0002')
        errors = self.tmp.validate(msg, {'field_2': '(0|3)'}, {})
        self.assertEquals(len(errors), 1)

    def test_validate_passing_int(self):
        msg = self._decode_and_set_fake_header('0xcafe0200')
        errors = self.tmp.validate(msg, {'field_2': '512'}, {})
        self.assertEquals(errors, [])

    def _decode_and_set_fake_header(self, bin_value):
        msg = self.tmp.decode(to_bin(bin_value))
        msg['_header'] = self.example._header
        return msg

    def test_failing_passing_int(self):
        msg = self._decode_and_set_fake_header('0xcafe0200')
        errors = self.tmp.validate(msg, {'field_2': '513'}, {})
        self.assertEquals(len(errors), 1)
開發者ID:caseyding,項目名稱:Rammbock,代碼行數:56,代碼來源:test_message_templates.py

示例8: setUp

 def setUp(self):
     self._protocol = Protocol('Test')
開發者ID:embeddedninja,項目名稱:Rammbock,代碼行數:2,代碼來源:test_containers.py

示例9: TestProtocol

class TestProtocol(TestCase):

    def setUp(self):
        self._protocol = Protocol('Test')

    def test_header_length(self):
        self._protocol.add(UInt(1, 'name1', None))
        self.assertEquals(self._protocol.header_length(), 1)

    def test_header_length_with_pdu(self):
        self._protocol.add(UInt(1, 'name1', None))
        self._protocol.add(UInt(2, 'name2', 5))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length'))
        self.assertEquals(self._protocol.header_length(), 5)

    def test_verify_undefined_length(self):
        self._protocol.add(UInt(1, 'name1', None))
        self._protocol.add(UInt(2, 'name2', 5))
        self.assertRaises(Exception, self._protocol.add, PDU('length'))

    def test_verify_calculated_length(self):
        self._protocol.add(UInt(1, 'name1', 1))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length-8'))
        self.assertEquals(self._protocol.header_length(), 3)
開發者ID:embeddedninja,項目名稱:Rammbock,代碼行數:26,代碼來源:test_containers.py

示例10: TestTBCDContainerTemplate

class TestTBCDContainerTemplate(TestCase):

    def setUp(self):
        self._protocol = Protocol('TestProtocol')
        self._protocol.add(UInt(2, 'msgId', 5))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length-4'))
        self.tmp = MessageTemplate('FooRequest', self._protocol, {})
        self.tmp.add(UInt(2, 'field_1', 1))
        self.tmp.add(UInt(2, 'field_2', 2))

    def test_verify_only_tbcd_number_passes(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('4', 'first', None))
        self.assertRaises(AssertionError, container.add, UInt(2, 'not allowed', None))

    def test_get_even_tbcd_field(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('4', 'first', '1234'))
        self.tmp.add(container)
        msg = self.tmp.encode({}, {})
        self.assertEqual('1234', msg.tbcd.first.tbcd)

    def test_get_odd_tbcd_field(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('3', 'first', '123'))
        self.tmp.add(container)
        msg = self.tmp.encode({}, {})
        self.assertEqual('123', msg.tbcd.first.tbcd)

    def test_get_binlength_for_odd_value_tbcd_container(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('1', 'first', '1'))
        self.assertEquals(8, container.binlength)
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('3', 'first', '123'))
        self.assertEquals(16, container.binlength)

    def test_get_binlength_for_even_value_tbcd_container(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('2', 'first', '12'))
        self.assertEquals(8, container.binlength)
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('4', 'first', '1234'))
        self.assertEquals(16, container.binlength)

    def test_decode(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('3', 'first', None))
        decoded = container.decode(to_bin('0x21f3'))
        self.assertEquals('123', decoded.first.tbcd)

    def test_little_endian_tbcd_unsupported(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('3', 'first', '123'))
        self.assertRaises(AssertionError, container.encode, {}, {}, little_endian=True)
        self.assertRaises(AssertionError, container.decode, to_bin('0x21f3'), little_endian=True)

    def test_get_encoded_raw_bytes(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('3', 'first', '123'))
        container.add(TBCD('13', 'second', '6100000000001'))
        encoded = container.encode({}, {})
        self.assertEquals(to_bin("0b0010000101100011000000010000000000000000000000000000000000010000"), encoded._get_raw_bytes())

    def test_decode_raw_bytes_with_star_length(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('3', 'first', '123'))
        container.add(TBCD('*', 'second', '6100000000001'))
        decoded = container.decode(to_bin("0b0010000101100011000000010000000000000000000000000000000000010000"))
        self.assertEquals('123', decoded.first.tbcd)
        self.assertEquals('6100000000001', decoded.second.tbcd)

    def test_encoded_even_value_container_returns_correct_length(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('3', 'first', '123'))
        container.add(TBCD('13', 'second', '6100000000001'))
        encoded = container.encode({})
        self.assertEquals(8, len(encoded))

    def test_encoded_odd_value_container_returns_correct_length(self):
        container = TBCDContainerTemplate('tbcd', None)
        container.add(TBCD('3', 'first', '456'))
        container.add(TBCD('4', 'second', '1234'))
        encoded = container.encode({})
        self.assertEquals(4, len(encoded))
開發者ID:embeddedninja,項目名稱:Rammbock,代碼行數:86,代碼來源:test_containers.py

示例11: TestDynamicMessageTemplate

class TestDynamicMessageTemplate(TestCase):

    def setUp(self):
        self._protocol = Protocol('TestProtocol')
        self._protocol.add(UInt(2, 'msgId', 5))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length-4'))

    def test_non_existing_dynamic_variable(self):
        tmp = MessageTemplate('Dymagic', self._protocol, {})
        self.assertRaises(Exception, tmp.add, Char('not_existing', 'foo', None))

    def test_non_existing_dynamic_list_variable(self):
        tmp = MessageTemplate('Dymagic', self._protocol, {})
        lst = ListTemplate('not_existing', 'foo', parent=None)
        lst.add(UInt(1, 'bar', None))
        self.assertRaises(Exception, tmp.add, lst)

    def test_decode_dynamic_primitive(self):
        tmp = MessageTemplate('Dymagic', self._protocol, {})
        tmp.add(UInt(1, 'len', None))
        tmp.add(Char('len', 'chars', None))
        tmp.add(UInt(1, 'len2', None))
        tmp.add(Char('len2', 'chars2', None))
        decoded = tmp.decode(to_bin('0x 04 6162 6364 02 6566'))
        self.assertEquals(decoded.len.int, 4)
        self.assertEquals(decoded.chars.ascii, 'abcd')
        self.assertEquals(decoded.chars2.ascii, 'ef')

    def test_encode_dynamic_primitive_with_defined_value(self):
        tmp = MessageTemplate('Dymagic', self._protocol, {})
        tmp.add(UInt(4, 'len', '4'))
        tmp.add(Char('len', 'chars', 'abcd'))
        tmp.add(UInt(4, 'len2', None))
        tmp.add(Char('len2', 'chars2', 'ef'))
        encoded = tmp.encode({'len2': '6'}, {})
        self.assertEquals(encoded.chars.ascii, 'abcd')
        self.assertEquals(len(encoded.chars), 4)
        self.assertEquals(encoded.chars2.ascii, 'ef')
        self.assertEquals(len(encoded.chars2), 6)

    def test_encode_dynamic_primitive_automatically(self):
        tmp = MessageTemplate('Dymagic', self._protocol, {})
        tmp.add(UInt(4, 'len', None))
        tmp.add(Char('len', 'chars', 'abcd'))
        encoded = tmp.encode({}, {})
        self.assertEquals(encoded.chars.ascii, 'abcd')
        self.assertEquals(encoded.len.int, 4)

    def test_decode_dynamic_list(self):
        tmp = MessageTemplate('Dymagic', self._protocol, {})
        tmp.add(UInt(2, 'len', None))
        lst = ListTemplate('len', 'foo', parent=None)
        lst.add(UInt(1, 'bar', None))
        tmp.add(lst)
        decoded = tmp.decode(to_bin('0x 00 04 6162 6364'))
        self.assertEquals(decoded.len.int, 4)
        self.assertEquals(decoded.foo[0].hex, '0x61')

    def test_encode_dynamic_list(self):
        tmp = MessageTemplate('Dymagic', self._protocol, {})
        tmp.add(UInt(2, 'len', None))
        lst = ListTemplate('len', 'foo', parent=None)
        lst.add(UInt(1, 'bar', 1))
        tmp.add(lst)
        encoded = tmp.encode({'len': 6}, {})
        self.assertEquals(len(encoded.foo), 6)

    def test_add_field_with_length_reference_to_parent(self):
        tmp = MessageTemplate('Dymagic', self._protocol, {})
        tmp.add(UInt(2, 'len', None))
        str = StructTemplate('FooType', 'foo', tmp)
        str.add(Char('len', "bar"))

    def test_add_field_with_length_reference_missing(self):
        tmp = MessageTemplate('Dymagic', self._protocol, {})
        tmp.add(UInt(2, 'len', None))
        str = StructTemplate('FooType', 'foo', tmp)
        self.assertRaises(AssertionError, str.add, Char('notfound', "bar"))
開發者ID:embeddedninja,項目名稱:Rammbock,代碼行數:79,代碼來源:test_containers.py

示例12: _get_template

def _get_template():
    protocol = Protocol('Test')
    protocol.add(UInt(1, 'id', 1))
    protocol.add(UInt(2, 'length', None))
    protocol.add(PDU('length-2'))
    return protocol
開發者ID:JMassapina,項目名稱:Rammbock,代碼行數:6,代碼來源:test_networking.py


注:本文中的Rammbock.templates.containers.Protocol類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。