summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenn Knowles <kenn.knowles@gmail.com>2014-02-07 17:43:09 -0500
committerKenn Knowles <kenn.knowles@gmail.com>2014-02-07 17:43:09 -0500
commitb01e2e5ae7cb843532aadbb7929d27ac1529077e (patch)
treee4493435c2f74757bb8ed5227a9cda57570fc243
parentf4cfe56b8e8370116c5275ddcea970e9a68fd75d (diff)
parent670cd30fe81f13be44345ed8c177e08204670a35 (diff)
downloadjsonpath-rw-b01e2e5ae7cb843532aadbb7929d27ac1529077e.tar.gz
Merge pull request #12 from hakanw/jsonpath_script
Add jsonpath.py script that can be used when installing this package
-rwxr-xr-xjsonpath_rw/bin/jsonpath.py51
-rw-r--r--setup.py1
2 files changed, 52 insertions, 0 deletions
diff --git a/jsonpath_rw/bin/jsonpath.py b/jsonpath_rw/bin/jsonpath.py
new file mode 100755
index 0000000..364615f
--- /dev/null
+++ b/jsonpath_rw/bin/jsonpath.py
@@ -0,0 +1,51 @@
+#!/usr/bin/python
+# encoding: utf-8
+# Copyright © 2012 Felix Richter <wtfpl@syntax-fehler.de>
+# This work is free. You can redistribute it and/or modify it under the
+# terms of the Do What The Fuck You Want To Public License, Version 2,
+# as published by Sam Hocevar. See the COPYING file for more details.
+
+from jsonpath_rw import parse
+import json
+import sys
+import glob
+if len(sys.argv) < 2:
+ print("""usage: jsonpath.py expression [files]
+
+The expression is JSONPath and can be:
+
+ atomics:
+ $ - root object
+ `this` - current object
+
+ operators:
+ path1.path2 - same as xpath /
+ path1|path2 - union
+ path1..path2 - somewhere in between
+
+ fiels:
+ fieldname - field with name
+ * - any field
+ [_start_?:_end_?] - array slice
+ [*] - any array index
+""")
+ sys.exit(1)
+
+expr = parse(sys.argv[1])
+
+def find_matches_for_file(f):
+ return [unicode(match.value) for match in expr.find(json.load(f))]
+
+def print_matches(matches):
+ print(u"\n".join(matches).encode("utf-8"))
+
+if len(sys.argv) < 3:
+ # stdin mode
+ print_matches(find_matches_for_file(sys.stdin))
+else:
+ # file paths mode
+ for pattern in sys.argv[2:]:
+ for filename in glob.glob(pattern):
+ with open(filename) as f:
+ print_matches(find_matches_for_file(f))
+
diff --git a/setup.py b/setup.py
index f8d9bd5..c37c0f6 100644
--- a/setup.py
+++ b/setup.py
@@ -14,6 +14,7 @@ setuptools.setup(
license='Apache 2.0',
long_description=io.open('README.rst', encoding='utf-8').read(),
packages = ['jsonpath_rw'],
+ scripts = ['jsonpath_rw/bin/jsonpath.py'],
test_suite = 'tests',
install_requires = [ 'ply', 'decorator', 'six' ],
classifiers = [