Exemple #1
0
    def encode_property(self, name: str, value: common.FieldValue) -> bytes:
        """Encode a single property value

        :raises: TypeError

        """
        return encode.by_type(value, self.amqp_type(name))
Exemple #2
0
    def marshal(self) -> bytes:
        """Dynamically encode the frame by taking the list of attributes and
        encode them item by item getting the value form the object attribute
        and the data type from the class attribute.

        """
        byte, offset, output, processing_bitset = -1, 0, [], False
        for argument in self.__slots__:
            data_type = self.amqp_type(argument)
            if not processing_bitset and data_type == 'bit':
                byte, offset, processing_bitset = 0, 0, True
            data_value = getattr(self, argument, 0)
            if processing_bitset:
                if data_type != 'bit':
                    processing_bitset = False
                    output.append(encode.octet(byte))
                else:
                    byte = encode.bit(data_value, byte, offset)
                    offset += 1
                    if offset == 8:  # pragma: nocover
                        output.append(encode.octet(byte))
                        processing_bitset = False
                    continue  # pragma: nocover
            output.append(encode.by_type(data_value, data_type))
        if processing_bitset:
            output.append(encode.octet(byte))
        return b''.join(output)
Exemple #3
0
    def encode_property(self, name: str, value: common.FieldValue) -> bytes:
        """Encode a single property value

        :param name: The name of the property to encode
        :param value: The property to encode
        :type value: :const:`pamqp.common.FieldValue`
        :raises: TypeError

        """
        return encode.by_type(value, self.amqp_type(name))
Exemple #4
0
    def test_encode_by_type_field_array(self):

        expectation = (b'\x00\x00\x00<s\x00\x01I\x00\x00\xaf\xc8S\x00\x00\x00'
                       b'\x04TestT\x00\x00\x00\x00Ec)\x92I\xbb\x9a\xca\x00D\x02'
                       b'\x00\x00\x01:f@H\xf5\xc3l\x00\x00\x00\x00\xc4e5\xffl'
                       b'\x80\x00\x00\x00\x00\x00\x00\x08')
        data = [1, 45000, b'Test', datetime(2006, 11, 21, 16, 30, 10),
                -1147483648, Decimal('3.14'), 3.14, long(3294967295),
                -9223372036854775800]
        self.assertEqual(encode.by_type(data, 'field_array'),
                         expectation)
Exemple #5
0
 def test_encode_by_type_field_table(self):
     expectation = (b'\x00\x00\x04\x13\x08arrayvalA\x00\x00\x00\ts\x00\x01s'
                    b'\x00\x02s\x00\x03\x07boolvalt\x01\x06decvalD\x02\x00'
                    b'\x00\x01:\x07dictvalF\x00\x00\x00\x0c\x03fooS\x00\x00'
                    b'\x00\x03bar\x08floatvlaf@H\xf5\xc3\x06intvals\x00\x01'
                    b'\x07longstrS\x00\x00\x03t0000000000000000000000000000'
                    b'00000000000000000000000011111111111111111111111111111'
                    b'11111111111111111111111222222222222222222222222222222'
                    b'22222222222222222222221111111111111111111111111111111'
                    b'11111111111111111111122222222222222222222222222222222'
                    b'22222222222222222222111111111111111111111111111111111'
                    b'11111111111111111112222222222222222222222222222222222'
                    b'22222222222222222211111111111111111111111111111111111'
                    b'11111111111111111222222222222222222222222222222222222'
                    b'22222222222222221111111111111111111111111111111111111'
                    b'11111111111111122222222222222222222222222222222222222'
                    b'22222222222222111111111111111111111111111111111111111'
                    b'11111111111112222222222222222222222222222222222222222'
                    b'22222222222211111111111111111111111111111111111111111'
                    b'11111111111222222222222222222222222222222222222222222'
                    b'22222222221111111111111111111111111111111111111111111'
                    b'11111111100000000000000000000000000000000000000000000'
                    b'00000000\x07longvalI6e&U\x06strvalS\x00\x00\x00\x04'
                    b'Test\x0ctimestampvalT\x00\x00\x00\x00Ec)\x92')
     data = {'intval': 1,
             'strval': b'Test',
             'boolval': True,
             'timestampval': datetime(2006, 11, 21, 16, 30, 10),
             'decval': Decimal('3.14'),
             'floatvla': 3.14,
             'longval': long(912598613),
             'dictval': {b'foo': b'bar'},
             'arrayval': [1, 2, 3],
             'longstr': ('0000000000000000000000000000000000000000000000000'
                         '0001111111111111111111111111111111111111111111111'
                         '1111112222222222222222222222222222222222222222222'
                         '2222222221111111111111111111111111111111111111111'
                         '1111111111112222222222222222222222222222222222222'
                         '2222222222222221111111111111111111111111111111111'
                         '1111111111111111112222222222222222222222222222222'
                         '2222222222222222222221111111111111111111111111111'
                         '1111111111111111111111112222222222222222222222222'
                         '2222222222222222222222222221111111111111111111111'
                         '1111111111111111111111111111112222222222222222222'
                         '2222222222222222222222222222222221111111111111111'
                         '1111111111111111111111111111111111112222222222222'
                         '2222222222222222222222222222222222222221111111111'
                         '1111111111111111111111111111111111111111112222222'
                         '2222222222222222222222222222222222222222222221111'
                         '1111111111111111111111111111111111111111111111110'
                         '0000000000000000000000000000000000000000000000000'
                         '00')}
     self.assertEqual(encode.by_type(data, 'table'), expectation)
Exemple #6
0
 def test_encode_by_type_field_array(self):
     expectation = (b'\x00\x00\x008b\x01sBhu\xaf\xc8S\x00\x00\x00\x04TestT'
                    b'\x00\x00\x00\x00Ec)\x92I\xbb\x9a\xca\x00D\x02\x00'
                    b'\x00\x01:f@H\xf5\xc3i\xc4e5\xffl\x80\x00\x00\x00\x00'
                    b'\x00\x00\x08')
     data = [
         1, 17000, 45000, 'Test',
         datetime.datetime(2006, 11, 21, 16, 30, 10), -1147483648,
         decimal.Decimal('3.14'), 3.14,
         3294967295, -9223372036854775800
     ]
     self.assertEqual(encode.by_type(data, 'field_array'), expectation)
    def test_encode_by_type_field_array(self):

        expectation = (
            b'\x00\x00\x00<s\x00\x01I\x00\x00\xaf\xc8S\x00\x00\x00'
            b'\x04TestT\x00\x00\x00\x00Ec)\x92I\xbb\x9a\xca\x00D\x02'
            b'\x00\x00\x01:f@H\xf5\xc3l\x00\x00\x00\x00\xc4e5\xffl'
            b'\x80\x00\x00\x00\x00\x00\x00\x08')
        data = [
            1, 45000, b'Test',
            datetime(2006, 11, 21, 16, 30, 10), -1147483648,
            Decimal('3.14'), 3.14,
            long(3294967295), -9223372036854775800
        ]
        self.assertEqual(encode.by_type(data, 'field_array'), expectation)
Exemple #8
0
 def test_encode_by_type_long_uint(self):
     self.assertEqual(encode.by_type(4294967295, 'long'),
                      b'\xff\xff\xff\xff')
Exemple #9
0
 def test_encode_by_type_long_long_int(self):
     self.assertEqual(encode.by_type(9223372036854775800, 'longlong'),
                      b'\x7f\xff\xff\xff\xff\xff\xff\xf8')
Exemple #10
0
 def test_encode_by_type_double(self):
     self.assertEqual(encode.by_type(float(4294967295), 'double'),
                      b'A\xef\xff\xff\xff\xe0\x00\x00')
Exemple #11
0
 def test_encode_by_type_double(self):
     self.assertEqual(encode.by_type(float(4294967295), 'double'),
                      b'A\xef\xff\xff\xff\xe0\x00\x00')
Exemple #12
0
 def test_encode_by_type_timestamp(self):
     self.assertEqual(
         encode.by_type(
             datetime.datetime(2006, 11, 21, 16, 30, 10), 'timestamp'),
         b'\x00\x00\x00\x00Ec)\x92')
Exemple #13
0
 def test_encode_by_type_field_table(self):
     expectation = (b'\x00\x00\x04\x13\x08arrayvalA\x00\x00\x00\ts\x00\x01s'
                    b'\x00\x02s\x00\x03\x07boolvalt\x01\x06decvalD\x02\x00'
                    b'\x00\x01:\x07dictvalF\x00\x00\x00\x0c\x03fooS\x00\x00'
                    b'\x00\x03bar\x08floatvlaf@H\xf5\xc3\x06intvals\x00\x01'
                    b'\x07longstrS\x00\x00\x03t0000000000000000000000000000'
                    b'00000000000000000000000011111111111111111111111111111'
                    b'11111111111111111111111222222222222222222222222222222'
                    b'22222222222222222222221111111111111111111111111111111'
                    b'11111111111111111111122222222222222222222222222222222'
                    b'22222222222222222222111111111111111111111111111111111'
                    b'11111111111111111112222222222222222222222222222222222'
                    b'22222222222222222211111111111111111111111111111111111'
                    b'11111111111111111222222222222222222222222222222222222'
                    b'22222222222222221111111111111111111111111111111111111'
                    b'11111111111111122222222222222222222222222222222222222'
                    b'22222222222222111111111111111111111111111111111111111'
                    b'11111111111112222222222222222222222222222222222222222'
                    b'22222222222211111111111111111111111111111111111111111'
                    b'11111111111222222222222222222222222222222222222222222'
                    b'22222222221111111111111111111111111111111111111111111'
                    b'11111111100000000000000000000000000000000000000000000'
                    b'00000000\x07longvalI6e&U\x06strvalS\x00\x00\x00\x04'
                    b'Test\x0ctimestampvalT\x00\x00\x00\x00Ec)\x92')
     data = {
         'intval':
         1,
         'strval':
         b'Test',
         'boolval':
         True,
         'timestampval':
         datetime(2006, 11, 21, 16, 30, 10),
         'decval':
         Decimal('3.14'),
         'floatvla':
         3.14,
         'longval':
         long(912598613),
         'dictval': {
             b'foo': b'bar'
         },
         'arrayval': [1, 2, 3],
         'longstr': ('0000000000000000000000000000000000000000000000000'
                     '0001111111111111111111111111111111111111111111111'
                     '1111112222222222222222222222222222222222222222222'
                     '2222222221111111111111111111111111111111111111111'
                     '1111111111112222222222222222222222222222222222222'
                     '2222222222222221111111111111111111111111111111111'
                     '1111111111111111112222222222222222222222222222222'
                     '2222222222222222222221111111111111111111111111111'
                     '1111111111111111111111112222222222222222222222222'
                     '2222222222222222222222222221111111111111111111111'
                     '1111111111111111111111111111112222222222222222222'
                     '2222222222222222222222222222222221111111111111111'
                     '1111111111111111111111111111111111112222222222222'
                     '2222222222222222222222222222222222222221111111111'
                     '1111111111111111111111111111111111111111112222222'
                     '2222222222222222222222222222222222222222222221111'
                     '1111111111111111111111111111111111111111111111110'
                     '0000000000000000000000000000000000000000000000000'
                     '00')
     }
     self.assertEqual(encode.by_type(data, 'table'), expectation)
Exemple #14
0
 def test_encode_by_type_short(self):
     self.assertEqual(encode.by_type(32767, 'short'), b'\x7f\xff')
Exemple #15
0
 def test_encode_by_type_octet(self):
     self.assertEqual(encode.by_type(1, 'octet'), b'\x01')
Exemple #16
0
 def test_encode_by_type_none(self):
     self.assertEqual(encode.by_type(None, 'void'), None)
Exemple #17
0
 def test_encode_by_type_octet(self):
     self.assertEqual(encode.by_type(1, 'octet'), b'\x01')
Exemple #18
0
 def test_encode_by_type_long_str(self):
     self.assertEqual(encode.by_type(b'0123456789', 'longstr'),
                      b'\x00\x00\x00\n0123456789')
Exemple #19
0
 def test_encode_by_type_long_long_int(self):
     self.assertEqual(encode.by_type(long(9223372036854775800), 'longlong'),
                      b'\x7f\xff\xff\xff\xff\xff\xff\xf8')
Exemple #20
0
 def test_encode_by_type_long_uint(self):
     self.assertEqual(encode.by_type(long(4294967295), 'long'),
                      b'\xff\xff\xff\xff')
Exemple #21
0
 def test_encode_by_type_long_str(self):
     self.assertEqual(encode.by_type('0123456789', 'longstr'),
                      b'\x00\x00\x00\n0123456789')
Exemple #22
0
 def test_encode_by_type_long_int(self):
     self.assertEqual(encode.by_type(long(2147483647), 'long'),
                      b'\x7f\xff\xff\xff')
Exemple #23
0
 def test_encode_by_type_none(self):
     self.assertEqual(encode.by_type(None, 'void'), None)
Exemple #24
0
 def test_encode_by_type_field_table(self):
     expectation = (b'\x00\x00\x04B\x08arrayvalA\x00\x00\x00\x08b\x01s\x10'
                    b'`u\xa4\x10\x07boolvalt\x01\x06decvalD\x02\x00\x00\x01'
                    b':\x07dictvalF\x00\x00\x00\x0c\x03fooS\x00\x00\x00\x03'
                    b'bar\x08floatvalf@H\xf5\xc3\x07longstrS\x00\x00\x03t00'
                    b'00000000000000000000000000000000000000000000000000111'
                    b'11111111111111111111111111111111111111111111111112222'
                    b'22222222222222222222222222222222222222222222222211111'
                    b'11111111111111111111111111111111111111111111111222222'
                    b'22222222222222222222222222222222222222222222221111111'
                    b'11111111111111111111111111111111111111111111122222222'
                    b'22222222222222222222222222222222222222222222111111111'
                    b'11111111111111111111111111111111111111111112222222222'
                    b'22222222222222222222222222222222222222222211111111111'
                    b'11111111111111111111111111111111111111111222222222222'
                    b'22222222222222222222222222222222222222221111111111111'
                    b'11111111111111111111111111111111111111122222222222222'
                    b'22222222222222222222222222222222222222111111111111111'
                    b'11111111111111111111111111111111111112222222222222222'
                    b'22222222222222222222222222222222222211111111111111111'
                    b'11111111111111111111111111111111111000000000000000000'
                    b'0000000000000000000000000000000000\x07longvalI6e&U'
                    b'\x06s32intI\xc4e6\x00\x06s64intl7\x82\xda\xce\x9d\x90'
                    b'\x00\x00\x06strvalS\x00\x00\x00\x04Test\x0ctimestamp'
                    b'valT\x00\x00\x00\x00Ec)\x92\x06u16ints \x00\x06u32int'
                    b'i\xeek(\x00\x05u8bitb ')
     data = {
         'u8bit': 32,
         'u16int': 8192,
         's32int': -1000000000,
         'u32int': 4000000000,
         's64int': 4000000000000000000,
         'strval': b'Test',
         'boolval': True,
         'timestampval': datetime(2006, 11, 21, 16, 30, 10),
         'decval': Decimal('3.14'),
         'floatval': 3.14,
         'longval': long(912598613),
         'dictval': {
             b'foo': b'bar'
         },
         'arrayval': [1, 4192, 42000],
         'longstr': ('0000000000000000000000000000000000000000000000000'
                     '0001111111111111111111111111111111111111111111111'
                     '1111112222222222222222222222222222222222222222222'
                     '2222222221111111111111111111111111111111111111111'
                     '1111111111112222222222222222222222222222222222222'
                     '2222222222222221111111111111111111111111111111111'
                     '1111111111111111112222222222222222222222222222222'
                     '2222222222222222222221111111111111111111111111111'
                     '1111111111111111111111112222222222222222222222222'
                     '2222222222222222222222222221111111111111111111111'
                     '1111111111111111111111111111112222222222222222222'
                     '2222222222222222222222222222222221111111111111111'
                     '1111111111111111111111111111111111112222222222222'
                     '2222222222222222222222222222222222222221111111111'
                     '1111111111111111111111111111111111111111112222222'
                     '2222222222222222222222222222222222222222222221111'
                     '1111111111111111111111111111111111111111111111110'
                     '0000000000000000000000000000000000000000000000000'
                     '00')
     }
     self.assertEqual(encode.by_type(data, 'table'), expectation)
Exemple #25
0
 def test_encode_by_type_short(self):
     self.assertEqual(encode.by_type(32767, 'short'), b'\x7f\xff')
Exemple #26
0
 def test_encode_by_type_long_int(self):
     self.assertEqual(encode.by_type(long(2147483647), 'long'),
                      b'\x7f\xff\xff\xff')
Exemple #27
0
 def test_encode_by_type_field_table(self):
     expectation = (b'\x00\x00\x04B\x08arrayvalA\x00\x00\x00\x08b\x01s\x10'
                    b'`u\xa4\x10\x07boolvalt\x01\x06decvalD\x02\x00\x00\x01'
                    b':\x07dictvalF\x00\x00\x00\x0c\x03fooS\x00\x00\x00\x03'
                    b'bar\x08floatvalf@H\xf5\xc3\x07longstrS\x00\x00\x03t00'
                    b'00000000000000000000000000000000000000000000000000111'
                    b'11111111111111111111111111111111111111111111111112222'
                    b'22222222222222222222222222222222222222222222222211111'
                    b'11111111111111111111111111111111111111111111111222222'
                    b'22222222222222222222222222222222222222222222221111111'
                    b'11111111111111111111111111111111111111111111122222222'
                    b'22222222222222222222222222222222222222222222111111111'
                    b'11111111111111111111111111111111111111111112222222222'
                    b'22222222222222222222222222222222222222222211111111111'
                    b'11111111111111111111111111111111111111111222222222222'
                    b'22222222222222222222222222222222222222221111111111111'
                    b'11111111111111111111111111111111111111122222222222222'
                    b'22222222222222222222222222222222222222111111111111111'
                    b'11111111111111111111111111111111111112222222222222222'
                    b'22222222222222222222222222222222222211111111111111111'
                    b'11111111111111111111111111111111111000000000000000000'
                    b'0000000000000000000000000000000000\x07longvalI6e&U'
                    b'\x06s32intI\xc4e6\x00\x06s64intl7\x82\xda\xce\x9d\x90'
                    b'\x00\x00\x06strvalS\x00\x00\x00\x04Test\x0ctimestamp'
                    b'valT\x00\x00\x00\x00Ec)\x92\x06u16ints \x00\x06u32int'
                    b'i\xeek(\x00\x05u8bitb ')
     data = {
         'u8bit': 32,
         'u16int': 8192,
         's32int': -1000000000,
         'u32int': 4000000000,
         's64int': 4000000000000000000,
         'strval': 'Test',
         'boolval': True,
         'timestampval': datetime.datetime(2006, 11, 21, 16, 30, 10),
         'decval': decimal.Decimal('3.14'),
         'floatval': 3.14,
         'longval': 912598613,
         'dictval': {
             'foo': 'bar'
         },
         'arrayval': [1, 4192, 42000],
         'longstr': ('0000000000000000000000000000000000000000000000000'
                     '0001111111111111111111111111111111111111111111111'
                     '1111112222222222222222222222222222222222222222222'
                     '2222222221111111111111111111111111111111111111111'
                     '1111111111112222222222222222222222222222222222222'
                     '2222222222222221111111111111111111111111111111111'
                     '1111111111111111112222222222222222222222222222222'
                     '2222222222222222222221111111111111111111111111111'
                     '1111111111111111111111112222222222222222222222222'
                     '2222222222222222222222222221111111111111111111111'
                     '1111111111111111111111111111112222222222222222222'
                     '2222222222222222222222222222222221111111111111111'
                     '1111111111111111111111111111111111112222222222222'
                     '2222222222222222222222222222222222222221111111111'
                     '1111111111111111111111111111111111111111112222222'
                     '2222222222222222222222222222222222222222222221111'
                     '1111111111111111111111111111111111111111111111110'
                     '0000000000000000000000000000000000000000000000000'
                     '00')
     }
     self.assertEqual(encode.by_type(data, 'table'), expectation)
Exemple #28
0
 def test_encode_by_type_byte_array(self):
     self.assertEqual(encode.by_type(bytearray((65, 66, 67)), 'bytearray'),
                      b'\x00\x00\x00\x03ABC')
Exemple #29
0
 def test_encode_by_type_timestamp(self):
     self.assertEqual(
         encode.by_type(datetime(2006, 11, 21, 16, 30, 10), 'timestamp'),
         b'\x00\x00\x00\x00Ec)\x92')
Exemple #30
0
 def test_encode_by_type_byte_array(self):
     self.assertEqual(encode.by_type(bytearray((65, 66, 67)), 'bytearray'),
                      b'\x00\x00\x00\x03ABC')