From 0e2e5379160c317914a403b5ea944f8f0c90684d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Fri, 23 Dec 2016 06:27:27 +0800 Subject: [PATCH] Added compatibility with Python 3 on file I/O. --- bin/obasync | 68 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/bin/obasync b/bin/obasync index 7247856..d440fb0 100755 --- a/bin/obasync +++ b/bin/obasync @@ -135,10 +135,7 @@ def read_in_source_dir(projdir): path = os.path.join(projdir, entry) if os.path.isfile(path) and entry.lower().endswith(".vb"): modname = entry[0:-3] - f = open(path) - modules[modname] = f.read().decode("utf-8").replace( - "\r\n", "\n") - f.close() + modules[modname] = read_file(path) return modules @@ -154,22 +151,15 @@ def update_source_dir(projdir, modules): for modname in sorted(modules.keys()): if modname not in curmods: path = os.path.join(projdir, modname + ".vb") - f = open(path, "w") - f.write(modules[modname]) - f.close() + write_file(path, modules[modname]) print("%s.vb added." % modname, file=sys.stderr) is_in_sync = False else: path = os.path.join(projdir, curmods[modname]) - f = open(path, "r+") - if modules[modname] != f.read().replace("\r\n", "\n"): - f.seek(0) - f.truncate(0) - f.write(modules[modname]) + if update_file(path, modules[modname]): print("%s updated." % curmods[modname], file=sys.stderr) is_in_sync = False - f.close() for modname in sorted(curmods.keys()): if modname not in modules: path = os.path.join(projdir, curmods[modname]) @@ -189,7 +179,7 @@ def read_basic_modules(libraries, libname): libraries.loadLibrary(libname) library = libraries.getByName(libname) for modname in library.getElementNames(): - modules[modname] = library.getByName(modname).encode("utf-8") + modules[modname] = library.getByName(modname) return modules @@ -234,6 +224,56 @@ def create_dialog_library(oo, libname): libraries.storeLibraries() +def read_file(path): + """ Reads a file, and deals with Python 3 / 2 compatibility. """ + if sys.version_info.major == 2: + f = open(path) + content = f.read().decode("utf-8").replace("\r\n", "\n") + f.close() + else: + f = open(path, encoding="utf-8") + content = f.read().replace("\r\n", "\n") + f.close() + return content + + +def write_file(path, content): + """ Writes to a file, and deals with Python 3 / 2 + compatibility. """ + if sys.version_info.major == 2: + f = open(path, "w") + f.write(content.encode("utf-8")) + f.close() + else: + f = open(path, "w", encoding="utf-8") + f.write(content) + f.close() + return + + +def update_file(path, content): + """ Updates a file, and deals with Python 3 / 2 + compatibility. """ + is_updated = False + if sys.version_info.major == 2: + f = open(path, "r+") + if content != f.read().decode("utf-8").replace("\r\n", "\n"): + f.seek(0) + f.truncate(0) + f.write(content.encode("utf-8")) + is_updated = True + f.close() + else: + f = open(path, "r+", encoding="utf-8") + if content != f.read().replace("\r\n", "\n"): + f.seek(0) + f.truncate(0) + f.write(content) + is_updated = True + f.close() + return is_updated + + class OpenOffice: """ The OpenOffice connection. """