jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/296224 )
Change subject: [IMPR] Add a new site method pages_with_property ......................................................................
[IMPR] Add a new site method pages_with_property
- pages_with_property is a new iterator method which retrieves pages like Special:PagesWithProp do - _get_property_names() gives a list of valid properties for different sites - tests added
Change-Id: I7c39f164d6f5b6fa6ed132e04fbb545c934801cf --- M pywikibot/site.py M tests/site_tests.py 2 files changed, 84 insertions(+), 0 deletions(-)
Approvals: Dalba: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index efac45f..0ee4981 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -6583,6 +6583,39 @@ return self.allpages(namespace=namespaces[0], protect_level=level, protect_type=type, total=total)
+ @need_version('1.21') + def _get_property_names(self, force=False): + """ + Get property names for pages_with_property(). + + @param force: force to retrieve userinfo ignoring cache + @type force: bool + """ + if force or not hasattr(self, '_property_names'): + ppngen = self._generator(api.ListGenerator, 'pagepropnames') + self._property_names = [pn['propname'] for pn in ppngen] + return self._property_names + + @need_version('1.21') + def pages_with_property(self, propname, total=None): + """Yield Page objects from Special:PagesWithProp. + + @param propname: must be a valid property. + @type propname: str + @param total: number of pages to return + @type total: int or None + @return: return a generator of Page objects + @rtype: iterator + """ + if propname not in self._get_property_names(): + raise NotImplementedError( + '{0} is not a valid page property'.format(propname)) + pwpgen = self._generator(api.PageGenerator, + type_arg='pageswithprop', + gpwppropname=propname, + total=total) + return pwpgen + @need_version("1.18") def compare(self, old, diff): """ diff --git a/tests/site_tests.py b/tests/site_tests.py index c4f1675..1b2030e 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -995,6 +995,26 @@ cnt += 1 self.assertLessEqual(cnt, 5)
+ def test_pages_with_property(self): + """Test pages_with_property method.""" + if MediaWikiVersion(self.site.version()) < MediaWikiVersion('1.21'): + raise unittest.SkipTest('requires v1.21+') + mysite = self.get_site() + pnames = mysite._get_property_names() + for item in ('defaultsort', 'disambiguation', 'displaytitle', + 'hiddencat', 'invalid_property'): + if item in pnames: + for page in mysite.pages_with_property(item, total=5): + self.assertIsInstance(page, pywikibot.Page) + self.assertTrue(mysite.page_exists(page)) + if item == 'disambiguation': + self.assertTrue(page.isDisambig) + else: + with self.assertRaises(NotImplementedError): + mysite.pages_with_property(item) + self.fail( + 'NotImplementedError not raised for {0}'.format(item)) +
class TestImageUsage(DefaultSiteTestCase):
@@ -3290,6 +3310,37 @@ self.assertRaises(pywikibot.UnknownExtension, lambda x: x.proofread_levels, site)
+class TestPropertyNames(DefaultSiteTestCase): + + """Test Special:PagesWithProp method.""" + + sites = { + 'en.ws': { + 'family': 'wikisource', + 'code': 'en', + }, + 'de.wp': { + 'family': 'wikipedia', + 'code': 'de', + }, + } + + cached = True + + def test_get_property_names(self, key): + """Test _get_property_names method.""" + mysite = self.get_site(key) + pnames = mysite._get_property_names() + self.assertIsInstance(pnames, list) + for item in ('defaultsort', 'disambiguation', 'displaytitle', + 'forcetoc', 'graph_specs', 'hiddencat', 'newsectionlink', + 'noeditsection', 'noexternallanglinks', 'nogallery', + 'noindex', 'nonewsectionlink', 'notoc', + 'page_top_level_section_count', 'score', 'templatedata', + 'wikibase-badge-Q17437796', 'wikibase_item'): + self.assertIn(item, pnames) + + if __name__ == '__main__': # pragma: no cover try: unittest.main()
pywikibot-commits@lists.wikimedia.org