Blame translation-canary/translation_canary/translatable/__init__.py

Packit Service af52df
# Framework for testing translatable strings
Packit Service af52df
#
Packit Service af52df
# Copyright (C) 2015  Red Hat, Inc.
Packit Service af52df
#
Packit Service af52df
# This copyrighted material is made available to anyone wishing to use,
Packit Service af52df
# modify, copy, or redistribute it subject to the terms and conditions of
Packit Service af52df
# the GNU Lesser General Public License v.2, or (at your option) any later
Packit Service af52df
# version. This program is distributed in the hope that it will be useful,
Packit Service af52df
# but WITHOUT ANY WARRANTY expressed or implied, including the implied
Packit Service af52df
# warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
Packit Service af52df
# the GNU Lesser General Public License for more details.  You should have
Packit Service af52df
# received a copy of the GNU Lesser General Public License along with this
Packit Service af52df
# program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Packit Service af52df
# Street, Fifth Floor, Boston, MA 02110-1301, USA.  Any Red Hat trademarks
Packit Service af52df
# that are incorporated in the source code or documentation are not subject
Packit Service af52df
# to the GNU Lesser General Public License and may only be used or
Packit Service af52df
# replicated with the express permission of Red Hat, Inc.
Packit Service af52df
#
Packit Service af52df
# Red Hat Author(s): David Shea <dshea@redhat.com>
Packit Service af52df
Packit Service af52df
"""
Packit Service af52df
Framework for running tests against translatable strings.
Packit Service af52df
Packit Service af52df
Tests are loaded from modules in this directory. A test is any callable object
Packit Service af52df
within the module with a name that starts with 'test_'.
Packit Service af52df
Packit Service af52df
Each test is called with a POEntry object as an argument. A test passes if it
Packit Service af52df
returns without raising an exception.
Packit Service af52df
"""
Packit Service af52df
Packit Service af52df
try:
Packit Service af52df
    import polib
Packit Service af52df
except ImportError:
Packit Service af52df
    print("You need to install the python-polib package to read translations")
Packit Service af52df
    raise
Packit Service af52df
Packit Service af52df
# Gather tests from this directory
Packit Service af52df
import pkgutil
Packit Service af52df
_tests = []
Packit Service af52df
for finder, mod_name, _ispkg in pkgutil.iter_modules(__path__):
Packit Service af52df
    # Skip __main__
Packit Service af52df
    if mod_name == "__main__":
Packit Service af52df
        continue
Packit Service af52df
Packit Service af52df
    # Load the module
Packit Service af52df
    module = finder.find_module(mod_name).load_module()
Packit Service af52df
Packit Service af52df
    # Look for attributes that start with 'test_' and add them to the test list
Packit Service af52df
    for attrname, attr in module.__dict__.items():
Packit Service af52df
        if attrname.startswith('test_') and callable(attr):
Packit Service af52df
            _tests.append(attr)
Packit Service af52df
Packit Service af52df
def testString(poentry):
Packit Service af52df
    """Run all tests against the given translatable string.
Packit Service af52df
Packit Service af52df
       :param polib.POEntry poentry: The PO file entry to test
Packit Service af52df
       :returns: whether the tests succeeded or not
Packit Service af52df
       :rtype: bool
Packit Service af52df
    """
Packit Service af52df
    success = True
Packit Service af52df
    for test in _tests:
Packit Service af52df
        try:
Packit Service af52df
            test(poentry)
Packit Service af52df
        except Exception as e: # pylint: disable=broad-except
Packit Service af52df
            success = False
Packit Service af52df
            print("%s failed on %s: %s" % (test.__name__, poentry.msgid, str(e)))
Packit Service af52df
Packit Service af52df
    return success
Packit Service af52df
Packit Service af52df
def testPOT(potfile):
Packit Service af52df
    """Run all tests against all entries in a POT file.
Packit Service af52df
Packit Service af52df
       :param str potfile: The name of a .pot file to test
Packit Service af52df
       :return: whether the checks succeeded or not
Packit Service af52df
       :rtype: bool
Packit Service af52df
    """
Packit Service af52df
    success = True
Packit Service af52df
Packit Service af52df
    parsed_pot = polib.pofile(potfile)
Packit Service af52df
Packit Service af52df
    for entry in parsed_pot:
Packit Service af52df
        if not testString(entry):
Packit Service af52df
            success = False
Packit Service af52df
Packit Service af52df
    return success