From c5a30f5dc7a54d81714a30f7f8140347db173e8f Mon Sep 17 00:00:00 2001 From: Patrice Clement Date: Fri, 11 Aug 2017 19:42:34 +0200 Subject: use context managers wherever possible --- src/py/xml-rewrite-2.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/py/xml-rewrite-2.py b/src/py/xml-rewrite-2.py index 721147b..2b4702d 100755 --- a/src/py/xml-rewrite-2.py +++ b/src/py/xml-rewrite-2.py @@ -204,8 +204,6 @@ parameters will break the script.""" print("ERROR: " + message) sys.exit(1) -# if len(sys.argv) == 1: -# usage(True) options_list = [ make_option( "-f", @@ -320,8 +318,7 @@ parameters will break the script.""" if options.doAdd and (len(options.values or []) != len(options.attributes or []) or len(options.source_values or []) != len(options.source_attributes or []) or len(options.target_values or []) != len(options.target_attributes or [])): - error( - "You must give attribute(s)/value(s) for every element you are changing.") + error("You must give attribute(s)/value(s) for every element you are changing.") # End Invalid Arguments Check @@ -354,17 +351,16 @@ parameters will break the script.""" dirname = os.path.dirname(file) if dirname != '': # for file = build.xml comes out as '' os.chdir(os.path.dirname(file)) - f = open(os.path.basename(file), "r") - if options.gentoo_classpath: - rewriter.process(f, add_gentoo_classpath) - else: - rewriter.process(f) - os.chdir(cwd) - f.close() - # Then write it back to the file - f = open(file, "w") - rewriter.write(f) - f.close() + + with open(os.path.basename(file), 'r') as f: + if options.gentoo_classpath: + rewriter.process(f, add_gentoo_classpath) + else: + rewriter.process(f) + + # Then write it back out to the file + with open(file, 'w') as f: + rewriter.write(f) else: if options.gentoo_classpath: @@ -375,4 +371,4 @@ parameters will break the script.""" if __name__ == '__main__': - main() + main() \ No newline at end of file -- cgit v1.2.3-65-gdbad