Skip to content

Commit b243fae

Browse files
committed
initial commit
0 parents  commit b243fae

File tree

7 files changed

+225
-0
lines changed

7 files changed

+225
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.pyc
2+
build
3+
.coverage

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Stefan Kögl <[email protected]>

COPYING

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2011 Stefan Kögl <[email protected]>
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions
6+
are met:
7+
8+
1. Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
2. Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
3. The name of the author may not be used to endorse or promote products
14+
derived from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19+
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+

README

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
python-json-pointer: Resolve JSON Pointers
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
Library to resolve JSON Pointers according to
6+
http://tools.ietf.org/html/draft-pbryan-zyp-json-pointer-00
7+
8+
See Sourcecode for Examples
9+
10+
Website: https://github.com/stefankoegl/python-json-pointer
11+
Repository: https://github.com/stefankoegl/python-json-pointer.git

jsonpointer.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 []

setup.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
from distutils.core import setup
4+
import re
5+
6+
src = open('jsonpatch.py').read()
7+
metadata = dict(re.findall("__([a-z]+)__ = '([^']+)'", src))
8+
docstrings = re.findall('"""(.*)"""', src)
9+
10+
PACKAGE = 'jsonpatch'
11+
12+
MODULES = (
13+
'jsonpatch',
14+
)
15+
16+
AUTHOR_EMAIL = metadata['author']
17+
VERSION = metadata['version']
18+
WEBSITE = metadata['website']
19+
LICENSE = metadata['license']
20+
DESCRIPTION = docstrings[0]
21+
22+
# Extract name and e-mail ("Firstname Lastname <[email protected]>")
23+
AUTHOR, EMAIL = re.match(r'(.*) <(.*)>', AUTHOR_EMAIL).groups()
24+
25+
setup(name=PACKAGE,
26+
version=VERSION,
27+
description=DESCRIPTION,
28+
author=AUTHOR,
29+
author_email=EMAIL,
30+
license=LICENSE,
31+
url=WEBSITE,
32+
py_modules=MODULES,
33+
)

tests.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from __future__ import print_function
5+
import doctest
6+
import unittest
7+
import sys
8+
9+
modules = ['jsonpointer']
10+
coverage_modules = []
11+
12+
suite = unittest.TestSuite()
13+
14+
for module in modules:
15+
m = __import__(module, fromlist=[module])
16+
coverage_modules.append(m)
17+
suite.addTest(doctest.DocTestSuite(m))
18+
19+
runner = unittest.TextTestRunner(verbosity=2)
20+
21+
try:
22+
import coverage
23+
except ImportError:
24+
coverage = None
25+
26+
if coverage is not None:
27+
coverage.erase()
28+
coverage.start()
29+
30+
result = runner.run(suite)
31+
32+
if not result.wasSuccessful():
33+
sys.exit(1)
34+
35+
if coverage is not None:
36+
coverage.stop()
37+
coverage.report(coverage_modules)
38+
coverage.erase()
39+
40+
if coverage is None:
41+
print("""
42+
No coverage reporting done (Python module "coverage" is missing)
43+
Please install the python-coverage package to get coverage reporting.
44+
""", file=sys.stderr)

0 commit comments

Comments
 (0)