Example #1
0
    def _convert_value(self, value, ftype):
        assert isinstance(value, six.binary_type)

        if ftype is None:
            warnings.warn(
                DeprecationWarning(
                    'Field type should not be "None". Please explicitly '
                    'use TYPE_BYTES instead.'))
            return value

        if ftype == TYPE_BYTES:
            return value

        if hasattr(ftype, '__call__'):
            return ftype(value, self.endianness)

        if ftype == TYPE_STRING:
            return value.decode('utf-8')

        if ftype in ('str', 'unicode'):
            warnings.warn(
                DeprecationWarning(
                    'The "{ftype}" field type is deprecated. Please use "string" '
                    '(TYPE_STRING) instead.'.format(ftype=ftype)))
            return six.u(value)

        _numeric_types = {
            TYPE_U8: 'B',
            TYPE_I8: 'b',
            TYPE_U16: 'H',
            TYPE_I16: 'h',
            TYPE_U32: 'I',
            TYPE_I32: 'i',
            TYPE_U64: 'Q',
            TYPE_I64: 'q',
        }
        if ftype in _numeric_types:
            fmt = self.endianness + _numeric_types[ftype]
            return struct.unpack(fmt, value)[0]

        if ftype == TYPE_IPV4:
            return unpack_ipv4(value)

        if ftype == TYPE_IPV4_MASK:
            return unpack_ipv4(value[:4]), unpack_ipv4(value[4:8])

        if ftype == TYPE_IPV6:
            return unpack_ipv6(value)

        if ftype == TYPE_IPV6_PREFIX:
            return (unpack_ipv6(value[:16]),
                    struct.unpack(self.endianness + 'B', value[16]))

        if ftype == TYPE_MACADDR:
            return unpack_macaddr(value)

        if ftype == TYPE_EUIADDR:
            return unpack_euiaddr(value)

        raise ValueError('Unsupported field type: {0}'.format(ftype))
Example #2
0
    def _convert_value(self, value, ftype):
        assert isinstance(value, six.binary_type)

        if ftype is None:
            warnings.warn(DeprecationWarning(
                'Field type should not be "None". Please explicitly '
                'use TYPE_BYTES instead.'))
            return value

        if ftype == TYPE_BYTES:
            return value

        if hasattr(ftype, '__call__'):
            return ftype(value, self.endianness)

        if ftype == TYPE_STRING:
            return value.decode('utf-8')

        if ftype in ('str', 'unicode'):
            warnings.warn(DeprecationWarning(
                'The "{ftype}" field type is deprecated. Please use "string" '
                '(TYPE_STRING) instead.'
                .format(ftype=ftype)))
            return six.u(value)

        _numeric_types = {
            TYPE_U8: 'B', TYPE_I8: 'b',
            TYPE_U16: 'H', TYPE_I16: 'h',
            TYPE_U32: 'I', TYPE_I32: 'i',
            TYPE_U64: 'Q', TYPE_I64: 'q',
        }
        if ftype in _numeric_types:
            fmt = self.endianness + _numeric_types[ftype]
            return struct.unpack(fmt, value)[0]

        if ftype == TYPE_IPV4:
            return unpack_ipv4(value)

        if ftype == TYPE_IPV4_MASK:
            return unpack_ipv4(value[:4]), unpack_ipv4(value[4:8])

        if ftype == TYPE_IPV6:
            return unpack_ipv6(value)

        if ftype == TYPE_IPV6_PREFIX:
            return (unpack_ipv6(value[:16]),
                    struct.unpack(self.endianness + 'B', value[16]))

        if ftype == TYPE_MACADDR:
            return unpack_macaddr(value)

        if ftype == TYPE_EUIADDR:
            return unpack_euiaddr(value)

        raise ValueError('Unsupported field type: {0}'.format(ftype))
Example #3
0
    def _convert_value(self, value, ftype):
        if ftype is None:
            return value

        if hasattr(ftype, '__call__'):
            return ftype(value, self.endianness)

        if ftype in ('str', 'string', 'unicode'):
            return value.decode('utf-8')

        _numeric_types = {
            'u8': 'B', 'i8': 'b',
            'u16': 'H', 'i16': 'h',
            'u32': 'I', 'i32': 'i',
            'u64': 'Q', 'i64': 'q',
        }
        if ftype in _numeric_types:
            return struct.unpack(
                self.endianness + _numeric_types[ftype], value)[0]

        if ftype == 'ipv4':
            return unpack_ipv4(value)

        if ftype == 'ipv4+mask':
            return unpack_ipv4(value[:4]), unpack_ipv4(value[4:8])

        if ftype == 'ipv6':
            return unpack_ipv6(value)

        if ftype == 'ipv6+prefix':
            return (unpack_ipv6(value[:16]),
                    struct.unpack(self.endianness + 'B', value[16]))

        if ftype == 'macaddr':
            return unpack_macaddr(value)

        if ftype == 'euiaddr':
            return unpack_euiaddr(value)

        raise ValueError('Unsupported field type: {0}'.format(ftype))
Example #4
0
    def _decode_value(self, value, ftype):
        assert isinstance(value, (bytes, bytearray))

        if ftype is None:
            warnings.warn(
                DeprecationWarning(
                    'Field type should not be "None". Please explicitly '
                    "use TYPE_BYTES instead."))
            return value

        if ftype == TYPE_BYTES:
            return value

        if hasattr(ftype, "__call__"):
            return ftype(value, self.endianness)

        if ftype == TYPE_STRING:
            return value.decode("utf-8")

        if ftype in ("str", "unicode"):
            warnings.warn(
                DeprecationWarning(
                    'The "{ftype}" field type is deprecated. Please use "string" '
                    "(TYPE_STRING) instead.".format(ftype=ftype)))
            return value.decode("utf-8")

        if ftype in _numeric_types:
            fmt = self.endianness + _numeric_types[ftype]
            return struct.unpack(fmt, value)[0]

        if ftype == TYPE_IPV4:
            return unpack_ipv4(value)

        if ftype == TYPE_IPV4_MASK:
            return unpack_ipv4(value[:4]), unpack_ipv4(value[4:8])

        if ftype == TYPE_IPV6:
            return unpack_ipv6(value)

        if ftype == TYPE_IPV6_PREFIX:
            return (
                unpack_ipv6(value[:16]),
                struct.unpack(self.endianness + "B", value[16]),
            )

        if ftype == TYPE_MACADDR:
            return unpack_macaddr(value)

        if ftype == TYPE_EUIADDR:
            return unpack_euiaddr(value)

        if ftype == TYPE_TYPE_BYTES:
            return (value[0], value[1:])

        if ftype == TYPE_EPBFLAGS:
            fmt = self.endianness + _numeric_types[TYPE_U32]
            flg = struct.unpack(fmt, value)[0]
            return EPBFlags(flg)

        if ftype == TYPE_OPT_CUSTOM_STR:
            fmt = self.endianness + _numeric_types[TYPE_U32]
            return (struct.unpack(fmt, value[0:4]), value[4:].decode("utf-8"))

        if ftype == TYPE_OPT_CUSTOM_BYTES:
            fmt = self.endianness + _numeric_types[TYPE_U32]
            return (struct.unpack(fmt, value[0:4]), value[4:])

        raise ValueError("Unsupported field type: {0}".format(ftype))
Example #5
0
def test_unpack_macaddr():
    assert unpack_macaddr('\x00\x11\x22\xaa\xbb\xcc') == \
        '00:11:22:aa:bb:cc'
Example #6
0
def test_unpack_macaddr():
    assert unpack_macaddr(b"\x00\x11\x22\xaa\xbb\xcc") == "00:11:22:aa:bb:cc"
Example #7
0
def test_unpack_macaddr():
    assert unpack_macaddr('\x00\x11\x22\xaa\xbb\xcc') == \
        '00:11:22:aa:bb:cc'
Example #8
0
def test_unpack_macaddr():
    assert unpack_macaddr("\x00\x11\x22\xaa\xbb\xcc") == "00:11:22:aa:bb:cc"