-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
jq.pyx
432 lines (337 loc) · 12.4 KB
/
jq.pyx
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import io
import json
import threading
from cpython.bytes cimport PyBytes_AsString
from cpython.bytes cimport PyBytes_AsStringAndSize
from libc.float cimport DBL_MAX
from libc.math cimport INFINITY, modf
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)
int jv_string_length_bytes(jv)
int jv_is_integer(jv)
double jv_number_value(jv)
jv jv_array()
jv jv_array_append(jv, jv)
int jv_array_length(jv)
jv jv_array_get(jv, int)
int jv_object_iter(jv)
int jv_object_iter_next(jv, int)
int jv_object_iter_valid(jv, int)
jv jv_object_iter_key(jv, int)
jv jv_object_iter_value(jv, int)
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*)
jv jv_parse(const char*)
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)
int jq_compile_args(jq_state *, const char* str, jv)
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 **)
cdef object _jv_to_python(jv value):
"""Unpack a jv value into a Python value"""
cdef jv_kind kind = jv_get_kind(value)
cdef int idx
cdef jv property_key
cdef jv property_value
cdef object python_value
cdef double number_value
if kind == JV_KIND_INVALID:
raise ValueError("Invalid value")
elif kind == JV_KIND_NULL:
python_value = None
elif kind == JV_KIND_FALSE:
python_value = False
elif kind == JV_KIND_TRUE:
python_value = True
elif kind == JV_KIND_NUMBER:
number_value = jv_number_value(value)
if number_value == INFINITY:
python_value = DBL_MAX
elif number_value == -INFINITY:
python_value = -DBL_MAX
elif number_value != number_value:
python_value = None
elif _is_integer(number_value):
python_value = int(number_value)
else:
python_value = number_value
elif kind == JV_KIND_STRING:
python_value = jv_string_to_py_string(value)
elif kind == JV_KIND_ARRAY:
python_value = []
for idx in range(0, jv_array_length(jv_copy(value))):
property_value = jv_array_get(jv_copy(value), idx)
python_value.append(_jv_to_python(property_value))
elif kind == JV_KIND_OBJECT:
python_value = {}
idx = jv_object_iter(value)
while jv_object_iter_valid(value, idx):
property_key = jv_object_iter_key(value, idx)
property_value = jv_object_iter_value(value, idx)
try:
python_value[jv_string_to_py_string(property_key)] = \
_jv_to_python(property_value)
finally:
jv_free(property_key)
idx = jv_object_iter_next(value, idx)
else:
raise ValueError("Invalid value kind: " + str(kind))
jv_free(value)
return python_value
cdef int _is_integer(double value) noexcept:
cdef double integral_part
cdef double fractional_part = modf(value, &integral_part)
return fractional_part == 0
def compile(object program, args=None):
cdef object program_bytes = program.encode("utf8")
return _Program(program_bytes, args=args)
_compilation_lock = threading.Lock()
cdef jq_state* _compile(object program_bytes, object args) except NULL:
cdef jq_state *jq = jq_init()
cdef _ErrorStore error_store
cdef jv jv_args
cdef int compiled
try:
if not jq:
raise Exception("jq_init failed")
error_store = _ErrorStore()
with _compilation_lock:
jq_set_error_cb(jq, _store_error, <void*>error_store)
if args is None:
compiled = jq_compile(jq, program_bytes)
else:
args_bytes = json.dumps(args).encode("utf-8")
jv_args = jv_parse(PyBytes_AsString(args_bytes))
compiled = jq_compile_args(jq, program_bytes, jv_args)
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) noexcept:
# TODO: handle errors not of JV_KIND_STRING
cdef _ErrorStore store = <_ErrorStore>store_ptr
if jv_get_kind(error) == JV_KIND_STRING:
try:
error_string = jv_string_to_py_string(error)
except:
error_string = "Internal error"
store.store_error(error_string)
jv_free(error)
cdef class _ErrorStore(object):
cdef object _errors
def __cinit__(self):
self.clear()
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 _JqStatePool(object):
cdef jq_state* _jq_state
cdef object _program_bytes
cdef object _args
cdef object _lock
def __cinit__(self, program_bytes, args):
self._program_bytes = program_bytes
self._args = args
self._jq_state = _compile(self._program_bytes, args=self._args)
self._lock = threading.Lock()
def __dealloc__(self):
jq_teardown(&self._jq_state)
cdef jq_state* acquire(self):
with self._lock:
if self._jq_state == NULL:
return _compile(self._program_bytes, args=self._args)
else:
state = self._jq_state
self._jq_state = NULL
return state
cdef void release(self, jq_state* state):
with self._lock:
if self._jq_state == NULL:
self._jq_state = state
else:
jq_teardown(&state)
cdef class _Program(object):
cdef object _program_bytes
cdef _JqStatePool _jq_state_pool
def __cinit__(self, program_bytes, args):
self._program_bytes = program_bytes
self._jq_state_pool = _JqStatePool(program_bytes, args=args)
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")
if text is not _NO_VALUE:
return self.input_text(text)
else:
return self.input_value(value)
def input_value(self, value):
return self.input_text(json.dumps(value))
def input_values(self, values):
fileobj = io.StringIO()
for value in values:
json.dump(value, fileobj)
fileobj.write("\n")
return self.input_text(fileobj.getvalue())
def input_text(self, text, *, slurp=False):
return _ProgramWithInput(self._jq_state_pool, text.encode("utf8"), slurp=slurp)
@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 _JqStatePool _jq_state_pool
cdef object _bytes_input
cdef bint _slurp
def __cinit__(self, jq_state_pool, bytes_input, *, bint slurp):
self._jq_state_pool = jq_state_pool
self._bytes_input = bytes_input
self._slurp = slurp
def __iter__(self):
return self._make_iterator()
cdef _ResultIterator _make_iterator(self):
return _ResultIterator(self._jq_state_pool, self._bytes_input, slurp=self._slurp)
def text(self):
# Performance testing suggests that using _jv_to_python (within the
# result iterator) followed by json.dumps is faster than using
# jv_dump_string to generate the string directly from the jv values.
# See: https://github.com/mwilliamson/jq.py/pull/50
return "\n".join(json.dumps(v) for v in self)
def all(self):
return list(self)
def first(self):
return next(_iter(self))
cdef class _ResultIterator(object):
cdef _JqStatePool _jq_state_pool
cdef jq_state* _jq
cdef jv_parser* _parser
cdef bytes _bytes_input
cdef bint _slurp
cdef bint _ready
def __dealloc__(self):
self._jq_state_pool.release(self._jq)
jv_parser_free(self._parser)
def __cinit__(self, _JqStatePool jq_state_pool, bytes bytes_input, *, bint slurp):
self._jq_state_pool = jq_state_pool
self._jq = jq_state_pool.acquire()
self._bytes_input = bytes_input
self._slurp = slurp
self._ready = False
cdef jv_parser* parser = jv_parser_new(0)
cdef char* cbytes_input
cdef ssize_t clen_input
PyBytes_AsStringAndSize(bytes_input, &cbytes_input, &clen_input)
jv_parser_set_buf(parser, cbytes_input, clen_input, 0)
self._parser = parser
def __iter__(self):
return self
def __next__(self):
while True:
if not self._ready:
self._ready_next_input()
self._ready = True
result = jq_next(self._jq)
if jv_is_valid(result):
return _jv_to_python(result)
elif jv_invalid_has_msg(jv_copy(result)):
error_message = jv_invalid_get_msg(result)
message = jv_string_to_py_string(error_message)
jv_free(error_message)
raise ValueError(message)
else:
jv_free(result)
self._ready = False
cdef bint _ready_next_input(self) except 1:
cdef int jq_flags = 0
cdef jv value
if self._slurp:
value = jv_array()
while True:
try:
next_value = self._parse_next_input()
value = jv_array_append(value, next_value)
except StopIteration:
self._slurp = False
break
else:
value = self._parse_next_input()
jq_start(self._jq, value, jq_flags)
return 0
cdef inline jv _parse_next_input(self) except *:
cdef jv value = jv_parser_next(self._parser)
if jv_is_valid(value):
return value
elif jv_invalid_has_msg(jv_copy(value)):
error_message = jv_invalid_get_msg(value)
message = jv_string_to_py_string(error_message)
jv_free(error_message)
raise ValueError(u"parse error: " + message)
else:
jv_free(value)
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)
cdef unicode jv_string_to_py_string(jv value):
cdef int length = jv_string_length_bytes(jv_copy(value))
cdef char* string_value = jv_string_value(value)
return string_value[:length].decode("utf-8")