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 SSH keys and certificates")
32 parser.add_option("-p", "--plinfo", dest="plinfo", action="store_true",
33 default=False, help = "print PlanetLab specific internal fields")
38 def printRec(record, filters, options):
41 for filter in filters:
42 if options.DEBUG: print "Filtering on %s" %filter
43 line += "%s: %s\n" % (filter,
44 printVal(record.dict["record"].get(filter, None)))
47 # print the wole thing
48 for (key, value) in record.dict["record"].iteritems():
49 if (not options.withkey and key in ('gid', 'keys')) or\
50 (not options.plinfo and key == 'pl_info'):
52 line += "%s: %s\n" % (key, printVal(value))
56 # fix the iteratable values
59 if type(value) in (tuple, list):
64 return line.rstrip("\n")
68 parser = create_parser();
69 (options, args) = parser.parse_args()
71 stdin = sys.stdin.read()
73 record = RecordSpec(xml = stdin)
75 if not record.dict.has_key("record"):
76 raise "RecordError", "Input record does not have 'record' tag."
80 print "#####################################################"
82 printRec(record, args, options)
84 if __name__ == '__main__':
87 print "RecordError. Is your record valid XML?"