jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/341995 )
Change subject: Add -property option to pagegenerators.py ......................................................................
Add -property option to pagegenerators.py
- new -property option for pagegenerators.py - ask for a poperty name or show the whole list when "?" no input (default) is given or the given property name was invalid - test the new option with GeneratorFactory - rename _get_property_names to get_property_names which is no longer a helper function for site.pages_with_property method
Change-Id: Ib128f197f2b43d00ea2ad1846a3d1788ad936abe --- M pywikibot/pagegenerators.py M pywikibot/site.py M tests/pagegenerators_tests.py M tests/site_tests.py 4 files changed, 63 insertions(+), 6 deletions(-)
Approvals: Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index 98ed579..e344976 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -50,6 +50,7 @@ )
from pywikibot import date, config, i18n, xmlreader +from pywikibot.bot import ListOption from pywikibot.comms import http from pywikibot.exceptions import ArgumentDeprecationWarning, UnknownExtension from pywikibot.logentries import LogEntryFactory @@ -246,6 +247,9 @@ -unwatched Work on all articles that are not watched by anyone. Argument can be given as "-unwatched:n" where n is the maximum number of articles to work on. + +-property:name Work on all pages with a given propery name from + Special:PagesWithProp.
-usercontribs Work on all articles that were edited by a certain user. (Example : -usercontribs:DumZiBoT) @@ -661,6 +665,20 @@ elif arg == '-unwatched': gen = UnwatchedPagesPageGenerator(total=intNone(value), site=self.site) + elif arg == '-property': + if not value: + question = 'Which property name to be used?' + value = pywikibot.input(question + ' (List [?])') + pnames = self.site.get_property_names() + # also use the default by <enter> key + if value in '?' or value not in pnames: + for i, item in enumerate(pnames, start=1): + pywikibot.output( + '{0:{1}}: {2}'.format(i, len(str(len(pnames))), + item)) + prefix, value = pywikibot.input_choice( + question, ListOption(self.site.get_property_names())) + gen = page_with_property_generator(value, site=self.site) elif arg == '-usercontribs': gen = UserContributionsGenerator(value) elif arg == '-withoutinterwiki': @@ -2168,6 +2186,22 @@ yield page
+def page_with_property_generator(name, total=None, site=None): + """ + Special:PagesWithProperty page generator. + + @param name: Property name of pages to be retrieved + @type name: str + @param total: Maximum number of pages to retrieve in total + @type total: int + @param site: Site for generator results. + @type site: L{pywikibot.site.BaseSite} + """ + if site is None: + site = pywikibot.Site() + return site.pages_with_property(name, total=total) + + def WantedPagesPageGenerator(total=100, site=None): """ Wanted page generator. diff --git a/pywikibot/site.py b/pywikibot/site.py index 0ee4981..28643e1 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -6584,7 +6584,7 @@ protect_type=type, total=total)
@need_version('1.21') - def _get_property_names(self, force=False): + def get_property_names(self, force=False): """ Get property names for pages_with_property().
@@ -6607,9 +6607,9 @@ @return: return a generator of Page objects @rtype: iterator """ - if propname not in self._get_property_names(): + if propname not in self.get_property_names(): raise NotImplementedError( - '{0} is not a valid page property'.format(propname)) + '"{0}" is not a valid page property'.format(propname)) pwpgen = self._generator(api.PageGenerator, type_arg='pageswithprop', gpwppropname=propname, diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py index 389c6ae..50628bd 100755 --- a/tests/pagegenerators_tests.py +++ b/tests/pagegenerators_tests.py @@ -1021,6 +1021,29 @@ self.assertIsNotNone(gen) self.assertPagesInNamespaces(gen, set([1, 3]))
+ def test_pages_with_property_generator(self): + """Test the pages_with_property_generator method.""" + mysite = self.get_site() + for item in ('defaultsort', 'disambiguation', 'displaytitle', + 'hiddencat', 'invalid_property'): + if item in mysite.get_property_names(): + gf = pagegenerators.GeneratorFactory() + gf.handleArg('-property:{0}'.format(item)) + gf.handleArg('-limit:10') + gen = gf.getCombinedGenerator() + self.assertIsNotNone(gen) + pages = list(gen) + self.assertLessEqual(len(pages), 10) + for page in pages: + self.assertIsInstance(page, pywikibot.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)) + def test_empty_generator(self): """Test empty generator.""" gf = pagegenerators.GeneratorFactory(site=self.site) diff --git a/tests/site_tests.py b/tests/site_tests.py index 1b2030e..5b9c4eb 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -1000,7 +1000,7 @@ if MediaWikiVersion(self.site.version()) < MediaWikiVersion('1.21'): raise unittest.SkipTest('requires v1.21+') mysite = self.get_site() - pnames = mysite._get_property_names() + pnames = mysite.get_property_names() for item in ('defaultsort', 'disambiguation', 'displaytitle', 'hiddencat', 'invalid_property'): if item in pnames: @@ -3328,9 +3328,9 @@ cached = True
def test_get_property_names(self, key): - """Test _get_property_names method.""" + """Test get_property_names method.""" mysite = self.get_site(key) - pnames = mysite._get_property_names() + pnames = mysite.get_property_names() self.assertIsInstance(pnames, list) for item in ('defaultsort', 'disambiguation', 'displaytitle', 'forcetoc', 'graph_specs', 'hiddencat', 'newsectionlink',
pywikibot-commits@lists.wikimedia.org