Python msgpack.ExtType() Examples

The following are 30 code examples of msgpack.ExtType(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module msgpack , or try the search function .
Example #1
Source File: mailbox.py    From mochi with MIT License 7 votes vote down vote up
def encode(obj):
    if type(obj) in (list, tuple) or isinstance(obj, PVector):
        return [encode(item) for item in obj]
    if isinstance(obj, Mapping):
        encoded_obj = {}
        for key in obj.keys():
            encoded_obj[encode(key)] = encode(obj[key])
        return encoded_obj
    if isinstance(obj, _native_builtin_types):
        return obj
    if isinstance(obj, Set):
        return ExtType(TYPE_PSET, packb([encode(item) for item in obj], use_bin_type=True))
    if isinstance(obj, PList):
        return ExtType(TYPE_PLIST, packb([encode(item) for item in obj], use_bin_type=True))
    if isinstance(obj, PBag):
        return ExtType(TYPE_PBAG, packb([encode(item) for item in obj], use_bin_type=True))
    if isinstance(obj, types.FunctionType):
        return ExtType(TYPE_FUNC, encode_func(obj))
    if isinstance(obj, Receiver):
        return ExtType(TYPE_MBOX, packb(obj.encode(), use_bin_type=True))
    # assume record
    cls = obj.__class__
    return ExtType(0, packb([cls.__module__, cls.__name__] + [encode(item) for item in obj],
                            use_bin_type=True)) 
Example #2
Source File: serializers.py    From king-phisher with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _msgpack_default(cls, obj):
		obj_type, obj_value = _serialize_ext_dump(obj)
		obj_type = next(i[0] for i in cls._ext_types.items() if i[1] == obj_type)
		if its.py_v3 and isinstance(obj_value, str):
			obj_value = obj_value.encode(cls.encoding)
		return msgpack.ExtType(obj_type, obj_value) 
Example #3
Source File: nvim.py    From pynvim with Apache License 2.0 5 votes vote down vote up
def _to_nvim(self, obj):
        if isinstance(obj, Remote):
            return ExtType(*obj.code_data)
        return obj 
Example #4
Source File: nvim.py    From pynvim with Apache License 2.0 5 votes vote down vote up
def _from_nvim(self, obj, decode=None):
        if decode is None:
            decode = self._decode
        if type(obj) is ExtType:
            cls = self.types[obj.code]
            return cls(self, (obj.code, obj.data))
        if decode:
            obj = decode_if_bytes(obj, decode)
        return obj 
Example #5
Source File: mailbox.py    From mochi with MIT License 5 votes vote down vote up
def decode(obj):
    if isinstance(obj, ExtType):
        if obj.code == TYPE_PSET:
            unpacked_data = unpackb(obj.data,
                                    use_list=False,
                                    encoding='utf-8')
            return pset(decode(item) for item in unpacked_data)
        if obj.code == TYPE_PLIST:
            unpacked_data = unpackb(obj.data,
                                    use_list=False,
                                    encoding='utf-8')
            return plist(decode(item) for item in unpacked_data)
        if obj.code == TYPE_PBAG:
            unpacked_data = unpackb(obj.data,
                                    use_list=False,
                                    encoding='utf-8')
            return pbag(decode(item) for item in unpacked_data)
        if obj.code == TYPE_FUNC:
            return decode_func(obj.data)
        module_name, class_name, *data = unpackb(obj.data,
                                                 use_list=False,
                                                 encoding='utf-8')
        cls = getattr(sys.modules[module_name],
                      class_name)
        if obj.code == TYPE_MBOX:
            return cls.decode(data)
        return cls(*(decode(item) for item in data))
    if isinstance(obj, tuple):
        return pvector(decode(item) for item in obj)
    if isinstance(obj, dict):
        new_dict = dict()
        for key in obj.keys():
            new_dict[decode(key)] = decode(obj[key])
        return pmap(new_dict)
    return obj 
Example #6
Source File: pack.py    From petlib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_streaming():

    # Define a custom class, encoder and decoder
    class CustomClass2:
        def __eq__(self, other):
            return isinstance(other, CustomClass2)

    def enc_CustomClass(obj):
        if isinstance(obj, CustomClass2):
            return msgpack.ExtType(12, b'')
        raise TypeError("Unknown type: %r" % (obj,))

    def dec_CustomClass(code, data):
        if code == 12:
            return CustomClass2()

        return msgpack.ExtType(code, data)

    # Define a structure
    G = EcGroup()
    custom_obj = CustomClass2()
    test_data = [G, G.generator(), G.order(), custom_obj]
    packed1 = encode(test_data, enc_CustomClass)
    packed2 = encode(test_data, enc_CustomClass)

    data = packed1 + packed2

    decoder = make_decoder(dec_CustomClass)
    Up = msgpack.Unpacker(ext_hook=decoder)
    Up.feed(data)
    for o in Up:
        assert o == test_data 
Example #7
Source File: pack.py    From petlib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_enc_dec_custom():

    # Define a custom class, encoder and decoder
    class CustomClass:
        def __eq__(self, other):
            return isinstance(other, CustomClass)

    def enc_CustomClass(obj):
        if isinstance(obj, CustomClass):
            return msgpack.ExtType(11, b'')
        raise TypeError("Unknown type: %r" % (obj,))

    def dec_CustomClass(code, data):
        if code == 11:
            return CustomClass()

        return msgpack.ExtType(code, data)

    # Define a structure
    G = EcGroup()
    custom_obj = CustomClass()
    test_data = [G, G.generator(), G.order(), custom_obj]

    # Encode and decode custom structure
    packed = encode(test_data, enc_CustomClass)
    x = decode(packed, dec_CustomClass)
    assert x == test_data 
Example #8
Source File: pack.py    From petlib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def make_decoder(custom_decoder=None):
    if custom_decoder is None:
        return ext_hook
    else:
        def new_decoder(code, data):
            out = ext_hook(code, data)
            if not isinstance(out, msgpack.ExtType):
                return out
            else:
                return custom_decoder(code, data)
        return new_decoder 
Example #9
Source File: pack.py    From petlib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def ext_hook(code, data):
    if code in _unpack_reg:
        _, _, _, dec = _unpack_reg[code]
        return dec(data)

    # Other
    return msgpack.ExtType(code, data) 
Example #10
Source File: pack.py    From petlib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def default(obj):
    # Serialize Bn objects
    for T in _pack_reg:
        if isinstance(obj, T):
            _, num, enc, _ = _pack_reg[T]
            return msgpack.ExtType(num, enc(obj))

    raise TypeError("Unknown type: %r" % (type(obj),)) 
Example #11
Source File: bridge.py    From enaml-native with MIT License 5 votes vote down vote up
def encode(obj):
    """ Encode an object for proper decoding by Java or ObjC
    """
    if hasattr(obj, '__id__'):
        return msgpack.ExtType(ExtType.REF, msgpack.packb(obj.__id__))
    return obj 
Example #12
Source File: contracts.py    From ledger-api-py with Apache License 2.0 5 votes vote down vote up
def _encode_msgpack_payload(cls, *args):
        items = []
        for value in args:
            if cls._is_primitive(value):
                items.append(value)
            elif isinstance(value, Address):
                items.append(msgpack.ExtType(77, bytes(value)))
            else:
                raise RuntimeError('Unknown item to pack: ' + value.__class__.__name__)
        return msgpack.packb(items) 
Example #13
Source File: common.py    From ledger-api-py with Apache License 2.0 5 votes vote down vote up
def _encode_msgpack_payload(cls, *args):
        items = []
        for value in args:
            if cls._is_primitive(value):
                items.append(value)
            elif isinstance(value, Address):
                items.append(msgpack.ExtType(77, bytes(value)))
            else:
                raise RuntimeError('Unknown item to pack: ' + value.__class__.__name__)
        return msgpack.packb(items) 
Example #14
Source File: properties.py    From anom-py with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _serialize(cls, value):
        if isinstance(value, model.Model):
            kind, value = cls.Extensions.Model, cls._entity_to_dict(value)

        elif isinstance(value, datetime):
            kind, value = cls.Extensions.DateTime, _seconds_since_epoch(value)

        else:
            raise TypeError(f"Value of type {type(value)} cannot be serialized.")

        return msgpack.ExtType(kind, cls._dumps(value)) 
Example #15
Source File: test_properties.py    From anom-py with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_msgpacks_fail_to_load_invalid_data():
    def default(ob):
        return msgpack.ExtType(127, b"")

    with pytest.raises(ValueError):
        props.Msgpack().prepare_to_load(None, msgpack.packb(object(), default=default)) 
Example #16
Source File: lastest_files_with_msgpack.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def ext_hook(code, data):
    if code == 42:
        p = PasteFile.from_dict(data)
        return p
    return msgpack.ExtType(code, data) 
Example #17
Source File: lastest_files_with_msgpack.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def default(obj):
    if isinstance(obj, PasteFile):
        return msgpack.ExtType(42, obj.to_dict())
    raise TypeError('Unknown type: %r' % (obj,)) 
Example #18
Source File: neovim_rpc_protocol.py    From vim-hug-neovim-rpc with MIT License 5 votes vote down vote up
def to_client(msg):
    def handler(obj):
        if type(obj) == BUFFER_TYPE:
            return msgpack.ExtType(BUFFER_TYPE_ID, msgpack.packb(obj.number))
        if type(obj) == WINDOW_TYPE:
            return msgpack.ExtType(WINDOW_TYPE_ID, msgpack.packb(obj.number))
        if type(obj) == vim.Function:
            try:
                return obj.name.encode()
            except Exception:
                return ""
        return obj
    return walk(handler, msg) 
Example #19
Source File: neovim_rpc_protocol.py    From vim-hug-neovim-rpc with MIT License 5 votes vote down vote up
def from_client(msg):
        def handler(obj):
            if type(obj) is msgpack.ExtType:
                if obj.code == BUFFER_TYPE_ID:
                    return vim.buffers[msgpack.unpackb(obj.data)]
                if obj.code == WINDOW_TYPE_ID:
                    return vim.windows[msgpack.unpackb(obj.data) - 1]
            elif obj is None:
                return ''
            if sys.version_info.major != 2:
                # python3 needs decode
                obj = decode_if_bytes(obj)
            return obj
        return walk(handler, msg) 
Example #20
Source File: neovim_rpc_protocol.py    From vim-hug-neovim-rpc with MIT License 5 votes vote down vote up
def from_client(msg):
        def handler(obj):
            if type(obj) is msgpack.ExtType:
                if obj.code == BUFFER_TYPE_ID:
                    return vim.buffers[msgpack.unpackb(obj.data)]
                if obj.code == WINDOW_TYPE_ID:
                    return vim.windows[msgpack.unpackb(obj.data) - 1]
            if sys.version_info.major != 2:
                # python3 needs decode
                obj = decode_if_bytes(obj)
            return obj
        return walk(handler, msg) 
Example #21
Source File: msgpackutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def _unserializer(registry, code, data):
    handler = registry.get(code)
    if not handler:
        return msgpack.ExtType(code, data)
    else:
        return handler.deserialize(data) 
Example #22
Source File: msgpackutils.py    From oslo.serialization with Apache License 2.0 5 votes vote down vote up
def _serializer(registry, obj):
    handler = registry.match(obj)
    if handler is None:
        raise ValueError("No serialization handler registered"
                         " for type '%s'" % (type(obj).__name__))
    return msgpack.ExtType(handler.identity, handler.serialize(obj)) 
Example #23
Source File: fallback.py    From io_three with MIT License 5 votes vote down vote up
def __init__(self, file_like=None, read_size=0, use_list=True,
                 object_hook=None, object_pairs_hook=None, list_hook=None,
                 encoding=None, unicode_errors='strict', max_buffer_size=0,
                 ext_hook=ExtType):
        if file_like is None:
            self._fb_feeding = True
        else:
            if not callable(file_like.read):
                raise TypeError("`file_like.read` must be callable")
            self.file_like = file_like
            self._fb_feeding = False
        self._fb_buffers = []
        self._fb_buf_o = 0
        self._fb_buf_i = 0
        self._fb_buf_n = 0
        self._max_buffer_size = max_buffer_size or 2**31-1
        if read_size > self._max_buffer_size:
            raise ValueError("read_size must be smaller than max_buffer_size")
        self._read_size = read_size or min(self._max_buffer_size, 2048)
        self._encoding = encoding
        self._unicode_errors = unicode_errors
        self._use_list = use_list
        self._list_hook = list_hook
        self._object_hook = object_hook
        self._object_pairs_hook = object_pairs_hook
        self._ext_hook = ext_hook

        if list_hook is not None and not callable(list_hook):
            raise TypeError('`list_hook` is not callable')
        if object_hook is not None and not callable(object_hook):
            raise TypeError('`object_hook` is not callable')
        if object_pairs_hook is not None and not callable(object_pairs_hook):
            raise TypeError('`object_pairs_hook` is not callable')
        if object_hook is not None and object_pairs_hook is not None:
            raise TypeError("object_pairs_hook and object_hook are mutually "
                            "exclusive")
        if not callable(ext_hook):
            raise TypeError("`ext_hook` is not callable") 
Example #24
Source File: advancedhttpserver.py    From AdvancedHTTPServer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _msgpack_ext_hook(code, obj_value):
		default = msgpack.ExtType(code, obj_value)
		if sys.version_info[0] == 3:
			obj_value = obj_value.decode('utf-8')
		obj_type = _MSGPACK_EXT_TYPES.get(code)
		return _serialize_ext_load(obj_type, obj_value, default) 
Example #25
Source File: advancedhttpserver.py    From AdvancedHTTPServer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _msgpack_default(obj):
		obj_type, obj_value = _serialize_ext_dump(obj)
		obj_type = next(i[0] for i in _MSGPACK_EXT_TYPES.items() if i[1] == obj_type)
		if sys.version_info[0] == 3:
			obj_value = obj_value.encode('utf-8')
		return msgpack.ExtType(obj_type, obj_value) 
Example #26
Source File: msgpack.py    From yosai with Apache License 2.0 5 votes vote down vote up
def _custom_object_hook(self, code: int, data: bytes):
        if code == self.custom_type_code:
            typename, payload = data.split(b':', 1)
            state = self.deserialize(payload)
            try:
                cls, unmarshaller = self._unmarshallers[typename]
            except KeyError:
                raise LookupError('no unmarshaller found for type "{}"'
                                  .format(typename.decode('utf-8'))) from None

            instance = cls.__new__(cls)
            unmarshaller(instance, state)
            return instance
        else:
            return ExtType(code, data) 
Example #27
Source File: msgpack.py    From yosai with Apache License 2.0 5 votes vote down vote up
def _default_encoder(self, obj):
        obj_type = obj.__class__
        try:
            typename, marshaller = self._marshallers[obj_type]
        except KeyError:
            raise LookupError('no marshaller found for type "{}"'
                              .format(obj_type.__class__.__name__)) from None

        state = marshaller(obj)
        data = typename + b':' + self.serialize(state)
        return ExtType(self.custom_type_code, data) 
Example #28
Source File: serializers.py    From king-phisher with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _msgpack_ext_hook(cls, code, obj_value):
		default = msgpack.ExtType(code, obj_value)
		if its.py_v3 and isinstance(obj_value, bytes):
			obj_value = obj_value.decode(cls.encoding)
		obj_type = cls._ext_types.get(code)
		return _serialize_ext_load(obj_type, obj_value, default) 
Example #29
Source File: serializers.py    From Pyro5 with MIT License 5 votes vote down vote up
def default(self, obj):
        replacer = self.__type_replacements.get(type(obj), None)
        if replacer:
            obj = replacer(obj)
        if isinstance(obj, set):
            return tuple(obj)  # msgpack module can't deal with sets so we make a tuple out of it
        if isinstance(obj, uuid.UUID):
            return str(obj)
        if isinstance(obj, complex):
            return msgpack.ExtType(0x30, struct.pack("dd", obj.real, obj.imag))
        if isinstance(obj, datetime.datetime):
            if obj.tzinfo:
                raise errors.SerializeError("msgpack cannot serialize datetime with timezone info")
            return msgpack.ExtType(0x32, struct.pack("d", obj.timestamp()))
        if isinstance(obj, datetime.date):
            return msgpack.ExtType(0x33, struct.pack("l", obj.toordinal()))
        if isinstance(obj, decimal.Decimal):
            return str(obj)
        if isinstance(obj, numbers.Number):
            return msgpack.ExtType(0x31, str(obj).encode("ascii"))     # long
        if isinstance(obj, array.array):
            if obj.typecode == 'c':
                return obj.tostring()
            if obj.typecode == 'u':
                return obj.tounicode()
            return obj.tolist()
        return self.class_to_dict(obj) 
Example #30
Source File: msgpack_serializer.py    From pysoa with Apache License 2.0 4 votes vote down vote up
def _default(self, obj):  # type: (Any) -> msgpack.ExtType
        """
        Encodes unknown object types (we use it to make extended types)
        """
        if isinstance(obj, datetime.datetime):
            # Serialize date-time objects. Make sure they're naive.
            ext_code = self.EXT_DATETIME
            epoch = self.EPOCH
            if obj.tzinfo is not None:
                if obj.tzinfo == pytz.UTC:
                    ext_code = self.EXT_DATETIME_UTC
                    epoch = self.EPOCH_UTC
                else:
                    raise TypeError('Cannot encode time zone-aware date-times to MessagePack')
            # Then, work out the timestamp in seconds.
            seconds = (obj - epoch).total_seconds()
            microseconds = int(seconds * 1000000.0)
            # Then pack it into a big-endian signed 64-bit integer.
            return msgpack.ExtType(
                ext_code,
                self.STRUCT_DATETIME.pack(microseconds),
            )
        elif isinstance(obj, datetime.date):
            # Serialize local-date objects by packing to a big-endian unsigned short and two big-endian unsigned chars.
            return msgpack.ExtType(
                self.EXT_DATE,
                self.STRUCT_DATE.pack(obj.year, obj.month, obj.day),
            )
        elif isinstance(obj, datetime.time):
            # Serialize dateless-time objects by packing to three big-endian unsigned chars and a big-endian unsigned
            # 32-bit integer.
            return msgpack.ExtType(
                self.EXT_TIME,
                self.STRUCT_TIME.pack(obj.hour, obj.minute, obj.second, obj.microsecond),
            )
        elif isinstance(obj, decimal.Decimal):
            obj_str = six.text_type(obj)[:65535].encode('utf-8')
            obj_len = len(obj_str)
            obj_encoder = struct.Struct(str('!H{}s'.format(obj_len)))
            return msgpack.ExtType(
                self.EXT_DECIMAL,
                obj_encoder.pack(obj_len, obj_str),
            )
        elif isinstance(obj, currint.Amount):
            # Serialize Amount objects. Start with the lowercased currency code as bytes.
            code = obj.currency.code.upper()
            if isinstance(code, six.text_type):
                code = code.encode('ascii')
            # Then pack it in with the minor value.
            return msgpack.ExtType(
                self.EXT_CURRINT,
                self.STRUCT_CURRINT.pack(code, obj.value),
            )

        # Wuh-woh
        raise TypeError('Cannot encode value of type {} to MessagePack: {}'.format(type(obj).__name__, obj))