jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/579737 )
Change subject: [IMPR] Move _InterwikiMap class to site/_interwikimap.py ......................................................................
[IMPR] Move _InterwikiMap class to site/_interwikimap.py
Change-Id: Ib85f22bb704b5421dfd89e7e1fac36bf731ce9f6 --- M docs/api_ref/pywikibot.site.rst M pywikibot/CONTENT.rst M pywikibot/site/__init__.py A pywikibot/site/_interwikimap.py M tests/textlib_tests.py 5 files changed, 111 insertions(+), 82 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/docs/api_ref/pywikibot.site.rst b/docs/api_ref/pywikibot.site.rst index 04e0380..6e5796e 100644 --- a/docs/api_ref/pywikibot.site.rst +++ b/docs/api_ref/pywikibot.site.rst @@ -2,3 +2,21 @@ =======================
.. automodule:: pywikibot.site + +Submodules +---------- + +pywikibot.site._decorators module +---------------------------------- + +.. automodule:: pywikibot.site._decorators + +pywikibot.site._interwikimap module +------------------------------------ + +.. automodule:: pywikibot.site._interwikimap + +pywikibot.site._siteinfo module +-------------------------------- + +.. automodule:: pywikibot.site._siteinfo diff --git a/pywikibot/CONTENT.rst b/pywikibot/CONTENT.rst index 59c15a6..f34e7fe 100644 --- a/pywikibot/CONTENT.rst +++ b/pywikibot/CONTENT.rst @@ -43,7 +43,7 @@ | i18n.py | Helper functions for both the internal translation | | | system and for TranslateWiki-based translations | +----------------------------+------------------------------------------------------+ - | interwiki_graph.py | Possible create graph with interwiki.py. | + | interwiki_graph.py | Possible create graph with interwiki.py script | +----------------------------+------------------------------------------------------+ | logentries.py | Objects representing Mediawiki log entries | +----------------------------+------------------------------------------------------+ @@ -115,6 +115,8 @@ +----------------------------+------------------------------------------------------+ | _decorators.py | Decorators used by site models. | +----------------------------+------------------------------------------------------+ + | _interwikimap.py | Objects representing interwiki map of MediaWiki site | + +----------------------------+------------------------------------------------------+ | _siteinfo.py | Objects representing site info data contents. | +----------------------------+------------------------------------------------------+
diff --git a/pywikibot/site/__init__.py b/pywikibot/site/__init__.py index dbee6f6..16f58c7 100644 --- a/pywikibot/site/__init__.py +++ b/pywikibot/site/__init__.py @@ -67,6 +67,7 @@ UnknownSite, ) from pywikibot.site._decorators import need_extension, need_right, need_version +from pywikibot.site._interwikimap import _InterwikiMap from pywikibot.site._siteinfo import Siteinfo from pywikibot.throttle import Throttle from pywikibot.tools import ( @@ -594,86 +595,6 @@ return result
-class _IWEntry: - - """An entry of the _InterwikiMap with a lazy loading site.""" - - def __init__(self, local, url): - self._site = None - self.local = local - self.url = url - - @property - def site(self): - if self._site is None: - try: - self._site = pywikibot.Site(url=self.url) - except Exception as e: - self._site = e - return self._site - - -class _InterwikiMap: - - """A representation of the interwiki map of a site.""" - - def __init__(self, site): - """ - Create an empty uninitialized interwiki map for the given site. - - @param site: Given site for which interwiki map is to be created - @type site: pywikibot.site.APISite - """ - super().__init__() - self._site = site - self._map = None - - def reset(self): - """Remove all mappings to force building a new mapping.""" - self._map = None - - @property - def _iw_sites(self): - """Fill the interwikimap cache with the basic entries.""" - # _iw_sites is a local cache to return a APISite instance depending - # on the interwiki prefix of that site - if self._map is None: - self._map = {iw['prefix']: _IWEntry('local' in iw, iw['url']) - for iw in self._site.siteinfo['interwikimap']} - return self._map - - def __getitem__(self, prefix): - """ - Return the site, locality and url for the requested prefix. - - @param prefix: Interwiki prefix - @type prefix: Dictionary key - @rtype: _IWEntry - @raises KeyError: Prefix is not a key - @raises TypeError: Site for the prefix is of wrong type - """ - if prefix not in self._iw_sites: - raise KeyError("'{0}' is not an interwiki prefix.".format(prefix)) - if isinstance(self._iw_sites[prefix].site, BaseSite): - return self._iw_sites[prefix] - elif isinstance(self._iw_sites[prefix].site, Exception): - raise self._iw_sites[prefix].site - else: - raise TypeError('_iw_sites[%s] is wrong type: %s' - % (prefix, type(self._iw_sites[prefix].site))) - - def get_by_url(self, url): - """ - Return a set of prefixes applying to the URL. - - @param url: URL for the interwiki - @type url: str - @rtype: set - """ - return {prefix for prefix, iw_entry in self._iw_sites - if iw_entry.url == url} - - class BaseSite(ComparableMixin):
"""Site methods that are independent of the communication interface.""" diff --git a/pywikibot/site/_interwikimap.py b/pywikibot/site/_interwikimap.py new file mode 100644 index 0000000..6352d26 --- /dev/null +++ b/pywikibot/site/_interwikimap.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- +"""Objects representing interwiki map of MediaWiki site.""" +# +# (C) Pywikibot team, 2015-2020 +# +# Distributed under the terms of the MIT license. +# +import pywikibot + + +class _IWEntry: + + """An entry of the _InterwikiMap with a lazy loading site.""" + + def __init__(self, local, url): + self._site = None + self.local = local + self.url = url + + @property + def site(self): + if self._site is None: + try: + self._site = pywikibot.Site(url=self.url) + except Exception as e: + self._site = e + return self._site + + +class _InterwikiMap: + + """A representation of the interwiki map of a site.""" + + def __init__(self, site): + """ + Create an empty uninitialized interwiki map for the given site. + + @param site: Given site for which interwiki map is to be created + @type site: pywikibot.site.APISite + """ + super().__init__() + self._site = site + self._map = None + + def reset(self): + """Remove all mappings to force building a new mapping.""" + self._map = None + + @property + def _iw_sites(self): + """Fill the interwikimap cache with the basic entries.""" + # _iw_sites is a local cache to return a APISite instance depending + # on the interwiki prefix of that site + if self._map is None: + self._map = {iw['prefix']: _IWEntry('local' in iw, iw['url']) + for iw in self._site.siteinfo['interwikimap']} + return self._map + + def __getitem__(self, prefix): + """ + Return the site, locality and url for the requested prefix. + + @param prefix: Interwiki prefix + @type prefix: Dictionary key + @rtype: _IWEntry + @raises KeyError: Prefix is not a key + @raises TypeError: Site for the prefix is of wrong type + """ + if prefix not in self._iw_sites: + raise KeyError("'{0}' is not an interwiki prefix.".format(prefix)) + if isinstance(self._iw_sites[prefix].site, pywikibot.site.BaseSite): + return self._iw_sites[prefix] + elif isinstance(self._iw_sites[prefix].site, Exception): + raise self._iw_sites[prefix].site + else: + raise TypeError('_iw_sites[%s] is wrong type: %s' + % (prefix, type(self._iw_sites[prefix].site))) + + def get_by_url(self, url): + """ + Return a set of prefixes applying to the URL. + + @param url: URL for the interwiki + @type url: str + @rtype: set + """ + return {prefix for prefix, iw_entry in self._iw_sites + if iw_entry.url == url} diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index 474ae16..3e2ae0a 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -16,7 +16,7 @@ import pywikibot import pywikibot.textlib as textlib
-from pywikibot.site import _IWEntry +from pywikibot.site._interwikimap import _IWEntry from pywikibot.textlib import _MultiTemplateMatchBuilder, extract_sections from pywikibot import UnknownSite