Skip to content
Snippets Groups Projects
Unverified Commit 896102d6 authored by Stefan Kögl's avatar Stefan Kögl Committed by GitHub
Browse files

Merge pull request #46 from PeterlitsZo/patch-1

Add method and add classmethod tag
parents 7d146bd7 a5e9f91e
No related branches found
No related tags found
1 merge request!1Fix some issues reported by lintian
......@@ -193,7 +193,7 @@ class JsonPointer(object):
for part in self.parts[:-1]:
doc = self.walk(doc, part)
return doc, self.get_part(doc, self.parts[-1])
return doc, JsonPointer.get_part(doc, self.parts[-1])
def resolve(self, doc, default=_nothing):
"""Resolves the pointer against doc and returns the referenced object"""
......@@ -228,7 +228,8 @@ class JsonPointer(object):
parent[part] = value
return doc
def get_part(self, doc, part):
@classmethod
def get_part(cls, doc, part):
"""Returns the next step in the correct type"""
if isinstance(doc, Mapping):
......@@ -239,7 +240,7 @@ class JsonPointer(object):
if part == '-':
return part
if not self._RE_ARRAY_INDEX.match(str(part)):
if not JsonPointer._RE_ARRAY_INDEX.match(str(part)):
raise JsonPointerException("'%s' is not a valid sequence index" % part)
return int(part)
......@@ -252,12 +253,17 @@ class JsonPointer(object):
else:
raise JsonPointerException("Document '%s' does not support indexing, "
"must be mapping/sequence or support __getitem__" % type(doc))
def get_parts(self):
"""Returns the list of the parts. For example, JsonPointer('/a/b').get_parts() == ['a', 'b']"""
return self.parts
def walk(self, doc, part):
""" Walks one step in doc and returns the referenced part """
part = self.get_part(doc, part)
part = JsonPointer.get_part(doc, part)
assert hasattr(doc, '__getitem__'), "invalid document type %s" % (type(doc),)
......
......@@ -70,10 +70,31 @@ class SpecificationTests(unittest.TestCase):
ptr = JsonPointer(path)
self.assertEqual(path, ptr.path)
parts = ptr.parts
parts = ptr.get_parts()
self.assertEqual(parts, ptr.parts)
new_ptr = JsonPointer.from_parts(parts)
self.assertEqual(ptr, new_ptr)
def test_parts(self):
paths = [
("", []),
("/foo", ['foo']),
("/foo/0", ['foo', '0']),
("/", ['']),
("/a~1b", ['a/b']),
("/c%d", ['c%d']),
("/e^f", ['e^f']),
("/g|h", ['g|h']),
("/i\\j", ['i\j']),
("/k\"l", ['k"l']),
("/ ", [' ']),
("/m~0n", ['m~n']),
('/\xee', ['\xee']),
]
for path in paths:
ptr = JsonPointer(path[0])
self.assertEqual(ptr.get_parts(), path[1])
class ComparisonTests(unittest.TestCase):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment