jsonpointer.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # -*- coding: utf-8 -*-
  2. #
  3. # python-json-pointer - An implementation of the JSON Pointer syntax
  4. # https://github.com/stefankoegl/python-json-pointer
  5. #
  6. # Copyright (c) 2011 Stefan Kögl <stefan@skoegl.net>
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # 2. Redistributions in binary form must reproduce the above copyright
  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.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #
  32. """ Identify specific nodes in a JSON document (RFC 6901) """
  33. # Will be parsed by setup.py to determine package metadata
  34. __author__ = 'Stefan Kögl <stefan@skoegl.net>'
  35. __version__ = '3.0.0'
  36. __website__ = 'https://github.com/stefankoegl/python-json-pointer'
  37. __license__ = 'Modified BSD License'
  38. import copy
  39. import re
  40. from collections.abc import Mapping, Sequence
  41. from itertools import tee, chain
  42. _nothing = object()
  43. def set_pointer(doc, pointer, value, inplace=True):
  44. """Resolves a pointer against doc and sets the value of the target within doc.
  45. With inplace set to true, doc is modified as long as pointer is not the
  46. root.
  47. >>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string' }}}
  48. >>> set_pointer(obj, '/foo/anArray/0/prop', 55) == \
  49. {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}}
  50. True
  51. >>> set_pointer(obj, '/foo/yet another prop', 'added prop') == \
  52. {'foo': {'another prop': {'baz': 'A string'}, 'yet another prop': 'added prop', 'anArray': [{'prop': 55}]}}
  53. True
  54. >>> obj = {'foo': {}}
  55. >>> set_pointer(obj, '/foo/a%20b', 'x') == \
  56. {'foo': {'a%20b': 'x' }}
  57. True
  58. """
  59. pointer = JsonPointer(pointer)
  60. return pointer.set(doc, value, inplace)
  61. def resolve_pointer(doc, pointer, default=_nothing):
  62. """ Resolves pointer against doc and returns the referenced object
  63. >>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string' }}, 'a%20b': 1, 'c d': 2}
  64. >>> resolve_pointer(obj, '') == obj
  65. True
  66. >>> resolve_pointer(obj, '/foo') == obj['foo']
  67. True
  68. >>> resolve_pointer(obj, '/foo/another prop') == obj['foo']['another prop']
  69. True
  70. >>> resolve_pointer(obj, '/foo/another prop/baz') == obj['foo']['another prop']['baz']
  71. True
  72. >>> resolve_pointer(obj, '/foo/anArray/0') == obj['foo']['anArray'][0]
  73. True
  74. >>> resolve_pointer(obj, '/some/path', None) == None
  75. True
  76. >>> resolve_pointer(obj, '/a b', None) == None
  77. True
  78. >>> resolve_pointer(obj, '/a%20b') == 1
  79. True
  80. >>> resolve_pointer(obj, '/c d') == 2
  81. True
  82. >>> resolve_pointer(obj, '/c%20d', None) == None
  83. True
  84. """
  85. pointer = JsonPointer(pointer)
  86. return pointer.resolve(doc, default)
  87. def pairwise(iterable):
  88. """ Transforms a list to a list of tuples of adjacent items
  89. s -> (s0,s1), (s1,s2), (s2, s3), ...
  90. >>> list(pairwise([]))
  91. []
  92. >>> list(pairwise([1]))
  93. []
  94. >>> list(pairwise([1, 2, 3, 4]))
  95. [(1, 2), (2, 3), (3, 4)]
  96. """
  97. a, b = tee(iterable)
  98. for _ in b:
  99. break
  100. return zip(a, b)
  101. class JsonPointerException(Exception):
  102. pass
  103. class EndOfList(object):
  104. """Result of accessing element "-" of a list"""
  105. def __init__(self, list_):
  106. self.list_ = list_
  107. def __repr__(self):
  108. return '{cls}({lst})'.format(cls=self.__class__.__name__,
  109. lst=repr(self.list_))
  110. class JsonPointer(object):
  111. """A JSON Pointer that can reference parts of a JSON document"""
  112. # Array indices must not contain:
  113. # leading zeros, signs, spaces, decimals, etc
  114. _RE_ARRAY_INDEX = re.compile('0|[1-9][0-9]*$')
  115. _RE_INVALID_ESCAPE = re.compile('(~[^01]|~$)')
  116. def __init__(self, pointer):
  117. # validate escapes
  118. invalid_escape = self._RE_INVALID_ESCAPE.search(pointer)
  119. if invalid_escape:
  120. raise JsonPointerException('Found invalid escape {}'.format(
  121. invalid_escape.group()))
  122. parts = pointer.split('/')
  123. if parts.pop(0) != '':
  124. raise JsonPointerException('Location must start with /')
  125. parts = [unescape(part) for part in parts]
  126. self.parts = parts
  127. def to_last(self, doc):
  128. """Resolves ptr until the last step, returns (sub-doc, last-step)"""
  129. if not self.parts:
  130. return doc, None
  131. for part in self.parts[:-1]:
  132. doc = self.walk(doc, part)
  133. return doc, JsonPointer.get_part(doc, self.parts[-1])
  134. def resolve(self, doc, default=_nothing):
  135. """Resolves the pointer against doc and returns the referenced object"""
  136. for part in self.parts:
  137. try:
  138. doc = self.walk(doc, part)
  139. except JsonPointerException:
  140. if default is _nothing:
  141. raise
  142. else:
  143. return default
  144. return doc
  145. get = resolve
  146. def set(self, doc, value, inplace=True):
  147. """Resolve the pointer against the doc and replace the target with value."""
  148. if len(self.parts) == 0:
  149. if inplace:
  150. raise JsonPointerException('Cannot set root in place')
  151. return value
  152. if not inplace:
  153. doc = copy.deepcopy(doc)
  154. (parent, part) = self.to_last(doc)
  155. if isinstance(parent, Sequence) and part == '-':
  156. parent.append(value)
  157. else:
  158. parent[part] = value
  159. return doc
  160. @classmethod
  161. def get_part(cls, doc, part):
  162. """Returns the next step in the correct type"""
  163. if isinstance(doc, Mapping):
  164. return part
  165. elif isinstance(doc, Sequence):
  166. if part == '-':
  167. return part
  168. if not JsonPointer._RE_ARRAY_INDEX.match(str(part)):
  169. raise JsonPointerException("'%s' is not a valid sequence index" % part)
  170. return int(part)
  171. elif hasattr(doc, '__getitem__'):
  172. # Allow indexing via ducktyping
  173. # if the target has defined __getitem__
  174. return part
  175. else:
  176. raise JsonPointerException("Document '%s' does not support indexing, "
  177. "must be mapping/sequence or support __getitem__" % type(doc))
  178. def get_parts(self):
  179. """Returns the list of the parts. For example, JsonPointer('/a/b').get_parts() == ['a', 'b']"""
  180. return self.parts
  181. def walk(self, doc, part):
  182. """ Walks one step in doc and returns the referenced part """
  183. part = JsonPointer.get_part(doc, part)
  184. assert hasattr(doc, '__getitem__'), "invalid document type %s" % (type(doc),)
  185. if isinstance(doc, Sequence):
  186. if part == '-':
  187. return EndOfList(doc)
  188. try:
  189. return doc[part]
  190. except IndexError:
  191. raise JsonPointerException("index '%s' is out of bounds" % (part,))
  192. # Else the object is a mapping or supports __getitem__(so assume custom indexing)
  193. try:
  194. return doc[part]
  195. except KeyError:
  196. raise JsonPointerException("member '%s' not found in %s" % (part, doc))
  197. def contains(self, ptr):
  198. """ Returns True if self contains the given ptr """
  199. return self.parts[:len(ptr.parts)] == ptr.parts
  200. def __contains__(self, item):
  201. """ Returns True if self contains the given ptr """
  202. return self.contains(item)
  203. def join(self, suffix):
  204. """ Returns a new JsonPointer with the given suffix append to this ptr """
  205. if isinstance(suffix, JsonPointer):
  206. suffix_parts = suffix.parts
  207. elif isinstance(suffix, str):
  208. suffix_parts = JsonPointer(suffix).parts
  209. else:
  210. suffix_parts = suffix
  211. try:
  212. return JsonPointer.from_parts(chain(self.parts, suffix_parts))
  213. except: # noqa E722
  214. raise JsonPointerException("Invalid suffix")
  215. def __truediv__(self, suffix): # Python 3
  216. return self.join(suffix)
  217. @property
  218. def path(self):
  219. """Returns the string representation of the pointer
  220. >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1'
  221. """
  222. parts = [escape(part) for part in self.parts]
  223. return ''.join('/' + part for part in parts)
  224. def __eq__(self, other):
  225. """Compares a pointer to another object
  226. Pointers can be compared by comparing their strings (or splitted
  227. strings), because no two different parts can point to the same
  228. structure in an object (eg no different number representations)
  229. """
  230. if not isinstance(other, JsonPointer):
  231. return False
  232. return self.parts == other.parts
  233. def __hash__(self):
  234. return hash(tuple(self.parts))
  235. def __str__(self):
  236. return self.path
  237. def __repr__(self):
  238. return type(self).__name__ + "(" + repr(self.path) + ")"
  239. @classmethod
  240. def from_parts(cls, parts):
  241. """Constructs a JsonPointer from a list of (unescaped) paths
  242. >>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0'
  243. True
  244. """
  245. parts = [escape(str(part)) for part in parts]
  246. ptr = cls(''.join('/' + part for part in parts))
  247. return ptr
  248. def escape(s):
  249. return s.replace('~', '~0').replace('/', '~1')
  250. def unescape(s):
  251. return s.replace('~1', '/').replace('~0', '~')