jenkins-bot has submitted this change and it was merged.
Change subject: Control output message when saving.
......................................................................
Control output message when saving.
Allow enabling/disabling of output message when saving.
Bot can manage display of successful operations via callback when needed.
In replace.py, migrate put() and put_async() to save().
Bug: T73646
Change-Id: I9701529241a07ad3f5524f157a91bb53268ff6d2
---
M pywikibot/page.py
M scripts/replace.py
2 files changed, 39 insertions(+), 10 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py
index d1ea5bc..50a0d48 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -1051,7 +1051,7 @@
@deprecated_args(comment='summary', sysop=None)
def save(self, summary=None, watch=None, minor=True, botflag=None,
force=False, async=False, callback=None,
- apply_cosmetic_changes=None, **kwargs):
+ apply_cosmetic_changes=None, quiet=False, **kwargs):
"""Save the current contents of page's text to the wiki.
@param summary: The edit summary for the modification (optional, but
@@ -1086,6 +1086,11 @@
@param apply_cosmetic_changes: Overwrites the cosmetic_changes
configuration value to this value unless it's None.
@type apply_cosmetic_changes: bool or None
+ @param quiet: enable/disable successful save operation message;
+ defaults to False.
+ In async mode, if True, it is up to the calling bot to manage the
+ ouput e.g. via callback.
+ @type quiet: bool
"""
if not summary:
summary = config.default_edit_summary
@@ -1100,14 +1105,15 @@
pywikibot.async_request(self._save, summary=summary, minor=minor,
watch=watch, botflag=botflag,
async=async, callback=callback,
- cc=apply_cosmetic_changes, **kwargs)
+ cc=apply_cosmetic_changes,
+ quiet=quiet, **kwargs)
else:
self._save(summary=summary, minor=minor, watch=watch,
botflag=botflag, async=async, callback=callback,
cc=apply_cosmetic_changes, **kwargs)
def _save(self, summary, minor, watch, botflag, async, callback,
- cc, **kwargs):
+ cc, quiet=False, **kwargs):
"""Helper function for save()."""
err = None
link = self.title(asLink=True)
@@ -1117,9 +1123,10 @@
done = self.site.editpage(self, summary=summary, minor=minor,
watch=watch, bot=botflag, **kwargs)
if not done:
- pywikibot.warning(u"Page %s not saved" % link)
+ if not quiet:
+ pywikibot.warning(u"Page %s not saved" % link)
raise pywikibot.PageNotSaved(self)
- else:
+ if not quiet:
pywikibot.output(u"Page %s saved" % link)
# TODO: other "expected" error types to catch?
except pywikibot.Error as edit_err:
diff --git a/scripts/replace.py b/scripts/replace.py
index da09945..4aebab4 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -144,6 +144,12 @@
import sys
import warnings
+if sys.version_info[0] > 2:
+ from queue import Queue
+ long = int
+else:
+ from Queue import Queue
+
import pywikibot
from pywikibot import i18n, textlib, pagegenerators, Bot
@@ -541,6 +547,7 @@
self.sleep = sleep
self.summary = summary
self.changed_pages = 0
+ self._pending_processed_titles = Queue()
def isTitleExcepted(self, title, exceptions=None):
"""
@@ -630,11 +637,14 @@
new_text = self.apply_replacements(original_text, set(), page=page)
return new_text
- def count_changes(self, page, err): # pylint: disable=unused-argument
- """Count succesfully changed pages."""
+ def _count_changes(self, page, err):
+ """Count succesfully changed pages; log changed titles for
display."""
# This is an async put callback
if not isinstance(err, Exception):
self.changed_pages += 1
+ self._pending_processed_titles.put((page.title(asLink=True), True))
+ else: # unsuccessful pages
+ self._pending_processed_titles.put((page.title(asLink=True), False))
def generate_summary(self, applied_replacements):
"""Generate a summary message for the
replacements."""
@@ -743,13 +753,20 @@
if choice == 'a':
self.options['always'] = True
if choice == 'y':
- page.put_async(new_text, self.generate_summary(applied),
- callback=self.count_changes)
+ page.text = new_text
+ page.save(self.generate_summary(applied), async=True,
+ callback=self._count_changes, quiet=True)
+ while not self._pending_processed_titles.empty():
+ proc_title, res = self._pending_processed_titles.get()
+ pywikibot.output('Page %s%s saved'
+ % (proc_title, '' if res else '
not'))
# choice must be 'N'
break
if self.getOption('always') and new_text != original_text:
try:
- page.put(new_text, self.generate_summary(applied),
callback=self.count_changes)
+ page.text = new_text
+ page.save(self.generate_summary(applied),
+ callback=self._count_changes, quiet=True)
except pywikibot.EditConflict:
pywikibot.output(u'Skipping %s because of edit conflict'
% (page.title(),))
@@ -763,6 +780,11 @@
except pywikibot.PageNotSaved as error:
pywikibot.output(u'Error putting page: %s'
% (error.args,))
+ if self._pending_processed_titles.qsize() > 50:
+ while not self._pending_processed_titles.empty():
+ proc_title, res = self._pending_processed_titles.get()
+ pywikibot.output('Page %s%s saved'
+ % (proc_title, '' if res else '
not'))
def prepareRegexForMySQL(pattern):
--
To view, visit
https://gerrit.wikimedia.org/r/248680
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I9701529241a07ad3f5524f157a91bb53268ff6d2
Gerrit-PatchSet: 8
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>