def _check_for_path_errors(self, pointer):
     if not re.match(self.PATH_REGEX_COMPILED, pointer):
         msg = _("Json path should start with a '/', "
                 "end with no '/', no 2 subsequent '/' are allowed.")
         raise exc.InvalidJsonPatchPath(path=pointer, explanation=msg)
     if re.search('~[^01]', pointer) or pointer.endswith('~'):
         msg = _("Pointer contains '~' which is not part of"
                 " a recognized escape sequence [~0, ~1].")
         raise exc.InvalidJsonPatchPath(path=pointer, explanation=msg)
Ejemplo n.º 2
0
def wrap_property(prop_value, full_path):
    if isinstance(prop_value, list):
        return ArtifactListPropertyProxy(prop_value, full_path)
    if isinstance(prop_value, dict):
        return ArtifactDictPropertyProxy(prop_value, full_path)
    # no other types are supported
    raise exc.InvalidJsonPatchPath(path=full_path)
Ejemplo n.º 3
0
    def _proc_key(self, idx_str, should_exist=True):
        """JsonPatchUpdateMixin method overload.

        Only integers less than current array length and '-' (last elem)
        in path are allowed.
        Raises an InvalidJsonPatchPath exception if any of the conditions above
        are not met.
        """
        if idx_str == '-':
            return len(self) - 1
        try:
            idx = int(idx_str)
            if not should_exist and len(self) == 0:
                return 0
            if len(self) < idx + 1:
                msg = _("Array has no element at position %d") % idx
                raise exc.InvalidJsonPatchPath(explanation=msg, path=idx)
            return idx
        except (ValueError, TypeError):
            msg = _("Not an array idx '%s'") % idx_str
            raise exc.InvalidJsonPatchPath(explanation=msg, path=idx_str)
Ejemplo n.º 4
0
 def _perform_op(self, op, **kwargs):
     path = kwargs.get("path")
     value = kwargs.get("value")
     prop_name, delimiter, path_left = path.lstrip('/').partition('/')
     if not path_left:
         return setattr(self, prop_name, value)
     try:
         prop = self._get_prop_to_update(prop_name, path_left)
         # correct path_left and call corresponding update method
         kwargs["path"] = path_left
         getattr(prop, op)(path=kwargs["path"], value=kwargs.get("value"))
         return setattr(self, prop_name, prop)
     except exc.InvalidJsonPatchPath:
         # NOTE(ivasilevskaya): here exception is reraised with
         # 'part of path' substituted with with 'full path' to form a
         # more relevant message
         raise exc.InvalidJsonPatchPath(
             path=path, explanation=_("No property to access"))
Ejemplo n.º 5
0
 def _proc_key(self, key_str, should_exist=True):
     """JsonPatchUpdateMixin method overload"""
     if should_exist and key_str not in self.keys():
         msg = _("No such key '%s' in a dict") % key_str
         raise exc.InvalidJsonPatchPath(path=key_str, explanation=msg)
     return key_str