Kaydet (Commit) d92a87f1 authored tarafından Miklos Vajna's avatar Miklos Vajna

fdo#41380: fix po2lo to support python3 as well

üst 8f8e9a4a
...@@ -78,7 +78,7 @@ class Template: ...@@ -78,7 +78,7 @@ class Template:
"""Represents a reference template in SDF format.""" """Represents a reference template in SDF format."""
def __init__(self, path): def __init__(self, path):
sock = open(path) sock = xopen(path, "r", encoding='utf-8')
self.lines = [] self.lines = []
for line in sock: for line in sock:
entry = Entry(line.split('\t')) entry = Entry(line.split('\t'))
...@@ -88,7 +88,7 @@ class Template: ...@@ -88,7 +88,7 @@ class Template:
def translate(self, translations): def translate(self, translations):
"""Translates entires in the template based on translations.""" """Translates entires in the template based on translations."""
sock = open(options.output, "w") sock = xopen(options.output, "w", encoding='utf-8')
for line in self.lines: for line in self.lines:
line.translate(translations) line.translate(translations)
sock.write("\t".join(line.items)+"\r\n") sock.write("\t".join(line.items)+"\r\n")
...@@ -103,7 +103,7 @@ class Translations: ...@@ -103,7 +103,7 @@ class Translations:
for root, dirs, files in os.walk(options.input): for root, dirs, files in os.walk(options.input):
for file in files: for file in files:
path = "%s/%s" % (root, file) path = "%s/%s" % (root, file)
sock = open(path) sock = xopen(path, "r", encoding='utf-8')
buf = [] buf = []
multiline = False multiline = False
fuzzy = False fuzzy = False
...@@ -166,6 +166,13 @@ class Translations: ...@@ -166,6 +166,13 @@ class Translations:
text = text.replace(tag, escaped_tag) text = text.replace(tag, escaped_tag)
return text return text
def xopen(path, mode, encoding):
"""Wrapper around open() to support both python2 and python3."""
if sys.version_info >= (3,):
return open(path, mode, encoding=encoding)
else:
return open(path, mode)
def main(): def main():
"""Main function of this script.""" """Main function of this script."""
...@@ -193,7 +200,7 @@ options = Options() ...@@ -193,7 +200,7 @@ options = Options()
# used by sdf2po() # used by sdf2po()
normalfilenamechars = "/#.0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" normalfilenamechars = "/#.0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
normalizetable = "" normalizetable = ""
for i in map(chr, range(256)): for i in map(chr, list(range(256))):
if i in normalfilenamechars: if i in normalfilenamechars:
normalizetable += i normalizetable += i
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment