jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/832730 )
Change subject: [bugfix] No longer raise NotimplementedError in Site.page_from_repository() ......................................................................
[bugfix] No longer raise NotimplementedError in Site.page_from_repository()
Also update documentation
Bug: T318033 Change-Id: I9fa8d34f2d89f561f9f01ec8ae6afe6472fae4d3 --- M pywikibot/page/_wikibase.py M pywikibot/site/_apisite.py M tests/wikibase_tests.py 3 files changed, 53 insertions(+), 12 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py index 4193764..1d68362 100644 --- a/pywikibot/page/_wikibase.py +++ b/pywikibot/page/_wikibase.py @@ -942,6 +942,8 @@ :param get_redirect: return the item content, do not follow the redirect, do not raise an exception. :raise NotImplementedError: a value in args or kwargs + :raise IsRedirectPageError: instance is a redirect page and + get_redirect is not True :return: actual data which entity holds
.. note:: dicts returned by this method are @@ -995,6 +997,10 @@ :param site: Site to find the linked page of. :type site: pywikibot.Site or database name :param force: override caching + :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` """ if force or not hasattr(self, '_content'): self.get(force=force) diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py index ef174a5..a290875 100644 --- a/pywikibot/site/_apisite.py +++ b/pywikibot/site/_apisite.py @@ -1177,21 +1177,51 @@ """ Return a Page for this site object specified by Wikibase item.
+ Usage: + + >>> site = pywikibot.Site('wikipedia:zh') + >>> page = site.page_from_repository('Q131303') + >>> page.title() + 'Hello World' + + This method is able to upcast categories: + + >>> site = pywikibot.Site('commons') + >>> page = site.page_from_repository('Q131303') + >>> page.title() + 'Category:Hello World' + >>> page + Category('Category:Hello World') + + It also works for wikibase repositories: + + >>> site = pywikibot.Site('wikidata') + >>> page = site.page_from_repository('Q5296') + >>> page.title() + 'Wikidata:Main Page' + + If no page exists for a given site, None is returned: + + >>> site = pywikibot.Site('wikidata') + >>> page = site.page_from_repository('Q131303') + >>> page is None + True + + .. versionchanged:: 7.7 + No longer raise NotimplementedError if used with a Wikibase + site. + :param item: id number of item, "Q###", :return: Page, or Category object given by Wikibase item number for this site object.
:raises pywikibot.exceptions.UnknownExtensionError: site has no Wikibase extension - :raises NotimplementedError: method not implemented for a Wikibase site """ if not self.has_data_repository: raise UnknownExtensionError( 'Wikibase is not implemented for {}.'.format(self)) - if self.is_data_repository(): - raise NotImplementedError( - 'page_from_repository method is not implemented for ' - 'Wikibase {}.'.format(self)) + repo = self.data_repository() dp = pywikibot.ItemPage(repo, item) try: diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index d0617a8..3e48fdb 100755 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -2056,11 +2056,13 @@ 'wikidata': { 'family': 'wikidata', 'code': 'wikidata', + 'title': 'Wikidata:Main Page', }, # test.wikidata is also 'wikidatatest': { 'family': 'wikidata', 'code': 'test', + 'title': r'Page [[wikidata:test:Q5296]] is a redirect page' }, }
@@ -2072,14 +2074,17 @@ item = ItemPage.fromPage(page) self.assertEqual(item.site, site)
- def test_page_from_repository_fails(self, key): - """Test that page_from_repository method fails.""" + def test_page_from_repository(self, key): + """Test that page_from_repository method works for wikibase too.""" site = self.get_site(key) - dummy_item = 'Q1' - regex = (r'^page_from_repository method is not implemented ' - r'for Wikibase .+.$') - with self.assertRaisesRegex(NotImplementedError, regex): - site.page_from_repository(dummy_item) + try: + page = site.page_from_repository('Q5296') + except IsRedirectPageError: + with self.assertRaisesRegex(IsRedirectPageError, + self.sites[key]['title']): + raise + else: + self.assertEqual(page.title(), self.sites[key]['title'])
class TestUnconnectedClient(TestCase):
pywikibot-commits@lists.wikimedia.org