jenkins-bot submitted this change.
[IMPR] reduce code complexity of pagegenerators.py
Added also test for RedirectFilterPageGenerator.
Change-Id: Ic37cb41bdf658a4d1c2ee081374017e60b381da7
---
M pywikibot/pagegenerators.py
M tests/pagegenerators_tests.py
2 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py
index 62d10a0..2af367f 100644
--- a/pywikibot/pagegenerators.py
+++ b/pywikibot/pagegenerators.py
@@ -1622,19 +1622,17 @@
redirects.
@param show_filtered: Output a message for each page not yielded
"""
- for page in generator or []:
- if no_redirects:
- if not page.isRedirectPage():
- yield page
- elif show_filtered:
- pywikibot.output('%s is a redirect page. Skipping.' % page)
+ fmt = '{page} is {what} redirect page. Skipping.'
+ what = 'a' if no_redirects else 'not a'
- else:
- if page.isRedirectPage():
- yield page
- elif show_filtered:
- pywikibot.output('%s is not a redirect page. Skipping.'
- % page)
+ for page in generator or []:
+ is_redirect = page.isRedirectPage()
+ if bool(no_redirects) != bool(is_redirect): # xor
+ yield page
+ continue
+
+ if show_filtered:
+ pywikibot.output(fmt.format(what=what, page=page))
class ItemClaimFilter:
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py
index 6c5df6f..7010ee2 100755
--- a/tests/pagegenerators_tests.py
+++ b/tests/pagegenerators_tests.py
@@ -316,6 +316,27 @@
self.assertIsEmpty(list(gen))
+class RedirectFilterPageGeneratorTestCase(TestCase):
+
+ """Test RedirectFilterPageGenerator."""
+
+ family = 'wikipedia'
+ code = 'en'
+
+ def test_redirect_filter(self):
+ """Test RedirectFilterPageGenerator."""
+ from pywikibot.pagegenerators import RedirectFilterPageGenerator
+ gf = pagegenerators.GeneratorFactory(site=self.site)
+ gf.handleArg('-randomredirect:3')
+ gf.handleArg('-page:Main_Page')
+ gen = gf.getCombinedGenerator()
+ pages = list(gen)
+ gen = RedirectFilterPageGenerator(pages, no_redirects=True)
+ self.assertLength(list(gen), 1)
+ gen = RedirectFilterPageGenerator(pages, no_redirects=False)
+ self.assertLength(list(gen), 3)
+
+
class SubpageFilterGeneratorTestCase(TestCase):
"""Test SubpageFilterGenerator."""
To view, visit change 638736. To unsubscribe, or for help writing mail filters, visit settings.