jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/640259 )
Change subject: [IMPR] reduce code complexity of pagegenerators.py ......................................................................
[IMPR] reduce code complexity of pagegenerators.py
Change-Id: Ibc6f9eb7172ba287e150733727a26c0a59ed63e9 --- M pywikibot/pagegenerators.py M tests/pagegenerators_tests.py 2 files changed, 46 insertions(+), 19 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index 22ca97b..ebb4a7f 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -419,6 +419,11 @@ filter_unique, key=lambda page: '{}:{}:{}'.format(*page._cmpkey()))
+def _output_if(predicate, msg): + if predicate: + pywikibot.output(msg) + + class GeneratorFactory:
"""Process command line arguments and return appropriate page generator. @@ -1878,10 +1883,6 @@ @type show_filtered: bool
""" - def output_if(predicate, msg): - if predicate: - pywikibot.output(msg) - def to_be_yielded(edit, page, show_filtered): if not edit.do_edit: return True @@ -1898,11 +1899,11 @@ time=edit_time.isoformat())
if edit_time < edit.edit_start: - output_if(show_filtered, msg.format(when='old')) + _output_if(show_filtered, msg.format(when='old')) return False
if edit_time > edit.edit_end: - output_if(show_filtered, msg.format(when='recent')) + _output_if(show_filtered, msg.format(when='recent')) return False
return True @@ -2162,24 +2163,19 @@ @return: Wrapped generator @rtype: generator """ + why = "doesn't" if has_item else 'has' + msg = '{{page}} {why} a wikidata item. Skipping.'.format(why=why) + for page in generator or []: try: page_item = pywikibot.ItemPage.fromPage(page, lazy_load=False) except pywikibot.NoPage: page_item = None
- if page_item: - if not has_item: - if show_filtered: - pywikibot.output( - '%s has a wikidata item. Skipping.' % page) - continue - else: - if has_item: - if show_filtered: - pywikibot.output( - "%s doesn't have a wikidata item. Skipping." % page) - continue + to_be_skipped = bool(page_item) != has_item + if to_be_skipped: + _output_if(show_filtered, msg.format(page=page)) + continue
yield page
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py index 3524234..357d8bb 100755 --- a/tests/pagegenerators_tests.py +++ b/tests/pagegenerators_tests.py @@ -21,7 +21,8 @@ from pywikibot.pagegenerators import ( PagesFromTitlesGenerator, PreloadingGenerator, - CategorizedPageGenerator + CategorizedPageGenerator, + WikibaseItemFilterPageGenerator, )
from pywikibot.tools import has_module, suppress_warnings @@ -633,6 +634,36 @@ pywikibot.ItemPage) for item in gen))
+class WikibaseItemFilterPageGeneratorTestCase(TestCase): + + """Test WikibaseItemFilterPageGenerator.""" + + family = 'wikipedia' + code = 'en' + + def test_filter_pages_with_item(self): + """Test WikibaseItemFilterPageGenerator on pages with item.""" + gf = pagegenerators.GeneratorFactory(site=self.site) + gf.handleArg('-page:Main_Page') + gen = gf.getCombinedGenerator() + pages = list(gen) + gen = WikibaseItemFilterPageGenerator(pages, has_item=True) + self.assertLength(list(gen), 1) + gen = WikibaseItemFilterPageGenerator(pages, has_item=False) + self.assertLength(list(gen), 0) + + def test_filter_pages_without_item(self): + """Test WikibaseItemFilterPageGenerator on pages without item.""" + gf = pagegenerators.GeneratorFactory(site=self.site) + gf.handleArg('-page:Talk:Main_Page') + gen = gf.getCombinedGenerator() + pages = list(gen) + gen = WikibaseItemFilterPageGenerator(pages, has_item=True) + self.assertLength(list(gen), 0) + gen = WikibaseItemFilterPageGenerator(pages, has_item=False) + self.assertLength(list(gen), 1) + + class DryFactoryGeneratorTest(TestCase):
"""Dry tests for pagegenerators.GeneratorFactory."""
pywikibot-commits@lists.wikimedia.org