diff options
author | Shaun McCance <shaunm@redhat.com> | 2018-08-21 11:28:54 -0400 |
---|---|---|
committer | Shaun McCance <shaunm@redhat.com> | 2018-08-21 11:28:54 -0400 |
commit | 2eb0a6cd3b15b0bca5abf19ba0fcff45bd457523 (patch) | |
tree | fb4784d97f2ce5f63e09dd119029806004de689d | |
parent | 2f82385da6faa2b33a346ba75d7efaf09a7f095c (diff) | |
download | itstool-2eb0a6cd3b15b0bca5abf19ba0fcff45bd457523.tar.gz |
Fix py 2/3 issues and add a test case
-rwxr-xr-x | itstool.in | 34 | ||||
-rw-r--r-- | tests/preservespace1xml.xml | 2 | ||||
-rw-r--r-- | tests/preservespace5xml.ll.po | 19 | ||||
-rw-r--r-- | tests/preservespace5xml.ll.xml | 10 | ||||
-rw-r--r-- | tests/preservespace5xml.pot | 18 | ||||
-rw-r--r-- | tests/preservespace5xml.xml | 16 | ||||
-rw-r--r-- | tests/run_tests.py | 16 |
7 files changed, 92 insertions, 23 deletions
@@ -1,6 +1,6 @@ #!@PYTHON@ -s # -# Copyright (c) 2010-2013 Shaun McCance <shaunm@gnome.org> +# Copyright (c) 2010-2018 Shaun McCance <shaunm@gnome.org> # # ITS Tool program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by the @@ -998,7 +998,10 @@ class Document (object): def translate_attrs(self, oldnode, newnode): trans_attrs = [attr for attr in xml_attr_iter(oldnode) if self._its_translate_nodes.get(attr, 'no') == 'yes'] for attr in trans_attrs: - newcontent = translations.ugettext(attr.get_content()) + srccontent = attr.get_content() + if not PY3: + srccontent = srccontent.decode('utf-8') + newcontent = translations.ugettext(srccontent) if newcontent: if not PY3: newcontent = newcontent.encode('utf-8') @@ -1594,6 +1597,7 @@ if __name__ == '__main__': try: doc.merge_translations(translations, opts.lang, strict=opts.strict) except Exception as e: + raise sys.stderr.write('Error: Could not merge translations:\n%s\n' % ustr(e)) sys.exit(1) serialized = doc._doc.serialize('utf-8') @@ -1625,25 +1629,17 @@ if __name__ == '__main__': if opts.output is None: out = sys.stdout elif os.path.isdir(opts.output): - out = open(os.path.join(opts.output, os.path.basename(filename)), 'w') + out = open(os.path.join(opts.output, os.path.basename(filename)), 'wb') else: - out = open(opts.output, 'w') + out = open(opts.output, 'wb') messages = MessageList() doc = Document(opts.join, messages) doc.apply_its_rules(not(opts.nobuiltins), params=params) doc.join_translations(translations, strict=opts.strict) - out.write(doc._doc.serialize('utf-8')) - if False: - if opts.itsfile is not None: - for itsfile in opts.itsfile: - doc.apply_its_file(itsfile, params=params) - try: - doc.merge_translations(translations, opts.lang, strict=opts.strict) - except Exception as e: - sys.stderr.write('Error: Could not merge translations:\n%s\n' % ustr(e)) - sys.exit(1) - fout = out - if isinstance(fout, string_types): - fout = open(os.path.join(fout, os.path.basename(filename)), 'w') - fout.write(doc._doc.serialize('utf-8')) - fout.flush() + serialized = doc._doc.serialize('utf-8') + if PY3: + # For some reason, under py3, our serialized data is returns as a str. + # Let's encode it to bytes + serialized = serialized.encode('utf-8') + out.write(serialized) + out.flush() diff --git a/tests/preservespace1xml.xml b/tests/preservespace1xml.xml index 3282291..ee61aee 100644 --- a/tests/preservespace1xml.xml +++ b/tests/preservespace1xml.xml @@ -2,7 +2,7 @@ xmlns:its="http://www.w3.org/2005/11/its" > <info> <its:rules - its:version="2.0"> + version="2.0"> <its:preserveSpaceRule selector="//verse" space="preserve"/> </its:rules> </info> diff --git a/tests/preservespace5xml.ll.po b/tests/preservespace5xml.ll.po new file mode 100644 index 0000000..a1dd918 --- /dev/null +++ b/tests/preservespace5xml.ll.po @@ -0,0 +1,19 @@ +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"POT-Creation-Date: 2018-08-20 18:22-0400\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" +"Language-Team: LANGUAGE <LL@li.org>\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#. (itstool) path: book/verse@text +#: tests/preservespace5xml.xml:15 +#, no-wrap +msgid "" +" ’Twas brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe. " +msgstr "" +" ’Translated brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe. " + diff --git a/tests/preservespace5xml.ll.xml b/tests/preservespace5xml.ll.xml new file mode 100644 index 0000000..0dff9c5 --- /dev/null +++ b/tests/preservespace5xml.ll.xml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="utf-8"?> +<book xmlns:its="http://www.w3.org/2005/11/its"> + <info> + <its:rules version="2.0"> + <its:translateRule selector="//verse/@text" translate="yes"/> + <its:preserveSpaceRule selector="//verse/@text" space="preserve"/> + </its:rules> + </info> + <verse text=" ’Translated brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe. "/> +</book> diff --git a/tests/preservespace5xml.pot b/tests/preservespace5xml.pot new file mode 100644 index 0000000..bf1917e --- /dev/null +++ b/tests/preservespace5xml.pot @@ -0,0 +1,18 @@ +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"POT-Creation-Date: 2018-08-20 18:22-0400\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" +"Language-Team: LANGUAGE <LL@li.org>\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#. (itstool) path: book/verse@text +#: tests/preservespace5xml.xml:15 +#, no-wrap +msgid "" +" ’Twas brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe. " +msgstr "" + diff --git a/tests/preservespace5xml.xml b/tests/preservespace5xml.xml new file mode 100644 index 0000000..af406fa --- /dev/null +++ b/tests/preservespace5xml.xml @@ -0,0 +1,16 @@ +<book + xmlns:its="http://www.w3.org/2005/11/its" > + <info> + <its:rules + version="2.0"> + <its:translateRule selector="//verse/@text" translate="yes"/> + <its:preserveSpaceRule selector="//verse/@text" space="preserve"/> + </its:rules> + </info> + <verse text=" + ’Twas brillig, and the slithy toves + Did gyre and gimble in the wabe; + All mimsy were the borogoves, + And the mome raths outgrabe. +"/> +</book> diff --git a/tests/run_tests.py b/tests/run_tests.py index af62d76..351f9af 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -6,6 +6,10 @@ import unittest TEST_DIR = os.path.dirname(os.path.abspath(__file__)) ITSTOOL_DIR = os.path.dirname(TEST_DIR) +if sys.version_info[0] == 3: + PYTHON = 'python3' +else: + PYTHON = 'python2' class ItstoolTests(unittest.TestCase): def tearDown(self): @@ -32,7 +36,8 @@ class ItstoolTests(unittest.TestCase): def _test_pot_generation(self, start_file, reference_pot=None, expected_status=0, options=None): start_file_base = os.path.splitext(start_file)[0] - result = self.run_command("cd %(dir)s && python itstool_test %(opt)s -n -o %(out)s %(in)s" % { + result = self.run_command("cd %(dir)s && %(python)s itstool_test %(opt)s -n -o %(out)s %(in)s" % { + 'python' : PYTHON, 'dir' : ITSTOOL_DIR, 'opt' : (options or ''), 'out' : os.path.join('tests', "test.pot"), @@ -57,7 +62,8 @@ class ItstoolTests(unittest.TestCase): mo_file = '%s.mo' % lang self.run_command("cd %(dir)s && msgfmt -o %(mo_file)s %(po_file)s" % {'dir': TEST_DIR, 'mo_file': mo_file, 'po_file': po_file}) - result = self.run_command("cd %(dir)s && python itstool_test -n -o%(res)s -j %(src)s %(mo)s" % { + result = self.run_command("cd %(dir)s && %(python)s itstool_test -n -o%(res)s -j %(src)s %(mo)s" % { + 'python' : PYTHON, 'dir': ITSTOOL_DIR, 'res': os.path.join(TEST_DIR, 'test.xml'), 'src': os.path.join(TEST_DIR, start_file), @@ -80,7 +86,8 @@ class ItstoolTests(unittest.TestCase): outputs = [("%s.ll.po" % start_file_base, "%s.ll.xml" % start_file_base, 'll')] for po_file, xml_file, lang in outputs: self.run_command("cd %(dir)s && msgfmt -o test.mo %(po_file)s" % {'dir': TEST_DIR, 'po_file': po_file}) - self.run_command("cd %(dir)s && python itstool_test -n %(opt)s -l %(lc)s -m %(mo)s -o %(res)s %(src)s" % { + self.run_command("cd %(dir)s && %(python)s itstool_test -n %(opt)s -l %(lc)s -m %(mo)s -o %(res)s %(src)s" % { + 'python' : PYTHON, 'dir': ITSTOOL_DIR, 'opt': (options or ''), 'lc' : lang, @@ -136,6 +143,9 @@ class ItstoolTests(unittest.TestCase): def test_PreserveSpace4(self): self._test_pot_generation('preservespace4xml.xml') + def test_PreserveSpace5(self): + self._test_translation_process('preservespace5xml.xml') + def test_Translate1(self): self._test_translation_process('Translate/Translate1.xml') |