4 Filters/Prints record objects
7 faiyaza at cs dot princeton dot edu
8 Copyright (c) 2009 Board of Trustees, Princeton University
16 from optparse import OptionParser
17 from pprint import pprint
19 from geni.util.rspec import RecordSpec
25 usage = "%(command)s [options]" % locals()
26 description = """getRecord will open a record file and print all key/values, or filter results based on a given key or set of keys."""
27 parser = OptionParser(usage=usage,description=description)
28 parser.add_option("-i", "--infile", dest="infile", metavar="FILE",
29 default=None, help = "record file path")
30 parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
31 default=False, help = "record file path")
35 def findRoot(r, filter):
38 if not r.has_key(filter):
40 root = findRoot(r[k], filter)
41 if root != None: return root
44 elif type(r) in (tuple, list):
46 root = findRoot(j, filter)
47 if root != None: return root
52 parser = create_parser();
53 (options, args) = parser.parse_args()
55 # Check the the file was specified
56 if not options.infile:
57 print "You must specify a record file"
60 print "Openning %s.\n" % options.infile
61 f = open(options.infile)
64 record = RecordSpec(xml = f)
68 print "#####################################################"
73 print "Filtering on key: %s" % args[0]
74 pprint(findRoot(record.dict, args[0]))
75 record.pprint({args[0]: findRoot(record.dict, args[0])})
77 record.pprint(record.dict)
79 if __name__ == '__main__':