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 printRecord(r, depth = 0):
38 for tab in range(0,depth): line += " "
39 # check if it's nested
41 if type(r[i]) == dict:
42 # print the parent, carry return
43 print line + "%s :\n" % i
44 #recurse here, increment depth
45 printRecord(r[i], depth + 1)
47 #assume we've hit the bottom
48 if (type(r[i]) == tuple) or (type(r[i]) == list):
49 print line + " %s:" % i
52 printRecord(j, depth + 2)
53 else: print line + line + " %s" % j
55 print line + " %s: %s" % (i, r[i])
58 parser = create_parser();
59 (options, args) = parser.parse_args()
61 # Check the the file was specified
62 if not options.infile:
63 print "You must specify a record file"
68 print "Openning %s.\n" % options.infile
69 record.parseFile(options.infile)
71 record.dict = record.toDict()
74 print "Filtering on key: %s" % args
78 print "#####################################################"
81 printRecord(record.dict)
83 if __name__ == '__main__':