forked from doloopwhile/pyjq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyjq.py
74 lines (53 loc) · 2.13 KB
/
pyjq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import json
import re
from six.moves import urllib
import _pyjq
__all__ = []
def compile(script, vars={}, library_paths=[]):
"""
Compile a jq script, retuning a script object.
library_paths is a list of strings that defines the module search path.
"""
return _pyjq.Script(script.encode('utf-8'), vars=vars,
library_paths=library_paths)
def default_opener(url):
res = urllib.request.urlopen(url)
content_type = res.getheader('Content-Type')
if content_type is None:
encoding = 'utf-8'
else:
m = re.search(r'charset\s*=\s*(\S+)', content_type, re.I)
if m is None:
encoding = 'utf-8'
else:
encoding = m.group(1)
return json.loads(res.read().decode(encoding))
def _get_value(value, url, opener):
if url is not None:
if value is not None:
raise TypeError("'value' and 'url' are not able to specified at the same time")
return opener(url)
return value
def all(script, value=None, vars={}, url=None, opener=default_opener, library_paths=[]):
"""
Transform value by script, returning all results as list.
"""
return compile(script, vars, library_paths).all(_get_value(value, url, opener))
def apply(script, value=None, vars={}, url=None, opener=default_opener, library_paths=[]):
"""
Transform value by script, returning all results as list.
"""
return all(script, value, vars, url, opener, library_paths)
apply.__doc__ = all.__doc__
def first(script, value=None, default=None, vars={}, url=None, opener=default_opener, library_paths=[]):
"""
Transform object by jq script, returning the first result.
Return default if result is empty.
"""
return compile(script, vars, library_paths).first(_get_value(value, url, opener), default)
def one(script, value=None, vars={}, url=None, opener=default_opener, library_paths=[]):
"""
Transform object by jq script, returning the first result.
Raise ValueError unless results does not include exactly one element.
"""
return compile(script, vars, library_paths).one(_get_value(value, url, opener))