4 Filters/Prints record objects
7 faiyaza at cs dot princeton dot edu
8 Copyright (c) 2009 Board of Trustees, Princeton University
14 from optparse import OptionParser
15 from pprint import pprint
16 from xml.parsers.expat import ExpatError
21 usage = "%(command)s [options]" % locals()
22 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."""
23 parser = OptionParser(usage=usage,description=description)
24 parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
25 default=False, help = "record file path")
26 parser.add_option("-k", "--key", dest="withkey", action="store_true",
27 default=False, help = "print SSH keys and certificates")
28 parser.add_option("-p", "--plinfo", dest="plinfo", action="store_true",
29 default=False, help = "print PlanetLab specific internal fields")
34 def printRec(record, filters, options):
37 for filter in filters:
38 if options.DEBUG: print "Filtering on %s" %filter
39 line += "%s: %s\n" % (filter,
40 printVal(record.dict["record"].get(filter, None)))
43 # print the wole thing
44 for (key, value) in record.dict["record"].iteritems():
45 if (not options.withkey and key in ('gid', 'keys')) or\
46 (not options.plinfo and key == 'pl_info'):
48 line += "%s: %s\n" % (key, printVal(value))
52 # fix the iteratable values
55 if type(value) in (tuple, list):
60 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?"