jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/696939 )
Change subject: [IMPR] Use IntEnum with cosmetic_changes CANCEL ......................................................................
[IMPR] Use IntEnum with cosmetic_changes CANCEL
cosmetic_changes uses four CANCEL constants for ignoring exceptions. Replace them with IntEnum class and deprecate the old usage.
Change-Id: I49e4df1394b7494c4548afd486fc3a9de39d5ee6 --- M pywikibot/cosmetic_changes.py M scripts/cosmetic_changes.py 2 files changed, 46 insertions(+), 20 deletions(-)
Approvals: Matěj Suchánek: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py index 9d7e684..8e60c34 100755 --- a/pywikibot/cosmetic_changes.py +++ b/pywikibot/cosmetic_changes.py @@ -56,6 +56,8 @@ # Distributed under the terms of the MIT license. # import re + +from enum import IntEnum from typing import Optional
import pywikibot @@ -72,6 +74,7 @@ first_lower, first_upper, issue_deprecation_warning, + ModuleDeprecationWrapper, ) from pywikibot.tools.chars import url2string
@@ -173,10 +176,19 @@ } }
-CANCEL_ALL = False -CANCEL_PAGE = 1 -CANCEL_METHOD = 2 -CANCEL_MATCH = 3 + +class CANCEL(IntEnum): + + """Cancel level to ignore exceptions. + + If an error occurred and either skips the page or the method + or a single match. ALL raises the exception. + """ + + ALL = 0 + PAGE = 1 + METHOD = 2 + MATCH = 3
def _format_isbn_match(match, strict=True): @@ -215,7 +227,7 @@ show_diff: bool = False, namespace: Optional[int] = None, pageTitle: Optional[str] = None, - ignore: int = CANCEL_ALL): + ignore: IntEnum = CANCEL.ALL): """Initializer.
@param page: the Page object containing the text to be modified @@ -295,7 +307,7 @@ @deprecated('CosmeticChangesToolkit with pywikibot.Page object', future_warning=True, since='20200415') @deprecated_args(diff='show_diff') - def from_page(cls, page, show_diff=False, ignore=CANCEL_ALL): + def from_page(cls, page, show_diff=False, ignore=CANCEL.ALL): """Create toolkit based on the page.""" return cls(page, show_diff=show_diff, ignore=ignore)
@@ -305,7 +317,7 @@ try: result = method(text) except Exception as e: - if self.ignore == CANCEL_METHOD: + if self.ignore == CANCEL.METHOD: pywikibot.warning('Unable to perform "{}" on "{}"!' .format(method.__name__, self.title)) pywikibot.exception(e) @@ -324,7 +336,7 @@ try: new_text = self._change(text) except Exception as e: - if self.ignore == CANCEL_PAGE: + if self.ignore == CANCEL.PAGE: pywikibot.warning('Skipped "{}", because an error occurred.' .format(self.title)) pywikibot.exception(e) @@ -1094,4 +1106,24 @@
def fix_ISBN(self, text): """Hyphenate ISBN numbers.""" - return _reformat_ISBNs(text, strict=self.ignore != CANCEL_MATCH) + return _reformat_ISBNs(text, strict=self.ignore != CANCEL.MATCH) + + +_CANCEL_ALL = CANCEL.ALL +_CANCEL_PAGE = CANCEL.PAGE +_CANCEL_METHOD = CANCEL.METHOD +_CANCEL_MATCH = CANCEL.MATCH + +wrapper = ModuleDeprecationWrapper(__name__) +wrapper.add_deprecated_attr('CANCEL_ALL', _CANCEL_ALL, + replacement_name='CANCEL.ALL', + since='20210528', future_warning=True) +wrapper.add_deprecated_attr('CANCEL_PAGE', _CANCEL_PAGE, + replacement_name='CANCEL.PAGE', + since='20210528', future_warning=True) +wrapper.add_deprecated_attr('CANCEL_METHOD', _CANCEL_METHOD, + replacement_name='CANCEL.METHOD', + since='20210528', future_warning=True) +wrapper.add_deprecated_attr('CANCEL_MATCH', _CANCEL_MATCH, + replacement_name='CANCEL.MATCH', + since='20210528', future_warning=True) diff --git a/scripts/cosmetic_changes.py b/scripts/cosmetic_changes.py index edb77c5..b9ee145 100755 --- a/scripts/cosmetic_changes.py +++ b/scripts/cosmetic_changes.py @@ -35,13 +35,7 @@ from pywikibot import config, i18n, pagegenerators from pywikibot.backports import Tuple from pywikibot.bot import ExistingPageBot, NoRedirectPageBot -from pywikibot.cosmetic_changes import ( - CANCEL_ALL, - CANCEL_MATCH, - CANCEL_METHOD, - CANCEL_PAGE, - CosmeticChangesToolkit, -) +from pywikibot.cosmetic_changes import CANCEL, CosmeticChangesToolkit
warning = """ @@ -65,7 +59,7 @@ self.available_options.update({ 'async': False, 'summary': 'Robot: Cosmetic changes', - 'ignore': CANCEL_ALL, + 'ignore': CANCEL.ALL, }) super().__init__(**kwargs)
@@ -106,11 +100,11 @@ elif arg.startswith('-ignore:'): ignore_mode = arg[len('-ignore:'):].lower() if ignore_mode == 'method': - options['ignore'] = CANCEL_METHOD + options['ignore'] = CANCEL.METHOD elif ignore_mode == 'page': - options['ignore'] = CANCEL_PAGE + options['ignore'] = CANCEL.PAGE elif ignore_mode == 'match': - options['ignore'] = CANCEL_MATCH + options['ignore'] = CANCEL.MATCH else: raise ValueError( 'Unknown ignore mode "{}"!'.format(ignore_mode))