예제 #1
0
    def apply(self, obj):
        try:
            from_ptr = JsonPointer(self.operation['from'])
        except KeyError as ex:
            raise InvalidJsonPatch(
                "The operation does not contain a 'from' member")

        subobj, part = from_ptr.to_last(obj)
        try:
            value = subobj[part]
        except (KeyError, IndexError) as ex:
            raise JsonPatchConflict(str(ex))

        # If source and target are equal, this is a no-op
        if self.pointer == from_ptr:
            return obj

        if isinstance(subobj, MutableMapping) and \
                self.pointer.contains(from_ptr):
            raise JsonPatchConflict('Cannot move values into its own children')

        obj = RemoveOperation({
            'op': 'remove',
            'path': self.operation['from']
        }).apply(obj)

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #2
0
파일: jsonpatch.py 프로젝트: 602p/spacegame
    def apply(self, obj):
        try:
            from_ptr = JsonPointer(self.operation['from'])
        except KeyError as ex:
            raise InvalidJsonPatch(
                "The operation does not contain a 'from' member")

        subobj, part = from_ptr.to_last(obj)
        try:
            value = subobj[part]
        except (KeyError, IndexError) as ex:
            raise JsonPatchConflict(str(ex))

        if isinstance(subobj, MutableMapping) and \
                self.pointer.contains(from_ptr):
            raise JsonPatchConflict('Cannot move values into its own children')

        obj = RemoveOperation({
            'op': 'remove',
            'path': self.operation['from']
        }).apply(obj)

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #3
0
    def apply(self, obj):
        from_ptr = JsonPointer(self.operation['from'])
        subobj, part = from_ptr.to_last(obj)
        value = copy.deepcopy(subobj[part])

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #4
0
    def apply(self, obj):
        from_ptr = JsonPointer(self.operation['from'])
        subobj, part = from_ptr.to_last(obj)
        value = copy.deepcopy(subobj[part])

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #5
0
    def apply(self, obj):
        from_ptr = JsonPointer(self.operation['from'])
        subobj, part = from_ptr.to_last(obj)
        try:
            value = copy.deepcopy(subobj[part])
        except (KeyError, IndexError) as ex:
            raise JsonPatchConflict(str(ex))

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #6
0
    def apply(self, obj):
        from_ptr = JsonPointer(self.operation['from'])
        subobj, part = from_ptr.to_last(obj)
        try:
            value = copy.deepcopy(subobj[part])
        except (KeyError, IndexError) as ex:
            raise JsonPatchConflict(str(ex))

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #7
0
    def apply(self, obj):
        try:
            from_ptr = JsonPointer(self.operation["from"])
        except KeyError as ex:
            raise InvalidJsonPatch("The operation does not contain a 'from' member")

        subobj, part = from_ptr.to_last(obj)
        try:
            value = copy.deepcopy(subobj[part])
        except (KeyError, IndexError) as ex:
            raise JsonPatchConflict(str(ex))

        obj = AddOperation({"op": "add", "path": self.location, "value": value}).apply(obj)

        return obj
예제 #8
0
    def apply(self, obj):
        from_ptr = JsonPointer(self.operation['from'])
        subobj, part = from_ptr.to_last(obj)
        value = subobj[part]

        if self.pointer.contains(from_ptr):
            raise JsonPatchException('Cannot move values into its own children')

        obj = RemoveOperation({
            'op': 'remove',
            'path': self.operation['from']
        }).apply(obj)

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #9
0
    def apply(self, obj):
        from_ptr = JsonPointer(self.operation['from'])
        subobj, part = from_ptr.to_last(obj)
        value = subobj[part]

        if self.pointer.contains(from_ptr):
            raise JsonPatchException(
                'Cannot move values into its own children')

        obj = RemoveOperation({
            'op': 'remove',
            'path': self.operation['from']
        }).apply(obj)

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #10
0
    def apply(self, obj):
        from_ptr = JsonPointer(self.operation['from'])
        subobj, part = from_ptr.to_last(obj)
        try:
            value = subobj[part]
        except (KeyError, IndexError) as ex:
            raise JsonPatchConflict(str(ex))

        if isinstance(subobj, dict) and self.pointer.contains(from_ptr):
            raise JsonPatchException('Cannot move values into its own children')

        obj = RemoveOperation({
            'op': 'remove',
            'path': self.operation['from']
        }).apply(obj)

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #11
0
    def apply(self, obj):
        from_ptr = JsonPointer(self.operation['from'])
        subobj, part = from_ptr.to_last(obj)
        try:
            value = subobj[part]
        except (KeyError, IndexError) as ex:
            raise JsonPatchConflict(str(ex))

        if isinstance(subobj, dict) and self.pointer.contains(from_ptr):
            raise JsonPatchException('Cannot move values into its own children')

        obj = RemoveOperation({
            'op': 'remove',
            'path': self.operation['from']
        }).apply(obj)

        obj = AddOperation({
            'op': 'add',
            'path': self.location,
            'value': value
        }).apply(obj)

        return obj
예제 #12
0
    def apply(self, obj):
        try:
            from_ptr = JsonPointer(self.operation["from"])
        except KeyError as ex:
            raise InvalidJsonPatch("The operation does not contain a 'from' member")

        subobj, part = from_ptr.to_last(obj)
        try:
            value = subobj[part]
        except (KeyError, IndexError) as ex:
            raise JsonPatchConflict(str(ex))

        # If source and target are equal, this is a no-op
        if self.pointer == from_ptr:
            return obj

        if isinstance(subobj, MutableMapping) and self.pointer.contains(from_ptr):
            raise JsonPatchConflict("Cannot move values into its own children")

        obj = RemoveOperation({"op": "remove", "path": self.operation["from"]}).apply(obj)

        obj = AddOperation({"op": "add", "path": self.location, "value": value}).apply(obj)

        return obj
예제 #13
0
 def test_path(self):
     doc = {'a': [{'b': 1, 'c': 2}, 5]}
     ptr = JsonPointer('/a/0/b')
     last, nxt = ptr.to_last(doc)
     self.assertEqual(last, {'b': 1, 'c': 2})
     self.assertEqual(nxt, 'b')
예제 #14
0
    def __init__(self, directive, arguments, options, content, lineno, content_offset, block_text, state, state_machine):
        assert directive == 'jsonschema'

        self.options = options
        self.state = state
        self.lineno = lineno
        self.statemachine = state_machine

        if len(arguments) == 1:
            filename, pointer = self._splitpointer(arguments[0])
            if filename != '':
                self._load_external(filename)
            else:
                self._load_internal(content)
            if pointer:
                self.schema = resolve_pointer(self.schema, pointer)
        else:
            self._load_internal(content)

        hidden_paths = self.options.get('hide')
        if hidden_paths is not None:
            orig_schema = json.loads(json.dumps(self.schema))

            for hidden_path in hidden_paths.split(' '):
                ptr = JsonPointer(hidden_path)
                parent, name = ptr.to_last(self.schema)
                del parent[name]

            shown_paths = self.options.get('show')

            for shown_path in shown_paths.split(' '):
                ptr = JsonPointer(shown_path)

                orig_parent = orig_schema
                current_parent = self.schema

                for part in ptr.parts[:-1]:
                    orig_parent = ptr.walk(orig_parent, part)

                    try:
                        current_parent = ptr.walk(current_parent, part)
                    except JsonPointerException:
                        if isinstance(orig_parent, Sequence):
                            new_entry = []
                        elif isinstance(orig_parent, Mapping):
                            new_entry = OrderedDict()
                        else:
                            raise Exception('Unsupported type parent')

                        if isinstance(current_parent, MutableSequence):
                            current_parent.append(new_entry)
                        elif isinstance(current_parent, MutableMapping):
                            current_parent[part] = new_entry

                        current_parent = new_entry

                if isinstance(current_parent, MutableSequence):
                    current_parent.append(ptr.resolve(orig_schema))
                elif isinstance(current_parent, MutableMapping):
                    current_parent[ptr.parts[-1]] = ptr.resolve(orig_schema)
                else:
                    raise Exception('Unsupported type parent')
예제 #15
0
 def test_empty_path(self):
     doc = {'a': [1, 2, 3]}
     ptr = JsonPointer('')
     last, nxt = ptr.to_last(doc)
     self.assertEqual(doc, last)
     self.assertTrue(nxt is None)
예제 #16
0
 def test_empty_path(self):
     doc = {'a': [1, 2, 3]}
     ptr = JsonPointer('')
     last, nxt = ptr.to_last(doc)
     self.assertEqual(doc, last)
     self.assertTrue(nxt is None)
예제 #17
0
 def test_path(self):
     doc = {'a': [{'b': 1, 'c': 2}, 5]}
     ptr = JsonPointer('/a/0/b')
     last, nxt = ptr.to_last(doc)
     self.assertEqual(last, {'b': 1, 'c': 2})
     self.assertEqual(nxt, 'b')