jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/750712 )
Change subject: [IMPR] Improve i18n module ......................................................................
[IMPR] Improve i18n module
- add i18n.bundles() function to get all bundle names - add i18n.known_languages() functions to get all languages with translations - add i18n._get_bundle function to get the dict from json text for a language bundle - add Generator to backports.py - add bundle tests
Change-Id: I9cb13bb215fadcb340b263748c3c21c115a95e9c --- M pywikibot/i18n.py M tests/l10n_tests.py 2 files changed, 67 insertions(+), 9 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 2dffea9..6dcdda5 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -26,6 +26,7 @@
from collections import abc, defaultdict from contextlib import suppress +from pathlib import Path from textwrap import fill from typing import Optional, Union
@@ -35,6 +36,7 @@ from pywikibot.backports import ( cache, Dict, + Generator, Iterable, Iterator, List, @@ -412,6 +414,24 @@
@cache +def _get_bundle(lang: str, dirname: str) -> Dict[str, str]: + """Return json data of certain bundle if exists. + + For internal use, don't use it directly. + + .. versionadded:: 7.0 + """ + filename = '{}/{}.json'.format(dirname, lang) + try: + data = pkgutil.get_data(_messages_package_name, filename) + assert data is not None + trans_text = data.decode('utf-8') + except OSError: # file open can cause several exceptions + return {} + + return json.loads(trans_text) + + def _get_translation(lang: str, twtitle: str) -> Optional[str]: """ Return message of certain twtitle if exists. @@ -419,15 +439,7 @@ For internal use, don't use it directly. """ message_bundle = twtitle.split('-')[0] - filename = '{}/{}.json'.format(message_bundle, lang) - try: - data = pkgutil.get_data(_messages_package_name, filename) - assert data is not None - trans_text = data.decode('utf-8') - except OSError: # file open can cause several exceptions - return None - - transdict = json.loads(trans_text) + transdict = _get_bundle(lang, message_bundle) return transdict.get(twtitle)
@@ -801,6 +813,34 @@ if lang != 'qqq' and _get_translation(lang, twtitle)]
+def bundles(stem: bool = False) -> Generator[Union[Path, str], None, None]: + """A generator which yields message bundle names or its path objects. + + :param stem: yield the Path.stem if True and the Path object otherwise + + .. versionadded:: 7.0 + """ + for dirpath in Path(*_messages_package_name.split('.')).iterdir(): + if dirpath.is_dir() and not dirpath.match('*__'): # ignore cache + if stem: + yield dirpath.stem + else: + yield dirpath + + +def known_languages() -> List[str]: + """All languages we have localizations for. + + .. versionadded:: 7.0 + """ + langs = set() + for dirpath in bundles(): + for fname in dirpath.iterdir(): + if fname.suffix == '.json': + langs.add(fname.stem) + return sorted(langs) + + def input(twtitle: str, parameters: Optional[Mapping[str, int]] = None, password: bool = False, diff --git a/tests/l10n_tests.py b/tests/l10n_tests.py index 04388de..61db833 100644 --- a/tests/l10n_tests.py +++ b/tests/l10n_tests.py @@ -114,6 +114,24 @@ self.assertIsNotEmpty(keys) self.assertIn('en', keys)
+ def test_package_bundles(self): + """Test whether package bundles has valid entries.""" + langs = i18n.known_languages() + self.assertIsInstance(langs, list) + self.assertIsNotEmpty(langs) + for dirname in i18n.bundles(stem=True): + for lang in langs: + with self.subTest(bundle=dirname, lang=lang): + bundle = i18n._get_bundle(lang, dirname) + if lang in ('en', 'qqq'): + self.assertIsNotEmpty(bundle) + for key in bundle.keys(): + if key == '@metadata': + continue + self.assertTrue(key.startswith(dirname), + '{!r} does not start with {!r}' + .format(key, dirname)) +
if __name__ == '__main__': # pragma: no cover with suppress(SystemExit):
pywikibot-commits@lists.wikimedia.org