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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
import json
from cpython.bytes cimport PyBytes_AsString
cdef extern from "jv.h":
ctypedef enum jv_kind:
JV_KIND_INVALID,
JV_KIND_NULL,
JV_KIND_FALSE,
JV_KIND_TRUE,
JV_KIND_NUMBER,
JV_KIND_STRING,
JV_KIND_ARRAY,
JV_KIND_OBJECT
ctypedef struct jv:
pass
jv_kind jv_get_kind(jv)
int jv_is_valid(jv)
jv jv_copy(jv)
void jv_free(jv)
jv jv_invalid_get_msg(jv)
int jv_invalid_has_msg(jv)
char* jv_string_value(jv)
jv jv_dump_string(jv, int flags)
cdef struct jv_parser:
pass
jv_parser* jv_parser_new(int)
void jv_parser_free(jv_parser*)
void jv_parser_set_buf(jv_parser*, const char*, int, int)
jv jv_parser_next(jv_parser*)
cdef extern from "jq.h":
ctypedef struct jq_state:
pass
ctypedef void (*jq_err_cb)(void *, jv)
jq_state *jq_init()
void jq_teardown(jq_state **)
int jq_compile(jq_state *, const char* str)
void jq_start(jq_state *, jv value, int flags)
jv jq_next(jq_state *)
void jq_set_error_cb(jq_state *, jq_err_cb, void *)
void jq_get_error_cb(jq_state *, jq_err_cb *, void **)
def compile(object program):
cdef object program_bytes = program.encode("utf8")
cdef jq_state* state = _compile(program_bytes)
jq_teardown(&state)
cdef _Program wrapped_program = _Program.__new__(_Program)
wrapped_program._program_bytes = program_bytes
return wrapped_program
cdef jq_state* _compile(object program_bytes) except NULL:
cdef jq_state *jq = jq_init()
cdef _ErrorStore error_store
cdef int compiled
try:
if not jq:
raise Exception("jq_init failed")
error_store = _ErrorStore.__new__(_ErrorStore)
error_store.clear()
jq_set_error_cb(jq, _store_error, <void*>error_store)
compiled = jq_compile(jq, program_bytes)
if error_store.has_errors():
raise ValueError(error_store.error_string())
if not compiled:
raise ValueError("program was not valid")
except:
jq_teardown(&jq)
raise
# TODO: unset error callback?
return jq
cdef void _store_error(void* store_ptr, jv error):
# TODO: handle errors not of JV_KIND_STRING
cdef _ErrorStore store = <_ErrorStore>store_ptr
if jv_get_kind(error) == JV_KIND_STRING:
store.store_error(jv_string_value(error).decode("utf8"))
jv_free(error)
cdef class _ErrorStore(object):
cdef object _errors
cdef int has_errors(self):
return len(self._errors)
cdef object error_string(self):
return "\n".join(self._errors)
cdef void store_error(self, unicode error):
self._errors.append(error)
cdef void clear(self):
self._errors = []
class _EmptyValue(object):
pass
_NO_VALUE = _EmptyValue()
cdef class _Program(object):
cdef object _program_bytes
def input(self, value=_NO_VALUE, text=_NO_VALUE):
if (value is _NO_VALUE) == (text is _NO_VALUE):
raise ValueError("Either the value or text argument should be set")
string_input = text if text is not _NO_VALUE else json.dumps(value)
cdef _ProgramWithInput program_with_input = _ProgramWithInput.__new__(_ProgramWithInput)
program_with_input._program_bytes = self._program_bytes
program_with_input._bytes_input = string_input.encode("utf8")
return program_with_input
@property
def program_string(self):
return self._program_bytes.decode("utf8")
def __repr__(self):
return "jq.compile({!r})".format(self.program_string)
# Support the 0.1.x API for backwards compatibility
def transform(self, value=_NO_VALUE, text=_NO_VALUE, text_output=False, multiple_output=False):
program_with_input = self.input(value, text=text)
if text_output:
return program_with_input.text()
elif multiple_output:
return program_with_input.all()
else:
return program_with_input.first()
cdef class _ProgramWithInput(object):
cdef object _program_bytes
cdef object _bytes_input
def __iter__(self):
return self._make_iterator()
cdef _ResultIterator _make_iterator(self):
iterator = _ResultIterator.__new__(_ResultIterator)
iterator._init(self._program_bytes, self._bytes_input)
return iterator
def text(self):
iterator = self._make_iterator()
results = []
while True:
try:
results.append(iterator._next_string())
except StopIteration:
return "\n".join(results)
def all(self):
return list(self)
def first(self):
return next(_iter(self))
cdef class _ResultIterator(object):
cdef jq_state* _jq
cdef jv_parser* _parser
cdef object _bytes_input
cdef bint _ready
def __dealloc__(self):
jq_teardown(&self._jq)
jv_parser_free(self._parser)
def _init(self, object program_bytes, object bytes_input):
self._jq = _compile(program_bytes)
self._bytes_input = bytes_input
self._ready = False
cdef jv_parser* parser = jv_parser_new(0)
cdef char* cbytes_input = PyBytes_AsString(bytes_input)
jv_parser_set_buf(parser, cbytes_input, len(cbytes_input), 0)
self._parser = parser
def __iter__(self):
return self
def __next__(self):
return json.loads(self._next_string())
cdef unicode _next_string(self):
cdef int dumpopts = 0
while True:
if not self._ready:
self._ready_next_input()
self._ready = True
result = jq_next(self._jq)
if jv_is_valid(result):
dumped = jv_dump_string(result, dumpopts)
value = jv_string_value(dumped).decode("utf8")
jv_free(dumped)
return value
elif jv_invalid_has_msg(jv_copy(result)):
error_message = jv_invalid_get_msg(result)
message = jv_string_value(error_message).decode("utf8")
jv_free(error_message)
raise ValueError(message)
else:
self._ready = False
cdef bint _ready_next_input(self) except 1:
cdef int jq_flags = 0
cdef jv value = jv_parser_next(self._parser)
if jv_is_valid(value):
jq_start(self._jq, value, jq_flags)
return 0
elif jv_invalid_has_msg(jv_copy(value)):
error_message = jv_invalid_get_msg(value)
message = jv_string_value(error_message).decode("utf8")
jv_free(error_message)
raise ValueError(u"parse error: " + message)
else:
raise StopIteration()
def all(program, value=_NO_VALUE, text=_NO_VALUE):
return compile(program).input(value, text=text).all()
def first(program, value=_NO_VALUE, text=_NO_VALUE):
return compile(program).input(value, text=text).first()
_iter = iter
def iter(program, value=_NO_VALUE, text=_NO_VALUE):
return _iter(compile(program).input(value, text=text))
def text(program, value=_NO_VALUE, text=_NO_VALUE):
return compile(program).input(value, text=text).text()
# Support the 0.1.x API for backwards compatibility
def jq(object program):
return compile(program)
|