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
18 from sfa.util.rspec import RecordSpec
24 usage = "%(command)s [options]" % locals()
25 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."""
26 parser = OptionParser(usage=usage,description=description)
27 parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
28 default=False, help = "record file path")
29 parser.add_option("-k", "--key", dest="withkey", action="store_true",
30 default=False, help = "print SSH keys and certificates")
31 parser.add_option("-p", "--plinfo", dest="plinfo", action="store_true",
32 default=False, help = "print PlanetLab specific internal fields")
37 def printRec(record, filters, options):
40 for filter in filters:
41 if options.DEBUG: print "Filtering on %s" %filter
42 line += "%s: %s\n" % (filter,
43 printVal(record.dict["record"].get(filter, None)))
46 # print the wole thing
47 for (key, value) in record.dict["record"].iteritems():
48 if (not options.withkey and key in ('gid', 'keys')) or\
49 (not options.plinfo and key == 'pl_info'):
51 line += "%s: %s\n" % (key, printVal(value))
55 # fix the iteratable values
58 if type(value) in (tuple, list):
63 return line.rstrip("\n")
67 parser = create_parser();
68 (options, args) = parser.parse_args()
70 stdin = sys.stdin.read()
72 record = RecordSpec(xml = stdin)
74 if not record.dict.has_key("record"):
75 raise "RecordError", "Input record does not have 'record' tag."
79 print "#####################################################"
81 printRec(record, args, options)
83 if __name__ == '__main__':
86 print "RecordError. Is your record valid XML?"