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
23 usage = "%(command)s [options]" % locals()
24 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."""
25 parser = OptionParser(usage=usage, description=description)
26 parser.add_option("-d", "--debug", dest="DEBUG", action="store_true",
27 default=False, help="record file path")
28 parser.add_option("-k", "--key", dest="withkey", action="store_true",
29 default=False, help="print SSH keys and certificates")
30 parser.add_option("-p", "--plinfo", dest="plinfo", action="store_true",
31 default=False, help="print PlanetLab specific internal fields")
36 def printRec(record_dict, filters, options):
39 for filter in filters:
41 print "Filtering on %s" % filter
42 line += "%s: %s\n" % (filter,
43 printVal(record_dict.get(filter, None)))
46 # print the wole thing
47 for (key, value) in record_dict.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()
73 record_dict = record.todict()
76 pprint(record.toxml())
77 print "#####################################################"
79 printRec(record_dict, args, options)
81 if __name__ == '__main__':
84 except ExpatError as e:
85 print "RecordError. Is your record valid XML?"
87 except Exception as e: