Esempio n. 1
0
def loads(blob, excp_cls=coordination.SerializationError):
    """Deserializes provided data using msgpack (from a prior byte string)."""
    try:
        return msgpackutils.loads(blob)
    except (msgpack.UnpackException, ValueError) as e:
        coordination.raise_with_cause(excp_cls, exception_message(e),
                                      cause=e)
Esempio n. 2
0
def loads(blob, excp_cls=SerializationError):
    """Deserializes provided data using msgpack (from a prior byte string)."""
    try:
        return msgpackutils.loads(blob)
    except (msgpack.UnpackException, ValueError) as e:
        raise_with_cause(excp_cls,
                         encodeutils.exception_to_unicode(e),
                         cause=e)
Esempio n. 3
0
 def deserialize(self, data):
     revoke_event_data = msgpackutils.loads(data, registry=self._registry)
     try:
         revoke_event = RevokeEvent(**revoke_event_data)
     except Exception:
         LOG.debug("Failed to deserialize RevokeEvent. Data is %s",
                   revoke_event_data)
         raise
     return revoke_event
Esempio n. 4
0
 def deserialize(self, data):
     revoke_event_data = msgpackutils.loads(data, registry=self._registry)
     try:
         revoke_event = RevokeEvent(**revoke_event_data)
     except Exception:
         LOG.debug("Failed to deserialize RevokeEvent. Data is %s",
                   revoke_event_data)
         raise
     return revoke_event
Esempio n. 5
0
    def _get_local_cache(self, key):
        # Return the version from our local request cache if it exists.
        ctx = self._get_request_context()
        try:
            value = getattr(ctx, self._get_request_key(key))
        except AttributeError:
            return api.NO_VALUE

        value = msgpackutils.loads(value)
        return api.CachedValue(payload=value["payload"], metadata=value["metadata"])
Esempio n. 6
0
    def test_custom_register(self):
        registry = msgpackutils.default_registry.copy(unfreeze=True)
        registry.register(ColorHandler())

        c = Color(255, 254, 253)
        c_b = msgpackutils.dumps(c, registry=registry)
        c = msgpackutils.loads(c_b, registry=registry)

        self.assertEqual(255, c.r)
        self.assertEqual(254, c.g)
        self.assertEqual(253, c.b)
Esempio n. 7
0
    def _get_local_cache(self, key):
        # Return the version from our local request cache if it exists.
        ctx = self._get_request_context()
        try:
            value = getattr(ctx, self._get_request_key(key))
        except AttributeError:
            return api.NO_VALUE

        value = msgpackutils.loads(value)
        return api.CachedValue(payload=value['payload'],
                               metadata=value['metadata'])
    def test_custom_register(self):
        registry = msgpackutils.default_registry.copy(unfreeze=True)
        registry.register(ColorHandler())

        c = Color(255, 254, 253)
        c_b = msgpackutils.dumps(c, registry=registry)
        c = msgpackutils.loads(c_b, registry=registry)

        self.assertEqual(255, c.r)
        self.assertEqual(254, c.g)
        self.assertEqual(253, c.b)
Esempio n. 9
0
 def deserialize(self, data):
     token_data = msgpackutils.loads(data, registry=self._registry)
     try:
         token_model = TokenModel()
         for k, v in iter(token_data.items()):
             setattr(token_model, k, v)
     except Exception:
         LOG.debug("Failed to deserialize TokenModel. Data is %s",
                   token_data)
         raise exception.CacheDeserializationError(TokenModel.__name__,
                                                   token_data)
     return token_model
Esempio n. 10
0
 def deserialize(self, data):
     receipt_data = msgpackutils.loads(data, registry=self._registry)
     try:
         receipt_model = ReceiptModel()
         for k, v in iter(receipt_data.items()):
             setattr(receipt_model, k, v)
     except Exception:
         LOG.debug("Failed to deserialize ReceiptModel. Data is %s",
                   receipt_data)
         raise exception.CacheDeserializationError(ReceiptModel.__name__,
                                                   receipt_data)
     return receipt_model
Esempio n. 11
0
 def deserialize(self, data):
     receipt_data = msgpackutils.loads(data, registry=self._registry)
     try:
         receipt_model = ReceiptModel()
         for k, v in iter(receipt_data.items()):
             setattr(receipt_model, k, v)
     except Exception:
         LOG.debug(
             "Failed to deserialize ReceiptModel. Data is %s", receipt_data
         )
         raise exception.CacheDeserializationError(
             ReceiptModel.__name__, receipt_data
         )
     return receipt_model
Esempio n. 12
0
 def deserialize(self, data):
     token_data = msgpackutils.loads(data, registry=self._registry)
     try:
         token_model = TokenModel()
         for k, v in iter(token_data.items()):
             setattr(token_model, k, v)
     except Exception:
         LOG.debug(
             "Failed to deserialize TokenModel. Data is %s", token_data
         )
         raise exception.CacheDeserializationError(
             TokenModel.__name__, token_data
         )
     return token_model
Esempio n. 13
0
def decode_msgpack(raw_data, root_types=(dict, )):
    """Parse raw data to get decoded object.

    Decodes a msgback encoded 'blob' from a given raw data binary string and
    checks that the root type of that decoded object is in the allowed set of
    types (by default a dict should be the root type).
    """
    try:
        data = msgpackutils.loads(raw_data)
    except Exception as e:
        # TODO(harlowja): fix this when msgpackutils exposes the msgpack
        # exceptions so that we can avoid catching just exception...
        raise ValueError("Expected msgpack decodable data: %s" % e)
    else:
        return _check_decoded_type(data, root_types=root_types)
Esempio n. 14
0
def decode_msgpack(raw_data, root_types=(dict,)):
    """Parse raw data to get decoded object.

    Decodes a msgback encoded 'blob' from a given raw data binary string and
    checks that the root type of that decoded object is in the allowed set of
    types (by default a dict should be the root type).
    """
    try:
        data = msgpackutils.loads(raw_data)
    except Exception as e:
        # TODO(harlowja): fix this when msgpackutils exposes the msgpack
        # exceptions so that we can avoid catching just exception...
        raise ValueError("Expected msgpack decodable data: %s" % e)
    else:
        return _check_decoded_type(data, root_types=root_types)
Esempio n. 15
0
 def deserialize(self, data):
     revoke_event_data = msgpackutils.loads(data, registry=self._registry)
     revoke_event = RevokeEvent(**revoke_event_data)
     return revoke_event
Esempio n. 16
0
 def load_from_bytes(self, s):
     return msgpackutils.loads(s, registry=self._registry)
Esempio n. 17
0
 def deserialize(self, data):
     revoke_event_data = msgpackutils.loads(data, registry=self._registry)
     revoke_event = RevokeEvent(**revoke_event_data)
     return revoke_event
Esempio n. 18
0
    def _validate(self, answer: str) -> Tuple[str, bool]:
        try:
            conn_str_bytes = base64.decode_as_bytes(answer.encode('ascii'))
        except TypeError:
            print(
                'The connection string contains non-ASCII'
                ' characters please make sure you entered'
                ' it as returned by the add-compute command.',
                file=sys.stderr)
            return answer, False

        try:
            conn_info = msgpackutils.loads(conn_str_bytes)
        except msgpack.exceptions.ExtraData:
            print(
                'The connection string contains extra data'
                ' characters please make sure you entered'
                ' it as returned by the add-compute command.',
                file=sys.stderr)
            return answer, False
        except ValueError:
            print(
                'The connection string contains extra data'
                ' characters please make sure you entered'
                ' it as returned by the add-compute command.',
                file=sys.stderr)
            return answer, False
        except msgpack.exceptions.FormatError:
            print(
                'The connection string format is invalid'
                ' please make sure you entered'
                ' it as returned by the add-compute command.',
                file=sys.stderr)
            return answer, False
        except Exception:
            print(
                'An unexpeted error has occured while trying'
                ' to decode the connection string. Please'
                ' make sure you entered it as returned by'
                ' the add-compute command and raise an'
                ' issue if the error persists',
                file=sys.stderr)
            return answer, False

        # Perform token field validation as well so that the rest of
        # the code-base can assume valid input.
        # The input can be either an IPv4 or IPv6 address or a hostname.
        hostname = conn_info.get('hostname')
        try:
            is_valid_address = self._validate_address(hostname)
            is_valid_address = True
        except ValueError:
            logger.debug('The hostname specified in the connection string is'
                         ' not an IPv4 or IPv6 address - treating it as'
                         ' a hostname.')
            is_valid_address = False
        if not is_valid_address:
            try:
                self._validate_hostname(hostname)
            except ValueError as e:
                print(
                    f'The hostname {hostname} provided in the connection'
                    f' string is invalid: {str(e)}',
                    file=sys.stderr)
                return answer, False

        fingerprint = conn_info.get('fingerprint')
        try:
            self._validate_fingerprint(fingerprint)
        except ValueError as e:
            print(
                'The clustering service TLS certificate fingerprint provided'
                f' in the connection string is invalid: {str(e)}',
                file=sys.stderr)
            return answer, False

        credential_id = conn_info.get('id')
        try:
            self._validate_credential_id(credential_id)
        except ValueError as e:
            print(
                'The credential id provided in the connection string is'
                f' invalid: {str(e)}',
                file=sys.stderr)
            return answer, False

        credential_secret = conn_info.get('secret')
        try:
            self._validate_credential_secret(credential_secret)
        except ValueError as e:
            print(
                'The credential secret provided in the connection string is'
                f' invalid: {str(e)}',
                file=sys.stderr)
            return answer, False

        self._conn_info = conn_info
        return answer, True
 def _unserialize_measures(data):
     return msgpackutils.loads(data)
Esempio n. 20
0
 def _unserialize_measures(data):
     return msgpackutils.loads(data)
 def load_from_bytes(self, s):
     return msgpackutils.loads(s, registry=self._registry)
Esempio n. 22
0
 def deserialize(self, data):
     revoke_map = msgpackutils.loads(data, registry=self._registry)
     revoke_tree = revoke_model.RevokeTree()
     revoke_tree.revoke_map = revoke_map
     return revoke_tree
def _dumps_loads(obj):
    obj = msgpackutils.dumps(obj)
    return msgpackutils.loads(obj)
Esempio n. 24
0
        print("color blob: ", blob)
        return blob
    
    @staticmethod
    def deserialize(data):
        chunks = [int(c.strip()) for c in data.split(b",")]
        return Color(chunks[0], chunks[1], chunks[2])

registry = msgpackutils.default_registry.copy(unfreeze=True)
registry.register(ColorHandler())

c = Color(255, 254, 253)
c_b = msgpackutils.dumps(c, registry=registry)
print(c_b)
c = msgpackutils.loads(c_b, registry=registry)
print(c.r, c.g, c.b)

'''
msgpackutils 用于将python中的数据对象(某种类型,如这里的UUID,Color)串行化到文件或者字符串变量中。
方法及过程为:
1. 定义对象
2. 定对象的处理逻辑,主要包含四个部分:
  a. handles 变量:用于表明可以处理的数据类型。
  b. identity 变量:用于串行化到字符串中表明原有数据类型。
  c. serialize 函数:用于串行化对象
  d. deserialize 函数:反串行化数据流(怎么串行化就怎么反过程处理)
3. 获取registry 对象,相当于一个namespace,所有的处理过程的定义在一个registry下。
4. 注册串行化Handler到registry。
5. 使用:
  a. msgpackutils.loads/dumps/load/dump 这都需要一个registry即处理函数注册的空间作为参数。
Esempio n. 25
0
def _dumps_loads(obj):
    obj = msgpackutils.dumps(obj)
    return msgpackutils.loads(obj)