jenkins-bot submitted this change.

View Change


Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] Add NoSiteLinkError for missing sitelinks

- Raise NoSiteLinkError instead of NoPageError when sitelink
is missing
- NoSiteLinkError derives from NoPageError for back-compat

Bug: T332341
Change-Id: Ied7796fdca1216064726d9049901ccb1f7b646cf
---
M pywikibot/exceptions.py
M pywikibot/page/_wikibase.py
2 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py
index 36da84e..9cd518a 100644
--- a/pywikibot/exceptions.py
+++ b/pywikibot/exceptions.py
@@ -26,6 +26,7 @@
| +-- PageLoadRelatedError
| | +-- InconsistentTitleError
| | +-- InvalidPageError
+ | | +-- NoSiteLinkError
| +-- PageSaveRelatedError
| | +-- EditConflictError
| | | +-- ArticleExistsConflictError
@@ -106,6 +107,7 @@

PageLoadRelatedError: any exception which happens while loading a Page.
- InconsistentTitleError: Page receives a title inconsistent with query
+ - NoSiteLinkError: ItemPage has no sitelink to given language

PageSaveRelatedError: page exceptions within the save operation on a Page

@@ -396,6 +398,21 @@
super().__init__(page)


+class NoSiteLinkError(PageLoadRelatedError, NoPageError):
+
+ """ItemPage has no sitelink to the given language."""
+
+ def __init__(self, page: 'pywikibot.page.ItemPage', lang: str) -> None:
+ """Initializer.
+
+ :param page: ItemPage that caused the exception
+ :param lang: language code of the queried sitelink
+
+ """
+ self.message = f'Item {{}} has no sitelink to language {lang!r}'
+ super().__init__(page)
+
+
class SiteDefinitionError(Error):

"""Site does not exist."""
diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index 3ce4424..d904039 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -31,6 +31,7 @@
IsNotRedirectPageError,
IsRedirectPageError,
NoPageError,
+ NoSiteLinkError,
NoWikibaseEntityError,
WikiBaseError,
)
@@ -1078,7 +1079,7 @@
"""
Return the title for the specific site.

- If the item doesn't have that language, raise NoPageError.
+ If the item doesn't have that language, raise NoSiteLinkError.

:param site: Site to find the linked page of.
:type site: pywikibot.Site or database name
@@ -1086,13 +1087,13 @@
:param get_redirect: return the item content, do not follow the
redirect, do not raise an exception.
:raise IsRedirectPageError: instance is a redirect page
- :raise NoPageError: site is not in :attr:`sitelinks`
+ :raise NoSiteLinkError: site is not in :attr:`sitelinks`
"""
if force or not hasattr(self, '_content'):
self.get(force=force)

if site not in self.sitelinks:
- raise NoPageError(self)
+ raise NoSiteLinkError(self, site.lang)

return self.sitelinks[site].canonical_title()


To view, visit change 904911. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ied7796fdca1216064726d9049901ccb1f7b646cf
Gerrit-Change-Number: 904911
Gerrit-PatchSet: 3
Gerrit-Owner: Ammarpad <ammarpad@yahoo.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged