John Vandenberg has submitted this change and it was merged.
Change subject: Page: ValueError with site as source and empty title ......................................................................
Page: ValueError with site as source and empty title
Created BasePage() class in order to allow different behaviour in subclasses Page and WikibasePage.
Page() will raise ValueError if no or empty string is provided as title when Site is used as source, as api.php cannot handle an empty string as title.
WikibasePage() will be able to be instantiated with minimal constructor values.
Also fixed pep257 errors in: - tests/page_tests.py - tests/wikibase_tests.py
Bug 60381
Change-Id: Iba0d3d7a53a434301fbea4261a0efda24ffd8af1 --- M pywikibot/page.py M tests/page_tests.py 2 files changed, 61 insertions(+), 15 deletions(-)
Approvals: John Vandenberg: Verified; Looks good to me, approved
diff --git a/pywikibot/page.py b/pywikibot/page.py index 2c3db36..5a4994c 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -63,17 +63,18 @@ # Note: Link objects (defined later on) represent a wiki-page's title, while # Page objects (defined here) represent the page itself, including its contents.
-class Page(pywikibot.UnicodeMixin, ComparableMixin): +class BasePage(pywikibot.UnicodeMixin, ComparableMixin):
- """Page: A MediaWiki page. + """BasePage: Base object for a MediaWiki page.
This object only implements internally methods that do not require reading from or writing to the wiki. All other methods are delegated to the Site object.
+ Will be subclassed by Page and WikibasePage. + """
- @deprecated_args(insite=None, defaultNamespace="ns") def __init__(self, source, title=u"", ns=0): """Instantiate a Page object.
@@ -105,6 +106,9 @@ @type ns: int
""" + if title is None: + raise ValueError(u'Title cannot be None.') + if isinstance(source, pywikibot.site.BaseSite): self._link = Link(title, source=source, defaultNamespace=ns) self._revisions = {} @@ -1815,6 +1819,21 @@ pywikibot.warning(u"Page.replaceImage() is no longer supported.")
+class Page(BasePage): + + """Page: A MediaWiki page.""" + + @deprecate_arg("insite", None) + @deprecate_arg("defaultNamespace", "ns") + def __init__(self, source, title=u"", ns=0): + """Instantiate a Page object.""" + if isinstance(source, pywikibot.site.BaseSite): + if not title: + raise ValueError(u'Title must be specified and not empty ' + 'if source is a Site.') + super(Page, self).__init__(source, title, ns) + + class FilePage(Page):
"""A subclass of Page representing a file description page. @@ -1825,7 +1844,7 @@ @deprecate_arg("insite", None) def __init__(self, source, title=u""): """Constructor.""" - Page.__init__(self, source, title, 6) + super(FilePage, self).__init__(source, title, 6) if self.namespace() != 6: raise ValueError(u"'%s' is not in the file namespace!" % title)
@@ -2606,7 +2625,7 @@ )
-class WikibasePage(Page): +class WikibasePage(BasePage):
""" The base page for the Wikibase extension. @@ -2679,7 +2698,7 @@ self._namespace = entity_type_ns kwargs['ns'] = self._namespace.id
- Page.__init__(self, site, title, **kwargs) + super(WikibasePage, self).__init__(site, title, **kwargs)
# If a title was not provided, # avoid checks which may cause an exception. @@ -3066,7 +3085,7 @@ -1 or None for an empty item. @type title: str """ - # Special case for empty item + # Special case for empty item. if title is None or title == '-1': super(ItemPage, self).__init__(site, u'-1', ns=site.item_namespace) self.id = u'-1' diff --git a/tests/page_tests.py b/tests/page_tests.py index c5213f5..ab229ea 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -96,7 +96,7 @@ ]
def testNamespaces(self): - """Test that Link() normalizes namespace names""" + """Test that Link() normalizes namespace names.""" for num in self.namespaces: for prefix in self.namespaces[num]: l = pywikibot.page.Link(prefix + list(self.titles.keys())[0], @@ -108,7 +108,7 @@ self.assertEqual(m.namespace, num)
def testTitles(self): - """Test that Link() normalizes titles""" + """Test that Link() normalizes titles.""" for title in self.titles: for num in (0, 1): l = pywikibot.page.Link(self.namespaces[num][0] + title, @@ -255,12 +255,12 @@ cached = True
def testSite(self): - """Test site() method""" + """Test site() method.""" mainpage = self.get_mainpage() self.assertEqual(mainpage.site, self.site)
def testNamespace(self): - """Test namespace() method""" + """Test namespace() method.""" mainpage = self.get_mainpage() maintalk = mainpage.toggleTalkPage()
@@ -270,6 +270,35 @@
badpage = self.get_missing_article() self.assertEqual(badpage.namespace(), 0) + + def testBasePageConstructor(self): + """Test BasePage constructor.""" + site = self.get_site() + + # Should not raise an error as the constructor only requires + # the site parameter, with the title parameter defaulted to + # empty string + page = pywikibot.page.BasePage(site) + self.assertEqual(page.title(), u'') + page = pywikibot.page.BasePage(site, title=u'') + self.assertEqual(page.title(), u'') + self.assertRaises(ValueError, pywikibot.page.BasePage, site, title=None) + + def testPageConstructor(self): + """Test Page constructor.""" + site = self.get_site() + mainpage = self.get_mainpage() + + # Test that Page() needs a title when Site is used as source. + self.assertRaises(ValueError, pywikibot.Page, site) + self.assertRaises(ValueError, pywikibot.Page, site, '') + + # Test Page as source. + p1 = pywikibot.Page(mainpage) + self.assertEqual(p1, mainpage) + + # Test not valid source. + self.assertRaises(pywikibot.Error, pywikibot.Page, 'dummy')
def testTitle(self): """Test title() method options in article namespace.""" @@ -378,9 +407,7 @@ self.assertIsInstance(mainpage.purge(), bool)
def testIsDisambig(self): - """ - Test the integration with Extension:Disambiguator. - """ + """Test the integration with Extension:Disambiguator.""" site = self.get_site() if not site.has_extension('Disambiguator'): raise unittest.SkipTest('Disambiguator extension not loaded on test site') @@ -528,7 +555,7 @@ cached = True
def test_isEmptyCategory(self): - """Test if category is empty or not""" + """Test if category is empty or not.""" site = self.get_site() cat_empty = pywikibot.Category(site, u'Category:foooooo') cat_not_empty = pywikibot.Category(site, u'Category:Wikipedia categories')
pywikibot-commits@lists.wikimedia.org