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