changeset 688:ad8146c60c4d

merge
author Jeff Hammel <k0scist@gmail.com>
date Mon, 02 Jun 2014 11:21:46 -0700
parents e50a95358db3 (diff) 34af6d5ac73c (current diff)
children 6f48a3e5ab41
files
diffstat 1 files changed, 23 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/python/jsonex.py	Sat May 24 15:27:16 2014 -0700
+++ b/python/jsonex.py	Mon Jun 02 11:21:46 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