def _runTests(path): for i, (json_path, selector_path, output_path) in enumerate(_fileTuples(path)): if DEBUG_FILTER and DEBUG_FILTER not in selector_path: continue data = jsonLoadOrdered(open(json_path).read()) selector = open(selector_path).read().strip() expected_output = open(output_path).read().strip() outputs = [] try: items = jsonselect.match(selector, data) actual_output = '\n'.join([json.dumps(o, indent=4) for o in items]) except jsonselect.JsonSelectError as e: actual_output = 'Error: {0}'.format(e) # Remove trailing whitespace, see http://bugs.python.org/issue16333 actual_output = '\n'.join([line.rstrip() for line in actual_output.split('\n')]) if expected_output != actual_output: open('/tmp/expected.txt', 'w').write(expected_output) open('/tmp/actual.txt', 'w').write(actual_output) eq_(expected_output, actual_output, msg='%s: %s\n%r != %r' % (selector_path, selector.strip(), expected_output, actual_output)) sys.stderr.write('%s %2d %s: passed\n' % (path, i, selector_path))
def _runTests(path): for i, (json_path, selector_path, output_path) in enumerate(_fileTuples(path)): if DEBUG_FILTER and DEBUG_FILTER not in selector_path: continue data = jsonLoadOrdered(open(json_path).read()) selector = open(selector_path).read().strip() expected_output = open(output_path).read().strip() outputs = [] try: items = jsonselect.match(selector, data) actual_output = "\n".join([json.dumps(o, indent=4) for o in items]) except jsonselect.JsonSelectError as e: actual_output = "Error: {0}".format(e) # Remove trailing whitespace, see http://bugs.python.org/issue16333 actual_output = "\n".join([line.rstrip() for line in actual_output.split("\n")]) if expected_output != actual_output: open("/tmp/expected.txt", "w").write(expected_output) open("/tmp/actual.txt", "w").write(actual_output) eq_( expected_output, actual_output, msg="%s: %s\n%r != %r" % (selector_path, selector.strip(), expected_output, actual_output), ) sys.stderr.write("%s %2d %s: passed\n" % (path, i, selector_path))
def apply_selector(objs, selector): '''Returns a list of objects which match the selector in any of objs.''' out = [] for obj in objs: timer.log('Applying selector: %s' % selector) out += list(jsonselect.match(selector, objs)) timer.log('done applying selector') return out
def test_abort(): '''Explicitly stopping the iterator should stop the search.''' obj = [0, 1, 2, 3, 4, 5, 6] iterator = jsonselect.match('number', obj) out = [] for v in iterator: out.append(v) if v == 3: iterator.close() eq_([0, 1, 2, 3], out)
def test_ignore_subtree(): obj = OrderedDict([('foo', [1, 2, 3]), ('bar', [4, 5, 6] ) ]) out = [] def bail(obj, matches): return matches and (obj == [1, 2, 3] or obj == [4, 5, 6]) iterator = jsonselect.match('*', obj, bailout_fn=bail) for v in iterator: out.append(v) eq_([[1, 2, 3], [4, 5, 6], obj], out)
def test_ignore_subtree(): obj = { 'foo': [1, 2, 3], 'bar': [4, 5, 6] } out = [] def bail(obj, matches): return matches and (obj == [1, 2, 3] or obj == [4, 5, 6]) iterator = jsonselect.match('*', obj, bailout_fn=bail) for v in iterator: out.append(v) eq_([[1, 2, 3], [4, 5, 6], obj], out)
def selector_to_ids(selector, obj, mode): def bail_on_match(obj, matches): return matches bail_fn = None if mode == DELETE: # There's no point in continuing a search below a node which will be # marked for deletion. bail_fn = bail_on_match matches = jsonselect.match(selector, obj, bailout_fn=bail_fn) return [id(node) for node in matches]
def match(sel, obj): return list(jsonselect.match(sel, obj))