1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
Description: Fix web2py/py2web helpers
See https://github.com/translate/translate/pull/3838 and
https://bugs.debian.org/916745.
Author: Vinyl Darkscratch <vinyldarkscratch@gmail.com>
Origin: https://github.com/translate/translate/pull/3838
Applied-Upstream: pending review
--- a/translate/convert/po2web2py.py
+++ b/translate/convert/po2web2py.py
@@ -24,11 +24,13 @@
for examples and usage instructions.
"""
-from io import BytesIO
+from io import StringIO
from translate.convert import convert
from translate.storage import factory
+import six
+
class po2pydict(object):
@@ -36,7 +38,7 @@
return
def convertstore(self, inputstore, includefuzzy):
- str_obj = BytesIO()
+ str_obj = StringIO()
mydict = dict()
for unit in inputstore.units:
@@ -46,13 +48,15 @@
mydict[unit.source] = unit.target
else:
mydict[unit.source] = unit.source
- # The older convention is to prefix with "*** ":
- #mydict[unit.source] = '*** ' + unit.source
- str_obj.write('{\n')
- for source_str in mydict:
- str_obj.write("%s:%s,\n" % (repr(str(source_str)), repr(str(mydict[source_str]))))
- str_obj.write('}\n')
+ str_obj.write(u'# -*- coding: utf-8 -*-\n')
+ str_obj.write(u'{\n')
+ for source_str, trans_str in sorted(mydict.items()):
+ if six.PY2:
+ source_str = source_str.encode('utf-8')
+ trans_str = trans_str.encode('utf-8')
+ str_obj.write(u"%s: %s,\n" % (repr(source_str), repr(trans_str)))
+ str_obj.write(u'}\n')
str_obj.seek(0)
return str_obj
@@ -62,16 +66,23 @@
inputstore = factory.getobject(inputfile)
if not convert.should_output_store(inputstore, outputthreshold):
- return False
+ return 0
convertor = po2pydict()
outputstring = convertor.convertstore(inputstore, includefuzzy)
- outputfile.write(outputstring.read())
+
+ if six.PY2:
+ outputfile.write(outputstring.read().decode('utf-8'))
+ else:
+ outputfile.write(bytes(outputstring.read(), 'utf-8'))
return 1
def main(argv=None):
- formats = {("po", "py"): ("py", convertpy), ("po"): ("py", convertpy)}
+ formats = {
+ ("po", "py"): ("py", convertpy),
+ ("po", None): ("py", convertpy)
+ }
parser = convert.ConvertOptionParser(formats, usetemplates=False, description=__doc__)
parser.add_threshold_option()
parser.add_fuzzy_option()
--- a/translate/convert/web2py2po.py
+++ b/translate/convert/web2py2po.py
@@ -26,11 +26,14 @@
from translate.storage import po
+import six
+
class web2py2po(object):
- def __init__(self, pofile=None):
+ def __init__(self, pofile=None, duplicatestyle="msgctxt"):
self.mypofile = pofile
+ self.duplicatestyle = duplicatestyle
def convertunit(self, source_str, target_str):
pounit = po.pounit(encoding="UTF-8")
@@ -40,25 +43,26 @@
return pounit
def convertstore(self, mydict):
-
targetheader = self.mypofile.header()
targetheader.addnote("extracted from web2py", "developer")
for source_str in mydict.keys():
target_str = mydict[source_str]
+ if six.PY2:
+ target_str = target_str.decode('utf-8')
+ source_str = source_str.decode('utf-8')
if target_str == source_str:
# a convention with new (untranslated) web2py files
target_str = u''
- elif target_str.startswith(u'*** '):
- # an older convention
- target_str = u''
pounit = self.convertunit(source_str, target_str)
self.mypofile.addunit(pounit)
+ self.mypofile.removeduplicates(self.duplicatestyle)
+
return self.mypofile
-def convertpy(inputfile, outputfile, encoding="UTF-8"):
+def convertpy(inputfile, outputfile, encoding="UTF-8", duplicatestyle="msgctxt"):
new_pofile = po.pofile()
convertor = web2py2po(new_pofile)
--- /dev/null
+++ b/translate/convert/test_po2web2py.py
@@ -0,0 +1,75 @@
+# -*- coding: utf-8 -*-
+
+import pytest
+
+from translate.convert import po2web2py
+from translate.misc import wStringIO
+from translate.storage import po
+
+import six
+
+class TestPO2WEB2PY(object):
+
+ def po2web2py(self, po_source):
+ """helper that converts po source to web2py source without requiring files"""
+ input_file = wStringIO.StringIO(po_source)
+ input_po = po.pofile(input_file)
+ convertor = po2web2py.po2pydict()
+ output_web2py = convertor.convertstore(input_po, False)
+ return output_web2py.read()
+
+ def test_basic(self):
+ """test a basic po to web2py conversion"""
+ input_po = '''#: .text
+msgid "A simple string"
+msgstr "Du texte simple"
+'''
+ expected_web2py = '''# -*- coding: utf-8 -*-
+{
+'A simple string': 'Du texte simple',
+}
+'''
+ web2py_out = self.po2web2py(input_po)
+ assert web2py_out == expected_web2py
+
+ @pytest.mark.skipif(six.PY2, reason='Not implemented for PY2')
+ def test_unicode(self):
+ """test a po to web2py conversion with unicode"""
+ input_po = '''#: .text
+msgid "Foobar"
+msgstr "Fúbär"
+'''
+ expected_web2py = '''# -*- coding: utf-8 -*-
+{
+'Foobar': 'Fúbär',
+}
+'''
+ web2py_out = self.po2web2py(input_po)
+ if six.PY2:
+ web2py_out = web2py_out.encode('utf-8')
+ assert web2py_out == expected_web2py
+
+ def test_ordering_serialize(self):
+ """test alphabetic ordering in po to web2py conversion"""
+ input_po = '''
+#: .foo
+msgid "foo"
+msgstr "oof"
+
+#: .bar
+msgid "bar"
+msgstr "rab"
+
+#: .baz
+msgid "baz"
+msgstr "zab"
+'''
+ expected_web2py = '''# -*- coding: utf-8 -*-
+{
+'bar': 'rab',
+'baz': 'zab',
+'foo': 'oof',
+}
+'''
+ web2py_out = self.po2web2py(input_po)
+ assert web2py_out == expected_web2py
--- /dev/null
+++ b/translate/convert/test_web2py2po.py
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+import sys
+
+import six
+import pytest
+
+from translate.convert import web2py2po
+from translate.storage import po
+from translate.storage.test_base import first_translatable, headerless_len
+
+
+class TestWEB2PY2PO(object):
+
+ def web2py2po(self, web2py_source):
+ """helper that converts po source to web2py source without requiring files"""
+ input_web2py = eval(web2py_source)
+ new_pofile = po.pofile()
+ convertor = web2py2po.web2py2po(new_pofile)
+ output_po = convertor.convertstore(input_web2py)
+ return output_po
+
+ def singleelement(self, storage):
+ """checks that the pofile contains a single non-header element, and returns it"""
+ assert headerless_len(storage.units) == 1
+ return first_translatable(storage)
+
+ def test_basic(self):
+ """test a basic web2py to po conversion"""
+ input_web2py = '''# -*- coding: utf-8 -*-
+{
+'A simple string': 'Du texte simple',
+}
+'''
+
+ po_out = self.web2py2po(input_web2py)
+ pounit = self.singleelement(po_out)
+ assert pounit.source == "A simple string"
+ assert pounit.target == "Du texte simple"
+
+ @pytest.mark.skipif(six.PY2, reason='Not implemented for PY2')
+ def test_unicode(self):
+ """test a web2py to po conversion with unicode"""
+ input_web2py = '''# -*- coding: utf-8 -*-
+{
+'Foobar': 'Fúbär',
+}
+'''
+
+ po_out = self.web2py2po(input_web2py)
+ pounit = self.singleelement(po_out)
+ assert pounit.source == "Foobar"
+ assert pounit.target == "Fúbär"
|