Mercurial > hg > config
comparison python/jsonex.py @ 687:e50a95358db3
allow loading urls
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Mon, 02 Jun 2014 11:20:19 -0700 |
| parents | cea81044578e |
| children | 6f48a3e5ab41 |
comparison
equal
deleted
inserted
replaced
| 685:3da85ac3c54f | 687:e50a95358db3 |
|---|---|
| 2 | 2 |
| 3 """ | 3 """ |
| 4 JSON explorer | 4 JSON explorer |
| 5 """ | 5 """ |
| 6 | 6 |
| 7 import argparse | |
| 7 import json | 8 import json |
| 9 import os | |
| 8 import sys | 10 import sys |
| 9 from pprint import pprint | 11 import urllib2 |
| 10 | 12 |
| 11 def main(args=sys.argv[1:]): | 13 def main(args=sys.argv[1:]): |
| 12 data = sys.stdin.read() # read from stdin | |
| 13 obj = json.loads(data) | |
| 14 | 14 |
| 15 if args: | 15 # command line |
| 16 for arg in args: | 16 parser = argparse.ArgumentParser(description='__doc__') |
| 17 parser.add_argument('input', nargs='?', | |
| 18 help="input file or url (read from stdin if ommitted)") | |
| 19 parser.add_argument('object', nargs='*', | |
| 20 help="object in dotted notation") | |
| 21 | |
| 22 options = parser.parse_args(args) | |
| 23 | |
| 24 # get data | |
| 25 if not options.input or options.input == '-': | |
| 26 data = sys.stdin | |
| 27 elif'://' in options.input: | |
| 28 data = urllib2.urlopen(options.input) | |
| 29 else: | |
| 30 data = open(options.input, 'r') | |
| 31 obj = json.load(data) | |
| 32 | |
| 33 if options.object: | |
| 34 for o in options.object: | |
| 17 foo = arg.split('.') # split into objects | 35 foo = arg.split('.') # split into objects |
| 18 # TODO: split into slice notation | 36 # TODO: split into slice notation |
| 19 pass # TODO | 37 pass # TODO |
| 20 else: | 38 else: |
| 21 print json.dumps(obj, indent=2, sort_keys=True) | 39 print json.dumps(obj, indent=2, sort_keys=True) |
