jsonpatch 1.46__py3-none-any.whl → 1.48__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {jsonpatch-1.46.dist-info → jsonpatch-1.48.dist-info}/METADATA +17 -12
- jsonpatch-1.48.dist-info/RECORD +9 -0
- jsonpatch-stubs/__init__.pyi +0 -3
- jsonpatch.py +162 -55
- jsonpatch_cli.py +74 -26
- jsonpatch-1.46.dist-info/RECORD +0 -10
- jsonpatch-1.46.dist-info/licenses/AUTHORS +0 -4
- {jsonpatch-1.46.dist-info → jsonpatch-1.48.dist-info}/WHEEL +0 -0
- {jsonpatch-1.46.dist-info → jsonpatch-1.48.dist-info}/entry_points.txt +0 -0
- {jsonpatch-1.46.dist-info → jsonpatch-1.48.dist-info}/licenses/LICENSE +0 -0
- {jsonpatch-1.46.dist-info → jsonpatch-1.48.dist-info}/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: jsonpatch
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.48
|
|
4
4
|
Summary: Apply JSON-Patches (RFC 6902)
|
|
5
5
|
Home-page: https://github.com/stefankoegl/python-json-patch
|
|
6
6
|
Author: Stefan Kögl
|
|
@@ -8,7 +8,7 @@ Author-email: stefan@skoegl.net
|
|
|
8
8
|
License-Expression: BSD-3-Clause
|
|
9
9
|
Project-URL: Website, https://github.com/stefankoegl/python-json-patch
|
|
10
10
|
Project-URL: Repository, https://github.com/stefankoegl/python-json-patch.git
|
|
11
|
-
Project-URL: Documentation, https://python-json-patch.readthedocs.
|
|
11
|
+
Project-URL: Documentation, https://python-json-patch.readthedocs.io/
|
|
12
12
|
Project-URL: PyPI, https://pypi.org/pypi/jsonpatch
|
|
13
13
|
Project-URL: Tests, https://github.com/stefankoegl/python-json-patch/actions
|
|
14
14
|
Project-URL: Test Coverage, https://coveralls.io/github/stefankoegl/python-json-patch
|
|
@@ -19,20 +19,19 @@ Classifier: Operating System :: OS Independent
|
|
|
19
19
|
Classifier: Programming Language :: Python
|
|
20
20
|
Classifier: Programming Language :: Python :: 3
|
|
21
21
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
22
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
23
22
|
Classifier: Programming Language :: Python :: 3.11
|
|
24
23
|
Classifier: Programming Language :: Python :: 3.12
|
|
25
24
|
Classifier: Programming Language :: Python :: 3.13
|
|
26
25
|
Classifier: Programming Language :: Python :: 3.14
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
27
27
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
28
28
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
29
29
|
Classifier: Topic :: Software Development :: Libraries
|
|
30
30
|
Classifier: Topic :: Utilities
|
|
31
31
|
Classifier: Typing :: Typed
|
|
32
|
-
Requires-Python: >=3.
|
|
32
|
+
Requires-Python: >=3.11
|
|
33
33
|
Description-Content-Type: text/markdown
|
|
34
34
|
License-File: LICENSE
|
|
35
|
-
License-File: AUTHORS
|
|
36
35
|
Requires-Dist: jsonpointer>=3.8
|
|
37
36
|
Dynamic: author
|
|
38
37
|
Dynamic: author-email
|
|
@@ -50,8 +49,8 @@ Dynamic: summary
|
|
|
50
49
|
python-json-patch
|
|
51
50
|
=================
|
|
52
51
|
|
|
53
|
-
[](https://pypi.org/project/jsonpatch
|
|
54
|
-
[](https://pypi.org/project/jsonpatch
|
|
52
|
+
[](https://pypi.org/project/jsonpatch/)
|
|
53
|
+
[](https://pypi.org/project/jsonpatch/)
|
|
55
54
|
[](https://github.com/stefankoegl/python-json-patch/actions/workflows/test.yaml)
|
|
56
55
|
[](https://coveralls.io/github/stefankoegl/python-json-patch?branch=master)
|
|
57
56
|
|
|
@@ -61,11 +60,12 @@ Applying JSON Patches in Python
|
|
|
61
60
|
Library to apply JSON Patches according to
|
|
62
61
|
[RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902)
|
|
63
62
|
|
|
64
|
-
See
|
|
63
|
+
See the [tutorial](https://python-json-patch.readthedocs.io/en/latest/tutorial.html)
|
|
64
|
+
for examples.
|
|
65
65
|
|
|
66
66
|
* Website: https://github.com/stefankoegl/python-json-patch
|
|
67
67
|
* Repository: https://github.com/stefankoegl/python-json-patch.git
|
|
68
|
-
* Documentation: https://python-json-patch.readthedocs.
|
|
68
|
+
* Documentation: https://python-json-patch.readthedocs.io/
|
|
69
69
|
* PyPI: https://pypi.org/project/jsonpatch/
|
|
70
70
|
* CI: https://github.com/stefankoegl/python-json-patch/actions
|
|
71
71
|
* Coveralls: https://coveralls.io/github/stefankoegl/python-json-patch
|
|
@@ -77,7 +77,8 @@ non-RFC string `append` operation. Its `from_diff()` method sends only the
|
|
|
77
77
|
new suffix when an object member's string grows. `JsonPatch`, `make_patch()`
|
|
78
78
|
and `apply_patch()` retain their standard RFC 6902 behavior.
|
|
79
79
|
|
|
80
|
-
See the [tutorial](
|
|
80
|
+
See the [tutorial](https://python-json-patch.readthedocs.io/en/latest/tutorial.html#string-append-extension)
|
|
81
|
+
for examples.
|
|
81
82
|
Both sender and receiver must support the extension.
|
|
82
83
|
|
|
83
84
|
Type hints
|
|
@@ -94,6 +95,11 @@ typical usage in `typetests/usage.py` type-checks:
|
|
|
94
95
|
python -m mypy.stubtest --mypy-config-file pyproject.toml jsonpatch
|
|
95
96
|
cd typetests && mypy --config-file ../pyproject.toml usage.py
|
|
96
97
|
|
|
98
|
+
Running tests
|
|
99
|
+
-------------
|
|
100
|
+
pip install -r requirements.txt -r requirements-dev.txt
|
|
101
|
+
python tests.py
|
|
102
|
+
|
|
97
103
|
Running external tests
|
|
98
104
|
----------------------
|
|
99
105
|
To run external tests (such as those from https://github.com/json-patch/json-patch-tests) use ext_tests.py
|
|
@@ -103,8 +109,7 @@ To run external tests (such as those from https://github.com/json-patch/json-pat
|
|
|
103
109
|
Running property-based tests
|
|
104
110
|
----------------------------
|
|
105
111
|
Property-based tests using [Hypothesis](https://hypothesis.readthedocs.io/)
|
|
106
|
-
are in property_tests.py.
|
|
107
|
-
yet are marked as expected failures.
|
|
112
|
+
are in property_tests.py.
|
|
108
113
|
|
|
109
114
|
pip install hypothesis
|
|
110
115
|
python property_tests.py
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
jsonpatch.py,sha256=SAqrta5PE-_bEL7lg9ZR-aRLYvDe9rlwms9k8rmZdDQ,72326
|
|
2
|
+
jsonpatch_cli.py,sha256=ouGStiuN87nsfrGe1svTgR8WVH0EhiLnu_KW5OFg9ys,8363
|
|
3
|
+
jsonpatch-1.48.dist-info/licenses/LICENSE,sha256=Q5PrZzP6HjZ_QXabOC9PG-e_Da6x4kPtkrP12Lr9I28,1481
|
|
4
|
+
jsonpatch-stubs/__init__.pyi,sha256=Bg2Z-QYkBV2nFsw410s5GcQxI-GV4Jb00eYHBP7ciFw,5498
|
|
5
|
+
jsonpatch-1.48.dist-info/METADATA,sha256=jJAfj1tggk6NjVE-0jo5ew0b8tdirIXF28ccYW3HJ-o,4860
|
|
6
|
+
jsonpatch-1.48.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
7
|
+
jsonpatch-1.48.dist-info/entry_points.txt,sha256=aY9ormBVRZHHDaAeMcdqTBwZ-ax7GvBTSQ33I75xtZM,98
|
|
8
|
+
jsonpatch-1.48.dist-info/top_level.txt,sha256=AGmziZtf5TSFAtbhYlhDGjQg1POmwzF9HwLdbzPkoio,40
|
|
9
|
+
jsonpatch-1.48.dist-info/RECORD,,
|
jsonpatch-stubs/__init__.pyi
CHANGED
|
@@ -65,7 +65,6 @@ class PatchOperation:
|
|
|
65
65
|
def apply(self, obj: Any) -> Any: ...
|
|
66
66
|
def __hash__(self) -> int: ...
|
|
67
67
|
def __eq__(self, other: object) -> bool: ...
|
|
68
|
-
def __ne__(self, other: object) -> bool: ...
|
|
69
68
|
@property
|
|
70
69
|
def path(self) -> str: ...
|
|
71
70
|
@property
|
|
@@ -104,8 +103,6 @@ class JsonPatch:
|
|
|
104
103
|
def __iter__(self) -> Iterator[dict[str, Any]]: ...
|
|
105
104
|
def __hash__(self) -> int: ...
|
|
106
105
|
def __eq__(self, other: object) -> bool: ...
|
|
107
|
-
def __ne__(self, other: object) -> bool: ...
|
|
108
|
-
__nonzero__ = __bool__
|
|
109
106
|
@classmethod
|
|
110
107
|
def from_string(
|
|
111
108
|
cls,
|
jsonpatch.py
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
#
|
|
3
1
|
# python-json-patch - An implementation of the JSON Patch format
|
|
4
2
|
# https://github.com/stefankoegl/python-json-patch
|
|
5
3
|
#
|
|
6
4
|
# Copyright (c) 2011 Stefan Kögl <stefan@skoegl.net>
|
|
7
|
-
# All rights reserved.
|
|
8
5
|
#
|
|
9
6
|
# Redistribution and use in source and binary forms, with or without
|
|
10
|
-
# modification, are permitted provided that the following conditions
|
|
11
|
-
#
|
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
|
8
|
+
#
|
|
9
|
+
# 1. Redistributions of source code must retain the above copyright notice,
|
|
10
|
+
# this list of conditions and the following disclaimer.
|
|
11
|
+
#
|
|
12
|
+
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
# this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
# and/or other materials provided with the distribution.
|
|
12
15
|
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
# notice, this list of conditions and the following disclaimer in the
|
|
17
|
-
# documentation and/or other materials provided with the distribution.
|
|
18
|
-
# 3. The name of the author may not be used to endorse or promote products
|
|
19
|
-
# derived from this software without specific prior written permission.
|
|
16
|
+
# 3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
# contributors may be used to endorse or promote products derived from this
|
|
18
|
+
# software without specific prior written permission.
|
|
20
19
|
#
|
|
21
|
-
# THIS SOFTWARE IS PROVIDED BY THE
|
|
22
|
-
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
23
|
-
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
24
|
-
# IN NO EVENT SHALL THE
|
|
25
|
-
# INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
26
|
-
# NOT LIMITED TO, PROCUREMENT OF
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
30
|
-
# THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
23
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
24
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
25
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
26
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
27
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
28
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
29
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
30
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
31
31
|
#
|
|
32
32
|
|
|
33
33
|
""" Apply JSON-Patches (RFC 6902) """
|
|
@@ -59,7 +59,7 @@ _JSONPOINTER_FROM_PARTS = JsonPointer.from_parts.__func__
|
|
|
59
59
|
|
|
60
60
|
# Will be parsed by setup.py to determine package metadata
|
|
61
61
|
__author__ = 'Stefan Kögl <stefan@skoegl.net>'
|
|
62
|
-
__version__ = '1.
|
|
62
|
+
__version__ = '1.48'
|
|
63
63
|
__website__ = 'https://github.com/stefankoegl/python-json-patch'
|
|
64
64
|
__license__ = 'BSD-3-Clause'
|
|
65
65
|
|
|
@@ -78,7 +78,7 @@ class JsonPatchException(Exception):
|
|
|
78
78
|
:type op: PatchOperation or dict or None
|
|
79
79
|
"""
|
|
80
80
|
self.op = kwargs.pop('op', None)
|
|
81
|
-
super(
|
|
81
|
+
super().__init__(*args, **kwargs)
|
|
82
82
|
|
|
83
83
|
|
|
84
84
|
class InvalidJsonPatch(JsonPatchException):
|
|
@@ -205,6 +205,9 @@ def make_patch(src, dst, pointer_cls=JsonPointer, generate_test_ops=False):
|
|
|
205
205
|
fails on documents that differ from `src` there.
|
|
206
206
|
:type generate_test_ops: bool
|
|
207
207
|
|
|
208
|
+
:raises TypeError: if the documents differ in an object member whose
|
|
209
|
+
name is not a string, see :meth:`JsonPatch.from_diff`.
|
|
210
|
+
|
|
208
211
|
>>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
|
|
209
212
|
>>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
|
|
210
213
|
>>> patch = make_patch(src, dst)
|
|
@@ -294,7 +297,7 @@ def path_join(parts, pointer_cls=JsonPointer):
|
|
|
294
297
|
return from_parts(parts).path
|
|
295
298
|
|
|
296
299
|
|
|
297
|
-
class _NullJournal
|
|
300
|
+
class _NullJournal:
|
|
298
301
|
"""Performs the changes of patch operations without recording them."""
|
|
299
302
|
|
|
300
303
|
@staticmethod
|
|
@@ -317,7 +320,7 @@ class _NullJournal(object):
|
|
|
317
320
|
_NO_JOURNAL = _NullJournal()
|
|
318
321
|
|
|
319
322
|
|
|
320
|
-
class _Journal
|
|
323
|
+
class _Journal:
|
|
321
324
|
"""Performs the changes of patch operations and records how to undo them.
|
|
322
325
|
|
|
323
326
|
Each undo step refers to the changed container itself rather than to a
|
|
@@ -437,7 +440,24 @@ def _reorder(mapping, keys):
|
|
|
437
440
|
mapping[key] = mapping.pop(key)
|
|
438
441
|
|
|
439
442
|
|
|
440
|
-
|
|
443
|
+
def _plain_operation(operation, pointer_cls):
|
|
444
|
+
"""Returns operation as a dict whose 'path' and 'from' are strings.
|
|
445
|
+
|
|
446
|
+
Both can be given as pointer objects instead. These are replaced by
|
|
447
|
+
their paths, so that the operation is JSON-serializable, and can be
|
|
448
|
+
compared to one that was given the paths. This is done in a copy; an
|
|
449
|
+
operation that is a dict already and has no pointer objects is returned
|
|
450
|
+
as it is.
|
|
451
|
+
"""
|
|
452
|
+
paths = {member: operation[member].path for member in ('path', 'from')
|
|
453
|
+
if member in operation and
|
|
454
|
+
isinstance(operation[member], pointer_cls)}
|
|
455
|
+
if paths:
|
|
456
|
+
return dict(operation, **paths)
|
|
457
|
+
return operation if isinstance(operation, dict) else dict(operation)
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
class PatchOperation:
|
|
441
461
|
"""A single operation inside a JSON Patch."""
|
|
442
462
|
|
|
443
463
|
def __init__(self, operation, pointer_cls=JsonPointer):
|
|
@@ -491,16 +511,17 @@ class PatchOperation(object):
|
|
|
491
511
|
def __hash__(self):
|
|
492
512
|
# Other members can hold arrays or objects, which are unhashable.
|
|
493
513
|
# Operations that compare equal still have equal 'op' and 'path'.
|
|
494
|
-
|
|
514
|
+
operation = _plain_operation(self.operation, self.pointer_cls)
|
|
515
|
+
return hash((operation.get('op'), operation['path']))
|
|
495
516
|
|
|
496
517
|
def __eq__(self, other):
|
|
497
518
|
if not isinstance(other, PatchOperation):
|
|
498
519
|
return False
|
|
499
520
|
# e.g. tests for 1 and for true behave differently
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
521
|
+
# and paths are equal to pointer objects for them
|
|
522
|
+
return _json_equal(
|
|
523
|
+
_plain_operation(self.operation, self.pointer_cls),
|
|
524
|
+
_plain_operation(other.operation, other.pointer_cls))
|
|
504
525
|
|
|
505
526
|
@property
|
|
506
527
|
def path(self):
|
|
@@ -625,7 +646,7 @@ class ReplaceOperation(PatchOperation):
|
|
|
625
646
|
raise InvalidJsonPatch(
|
|
626
647
|
"The operation does not contain a 'value' member", op=self)
|
|
627
648
|
|
|
628
|
-
# copied for the same reason as in AddOperation.
|
|
649
|
+
# copied for the same reason as in AddOperation._apply
|
|
629
650
|
value = copy.deepcopy(value)
|
|
630
651
|
|
|
631
652
|
subobj, part = self.pointer.to_last(obj)
|
|
@@ -871,7 +892,7 @@ def _supports_journal(operation):
|
|
|
871
892
|
return True
|
|
872
893
|
|
|
873
894
|
|
|
874
|
-
class JsonPatch
|
|
895
|
+
class JsonPatch:
|
|
875
896
|
"""A JSON Patch is an iterable of Patch Operations.
|
|
876
897
|
|
|
877
898
|
Accepts sequences, iterators, generators, and other iterables of operation
|
|
@@ -965,8 +986,7 @@ class JsonPatch(object):
|
|
|
965
986
|
self._get_operation(op)
|
|
966
987
|
# Keep validation errors tied to the original input, but expose
|
|
967
988
|
# concrete, JSON-serializable operation dictionaries on success.
|
|
968
|
-
|
|
969
|
-
self.patch[index] = dict(op)
|
|
989
|
+
self.patch[index] = _plain_operation(op, self.pointer_cls)
|
|
970
990
|
|
|
971
991
|
def __str__(self):
|
|
972
992
|
"""str(self) -> self.to_string()"""
|
|
@@ -975,8 +995,6 @@ class JsonPatch(object):
|
|
|
975
995
|
def __bool__(self):
|
|
976
996
|
return bool(self.patch)
|
|
977
997
|
|
|
978
|
-
__nonzero__ = __bool__
|
|
979
|
-
|
|
980
998
|
def __iter__(self):
|
|
981
999
|
return iter(self.patch)
|
|
982
1000
|
|
|
@@ -988,9 +1006,6 @@ class JsonPatch(object):
|
|
|
988
1006
|
return False
|
|
989
1007
|
return self._ops == other._ops
|
|
990
1008
|
|
|
991
|
-
def __ne__(self, other):
|
|
992
|
-
return not(self == other)
|
|
993
|
-
|
|
994
1009
|
@classmethod
|
|
995
1010
|
def from_string(cls, patch_str, loads=None, pointer_cls=JsonPointer):
|
|
996
1011
|
"""Creates JsonPatch instance from string source.
|
|
@@ -1051,6 +1066,13 @@ class JsonPatch(object):
|
|
|
1051
1066
|
|
|
1052
1067
|
:return: :class:`JsonPatch` instance.
|
|
1053
1068
|
|
|
1069
|
+
:raises TypeError: if the documents differ in, or inside of, an
|
|
1070
|
+
object member whose name is not a string, such
|
|
1071
|
+
as the ``1`` of ``{1: 'a'}``. The path of an
|
|
1072
|
+
operation can't refer to such a member, which
|
|
1073
|
+
does not exist in JSON. Other differences of
|
|
1074
|
+
documents with such members are no problem.
|
|
1075
|
+
|
|
1054
1076
|
>>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
|
|
1055
1077
|
>>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
|
|
1056
1078
|
>>> patch = JsonPatch.from_diff(src, dst)
|
|
@@ -1063,7 +1085,7 @@ class JsonPatch(object):
|
|
|
1063
1085
|
optimization=optimization,
|
|
1064
1086
|
generate_test_ops=generate_test_ops)
|
|
1065
1087
|
builder._compare_values((), None, src, dst)
|
|
1066
|
-
ops =
|
|
1088
|
+
ops = _json_copy(list(builder.execute()))
|
|
1067
1089
|
return cls(ops, pointer_cls=pointer_cls)
|
|
1068
1090
|
|
|
1069
1091
|
def to_string(self, dumps=None):
|
|
@@ -1183,15 +1205,19 @@ class JsonPatchWithAppend(JsonPatch):
|
|
|
1183
1205
|
optimization=optimization,
|
|
1184
1206
|
generate_test_ops=generate_test_ops)
|
|
1185
1207
|
builder._compare_values((), None, src, dst)
|
|
1186
|
-
ops =
|
|
1208
|
+
ops = _json_copy(list(builder.execute()))
|
|
1187
1209
|
return cls(ops, pointer_cls=pointer_cls)
|
|
1188
1210
|
|
|
1189
1211
|
|
|
1190
|
-
class DiffBuilder
|
|
1212
|
+
class DiffBuilder:
|
|
1191
1213
|
|
|
1192
1214
|
def __init__(self, src_doc, dst_doc, dumps=json.dumps, pointer_cls=JsonPointer,
|
|
1193
1215
|
optimization=True, generate_test_ops=False):
|
|
1194
1216
|
self.dumps = dumps
|
|
1217
|
+
# json.dumps builds a JSONEncoder for every call; the default dumps
|
|
1218
|
+
# is called once per added and removed value, so cache its encoder
|
|
1219
|
+
self._canonical_dumps = (json.JSONEncoder(sort_keys=True).encode
|
|
1220
|
+
if dumps is json.dumps else None)
|
|
1195
1221
|
self.pointer_cls = pointer_cls
|
|
1196
1222
|
self.optimization = optimization
|
|
1197
1223
|
self.generate_test_ops = generate_test_ops
|
|
@@ -1209,6 +1235,17 @@ class DiffBuilder(object):
|
|
|
1209
1235
|
def _move_key(self, value):
|
|
1210
1236
|
""" A key of value, which is equal for values that are equal in JSON,
|
|
1211
1237
|
so that e.g. [1] is not moved where [true] is added """
|
|
1238
|
+
if self._canonical_dumps is not None:
|
|
1239
|
+
try:
|
|
1240
|
+
# sort_keys gives the same string as _sorted_members for JSON
|
|
1241
|
+
# documents, without building a sorted copy of every added or
|
|
1242
|
+
# removed subtree
|
|
1243
|
+
return self._canonical_dumps(value)
|
|
1244
|
+
except (TypeError, ValueError, RecursionError):
|
|
1245
|
+
# Values like UserDict and UserList are normalized by
|
|
1246
|
+
# _sorted_members, but the encoder cannot serialize them as
|
|
1247
|
+
# they are, so fall through to the generic path below
|
|
1248
|
+
pass
|
|
1212
1249
|
try:
|
|
1213
1250
|
# Serialized like in _differing_runs
|
|
1214
1251
|
return self.dumps(_sorted_members(value))
|
|
@@ -1260,8 +1297,7 @@ class DiffBuilder(object):
|
|
|
1260
1297
|
op_second['op'] == 'add':
|
|
1261
1298
|
yield {
|
|
1262
1299
|
'op': 'replace',
|
|
1263
|
-
'path':
|
|
1264
|
-
self.pointer_cls),
|
|
1300
|
+
'path': self._path(op_second['path']),
|
|
1265
1301
|
'value': op_second['value'],
|
|
1266
1302
|
}
|
|
1267
1303
|
i += 2
|
|
@@ -1270,11 +1306,20 @@ class DiffBuilder(object):
|
|
|
1270
1306
|
operation = operations[i]
|
|
1271
1307
|
for member in ('from', 'path'):
|
|
1272
1308
|
if member in operation:
|
|
1273
|
-
operation[member] =
|
|
1274
|
-
self.pointer_cls)
|
|
1309
|
+
operation[member] = self._path(operation[member])
|
|
1275
1310
|
yield operation
|
|
1276
1311
|
i += 1
|
|
1277
1312
|
|
|
1313
|
+
def _path(self, parts):
|
|
1314
|
+
""" The path of an operation, in the syntax of pointer_cls """
|
|
1315
|
+
for part in parts:
|
|
1316
|
+
if isinstance(part, _NonStringKey):
|
|
1317
|
+
raise TypeError(
|
|
1318
|
+
"can't create an operation for the object member "
|
|
1319
|
+
"{0!r}: its name is not a string, which the path of an "
|
|
1320
|
+
"operation can't refer to".format(part.key))
|
|
1321
|
+
return path_join(parts, self.pointer_cls)
|
|
1322
|
+
|
|
1278
1323
|
def _replay(self):
|
|
1279
1324
|
""" Applies the operations to the arrays of the source document, and
|
|
1280
1325
|
returns them with the array indices at which they apply """
|
|
@@ -1395,9 +1440,9 @@ class DiffBuilder(object):
|
|
|
1395
1440
|
""" Inserts a test operation that checks the value at location, if
|
|
1396
1441
|
test operations are generated """
|
|
1397
1442
|
if self.generate_test_ops:
|
|
1398
|
-
#
|
|
1443
|
+
# like all values, it is copied when the patch is created
|
|
1399
1444
|
return self.insert({'op': 'test', 'path': location,
|
|
1400
|
-
'value':
|
|
1445
|
+
'value': value})
|
|
1401
1446
|
|
|
1402
1447
|
def _item_replaced(self, path, key, item, old_item):
|
|
1403
1448
|
location = self._location(_path_join(path, key))
|
|
@@ -1414,13 +1459,13 @@ class DiffBuilder(object):
|
|
|
1414
1459
|
intersection = [key for key in src if key in dst]
|
|
1415
1460
|
|
|
1416
1461
|
for key in removed_keys:
|
|
1417
|
-
self._item_removed(path,
|
|
1462
|
+
self._item_removed(path, _member_name(key), src[key])
|
|
1418
1463
|
|
|
1419
1464
|
for key in added_keys:
|
|
1420
|
-
self._item_added(path,
|
|
1465
|
+
self._item_added(path, _member_name(key), dst[key])
|
|
1421
1466
|
|
|
1422
1467
|
for key in intersection:
|
|
1423
|
-
self._compare_values(path,
|
|
1468
|
+
self._compare_values(path, _member_name(key), src[key], dst[key])
|
|
1424
1469
|
|
|
1425
1470
|
def _differing_runs(self, src, dst):
|
|
1426
1471
|
""" Returns (i1, i2, j1, j2) for each run of items src[i1:i2] that is
|
|
@@ -1556,6 +1601,37 @@ class _AppendDiffBuilder(DiffBuilder):
|
|
|
1556
1601
|
super()._item_replaced(path, key, item, old_item)
|
|
1557
1602
|
|
|
1558
1603
|
|
|
1604
|
+
class _NonStringKey(str):
|
|
1605
|
+
""" Takes the place of an object key that is not a string in the parts
|
|
1606
|
+
of an operation.
|
|
1607
|
+
|
|
1608
|
+
A path can only refer to members whose names are strings: '/1' is the
|
|
1609
|
+
member '1', not the member 1. Members with other names are compared like
|
|
1610
|
+
all others, as nothing is wrong unless they differ; an operation for
|
|
1611
|
+
such a member, or for something in it, can't be written, though (see
|
|
1612
|
+
DiffBuilder._path). """
|
|
1613
|
+
|
|
1614
|
+
def __new__(cls, key):
|
|
1615
|
+
self = super().__new__(cls, key)
|
|
1616
|
+
self.key = key
|
|
1617
|
+
return self
|
|
1618
|
+
|
|
1619
|
+
# The member 1 is not the member '1', which this is equal to as a str
|
|
1620
|
+
def __eq__(self, other):
|
|
1621
|
+
return isinstance(other, _NonStringKey) and self.key == other.key
|
|
1622
|
+
|
|
1623
|
+
def __ne__(self, other):
|
|
1624
|
+
return not self == other
|
|
1625
|
+
|
|
1626
|
+
def __hash__(self):
|
|
1627
|
+
return hash(self.key)
|
|
1628
|
+
|
|
1629
|
+
|
|
1630
|
+
def _member_name(key):
|
|
1631
|
+
""" The part of an operation for the object key """
|
|
1632
|
+
return key if isinstance(key, str) else _NonStringKey(key)
|
|
1633
|
+
|
|
1634
|
+
|
|
1559
1635
|
def _positional_cost(old, new):
|
|
1560
1636
|
""" Estimates how many operations change old into new, if the items of
|
|
1561
1637
|
arrays are compared by position """
|
|
@@ -1589,6 +1665,37 @@ def _json_equal(first, second):
|
|
|
1589
1665
|
return first == second
|
|
1590
1666
|
|
|
1591
1667
|
|
|
1668
|
+
def _json_copy(value, memo=None):
|
|
1669
|
+
""" Copies a JSON value like copy.deepcopy, but without the generic
|
|
1670
|
+
dispatch for dicts, lists and scalars, which dominate patch creation.
|
|
1671
|
+
|
|
1672
|
+
Values that are not plain JSON containers keep the copy.deepcopy
|
|
1673
|
+
behaviour, and shared references are preserved through memo. """
|
|
1674
|
+
if value is None or type(value) in (str, int, float, bool):
|
|
1675
|
+
return value
|
|
1676
|
+
if memo is None:
|
|
1677
|
+
memo = {}
|
|
1678
|
+
value_id = id(value)
|
|
1679
|
+
copied = memo.get(value_id)
|
|
1680
|
+
if copied is not None:
|
|
1681
|
+
return copied
|
|
1682
|
+
value_type = type(value)
|
|
1683
|
+
if value_type is dict:
|
|
1684
|
+
# memo must hold the new dict before the children are copied, so
|
|
1685
|
+
# that self-referential documents terminate like copy.deepcopy
|
|
1686
|
+
copied = memo[value_id] = {}
|
|
1687
|
+
for key, item in value.items():
|
|
1688
|
+
# copy.deepcopy copies the keys too, through the same memo, so
|
|
1689
|
+
# that a key and a value that are the same object stay shared
|
|
1690
|
+
copied[_json_copy(key, memo)] = _json_copy(item, memo)
|
|
1691
|
+
return copied
|
|
1692
|
+
if value_type is list:
|
|
1693
|
+
copied = memo[value_id] = []
|
|
1694
|
+
copied.extend(_json_copy(item, memo) for item in value)
|
|
1695
|
+
return copied
|
|
1696
|
+
return copy.deepcopy(value, memo)
|
|
1697
|
+
|
|
1698
|
+
|
|
1592
1699
|
def _sorted_members(value):
|
|
1593
1700
|
""" Copies the arrays and objects in value, with the members of objects
|
|
1594
1701
|
sorted by key """
|
|
@@ -1609,7 +1716,7 @@ def _sorted_members(value):
|
|
|
1609
1716
|
# change, but their locations do not.
|
|
1610
1717
|
|
|
1611
1718
|
|
|
1612
|
-
class _ArrayItem
|
|
1719
|
+
class _ArrayItem:
|
|
1613
1720
|
""" An item that an array has at some point of the diff """
|
|
1614
1721
|
|
|
1615
1722
|
__slots__ = ('array', 'label', 'initial')
|
|
@@ -1626,7 +1733,7 @@ class _ArrayItem(object):
|
|
|
1626
1733
|
return bisect.bisect_left(self.array.present_labels, self.label)
|
|
1627
1734
|
|
|
1628
1735
|
|
|
1629
|
-
class _ArrayItems
|
|
1736
|
+
class _ArrayItems:
|
|
1630
1737
|
""" All items that an array has at some point of the diff, and the items
|
|
1631
1738
|
that it has at the moment, both ordered by their labels.
|
|
1632
1739
|
|
jsonpatch_cli.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
1
|
""" The jsondiff and jsonpatch command line programs """
|
|
4
2
|
|
|
5
3
|
import sys
|
|
@@ -26,25 +24,51 @@ jsondiff_parser.add_argument('-v', '--version', action='version',
|
|
|
26
24
|
version='%(prog)s ' + jsonpatch.__version__)
|
|
27
25
|
|
|
28
26
|
|
|
29
|
-
def
|
|
27
|
+
def _input_files(*streams):
|
|
28
|
+
""" Closes the input files when the returned context is left; stdin is
|
|
29
|
+
left open, as it is not ours """
|
|
30
|
+
inputs = ExitStack()
|
|
31
|
+
for stream in streams:
|
|
32
|
+
if stream is not sys.stdin:
|
|
33
|
+
inputs.enter_context(stream)
|
|
34
|
+
return inputs
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _load(stream, loads=json.loads):
|
|
38
|
+
""" Parses the JSON document in stream; errors name the file """
|
|
30
39
|
try:
|
|
31
|
-
|
|
40
|
+
return loads(stream.read())
|
|
41
|
+
except ValueError as ex:
|
|
42
|
+
# invalid JSON or, as a UnicodeDecodeError, invalid UTF-8
|
|
43
|
+
name = getattr(stream, 'name', '<input>')
|
|
44
|
+
raise ValueError('{0}: {1}'.format(name, ex)) from ex
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _run(parser, program):
|
|
48
|
+
""" Runs a command line program. Errors in its input, and in reading
|
|
49
|
+
and writing files, are reported in one line, with exit status 2 """
|
|
50
|
+
try:
|
|
51
|
+
program()
|
|
32
52
|
except KeyboardInterrupt:
|
|
33
53
|
sys.exit(1)
|
|
54
|
+
except (jsonpatch.JsonPatchException, jsonpatch.JsonPointerException,
|
|
55
|
+
ValueError, OSError) as ex:
|
|
56
|
+
parser.exit(2, '{0}: error: {1}\n'.format(parser.prog, ex))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def jsondiff_main():
|
|
60
|
+
_run(jsondiff_parser, diff_files)
|
|
34
61
|
|
|
35
62
|
|
|
36
63
|
def diff_files():
|
|
37
64
|
""" Diffs two JSON files and prints a patch """
|
|
38
65
|
args = jsondiff_parser.parse_args()
|
|
39
|
-
with
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
inputs.enter_context(stream)
|
|
43
|
-
doc1 = json.load(args.FILE1)
|
|
44
|
-
doc2 = json.load(args.FILE2)
|
|
66
|
+
with _input_files(args.FILE1, args.FILE2):
|
|
67
|
+
doc1 = _load(args.FILE1)
|
|
68
|
+
doc2 = _load(args.FILE2)
|
|
45
69
|
patch = jsonpatch.make_patch(doc1, doc2, generate_test_ops=args.test_ops)
|
|
46
70
|
if patch.patch:
|
|
47
|
-
print(json.dumps(patch.patch, indent=args.indent, ensure_ascii=not
|
|
71
|
+
print(json.dumps(patch.patch, indent=args.indent, ensure_ascii=not args.preserve_unicode))
|
|
48
72
|
sys.exit(1)
|
|
49
73
|
|
|
50
74
|
|
|
@@ -67,22 +91,39 @@ jsonpatch_parser.add_argument('-v', '--version', action='version',
|
|
|
67
91
|
jsonpatch_parser.add_argument('-u', '--preserve-unicode', action='store_true',
|
|
68
92
|
help='Output Unicode character as-is without using Code Point')
|
|
69
93
|
|
|
70
|
-
|
|
94
|
+
|
|
95
|
+
def _link_backup(original, temporary):
|
|
96
|
+
""" Links original to its backup name, replacing an older backup """
|
|
97
|
+
# link() does not overwrite an existing file, so the link is created
|
|
98
|
+
# under a temporary name, and then moved over an older backup
|
|
99
|
+
link = temporary + '.orig'
|
|
100
|
+
os.link(original, link)
|
|
71
101
|
try:
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
102
|
+
os.replace(link, original + '.orig')
|
|
103
|
+
finally:
|
|
104
|
+
# It is still there if replacing failed, and if the older backup is
|
|
105
|
+
# a link to original already, as replace() does nothing then
|
|
106
|
+
try:
|
|
107
|
+
os.unlink(link)
|
|
108
|
+
except OSError:
|
|
109
|
+
pass
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def jsonpatch_main():
|
|
113
|
+
_run(jsonpatch_parser, patch_files)
|
|
75
114
|
|
|
76
115
|
|
|
77
116
|
def patch_files():
|
|
78
117
|
""" Applies a JSON patch on a JSON file """
|
|
79
118
|
args = jsonpatch_parser.parse_args()
|
|
80
|
-
|
|
81
|
-
|
|
119
|
+
# The inputs are closed before anything is written, as Windows cannot
|
|
120
|
+
# replace ORIGINAL while it is open.
|
|
121
|
+
with _input_files(args.ORIGINAL, args.PATCH):
|
|
122
|
+
doc = _load(args.ORIGINAL)
|
|
123
|
+
patch = _load(args.PATCH, jsonpatch.JsonPatch.from_string)
|
|
124
|
+
result = patch.apply(doc)
|
|
82
125
|
|
|
83
126
|
if args.in_place:
|
|
84
|
-
# 2026-10-11: Release the input handle before replacing it on Windows.
|
|
85
|
-
args.ORIGINAL.close()
|
|
86
127
|
dirname = os.path.abspath(os.path.dirname(args.ORIGINAL.name))
|
|
87
128
|
|
|
88
129
|
fd = None
|
|
@@ -91,7 +132,7 @@ def patch_files():
|
|
|
91
132
|
# creating a temporary file in the same directory as the
|
|
92
133
|
# original file so we can atomically move the new file over
|
|
93
134
|
# the original later. (This is done in the same directory
|
|
94
|
-
|
|
135
|
+
# because atomic renames do not work across mount points.)
|
|
95
136
|
|
|
96
137
|
fd, pathname = tempfile.mkstemp(dir=dirname)
|
|
97
138
|
fp = os.fdopen(fd, 'w', encoding='utf-8')
|
|
@@ -114,7 +155,13 @@ def patch_files():
|
|
|
114
155
|
# the original (if desired) and writing a new file.
|
|
115
156
|
|
|
116
157
|
if args.backup:
|
|
117
|
-
|
|
158
|
+
# like in atomic mode, an older backup is replaced
|
|
159
|
+
os.replace(args.ORIGINAL.name, args.ORIGINAL.name + '.orig')
|
|
160
|
+
# A same-inode replace may leave the original name in place.
|
|
161
|
+
try:
|
|
162
|
+
os.unlink(args.ORIGINAL.name)
|
|
163
|
+
except FileNotFoundError:
|
|
164
|
+
pass
|
|
118
165
|
fp = open(args.ORIGINAL.name, 'w', encoding='utf-8')
|
|
119
166
|
atomic = False
|
|
120
167
|
|
|
@@ -124,11 +171,11 @@ def patch_files():
|
|
|
124
171
|
|
|
125
172
|
fp = sys.stdout
|
|
126
173
|
|
|
127
|
-
# By this point we have some sort of file object we can write the
|
|
174
|
+
# By this point we have some sort of file object we can write the
|
|
128
175
|
# modified JSON to.
|
|
129
|
-
|
|
176
|
+
|
|
130
177
|
try:
|
|
131
|
-
json.dump(result, fp, indent=args.indent, ensure_ascii=not
|
|
178
|
+
json.dump(result, fp, indent=args.indent, ensure_ascii=not args.preserve_unicode)
|
|
132
179
|
fp.write('\n')
|
|
133
180
|
if args.in_place:
|
|
134
181
|
fp.close()
|
|
@@ -150,11 +197,12 @@ def patch_files():
|
|
|
150
197
|
if atomic:
|
|
151
198
|
try:
|
|
152
199
|
# Complete the atomic replace by linking the original
|
|
153
|
-
# to a backup (if desired),
|
|
200
|
+
# to a backup (if desired), which replaces an older
|
|
201
|
+
# backup, fixing up the permissions
|
|
154
202
|
# on the temporary file, and moving it into place.
|
|
155
203
|
|
|
156
204
|
if args.backup:
|
|
157
|
-
|
|
205
|
+
_link_backup(args.ORIGINAL.name, pathname)
|
|
158
206
|
os.chmod(pathname, os.stat(args.ORIGINAL.name).st_mode)
|
|
159
207
|
os.replace(pathname, args.ORIGINAL.name)
|
|
160
208
|
|
jsonpatch-1.46.dist-info/RECORD
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
jsonpatch.py,sha256=sz9iNxPEu-f_77tMj8BPsJW5QlocVZJCbtK6tSVL9Lk,67486
|
|
2
|
-
jsonpatch_cli.py,sha256=I6sWWbPzQ8uMo86Akufb3GSt10_mxg56z71HmpgxkZw,6496
|
|
3
|
-
jsonpatch-1.46.dist-info/licenses/AUTHORS,sha256=hOtEnBGbfDegKjzMDEl5vuVJyczbI2PlYE2ho4LjGNQ,140
|
|
4
|
-
jsonpatch-1.46.dist-info/licenses/LICENSE,sha256=Q5PrZzP6HjZ_QXabOC9PG-e_Da6x4kPtkrP12Lr9I28,1481
|
|
5
|
-
jsonpatch-stubs/__init__.pyi,sha256=ARV0JxPJ3TZbYk1hUbayPO3gJVP0geIQK6fftWM-4KY,5623
|
|
6
|
-
jsonpatch-1.46.dist-info/METADATA,sha256=oUhUkUlyRTxTLA2yjjzjC5S22f2ucDfeQ2i8kmJyzNs,4748
|
|
7
|
-
jsonpatch-1.46.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
8
|
-
jsonpatch-1.46.dist-info/entry_points.txt,sha256=aY9ormBVRZHHDaAeMcdqTBwZ-ax7GvBTSQ33I75xtZM,98
|
|
9
|
-
jsonpatch-1.46.dist-info/top_level.txt,sha256=AGmziZtf5TSFAtbhYlhDGjQg1POmwzF9HwLdbzPkoio,40
|
|
10
|
-
jsonpatch-1.46.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|