annotate python/jsonex.py @ 769:489204193cd7

fix syntax
author Jeff Hammel <k0scist@gmail.com>
date Fri, 12 Feb 2016 09:53:58 -0800
parents 80c47cc35f37
children 0fa456446fea
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
692
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
13 def dereference(obj, key):
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
14 if obj is None:
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
15 return None
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
16 if isinstance(obj, dict):
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
17 return obj[key]
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
18 return obj[int(key)]
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
19
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 def main(args=sys.argv[1:]):
687
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 # command line
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
23 parser = argparse.ArgumentParser(description='__doc__')
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
24 parser.add_argument('input', nargs='?',
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
25 help="input file or url (read from stdin if ommitted)")
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
26 parser.add_argument('object', nargs='*',
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
27 help="object in dotted notation")
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
28
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
29 options = parser.parse_args(args)
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
687
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
31 # get data
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
32 if not options.input or options.input == '-':
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
33 data = sys.stdin
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
34 elif'://' in options.input:
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
35 data = urllib2.urlopen(options.input)
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
36 else:
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
37 data = open(options.input, 'r')
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
38 obj = json.load(data)
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
39
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
40 if options.object:
e50a95358db3 allow loading urls
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
41 for o in options.object:
689
6f48a3e5ab41 STUB: python/jsonex.py
Jeff Hammel <k0scist@gmail.com>
parents: 687
diff changeset
42 base = obj
692
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
43 for key in o.strip().split('.'): # split into objects
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
44 base = dereference(base, key)
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
45 if base is None:
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
46 break
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
47 print (json.dumps(base))
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 else:
692
80c47cc35f37 to method
Jeff Hammel <k0scist@gmail.com>
parents: 689
diff changeset
49 print (json.dumps(obj, indent=2, sort_keys=True))
134
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 if __name__ == '__main__':
cea81044578e add a JSON explorer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 main()