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."""
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/721990
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Id6910b56ed3902fb90b7294dfeea40f77768d0ba
Gerrit-Change-Number: 721990
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/721297 )
Change subject: [IMPR] Provide -create and -createonly options with add_text
......................................................................
[IMPR] Provide -create and -createonly options with add_text
https://www.mediawiki.org/wiki/Topic:Wgdnu8bl29esui34
Also fix type hints
Bug: T291354
Change-Id: Ib77d7ab94285ae74c37da000d63b72e8a92d3d3f
---
M pywikibot/bot.py
M scripts/add_text.py
2 files changed, 32 insertions(+), 14 deletions(-)
Approvals:
Inductiveload: Looks good to me, but someone else must approve
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 3197e60..c50d36c 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -1895,7 +1895,8 @@
parameters for the translated message.
"""
- summary_key = None # must be defined in subclasses
+ #: Must be defined in subclasses.
+ summary_key = None # type: Optional[str]
@property
def summary_parameters(self) -> Dict[str, str]:
diff --git a/scripts/add_text.py b/scripts/add_text.py
index 63cdfd7..d664adb 100755
--- a/scripts/add_text.py
+++ b/scripts/add_text.py
@@ -18,6 +18,11 @@
-up If used, put the text at the top of the page
+-create Create the page if necessary. Note that talk pages are
+ created already without of this option.
+
+-createonly Only create the page but do not edit existing ones
+
-always If used, the bot won't ask if it should add the specified
text
@@ -88,6 +93,8 @@
'textfile': '',
'summary': '',
'up': False,
+ 'create': False,
+ 'createonly': False,
'always': False,
'minor': True,
'talk_page': False,
@@ -109,7 +116,7 @@
@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) -> str:
+ create: bool) -> Optional[str]:
"""
Get text on page. If old is not None, return old.
@@ -336,20 +343,30 @@
def skip_page(self, page):
"""Skip if -exceptUrl matches or page does not exists."""
- if page.exists() and self.opt.regex_skip_url:
- url = page.full_url()
- result = re.findall(self.opt.regex_skip_url, page.site.getUrl(url))
-
- if result:
- pywikibot.warning(
- 'Skipping {page} because -excepturl matches {result}.'
- .format(page=page, result=result))
+ if page.exists():
+ if self.opt.createonly:
+ pywikibot.warning('Skipping because {page} already exists'
+ .format(page=page))
return True
- if page.isTalkPage() and not page.exists():
+ if self.opt.regex_skip_url:
+ url = page.full_url()
+ result = re.findall(self.opt.regex_skip_url,
+ page.site.getUrl(url))
+
+ if result:
+ pywikibot.warning(
+ 'Skipping {page} because -excepturl matches {result}.'
+ .format(page=page, result=result))
+ return True
+
+ elif page.isTalkPage():
pywikibot.output("{} doesn't exist, creating it!".format(page))
return False
+ elif self.opt.create:
+ return False
+
return super().skip_page(page)
def treat_page(self):
@@ -367,7 +384,7 @@
self.put_current(text, summary=self.opt.summary, minor=self.opt.minor)
-def main(*argv: Tuple[str, ...]) -> None:
+def main(*argv: str) -> None:
"""
Process command line arguments and invoke bot.
@@ -393,7 +410,7 @@
def parse(argv: Tuple[str, ...],
generator_factory: pagegenerators.GeneratorFactory
- ) -> Dict[str, str]:
+ ) -> Dict[str, Union[bool, str]]:
"""
Parses our arguments and provide a named tuple with their values.
@@ -414,7 +431,7 @@
if option in ('-text', '-textfile', '-summary'):
args[option[1:]] = value
- elif option in ('-up', '-always'):
+ elif option in ('-up', '-always', '-create', 'createonly'):
args[option[1:]] = True
elif option in ('-talk', '-talkpage'):
args['talk_page'] = True
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/721297
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ib77d7ab94285ae74c37da000d63b72e8a92d3d3f
Gerrit-Change-Number: 721297
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Inductiveload <inductiveload(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged