-
Notifications
You must be signed in to change notification settings - Fork 214
Add jsonpath.py script that can be used when installing this package #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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))] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably will have to use some Python 2 & 3 adaptation here - |
||
|
|
||
| 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): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just noticed this. This makes it break when the files have special characters in their name. Globbing is the shell's job. Do you actually require this? Is this a Windows thing?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh. Just discovered that, at least at the time of some SO answers, DOS/Windows shell does not glob.This behavior is a bug in the DOS/Windows shell. I'll leave this in until someone on a unixy OS reports a bug, at which point I'll be forced to fix this to not glob. It is just a rare corner case so who knows if it will ever come up. |
||
| with open(filename) as f: | ||
| print_matches(find_matches_for_file(f)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| license='Apache 2.0', | ||
| long_description=io.open('README.rst', encoding='utf-8').read(), | ||
| packages = ['jsonpath_rw'], | ||
| scripts = ['jsonpath_rw/bin/jsonpath.py'], | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the past I've used something like Two things I really like about that are:
Is there any pitfall to this method? I know the packaging situation is confusing in Python so I could have got something wrong. |
||
| test_suite = 'tests', | ||
| install_requires = [ 'ply', 'decorator', 'six' ], | ||
| classifiers = [ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below. Would love the main logic to be wrapped up into a function (maybe using argparse since I'm sure it will end up having options) so that it can be tested and generally invoked from Python as well. I can merge and do the adaptation but if you would be willing to, that would be great too.
An example of the CLI style I like is https://github.com/dimagi/commcare-export/blob/master/commcare_export/cli.py (I wrote it).