X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=clientbin%2FgetRecord.py;h=97d17942446d176590acd4631728374ef1aca458;hb=648ac3fd865ea29425485f26eb0a7bf1a62281e0;hp=e2be593b195c783221a893075e361dcef5b33400;hpb=a0b08c3177b6273ad22f3882cd62495743ed404c;p=sfa.git diff --git a/clientbin/getRecord.py b/clientbin/getRecord.py index e2be593b..97d17942 100755 --- a/clientbin/getRecord.py +++ b/clientbin/getRecord.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python2 """ Filters/Prints record objects @@ -14,37 +14,39 @@ import os from optparse import OptionParser from pprint import pprint from xml.parsers.expat import ExpatError -from sfa.util.xml import XML +from sfa.util.xml import XML + def create_parser(): command = sys.argv[0] argv = sys.argv[1:] usage = "%(command)s [options]" % locals() 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.""" - parser = OptionParser(usage=usage,description=description) + parser = OptionParser(usage=usage, description=description) parser.add_option("-d", "--debug", dest="DEBUG", action="store_true", - default=False, help = "record file path") + default=False, help="record file path") parser.add_option("-k", "--key", dest="withkey", action="store_true", - default=False, help = "print SSH keys and certificates") + default=False, help="print SSH keys and certificates") parser.add_option("-p", "--plinfo", dest="plinfo", action="store_true", - default=False, help = "print PlanetLab specific internal fields") - - return parser + default=False, help="print PlanetLab specific internal fields") + + return parser def printRec(record_dict, filters, options): line = "" if len(filters): for filter in filters: - if options.DEBUG: print "Filtering on %s" %filter - line += "%s: %s\n" % (filter, - printVal(record_dict.get(filter, None))) + if options.DEBUG: + print "Filtering on %s" % filter + line += "%s: %s\n" % (filter, + printVal(record_dict.get(filter, None))) print line else: # print the wole thing for (key, value) in record_dict.iteritems(): if (not options.withkey and key in ('gid', 'keys')) or\ - (not options.plinfo and key == 'pl_info'): + (not options.plinfo and key == 'pl_info'): continue line += "%s: %s\n" % (key, printVal(value)) print line @@ -62,24 +64,25 @@ def printVal(value): def main(): - parser = create_parser(); + parser = create_parser() (options, args) = parser.parse_args() stdin = sys.stdin.read() - + record = XML(stdin) record_dict = record.todict() - - if options.DEBUG: + + if options.DEBUG: pprint(record.toxml()) print "#####################################################" printRec(record_dict, args, options) if __name__ == '__main__': - try: main() - except ExpatError, e: + try: + main() + except ExpatError as e: print "RecordError. Is your record valid XML?" print e - except Exception, e: + except Exception as e: print e