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
17 from sfa.util.xml import XML
22 usage = "%(command)s [options]" % locals()
23 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."""
24 parser = OptionParser(usage=usage,description=description)
25 parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
26 default=False, help = "record file path")
27 parser.add_option("-k", "--key", dest="withkey", action="store_true",
28 default=False, help = "print SSH keys and certificates")
29 parser.add_option("-p", "--plinfo", dest="plinfo", action="store_true",
30 default=False, help = "print PlanetLab specific internal fields")
35 def printRec(record_dict, filters, options):
38 for filter in filters:
39 if options.DEBUG: print "Filtering on %s" %filter
40 line += "%s: %s\n" % (filter,
41 printVal(record_dict.get(filter, None)))
44 # print the wole thing
45 for (key, value) in record_dict.iteritems():
46 if (not options.withkey and key in ('gid', 'keys')) or\
47 (not options.plinfo and key == 'pl_info'):
49 line += "%s: %s\n" % (key, printVal(value))
53 # fix the iteratable values
56 if type(value) in (tuple, list):
61 return line.rstrip("\n")
65 parser = create_parser();
66 (options, args) = parser.parse_args()
68 stdin = sys.stdin.read()
71 record_dict = record.todict()
74 pprint(record.toxml())
75 print "#####################################################"
77 printRec(record_dict, args, options)
79 if __name__ == '__main__':
82 print "RecordError. Is your record valid XML?"