jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/842487 )

Change subject: [IMPR] Improvements for i18n module
......................................................................

[IMPR] Improvements for i18n module

- shorten messages_available function by using bool()
- use set comprehension for known_languages which is 10% faster
- add doctests
- add documentations

Change-Id: I4e70fee0db8c7d6816ed7994060c918d4abf5971
---
M pywikibot/i18n.py
1 file changed, 63 insertions(+), 14 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index f94a176..9a19dac 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -375,23 +375,31 @@

     To determine if messages are available, it looks for the package name
     set using :py:obj:`set_messages_package` for a message bundle called
-    'pywikibot' containing messages.
+    ``pywikibot`` containing messages.
+
+    >>> from pywikibot import i18n
+    >>> i18n.messages_available()
+    True
+    >>> old_package = i18n._messages_package_name  # save the old package name
+    >>> i18n.set_messages_package('foo')
+    >>> i18n.messages_available()
+    False
+    >>> i18n.set_messages_package(old_package)
+    >>> i18n.messages_available()
+    True
     """
     global _messages_available
     if _messages_available is not None:
         return _messages_available
+
     try:
         mod = __import__(_messages_package_name, fromlist=['__path__'])
     except ImportError:
         _messages_available = False
         return False

-    if not os.listdir(next(iter(mod.__path__))):
-        _messages_available = False
-        return False
-
-    _messages_available = True
-    return True
+    _messages_available = bool(os.listdir(next(iter(mod.__path__))))
+    return _messages_available


 def _altlang(lang: str) -> List[str]:
@@ -815,9 +823,33 @@
 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
+    The bundle name usually corresponds with the script name which is
+    localized.
+
+    With ``stem=True`` the bundle names are given:
+
+    >>> from pywikibot import i18n
+    >>> bundles = sorted(i18n.bundles(stem=True))
+    >>> len(bundles)
+    37
+    >>> bundles[:4]
+    ['add_text', 'archivebot', 'basic', 'blockpageschecker']
+    >>> bundles[-5:]
+    ['undelete', 'unprotect', 'unusedfiles', 'weblinkchecker', 'welcome']
+    >>> 'pywikibot' in bundles
+    True
+
+    With ``stem=False`` we get Path objects:
+
+    >>> path = next(i18n.bundles())
+    >>> path.is_dir()
+    True
+    >>> path.parent.as_posix()
+    'scripts/i18n'

     .. versionadded:: 7.0
+
+    :param stem: yield the Path.stem if True and the Path object otherwise
     """
     for dirpath in Path(*_messages_package_name.split('.')).iterdir():
         if dirpath.is_dir() and not dirpath.match('*__'):  # ignore cache
@@ -830,14 +862,31 @@
 def known_languages() -> List[str]:
     """All languages we have localizations for.

+    >>> from pywikibot import i18n
+    >>> i18n.known_languages()[:10]
+    ['ab', 'aeb', 'af', 'am', 'an', 'ang', 'anp', 'ar', 'arc', 'ary']
+    >>> i18n.known_languages()[-10:]
+    ['vo', 'vro', 'wa', 'war', 'xal', 'xmf', 'yi', 'yo', 'yue', 'zh']
+    >>> len(i18n.known_languages())
+    253
+
+    The implementation is roughly equivalent to:
+
+    .. code-block:: Python
+
+       langs = set()
+       for dirpath in bundles():
+           for fname in dirpath.iterdir():
+               if fname.suffix == '.json':
+                   langs.add(fname.stem)
+        return sorted(langs)
+
     .. 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)
+    return sorted(
+        {fname.stem for dirpath in bundles() for fname in dirpath.iterdir()
+         if fname.suffix == '.json'}
+    )

 
 def input(twtitle: str,

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/842487
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I4e70fee0db8c7d6816ed7994060c918d4abf5971
Gerrit-Change-Number: 842487
Gerrit-PatchSet: 6
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to