annotate python/jsonex.py @ 689:6f48a3e5ab41

STUB: python/jsonex.py
author Jeff Hammel <k0scist@gmail.com>
date Mon, 02 Jun 2014 11:25:31 -0700
parents e50a95358db3
children 80c47cc35f37
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 JSON explorer
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
687
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
7 import argparse
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import json
687
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
9 import os
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import sys
687
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
11 import urllib2
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 def main(args=sys.argv[1:]):
687
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
14
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
15 # command line
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
16 parser = argparse.ArgumentParser(description='__doc__')
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
17 parser.add_argument('input', nargs='?',
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
18 help="input file or url (read from stdin if ommitted)")
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
19 parser.add_argument('object', nargs='*',
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
20 help="object in dotted notation")
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
21
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
22 options = parser.parse_args(args)
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
687
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
24 # get data
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
25 if not options.input or options.input == '-':
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
26 data = sys.stdin
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
27 elif'://' in options.input:
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
28 data = urllib2.urlopen(options.input)
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
29 else:
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
30 data = open(options.input, 'r')
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
31 obj = json.load(data)
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
32
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
33 if options.object:
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
34 for o in options.object:
689
6f48a3e5ab41 STUB: python/jsonex.py
Jeff Hammel <k0scist@gmail.com>
parents: 687
diff changeset
35 base = obj
6f48a3e5ab41 STUB: python/jsonex.py
Jeff Hammel <k0scist@gmail.com>
parents: 687
diff changeset
36 for part in o.strip().split('.'): # split into objects
6f48a3e5ab41 STUB: python/jsonex.py
Jeff Hammel <k0scist@gmail.com>
parents: 687
diff changeset
37 raise NotImplementedError('TODO')
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 else:
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 print json.dumps(obj, indent=2, sort_keys=True)
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 if __name__ == '__main__':
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 main()