Debian Codegraph @

translation-canary/translation_canary/translatable/__init__.py

raw
1 # Framework for testing translatable strings
2 #
3 # Copyright (C) 2015 Red Hat, Inc.
4 #
5 # This copyrighted material is made available to anyone wishing to use,
6 # modify, copy, or redistribute it subject to the terms and conditions of
7 # the GNU Lesser General Public License v.2, or (at your option) any later
8 # version. This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY expressed or implied, including the implied
10 # warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 # the GNU Lesser General Public License for more details. You should have
12 # received a copy of the GNU Lesser General Public License along with this
13 # program; if not, write to the Free Software Foundation, Inc., 51 Franklin
14 # Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks
15 # that are incorporated in the source code or documentation are not subject
16 # to the GNU Lesser General Public License and may only be used or
17 # replicated with the express permission of Red Hat, Inc.
18 #
19 # Red Hat Author(s): David Shea <[email protected]>
20
21 """
22 Framework for running tests against translatable strings.
23
24 Tests are loaded from modules in this directory. A test is any callable object
25 within the module with a name that starts with 'test_'.
26
27 Each test is called with a POEntry object as an argument. A test passes if it
28 returns without raising an exception.
29 """
30
31 try:
32 import polib
33 except ImportError:
34 print("You need to install the python-polib package to read translations")
35 raise
36
37 # Gather tests from this directory
38 import pkgutil
39 import importlib
40
41 _tests = []
42 for finder, mod_name, _ispkg in pkgutil.iter_modules(__path__):
43 # Skip __main__
44 if mod_name == "__main__":
45 continue
46
47 # Load the module
48 full_name = "{}.{}".format(__name__, mod_name)
49 module = importlib.import_module(full_name)
50
51 # Look for attributes that start with 'test_' and add them to the test list
52 for attrname, attr in module.__dict__.items():
53 if attrname.startswith('test_') and callable(attr):
54 _tests.append(attr)
55
56 def testString(poentry):
57 """Run all tests against the given translatable string.
58
59 :param polib.POEntry poentry: The PO file entry to test
60 :returns: whether the tests succeeded or not
61 :rtype: bool
62 """
63 success = True
64 for test in _tests:
65 try:
66 test(poentry)
67 except Exception as e: # pylint: disable=broad-except
68 success = False
69 print("%s failed on %s: %s" % (test.__name__, poentry.msgid, str(e)))
70
71 return success
72
73 def testPOT(potfile):
74 """Run all tests against all entries in a POT file.
75
76 :param str potfile: The name of a .pot file to test
77 :return: whether the checks succeeded or not
78 :rtype: bool
79 """
80 success = True
81
82 parsed_pot = polib.pofile(potfile)
83
84 for entry in parsed_pot:
85 if not testString(entry):
86 success = False
87
88 return success