diff options
author | David Pursehouse <dpursehouse@collab.net> | 2018-03-27 17:12:43 +0100 |
---|---|---|
committer | David Pursehouse <dpursehouse@collab.net> | 2018-03-27 17:12:43 +0100 |
commit | 2528abf059653db8eef1cbaa29ea972e91b5a0ad (patch) | |
tree | 634241abf67a5a40a5fdb09be3c0a57f8e75d6a2 | |
parent | 6c662384a6bf34aabe9a0036ba17aec82a489137 (diff) |
PG: Make convert_for_template_tests.py compatible with Python 3
Add a wrapper method that tries to call open() with Python 3 semantics,
and falls back to Python 2 if it fails.
Bug: Issue 8609
Helped-by: Paladox none <thomasmulhall410@yahoo.com>
Change-Id: Ic46eb31a23506d47edcf6518dc1ccfb274f45c52
-rw-r--r-- | polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py b/polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py index 89bada344e..3a5cd83b65 100644 --- a/polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py +++ b/polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py @@ -10,26 +10,32 @@ fnCompiledRegex = re.compile(removeSelfInvokeRegex, re.DOTALL) regexBehavior = r"<script>(.+)<\/script>" behaviorCompiledRegex = re.compile(regexBehavior, re.DOTALL) +def _open(filename, mode="r"): + try: + return open(filename, mode, encoding="utf-8") + except TypeError: + return open(filename, mode) + def replaceBehaviorLikeHTML (fileIn, fileOut): - with open(fileIn) as f: + with _open(fileIn) as f: file_str = f.read() match = behaviorCompiledRegex.search(file_str) if (match): - with open("polygerrit-ui/temp/behaviors/" + fileOut.replace("html", "js") , "w+") as f: + with _open("polygerrit-ui/temp/behaviors/" + fileOut.replace("html", "js") , "w+") as f: f.write(match.group(1)) def replaceBehaviorLikeJS (fileIn, fileOut): - with open(fileIn) as f: + with _open(fileIn) as f: file_str = f.read() - with open("polygerrit-ui/temp/behaviors/" + fileOut , "w+") as f: + with _open("polygerrit-ui/temp/behaviors/" + fileOut , "w+") as f: f.write(file_str) def generateStubBehavior(behaviorName): - with open("polygerrit-ui/temp/behaviors/" + behaviorName + ".js", "w+") as f: + with _open("polygerrit-ui/temp/behaviors/" + behaviorName + ".js", "w+") as f: f.write("/** @polymerBehavior **/\n" + behaviorName + "= {};") def replacePolymerElement (fileIn, fileOut, root): - with open(fileIn) as f: + with _open(fileIn) as f: key = fileOut.split('.')[0] # Removed self invoked function file_str = f.read() @@ -38,7 +44,7 @@ def replacePolymerElement (fileIn, fileOut, root): if file_str_no_fn: package = root.replace("/", ".") + "." + fileOut - with open("polygerrit-ui/temp/" + fileOut, "w+") as f: + with _open("polygerrit-ui/temp/" + fileOut, "w+") as f: mainFileContents = re.sub(polymerCompiledRegex, "exports = Polymer({", file_str_no_fn.group(1)).replace("'use strict';", "") f.write("/** \n" \ "* @fileoverview \n" \ @@ -95,7 +101,7 @@ if __name__ == "__main__": #TODO figure out something to do with iron-overlay-behavior. it is hard-coded reformatted. - with open("polygerrit-ui/temp/map.json", "w+") as f: + with _open("polygerrit-ui/temp/map.json", "w+") as f: f.write(json.dumps(elements)) for root, dirs, files in os.walk("polygerrit-ui/app/behaviors"): @@ -103,4 +109,4 @@ if __name__ == "__main__": if file.endswith("behavior.html"): replaceBehaviorLikeHTML(os.path.join(root, file), file) elif file.endswith("behavior.js"): - replaceBehaviorLikeJS(os.path.join(root, file), file)
\ No newline at end of file + replaceBehaviorLikeJS(os.path.join(root, file), file) |