8000 Merge pull request #12 from hakanw/jsonpath_script · Evgenus/python-jsonpath-rw@b01e2e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit b01e2e5

Browse files
committed
Merge pull request kennknowles#12 from hakanw/jsonpath_script
Add jsonpath.py script that can be used when installing this package
2 parents f4cfe56 + 670cd30 commit b01e2e5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

jsonpath_rw/bin/jsonpath.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/python
2+
# encoding: utf-8
3+
# Copyright © 2012 Felix Richter <wtfpl@syntax-fehler.de>
4+
# This work is free. You can redistribute it and/or modify it under the
5+
# terms of the Do What The Fuck You Want To Public License, Version 2,
6+
# as published by Sam Hocevar. See the COPYING file for more details.
7+
8+
from jsonpath_rw import parse
9+
import json
10+
import sys
11+
import glob
12+
if len(sys.argv) < 2:
13+
print("""usage: jsonpath.py expression [files]
14+
15+
The expression is JSONPath and can be:
16+
17+
atomics:
18+
$ - root object
19+
`this` - current object
20+
21+
operators:
22+
path1.path2 - same as xpath /
23+
path1|path2 - union
24+
path1..path2 - somewhere in between
25+
26+
fiels:
27+
fieldname - field with name
28+
* - any field
29+
[_start_?:_end_?] - array slice
30+
[*] - any array index
31+
""")
32+
sys.exit(1)
33+
34+
expr = parse(sys.argv[1])
35+
36+
def find_matches_for_file(f):
37+
return [unicode(match.value) for match in expr.find(json.load(f))]
38+
39 C113 +
def print_matches(matches):
40+
print(u"\n".join(matches).encode("utf-8"))
41+
42+
if len(sys.argv) < 3:
43+
# stdin mode
44+
print_matches(find_matches_for_file(sys.stdin))
45+
else:
46+
# file paths mode
47+
for pattern in sys.argv[2:]:
48+
for filename in glob.glob(pattern):
49+
with open(filename) as f:
50+
print_matches(find_matches_for_file(f))
51+

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
license='Apache 2.0',
1515
long_description=io.open('README.rst', encoding='utf-8').read(),
1616
packages = ['jsonpath_rw'],
17+
scripts = ['jsonpath_rw/bin/jsonpath.py'],
1718
test_suite = 'tests',
1819
install_requires = [ 'ply', 'decorator', 'six' ],
1920
classifiers = [

0 commit comments

Comments
 (0)
0