jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/721990 )
Change subject: [cleanup] remove deprecated add_text functions ......................................................................
[cleanup] remove deprecated add_text functions
Change-Id: Id6910b56ed3902fb90b7294dfeea40f77768d0ba --- M scripts/add_text.py 1 file changed, 3 insertions(+), 213 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/add_text.py b/scripts/add_text.py index d664adb..6b3b20e 100755 --- a/scripts/add_text.py +++ b/scripts/add_text.py @@ -63,30 +63,18 @@ # import codecs import re -import sys -from typing import Optional, Union + +from typing import Union
import pywikibot
-from pywikibot import config, i18n, pagegenerators, textlib +from pywikibot import config, pagegenerators, textlib from pywikibot.backports import Dict, Tuple from pywikibot.bot import ( AutomaticTWSummaryBot, ExistingPageBot, NoRedirectPageBot, ) -from pywikibot.bot_choice import QuitKeyboardInterrupt -from pywikibot.exceptions import ( - EditConflictError, - IsRedirectPageError, - LockedPageError, - NoPageError, - PageSaveRelatedError, - ServerError, - SpamblacklistError, -) -from pywikibot.tools import deprecated -from pywikibot.tools.formatter import color_format
DEFAULT_ARGS = { 'text': '', @@ -113,204 +101,6 @@ docuReplacements = {'¶ms;': pagegenerators.parameterHelp} # noqa: N816
-@deprecated('Page.text, NoRedirectPageBot class and BaseBot.skip_page() ' - '(see add_text.AddTextBot for example)', since='6.4.0') -def get_text(page: pywikibot.page.BasePage, old: Optional[str], - create: bool) -> Optional[str]: - """ - Get text on page. If old is not None, return old. - - :param page: The page to get text from - :param old: If not None, return this rather than the page's text - :param create: Declare that the page will be created if it doesn't exist - :return: The page's text or the old parameter if not None - """ - if old is not None: - return old - - try: - return page.get() - except NoPageError: - if create: - pywikibot.output("{} doesn't exist, creating it!" - .format(page.title())) - return '' - else: - pywikibot.output("{} doesn't exist, skip!".format(page.title())) - return None - except IsRedirectPageError: - pywikibot.output('{} is a redirect, skip!'.format(page.title())) - return None - - -@deprecated('BaseBot.userPut()', since='6.4.0') -def put_text(page: pywikibot.page.BasePage, new: str, summary: str, count: int, - asynchronous: bool = False) -> Optional[bool]: - """ - Save the new text. - - :param page: The page to change the text of - :param new: The new text for the page - :param summary: Summary of the page change - :param count: Maximum number of attempts to reach the server - :param asynchronous: If True, saves the page asynchronously - :return: True if successful, False if unsuccessful, and None if - awaiting the server - """ - page.text = new - try: - page.save(summary=summary, asynchronous=asynchronous, - minor=page.namespace() != 3) - except EditConflictError: - pywikibot.output('Edit conflict! skip!') - except ServerError: - if count <= config.max_retries: - pywikibot.output('Server Error! Wait..') - pywikibot.sleep(config.retry_wait) - return None - raise ServerError( - 'Server Error! Maximum retries exceeded') - except SpamblacklistError as e: - pywikibot.output( - 'Cannot change {} because of blacklist entry {}' - .format(page.title(), e.url)) - except LockedPageError: - pywikibot.output('Skipping {} (locked page)'.format(page.title())) - except PageSaveRelatedError as error: - pywikibot.output('Error putting page: {}'.format(error.args)) - else: - return True - return False - - -@deprecated('Page.text, textlib.add_text, NoRedirectPageBot class, ' - 'BaseBot.skip_page() and CurrentPage.put_current() ' - '(see add_text.AddTextBot for example)', since='6.4.0') -def add_text(page: pywikibot.page.BasePage, addText: str, - summary: Optional[str] = None, - regexSkip: Optional[str] = None, - regexSkipUrl: Optional[str] = None, - always: bool = False, up: bool = False, - putText: bool = True, oldTextGiven: Optional[str] = None, - reorderEnabled: bool = True, create: bool = False - ) -> Union[Tuple[bool, bool, bool], Tuple[str, str, bool]]: - """ - Add text to a page. - - :param page: The page to add text to - :param addText: Text to add - :param summary: Change summary, if None this uses the beginning of addText - :param regexSkip: Abort if the text on the page matches this - :param regexSkipUrl: Abort if the url matches this - :param always: Edit without user confirmation - :param up: Append text to the top of the page if True, otherwise the - bottom - :param putText: Save changes to the page if True, otherwise return - (text, newtext, always) - :param oldTextGiven: If None fetch page text, else use this text - :param reorderEnabled: If True place text above categories and - interwiki, else place at page bottom. No effect if up = False. - :param create: Create the page if it does not exist - :return: (success, success, always) if putText is True, otherwise - (text, newtext, always) - """ - site = page.site - if not summary: - summary = i18n.twtranslate(site, 'add_text-adding', - {'adding': addText[:200]}) - if putText: - pywikibot.output('Loading {}...'.format(page.title())) - - text = get_text(page, oldTextGiven, create) - if text is None: - return (False, False, always) - - # Understand if the bot has to skip the page or not - # In this way you can use both -except and -excepturl - if regexSkipUrl is not None: - url = page.full_url() - result = re.findall(regexSkipUrl, site.getUrl(url)) - if result != []: - pywikibot.output( - 'Exception! regex (or word) used with -exceptUrl ' - 'is in the page. Skip!\n' - 'Match was: {}'.format(result)) - return (False, False, always) - if regexSkip is not None: - result = re.findall(regexSkip, text) - if result != []: - pywikibot.output( - 'Exception! regex (or word) used with -except ' - 'is in the page. Skip!\n' - 'Match was: {}'.format(result)) - return (False, False, always) - # If not up, text put below - if not up: - newtext = text - # Translating the \n into binary \n - addText = addText.replace('\n', '\n') - if reorderEnabled: - # Getting the categories - categoriesInside = textlib.getCategoryLinks(newtext, site) - # Deleting the categories - newtext = textlib.removeCategoryLinks(newtext, site) - # Getting the interwiki - interwikiInside = textlib.getLanguageLinks(newtext, site) - # Removing the interwiki - newtext = textlib.removeLanguageLinks(newtext, site) - - # Adding the text - newtext += '\n' + addText - # Reputting the categories - newtext = textlib.replaceCategoryLinks(newtext, - categoriesInside, site, - True) - # Adding the interwiki - newtext = textlib.replaceLanguageLinks(newtext, interwikiInside, - site) - else: - newtext += '\n' + addText - else: - newtext = addText + '\n' + text - - if not putText: - # If someone load it as module, maybe it's not so useful to put the - # text in the page - return (text, newtext, always) - - if text != newtext: - pywikibot.output(color_format( - '\n\n>>> {lightpurple}{0}{default} <<<', page.title())) - pywikibot.showDiff(text, newtext) - - # Let's put the changes. - error_count = 0 - while True: - if not always: - try: - choice = pywikibot.input_choice( - 'Do you want to accept these changes?', - [('Yes', 'y'), ('No', 'n'), ('All', 'a'), - ('open in Browser', 'b')], 'n') - except QuitKeyboardInterrupt: - sys.exit('User quit bot run.') - - if choice == 'a': - always = True - elif choice == 'n': - return (False, False, always) - elif choice == 'b': - pywikibot.bot.open_webbrowser(page) - continue - - # either always or choice == 'y' is selected - result = put_text(page, newtext, summary, error_count, - asynchronous=not always) - if result is not None: - return (result, result, always) - error_count += 1 - - class AddTextBot(AutomaticTWSummaryBot, ExistingPageBot, NoRedirectPageBot):
"""A bot which adds a text to a page."""
pywikibot-commits@lists.wikimedia.org