jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/584919 )
Change subject: [IMPR] Create a Site with AutoFamily if a family isn't predefined ......................................................................
[IMPR] Create a Site with AutoFamily if a family isn't predefined
This enables Site.interwiki() to create a Site object even the corresponding family file isn't predefined. Now the known_families dict and get_known_families method in Family object can be replaced as suggested because the Site object is created instead of raising SiteDefinitionError.
- If a family is not predefined in family folder use AutoFamily to create the family within _code_fam_from_url. - A new parameter "name" is used for the family name. Create the family name from url if name isn't given. - Store prefix in IWEntry that it can be reused for AutoFamily name.
Bug: T249087 Change-Id: Ic7fb876570a15a5808b5fd56cafa5c3335013955 --- M pywikibot/__init__.py M pywikibot/site/_interwikimap.py 2 files changed, 20 insertions(+), 14 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index b2dc8ff..5b60012 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -16,6 +16,7 @@ from decimal import Decimal from queue import Queue from typing import Optional, Union +from urllib.parse import urlparse from warnings import warn
from pywikibot.__metadata__ import ( @@ -23,7 +24,7 @@ __maintainer__, __maintainer_email__, __name__, __url__, __version__)
from pywikibot._wbtypes import WbRepresentation as _WbRepresentation -from pywikibot.backports import cache +from pywikibot.backports import cache, removesuffix from pywikibot.bot import ( input, input_choice, input_yn, handle_args, show_help, ui, calledModuleName, Bot, CurrentPageBot, WikidataBot, @@ -48,7 +49,7 @@ UnknownExtension, UnknownFamily, UnknownSite, UnsupportedPage, WikiBaseError, ) -from pywikibot.family import Family +from pywikibot.family import AutoFamily, Family from pywikibot.i18n import translate from pywikibot.logging import ( critical, debug, error, exception, log, output, stdout, warning @@ -1050,12 +1051,12 @@
@cache -def _code_fam_from_url(url: str): +def _code_fam_from_url(url: str, name: Optional[str] = None): """Set url to cache and get code and family from cache.
Site helper method. @param url: The site URL to get code and family - @raises pywikibot.exceptions.SiteDefinitionError: Unknown URL + @param name: A family name used by AutoFamily """ matched_sites = [] # Iterate through all families and look, which does apply to @@ -1067,9 +1068,12 @@ matched_sites.append((code, family))
if not matched_sites: - # TODO: As soon as AutoFamily is ready, try and use an - # AutoFamily - raise SiteDefinitionError("Unknown URL '{}'.".format(url)) + if not name: # create a name from url + name = urlparse(url).netloc.split('.')[-2] + name = removesuffix(name, 'wiki') + family = AutoFamily(name, url) + matched_sites.append((family.code, family)) + if len(matched_sites) > 1: warning('Found multiple matches for URL "{}": {} (use first)' .format(url, ', '.join(str(s) for s in matched_sites))) @@ -1099,17 +1103,16 @@ URL. Still requires that the family supporting that URL exists. @raises ValueError: URL and pair of code and family given @raises ValueError: Invalid interface name - @raises pywikibot.exceptions.SiteDefinitionError: Unknown URL """ _logger = 'wiki'
if url: - # Either code and fam or only url - if code or fam: + # Either code and fam or url with optional fam for AutoFamily name + if code: raise ValueError( 'URL to the wiki OR a pair of code and family name ' 'should be provided') - code, fam = _code_fam_from_url(url) + code, fam = _code_fam_from_url(url, fam) elif code and ':' in code: if fam: raise ValueError( diff --git a/pywikibot/site/_interwikimap.py b/pywikibot/site/_interwikimap.py index 2f18e54..ea894b0 100644 --- a/pywikibot/site/_interwikimap.py +++ b/pywikibot/site/_interwikimap.py @@ -12,7 +12,7 @@
"""An entry of the _InterwikiMap with a lazy loading site."""
- def __init__(self, local, url): + def __init__(self, local, url, prefix=None): self._site = None self.local = local self.url = url @@ -21,7 +21,8 @@ def site(self): if self._site is None: try: - self._site = pywikibot.Site(url=self.url) + self._site = pywikibot.Site( + url=self.url, fam=None if self.local else self.prefix) except Exception as e: self._site = e return self._site @@ -52,7 +53,9 @@ # _iw_sites is a local cache to return an 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']) + self._map = {iw['prefix']: _IWEntry('local' in iw, + iw['url'], + iw['prefix']) for iw in self._site.siteinfo['interwikimap']} return self._map