def dtd2po(self, dtdsource, dtdtemplate=None): """helper that converts dtd source to po source without requiring files""" inputfile = wStringIO.StringIO(dtdsource) inputdtd = dtd.dtdfile(inputfile) convertor = dtd2po.dtd2po() if dtdtemplate is None: outputpo = convertor.convertstore(inputdtd) else: templatefile = wStringIO.StringIO(dtdtemplate) templatedtd = dtd.dtdfile(templatefile) outputpo = convertor.mergestore(templatedtd, inputdtd) return outputpo
def test_escape_conversion(self): """test to ensure that we convert escapes correctly""" oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Column1\tColumn2\r\n 2002-02-02 02:02:02' + '\r\n' posource = '''#: numpages.src#RID_SVXPAGE_NUM_OPTIONS.STR_BULLET.string.text\nmsgid "Column1\\tColumn2\\r\\n"\nmsgstr "Kolom1\\tKolom2\\r\\n"\n''' inputfile = wStringIO.StringIO(posource) outputfile = wStringIO.StringIO() templatefile = wStringIO.StringIO(oosource) assert po2oo.convertoo(inputfile, outputfile, templatefile, targetlanguage="af-ZA") assert b"\tKolom1\\tKolom2\\r\\n\t" in outputfile.getvalue()
def prop2po(self, propsource, proptemplate=None, personality="java"): """helper that converts .properties source to po source without requiring files""" inputfile = wStringIO.StringIO(propsource) inputprop = properties.propfile(inputfile, personality=personality) convertor = prop2po.prop2po(personality=personality) if proptemplate: templatefile = wStringIO.StringIO(proptemplate) templateprop = properties.propfile(templatefile) outputpo = convertor.mergestore(templateprop, inputprop) else: outputpo = convertor.convertstore(inputprop) return outputpo
def php2po(self, phpsource, phptemplate=None): """helper that converts .phperties source to po source without requiring files""" inputfile = wStringIO.StringIO(phpsource) inputphp = php.phpfile(inputfile) convertor = php2po.php2po() if phptemplate: templatefile = wStringIO.StringIO(phptemplate) templatephp = php.phpfile(templatefile) outputpo = convertor.mergestore(templatephp, inputphp) else: outputpo = convertor.convertstore(inputphp) return outputpo
def csv2po(self, csvsource, template=None): """helper that converts csv source to po source without requiring files""" inputfile = wStringIO.StringIO(csvsource) inputcsv = csvl10n.csvfile(inputfile) if template: templatefile = wStringIO.StringIO(template) inputpot = po.pofile(templatefile) else: inputpot = None convertor = csv2po.csv2po(templatepo=inputpot) outputpo = convertor.convertstore(inputcsv) return outputpo
def po2tmx(self, posource, sourcelanguage='en', targetlanguage='af'): """helper that converts po source to tmx source without requiring files""" inputfile = wStringIO.StringIO(posource) outputfile = wStringIO.StringIO() outputfile.tmxfile = tmx.tmxfile(inputfile=None, sourcelanguage=sourcelanguage) po2tmx.convertpo(inputfile, outputfile, templatefile=None, sourcelanguage=sourcelanguage, targetlanguage=targetlanguage) return outputfile.tmxfile
def pretranslatepo(self, input_source, template_source=None): """helper that converts strings to po source without requiring files""" input_file = wStringIO.StringIO(input_source) if template_source: template_file = wStringIO.StringIO(template_source) else: template_file = None output_file = wStringIO.StringIO() pretranslate.pretranslate_file(input_file, output_file, template_file) output_file.seek(0) return po.pofile(output_file.read())
def inc2po(self, incsource, inctemplate=None): """helper that converts .inc source to po source without requiring files""" inputfile = wStringIO.StringIO(incsource) if inctemplate: templatefile = wStringIO.StringIO(inctemplate) else: templatefile = None outputfile = wStringIO.StringIO() result = mozfunny2prop.inc2po(inputfile, outputfile, templatefile) outputpo = outputfile.getvalue() outputpofile = po.pofile(wStringIO.StringIO(outputpo)) return outputpofile
def test_helpcontent_escapes2(self): """test to ensure that we convert helpcontent escapes correctly""" oosource = r'helpcontent2 source\text\scalc\05\empty_cells.xhp 0 help par_id2629474 0 en-US A1: <empty> 2002-02-02 02:02:02' + '\r\n' posource = r'''#: empty_cells.xhp#par_id2629474.help.text msgid "A1: <empty>" msgstr "Aa1: <empty>" ''' inputfile = wStringIO.StringIO(posource) outputfile = wStringIO.StringIO() templatefile = wStringIO.StringIO(oosource) assert po2oo.convertoo(inputfile, outputfile, templatefile, targetlanguage="af-ZA") assert b"Aa1: <empty>" in outputfile.getvalue()
def merge2prop(self, propsource, posource, personality="java"): """helper that merges po translations to .properties source without requiring files""" inputfile = wStringIO.StringIO(posource) inputpo = po.pofile(inputfile) templatefile = wStringIO.StringIO(propsource) #templateprop = properties.propfile(templatefile) convertor = po2prop.reprop(templatefile, inputpo, personality=personality) outputprop = convertor.convertstore() print outputprop return outputprop
def l20n2po(self, l20n_source, l20n_template=None): """helper that converts .ftl (l20n) source to po source without requiring files""" inputfile = wStringIO.StringIO(l20n_source) input_l20n = l20n.l20nfile(inputfile) convertor = l20n2po.l20n2po() if l20n_template: templatefile = wStringIO.StringIO(l20n_template) template_l20n = l20n.l20nfile(templatefile) outputpo = convertor.merge_stores(template_l20n, input_l20n) else: outputpo = convertor.convert_store(input_l20n) return outputpo
def _convert(self, input_string, template_string=None, include_fuzzy=False, output_threshold=None, success_expected=True): """Helper that converts to target format without using files.""" input_file = wStringIO.StringIO(input_string) output_file = wStringIO.StringIO() template_file = None if template_string: template_file = wStringIO.StringIO(template_string) expected_result = 1 if success_expected else 0 converter = self.ConverterClass(input_file, output_file, template_file, include_fuzzy, output_threshold) assert converter.run() == expected_result return None, output_file
def _convert(self, input_string, template_string=None, blank_msgstr=False, duplicate_style="msgctxt", success_expected=True): """Helper that converts to target format without using files.""" input_file = wStringIO.StringIO(input_string) output_file = wStringIO.StringIO() template_file = None if template_string: template_file = wStringIO.StringIO(template_string) expected_result = 1 if success_expected else 0 converter = self.ConverterClass(input_file, output_file, template_file, blank_msgstr, duplicate_style) assert converter.run() == expected_result return converter.target_store, output_file
def l20n2po(self, input_string, l20n_template=None, blank_msgstr=False, duplicate_style="msgctxt", success_expected=True): """helper that converts .ftl (l20n) source to po source without requiring files""" inputfile = wStringIO.StringIO(input_string) outputfile = wStringIO.StringIO() templatefile = None if l20n_template: templatefile = wStringIO.StringIO(l20n_template) expected_result = 1 if success_expected else 0 result = l20n2po.convertl20n(inputfile, outputfile, templatefile, blank_msgstr, duplicate_style) assert result == expected_result return po.pofile(wStringIO.StringIO(outputfile.getvalue()))
def test_upload_xliff_over_file(self): """Tests that we can upload a new version of a XLIFF file into a project.""" pocontent = wStringIO.StringIO('#: test.c\nmsgid "test"\nmsgstr "rest"\n\n#: frog.c\nmsgid "tadpole"\nmsgstr "fish"\n') pocontent.name = "test_upload_xliff.po" post_dict = { 'file': pocontent, 'overwrite': 'overwrite', 'do_upload': 'upload', } response = self.client.post("/ar/tutorial/", post_dict) xlfcontent = wStringIO.StringIO('''<?xml version="1.0" encoding="utf-8"?> <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1"> <file datatype="po" original="test_upload_xliff.po" source-language="en-US"> <body> <trans-unit id="test" xml:space="preserve" approved="yes"> <source>test</source> <target state="translated">rested</target> <context-group name="po-reference" purpose="location"> <context context-type="sourcefile">test.c</context> </context-group> </trans-unit> <trans-unit id="slink" xml:space="preserve" approved="yes"> <source>slink</source> <target state="translated">stink</target> <context-group name="po-reference" purpose="location"> <context context-type="sourcefile">toad.c</context> </context-group> </trans-unit> </body> </file> </xliff>''') xlfcontent.name = "test_upload_xliff.xlf" post_dict = { 'file': xlfcontent, 'overwrite': 'merge', 'do_upload': 'upload', } response = self.client.post("/ar/tutorial/", post_dict) # NOTE: this is what we do currently: any altered strings become suggestions. # It may be a good idea to change this mergedcontent = '#: test.c\nmsgid "test"\nmsgstr "rest"\n\n#: frog.c\nmsgid "tadpole"\nmsgstr "fish"\n' suggestedcontent = '#: test.c\nmsgid ""\n"_: suggested by admin [595179475]\\n"\n"test"\nmsgstr "rested"\n' store = Store.objects.get(pootle_path="/ar/tutorial/test_upload_xliff.po") self.assertTrue(os.path.isfile(store.file.path)) self.assertTrue(store.file.read().find(mergedcontent) >= 0) suggestions = [str(sug) for sug in store.findunit('test').get_suggestions()] self.assertTrue('rested' in suggestions)
def mergexliff(self, templatesource, inputsource, mergeblanks="yes", mergecomments="yes"): """merges the sources of the given files and returns a new xlifffile object""" templatefile = wStringIO.StringIO(templatesource) inputfile = wStringIO.StringIO(inputsource) outputfile = wStringIO.StringIO() assert pomerge.mergestore(inputfile, outputfile, templatefile, mergeblanks=mergeblanks, mergecomments=mergecomments) outputxliffstring = outputfile.getvalue() print "Generated XML:" print outputxliffstring outputxlifffile = xliff.xlifffile(outputxliffstring) return outputxlifffile
def test_default_timestamp(self): """test to ensure that we revert to the default timestamp""" oointro, oooutro = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Text ', '\r\n' posource = '''#: numpages.src#RID_SVXPAGE_NUM_OPTIONS.STR_BULLET.string.text\nmsgid "Text"\nmsgstr "Text"\n''' inputfile = wStringIO.StringIO(posource) outputfile = wStringIO.StringIO() templatefile = wStringIO.StringIO(oointro + '20050924 09:13:58' + oooutro) assert po2oo.convertoo(inputfile, outputfile, templatefile, targetlanguage="en-US") assert outputfile.getvalue().decode( 'utf-8') == oointro + '2002-02-02 02:02:02' + oooutro
def test_convertphp(self): """test convertphp helper""" posource = '''#: $lang['name'] msgid "value" msgstr "waarde" ''' phptemplate = '''$lang['name'] = 'value'; ''' phpexpected = b'''$lang['name'] = 'waarde'; ''' inputfile = wStringIO.StringIO(posource) templatefile = wStringIO.StringIO(phptemplate) outputfile = wStringIO.StringIO() assert po2php.convertphp(inputfile, outputfile, templatefile) == 1 assert outputfile.getvalue() == phpexpected
def convert_to_target_text(self, input_source, template_source=None, blank_msgstr=False, duplicate_style="msgctxt"): """Helper that converts format input to PO output without files.""" input_file = wStringIO.StringIO(input_source) output_file = wStringIO.StringIO() template_file = None if template_source: template_file = wStringIO.StringIO(template_source) result = ical2po.convertical(input_file, output_file, template_file, blank_msgstr, duplicate_style) assert result == 1 return output_file.getvalue().decode('utf-8')
def test_mergesore_bad_data(self): """Test that we catch bad options sent to mergestore""" templatefile = wStringIO.StringIO("") inputfile = wStringIO.StringIO("") outputfile = wStringIO.StringIO() with pytest.raises(ValueError): pomerge.mergestore(inputfile, outputfile, templatefile, mergeblanks="yay") with pytest.raises(ValueError): pomerge.mergestore(inputfile, outputfile, templatefile, mergecomments="yay")
def convert_to_target_text(self, input_source, template_source=None, include_fuzzy=False, output_threshold=None): """Helper that converts PO input to format output without files.""" input_file = wStringIO.StringIO(input_source) output_file = wStringIO.StringIO() template_file = None if template_source: template_file = wStringIO.StringIO(template_source) result = po2ical.convertical(input_file, output_file, template_file, include_fuzzy, output_threshold) assert result == 1 return output_file.getvalue().decode('utf-8')
def mergestore(self, templatesource, inputsource, mergeblanks="yes", mergefuzzy="yes", mergecomments="yes"): """merges the sources of the given files and returns a new pofile object""" templatefile = wStringIO.StringIO(templatesource) inputfile = wStringIO.StringIO(inputsource) outputfile = wStringIO.StringIO() assert pomerge.mergestore(inputfile, outputfile, templatefile, mergeblanks=mergeblanks, mergefuzzy=mergefuzzy, mergecomments=mergecomments,) outputpostring = outputfile.getvalue() outputpofile = po.pofile(outputpostring) return outputpofile
def ts2po(self, tssource): converter = ts2po.ts2po() tsfile = wStringIO.StringIO(tssource) outputpo = converter.convertfile(tsfile) print("The generated po:") print(bytes(outputpo)) return outputpo
def po2csv(self, posource): """helper that converts po source to csv source without requiring files""" inputfile = wStringIO.StringIO(posource) inputpo = po.pofile(inputfile) convertor = po2csv.po2csv() outputcsv = convertor.convertstore(inputpo) return outputcsv
def po2lang(self, posource): """helper that converts po source to .lang source without requiring files""" inputfile = wStringIO.StringIO(posource) inputpo = po.pofile(inputfile) convertor = po2mozlang.po2lang(mark_active=False) outputlang = convertor.convertstore(inputpo) return bytes(outputlang).decode('utf-8')
def json2po(self, jsonsource, template=None, filter=None): """helper that converts json source to po source without requiring files""" inputfile = wStringIO.StringIO(jsonsource) inputjson = jsonl10n.JsonFile(inputfile, filter=filter) convertor = json2po.json2po() outputpo = convertor.convert_store(inputjson) return outputpo
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 po2dtd(self, posource, remove_untranslated=False): """helper that converts po source to dtd source without requiring files""" inputfile = wStringIO.StringIO(posource) inputpo = po.pofile(inputfile) convertor = po2dtd.po2dtd(remove_untranslated=remove_untranslated) outputdtd = convertor.convertstore(inputpo) return outputdtd
def po2prop(self, posource): """helper that converts po source to .properties source without requiring files""" inputfile = wStringIO.StringIO(posource) inputpo = po.pofile(inputfile) convertor = po2prop.po2prop() outputprop = convertor.convertstore(inputpo) return outputprop
def test_upload_new_archive(admin_client): """Tests that we can upload a new archive of files into a project.""" import zipfile po_content_1 = '#: test.c\nmsgid "test"\nmsgstr "rest"\n' po_content_2 = '#: frog.c\nmsgid "tadpole"\nmsgstr "fish"\n' archivefile = wStringIO.StringIO() archivefile.name = "fish.zip" archive = zipfile.ZipFile(archivefile, "w", zipfile.ZIP_DEFLATED) archive.writestr("test_archive_1.po", po_content_1) archive.writestr("test_archive_2.po", po_content_2) archive.close() archivefile.seek(0) post_dict = { 'file': archivefile, 'overwrite': 'merge', 'do_upload': 'upload', } response = admin_client.post("/ar/tutorial/", post_dict) assert 'href="/ar/tutorial/test_archive_1.po' in response assert 'href="/ar/tutorial/test_archive_2.po' in response store = Store.objects.get(pootle_path="/ar/tutorial/test_archive_1.po") assert os.path.isfile(store.file.path) assert store.file.read() == po_content_1 store = Store.objects.get(pootle_path="/ar/tutorial/test_archive_2.po") assert os.path.isfile(store.file.path) assert store.file.read() == po_content_2