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
18 from xml.parsers.expat import ExpatError
19 from geni.util.rspec import RecordSpec
25 usage = "%(command)s [options]" % locals()
26 description = """getRecord will parse a supplied (via stdin) record and print all values or key/values, and filter results based on a given key or set of keys."""
27 parser = OptionParser(usage=usage,description=description)
28 parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
29 default=False, help = "record file path")
30 parser.add_option("-k", "--key", dest="withkey", action="store_true",
31 default=False, help = "print keys")
35 def printRec(record, filters, options):
38 for filter in filters:
39 if options.DEBUG: print "Filtering on %s" %filter
41 line += "%s: " %filter
43 printVal(record.dict["record"].get(filter, None))
46 # print the wole thing
47 for (key, value) in record.dict["record"].iteritems():
50 line += "%s\n" % printVal(value)
53 # fix the iteratable values
56 if type(value) in (tuple, list):
61 return line.rstrip("\n")
64 parser = create_parser();
65 (options, args) = parser.parse_args()
67 stdin = sys.stdin.read()
69 record = RecordSpec(xml = stdin)
71 if not record.dict.has_key("record"):
72 raise "RecordError", "Input record does not have 'record' tag."
76 print "#####################################################"
78 printRec(record, args, options)
80 if __name__ == '__main__':
83 print "RecordError. Is your record valid XML?"