jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/640707 )
Change subject: [IMPR] reduce code complexity of pagegenerators.py ......................................................................
[IMPR] reduce code complexity of pagegenerators.py
There is a small incompatibility: - AssertionError is raised instead of ValueError but it should cause no harm as it is difficult that someone is currently catching it.
Change-Id: I5d068a3a79861cbc358b32669602b490687ab389 --- M pywikibot/pagegenerators.py M tests/pagegenerators_tests.py 2 files changed, 12 insertions(+), 11 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index 5d1c164..e92f6a1 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -703,19 +703,22 @@ cats = self.site.siteinfo.get('linter') # Get linter categories. valid_cats = [c for _list in cats.values() for c in _list]
- value = '' if value is None else value + value = value or '' cat, _, lint_from = value.partition('/') - if not lint_from: - lint_from = None + lint_from = lint_from or None
- if cat == 'show': # Display categories of lint errors. + def show_available_categories(cats): _i = ' ' * 4 + _2i = 2 * _i txt = 'Available categories of lint errors:\n' for prio, _list in cats.items(): txt += '{indent}{prio}\n'.format(indent=_i, prio=prio) - for c in _list: - txt += '{indent}{cat}\n'.format(indent=2 * _i, cat=c) + txt += ''.join( + '{indent}{cat}\n'.format(indent=_2i, cat=c) for c in _list) pywikibot.output('%s' % txt) + + if cat == 'show': # Display categories of lint errors. + show_available_categories(cats) sys.exit(0)
if not cat: @@ -724,10 +727,8 @@ lint_cats = cats[cat] else: lint_cats = cat.split(',') - for lint_cat in lint_cats: - if lint_cat not in valid_cats: - raise ValueError('Invalid category of lint errors: %s' - % cat) + assert set(lint_cats) <= set(valid_cats), \ + 'Invalid category of lint errors: %s' % cat
return self.site.linter_pages( lint_categories='|'.join(lint_cats), namespaces=self.namespaces, diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py index 41302eb..1762769 100755 --- a/tests/pagegenerators_tests.py +++ b/tests/pagegenerators_tests.py @@ -1247,7 +1247,7 @@ self.skipTest('The site {0} does not use Linter extension' .format(self.site)) gf = pagegenerators.GeneratorFactory(site=self.site) - self.assertRaises(ValueError, gf.handleArg, '-linter:dummy') + self.assertRaises(AssertionError, gf.handleArg, '-linter:dummy')
def test_linter_generator_show(self): """Test generator of pages with lint errors."""