add JSON helpers
This commit is contained in:
parent
eb2fd26332
commit
62adf86daa
2 changed files with 55 additions and 1 deletions
|
|
@ -412,6 +412,18 @@ class JsonPage(Page):
|
||||||
def data(self):
|
def data(self):
|
||||||
return self.response.text
|
return self.response.text
|
||||||
|
|
||||||
|
def get(self, path):
|
||||||
|
node = self.doc
|
||||||
|
for name in filter(None, path.strip('.').split('.')):
|
||||||
|
node = node.get(name)
|
||||||
|
if node is None:
|
||||||
|
break
|
||||||
|
return node
|
||||||
|
|
||||||
|
def path(self, path):
|
||||||
|
from weboob.tools.json import mini_jsonpath
|
||||||
|
return mini_jsonpath(self.doc, path)
|
||||||
|
|
||||||
def build_doc(self, text):
|
def build_doc(self, text):
|
||||||
from weboob.tools.json import json
|
from weboob.tools.json import json
|
||||||
return json.loads(text)
|
return json.loads(text)
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
# because we don't want to import this file by "import json"
|
# because we don't want to import this file by "import json"
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
__all__ = ['json']
|
__all__ = ['json', 'mini_jsonpath']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# try simplejson first because it is faster
|
# try simplejson first because it is faster
|
||||||
|
|
@ -28,3 +28,45 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# Python 2.6+ has a module similar to simplejson
|
# Python 2.6+ has a module similar to simplejson
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def mini_jsonpath(node, path):
|
||||||
|
"""
|
||||||
|
Evaluates a dot separated path against JSON data. Path can contains
|
||||||
|
star wilcards. Always returns a generator.
|
||||||
|
|
||||||
|
Relates to http://goessner.net/articles/JsonPath/ but in a really basic
|
||||||
|
and simpler form.
|
||||||
|
|
||||||
|
>>> list(mini_jsonpath({"x": 95, "y": 77, "z": 68}, 'y'))
|
||||||
|
[77]
|
||||||
|
>>> list(mini_jsonpath({"x": {"y": {"z": "nested"}}}, 'x.y.z'))
|
||||||
|
['nested']
|
||||||
|
>>> list(mini_jsonpath('{"data": [{"x": "foo", "y": 13}, {"x": "bar", "y": 42}, {"x": "baz", "y": 128}]}', 'data.*.y'))
|
||||||
|
[13, 42, 128]
|
||||||
|
"""
|
||||||
|
|
||||||
|
def iterkeys(i):
|
||||||
|
return range(len(i)) if type(i) is list else i.iterkeys()
|
||||||
|
|
||||||
|
def cut(s):
|
||||||
|
p = s.split('.', 1) if s else [None]
|
||||||
|
return p + [None] if len(p) == 1 else p
|
||||||
|
|
||||||
|
if isinstance(node, basestring):
|
||||||
|
node = json.loads(node)
|
||||||
|
|
||||||
|
queue = [(node, cut(path))]
|
||||||
|
while queue:
|
||||||
|
node, (name, rest) = queue.pop(0)
|
||||||
|
if name is None:
|
||||||
|
yield node
|
||||||
|
continue
|
||||||
|
elif type(node) not in (dict, list):
|
||||||
|
continue
|
||||||
|
if name == '*':
|
||||||
|
keys = iterkeys(node)
|
||||||
|
else:
|
||||||
|
keys = [int(name) if type(node) is list else name]
|
||||||
|
for k in keys:
|
||||||
|
queue.append((node[k], cut(rest)))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue