|
| 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 <[email protected]> |
| 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 | + |
| 33 | +""" Identify specific nodes in a JSON document according to |
| 34 | +http://tools.ietf.org/html/draft-pbryan-zyp-json-pointer-00 """ |
| 35 | + |
| 36 | +# Will be parsed by setup.py to determine package metadata |
| 37 | +__author__ = 'Stefan Kögl <[email protected]>' |
| 38 | +__version__ = '0.1' |
| 39 | +__website__ = 'https://github.com/stefankoegl/python-json-pointer' |
| 40 | +__license__ = 'Modified BSD License' |
| 41 | + |
| 42 | + |
| 43 | +import urllib |
| 44 | + |
| 45 | + |
| 46 | +class JsonPointerException(Exception): |
| 47 | + pass |
| 48 | + |
| 49 | + |
| 50 | +def resolve_pointer(doc, pointer): |
| 51 | + """ |
| 52 | + Resolves pointer against doc and returns the referenced object |
| 53 | +
|
| 54 | + >>> obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}} |
| 55 | +
|
| 56 | + >>> resolve_pointer(obj, '/') == obj |
| 57 | + True |
| 58 | +
|
| 59 | + >>> resolve_pointer(obj, '/foo') |
| 60 | + {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 44}]} |
| 61 | +
|
| 62 | + >>> resolve_pointer(obj, '/foo/another%20prop') |
| 63 | + {'baz': 'A string'} |
| 64 | +
|
| 65 | + >>> resolve_pointer(obj, '/foo/another%20prop/baz') |
| 66 | + 'A string' |
| 67 | +
|
| 68 | + >>> resolve_pointer(obj, '/foo/anArray/0') |
| 69 | + {'prop': 44} |
| 70 | + """ |
| 71 | + |
| 72 | + parts = pointer.split('/') |
| 73 | + if parts.pop(0) != '': |
| 74 | + raise JsonPointerException('location must starts with /') |
| 75 | + |
| 76 | + parts = map(urllib.unquote, parts) |
| 77 | + |
| 78 | + for part in parts: |
| 79 | + doc = walk(doc, part) |
| 80 | + |
| 81 | + return doc |
| 82 | + |
| 83 | + |
| 84 | +def walk(doc, part): |
| 85 | + """ Walks one step in doc and returns the referenced part """ |
| 86 | + |
| 87 | + if not part: |
| 88 | + return doc |
| 89 | + |
| 90 | + # Its not clear if a location "1" should be considered as 1 or "1" |
| 91 | + # We prefer the integer-variant if possible |
| 92 | + part_variants = _try_parse(part) + [part] |
| 93 | + |
| 94 | + for variant in part_variants: |
| 95 | + try: |
| 96 | + return doc[variant] |
| 97 | + except: |
| 98 | + continue |
| 99 | + |
| 100 | + raise JsonPointerException("'%s' not found in %s" % (part, doc)) |
| 101 | + |
| 102 | + |
| 103 | +def _try_parse(val, cls=int): |
| 104 | + try: |
| 105 | + return [cls(val)] |
| 106 | + except: |
| 107 | + return [] |
0 commit comments