diff options
author | 2017-08-11 19:42:34 +0200 | |
---|---|---|
committer | 2017-08-12 10:50:00 +0200 | |
commit | c5a30f5dc7a54d81714a30f7f8140347db173e8f (patch) | |
tree | ea12afe091fa5cd32cfc55bd87f50126b5ed3be5 | |
parent | reorganise imports (diff) | |
download | javatoolkit-c5a30f5dc7a54d81714a30f7f8140347db173e8f.tar.gz javatoolkit-c5a30f5dc7a54d81714a30f7f8140347db173e8f.tar.bz2 javatoolkit-c5a30f5dc7a54d81714a30f7f8140347db173e8f.zip |
use context managers wherever possible
-rwxr-xr-x | src/py/xml-rewrite-2.py | 28 |
1 files 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 |