X-Git-Url: http://git.onelab.eu/?p=sfa.git;a=blobdiff_plain;f=clientbin%2FsetRecord.py;h=493400c3ba6cf0ec1d77ff39636ee7ed36afae5e;hp=bd776069405b410cd7a811a7249a43fab112f766;hb=HEAD;hpb=ecc85e0b923922cf7117d29b380f5284edb88f21 diff --git a/clientbin/setRecord.py b/clientbin/setRecord.py index bd776069..493400c3 100755 --- a/clientbin/setRecord.py +++ b/clientbin/setRecord.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 """ Updates record objects @@ -16,16 +16,17 @@ from optparse import OptionParser from pprint import pprint from sfa.util.xml import XML + def create_parser(): command = sys.argv[0] argv = sys.argv[1:] usage = "%(command)s [options]" % locals() description = """setRecord will edit a record (from stdin), modify its contents, then print the new record to stdout""" - 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 = "print debug info") - - return parser + default=False, help="print debug info") + + return parser def editDict(args, recordDict, options): @@ -37,19 +38,19 @@ def editDict(args, recordDict, options): if vect.count("+="): # append value modDict({vect.split("+=")[0]: returnVal(vect.split("+=")[1])}, - recordDict, options) - + recordDict, options) + elif vect.count("="): # reassign value replaceDict({vect.split("=")[0]: returnVal("=".join(vect.split("=")[1:]))}, - recordDict, options) + recordDict, options) else: if vect in recordDict: del recordDict[vect] else: - raise TypeError("Argument error: Records are updated with \n" \ - "key=val1,val2,valN or\n" \ - "key+=val1,val2,valN \n%s Unknown key/val" % vect) + raise TypeError("Argument error: Records are updated with \n" + "key=val1,val2,valN or\n" + "key+=val1,val2,valN \n%s Unknown key/val" % vect) def replaceDict(newval, recordDict, options): @@ -57,14 +58,15 @@ def replaceDict(newval, recordDict, options): Replaces field in dict """ # Check type of old field matches type of new field - for (key, val) in newval.iteritems(): + for (key, val) in newval.items(): recordDict[key] = val + def modDict(newval, recordDict, options): """ Checks type of existing field, addends new field """ - for (key, val) in newval.iteritems(): + for (key, val) in newval.items(): if (type(recordDict[key]) == list): if (type(val) == list): recordDict[key] = recordDict[key] + val @@ -86,8 +88,9 @@ def returnVal(arg): else: return arg + def main(): - parser = create_parser(); + parser = create_parser() (options, args) = parser.parse_args() record = XML(sys.stdin.read()) @@ -95,13 +98,14 @@ def main(): if args: editDict(args, record_dict, options) if options.DEBUG: - print "New Record:\n%s" % record_dict - + print("New Record:\n%s" % record_dict) + record.parse_dict(record_dict) s = record.toxml() sys.stdout.write(s) if __name__ == '__main__': - try: main() - except Exception, e: - print e + try: + main() + except Exception as e: + print(e)