jenkins-bot has submitted this change and it was merged. Change subject: L10N test for valid templates ......................................................................
L10N test for valid templates - test for redirect-broken-redirect-template - test for archivebot-archiveheader New parameter fallback for i18n.twtranslate() to disable language code fallback for test purposes and L10N. New method i18n.twget_keys to retrieve all language codes of a package. Add l10n_tests to flake8-docstrings-mandatory Change-Id: I891a4862e16d2d51eec0cca03367e74feedf4d2b --- M pywikibot/i18n.py A tests/l10n_tests.py M tox.ini 3 files changed, 107 insertions(+), 12 deletions(-) Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 50cf790..ac10b82 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -347,16 +347,21 @@ return trans -def twtranslate(code, twtitle, parameters=None): +def twtranslate(code, twtitle, parameters=None, fallback=True): """ Translate a message. The translations are retrieved from i18n.<package>, based on the callers import table. + fallback parameter must be True for i18n and False for L10N or testing + purposes. + @param code: The language code @param twtitle: The TranslateWiki string title, in <package>-<key> format @param parameters: For passing parameters. + @param fallback: Try an alternate language code + @type fallback: boolean """ package = twtitle.split("-")[0] transdict = getattr(__import__(messages_package_name, fromlist=[package]), @@ -382,17 +387,19 @@ trans = transdict[lang][twtitle] except KeyError: # try alternative languages and English - for alt in _altlang(lang) + ['en']: - try: - trans = transdict[alt][twtitle] - if code_needed: - lang = alt - break - except KeyError: - continue - if trans is None: - raise TranslationError("No English translation has been defined " - "for TranslateWiki key %r" % twtitle) + if fallback: + for alt in _altlang(lang) + ['en']: + try: + trans = transdict[alt][twtitle] + if code_needed: + lang = alt + break + except KeyError: + continue + if trans is None: + raise TranslationError( + "No English translation has been defined " + "for TranslateWiki key %r" % twtitle) # send the language code back via the given list if code_needed: code.append(lang) @@ -499,6 +506,18 @@ return code in transdict and twtitle in transdict[code] +def twget_keys(twtitle): + """ + Return all language codes for a special message. + + @param twtitle: The TranslateWiki string title, in <package>-<key> format + """ + package = twtitle.split("-")[0] + transdict = getattr(__import__(messages_package_name, fromlist=[package]), + package).msg + return (lang for lang in sorted(transdict.keys()) if lang != 'qqq') + + def input(twtitle, parameters=None, password=False): """ Ask the user a question, return the user's answer. diff --git a/tests/l10n_tests.py b/tests/l10n_tests.py new file mode 100644 index 0000000..e6bd7bc --- /dev/null +++ b/tests/l10n_tests.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +"""Test valid templates.""" +# +# (C) Pywikibot team, 2015 +# +# Distributed under the terms of the MIT license. +# +__version__ = '$Id$' + +import re +from tests.aspects import unittest, MetaTestCaseClass, TestCase +import pywikibot +from pywikibot import i18n + + +PACKAGES = ( + 'redirect-broken-redirect-template', # speedy deletion template + 'archivebot-archiveheader', # archive header template +) + + +class TestValidTemplateMeta(MetaTestCaseClass): + + """Test meta class.""" + + def __new__(cls, name, bases, dct): + """Create the new class.""" + # this comment is to avoid senseless flake8 warning + + def test_method(msg, code): + + def test_template(self): + """Test validity of template.""" + if msg: + # check whether the message contains a template + template = re.findall(u'.*?{{(.*?)[|}]', msg) + self.assertTrue(template) + + if template: + # check whether site is valid + site = pywikibot.Site('en', 'wikipedia') + self.assertTrue(code in site.languages()) + + # check whether template exists + title = template[0] + site = pywikibot.Site(code, 'wikipedia') + page = pywikibot.Page(site, title, ns=10) + self.assertTrue(page.exists()) + + return test_template + + # create test methods for package messages processed by unittest + for package in PACKAGES: + for lang in i18n.twget_keys(package): + template_msg = i18n.twtranslate(lang, package, fallback=False) + if template_msg is None: + continue + test_name = "test_%s_%s" % (package.replace('-', '_'), lang) + dct[test_name] = test_method(template_msg, lang) + return type.__new__(cls, name, bases, dct) + + +class TestValidTemplate(TestCase): + + """Test cases for date library processed by unittest.""" + + __metaclass__ = TestValidTemplateMeta + net = True # magic flag tells jenkins to not run the test. + + +if __name__ == '__main__': + try: + unittest.main() + except SystemExit: + pass diff --git a/tox.ini b/tox.ini index aa99a97..eb8cf2c 100644 --- a/tox.ini +++ b/tox.ini @@ -98,6 +98,7 @@ tests/exceptions_tests.py \ tests/family_tests.py \ tests/i18n/ \ + tests/l10n_tests.py \ tests/pwb/ \ tests/pwb_tests.py \ tests/script_tests.py \ -- To view, visit https://gerrit.wikimedia.org/r/137904 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I891a4862e16d2d51eec0cca03367e74feedf4d2b Gerrit-PatchSet: 9 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Xqt <i...@gno.de> Gerrit-Reviewer: John Vandenberg <jay...@gmail.com> Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com> Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl> Gerrit-Reviewer: Xqt <i...@gno.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits