changeset 687:e50a95358db3

allow loading urls
author Jeff Hammel <k0scist@gmail.com>
date Mon, 02 Jun 2014 11:20:19 -0700
parents 3da85ac3c54f
children ad8146c60c4d
files python/jsonex.py
diffstat 1 files changed, 23 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/python/jsonex.py	Mon May 19 13:16:53 2014 -0700
+++ b/python/jsonex.py	Mon Jun 02 11:20:19 2014 -0700
@@ -4,16 +4,34 @@
 JSON explorer
 """
 
+import argparse
 import json
+import os
 import sys
-from pprint import pprint
+import urllib2
 
 def main(args=sys.argv[1:]):
-    data = sys.stdin.read() # read from stdin
-    obj = json.loads(data)
+
+    # command line
+    parser = argparse.ArgumentParser(description='__doc__')
+    parser.add_argument('input', nargs='?',
+                        help="input file or url (read from stdin if ommitted)")
+    parser.add_argument('object', nargs='*',
+                        help="object in dotted notation")
+
+    options = parser.parse_args(args)
 
-    if args:
-        for arg in args:
+    # get data
+    if not options.input or options.input == '-':
+        data = sys.stdin
+    elif'://' in options.input:
+        data = urllib2.urlopen(options.input)
+    else:
+        data = open(options.input, 'r')
+    obj = json.load(data)
+
+    if options.object:
+        for o in options.object:
             foo = arg.split('.') # split into objects
             # TODO: split into slice notation
             pass # TODO