jenkins-bot has submitted this change and it was merged.
Change subject: Move typical page save exception handling into Bot ......................................................................
Move typical page save exception handling into Bot
Most bots wish to continue processing pages from their generator even if a page fails to save, whether because of a page-specific or server related error.
This functionality has been added to Bot.userPut, not enabled by default. reflinks.py has been migrated to use this functionality.
Also, add show_diff to Bot.userPut, which can be used to disable showing the changes that will be made to the page.
Change-Id: I34898b36f915a1399522040201aac84fa8f1324c --- M pywikibot/bot.py M scripts/reflinks.py 2 files changed, 44 insertions(+), 18 deletions(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 8fbf9ad..3156289 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -900,6 +900,13 @@
Option used: * 'always' + + Keyword args used: + * 'async' - passed to page.save + * 'comment' - passed to page.save + * 'show_diff' - show changes between oldtext and newtext (enabled) + * 'ignore_save_related_errors' - report and ignore (disabled) + * 'ignore_server_errors' - report and ignore (disabled) """ if oldtext == newtext: pywikibot.output(u'No changes were needed on %s' @@ -907,7 +914,12 @@ return
self.current_page = page - pywikibot.showDiff(oldtext, newtext) + + show_diff = kwargs.pop('show_diff', True) + + if show_diff: + pywikibot.showDiff(oldtext, newtext) + if 'comment' in kwargs: pywikibot.output(u'Comment: %s' % kwargs['comment'])
@@ -918,7 +930,34 @@ kwargs['async'] = True
page.text = newtext - page.save(**kwargs) + + ignore_save_related_errors = kwargs.pop('ignore_save_related_errors', False) + ignore_server_errors = kwargs.pop('ignore_server_errors', False) + + try: + page.save(**kwargs) + except pywikibot.PageSaveRelatedError as e: + if not ignore_save_related_errors: + raise + if isinstance(e, pywikibot.EditConflict): + pywikibot.output(u'Skipping %s because of edit conflict' + % page.title()) + elif isinstance(e, pywikibot.SpamfilterError): + pywikibot.output( + u'Cannot change %s because of blacklist entry %s' + % (page.title(), e.url)) + elif isinstance(e, pywikibot.LockedPage): + pywikibot.output(u'Skipping %s (locked page)' + % page.title()) + else: + pywikibot.error( + u'Skipping %s because of a save related error: %s' + % (page.title(), e)) + except pywikibot.ServerError as e: + if not ignore_server_errors: + raise + pywikibot.error(u'Server Error while processing %s: %s' + % (page.title(), e))
def quit(self): """Cleanup and quit processing.""" diff --git a/scripts/reflinks.py b/scripts/reflinks.py index 477b668..f0b7d61 100644 --- a/scripts/reflinks.py +++ b/scripts/reflinks.py @@ -744,22 +744,9 @@
new_text = self.deduplicator.process(new_text)
- try: - self.userPut(page, page.text, new_text, comment=self.msg) - except pywikibot.EditConflict: - pywikibot.output(u'Skipping %s because of edit conflict' - % page.title()) - except pywikibot.SpamfilterError as e: - pywikibot.output( - u'Cannot change %s because of blacklist entry %s' - % (page.title(), e.url)) - except pywikibot.LockedPage: - pywikibot.output(u'Skipping %s (locked page)' - % page.title()) - except pywikibot.PageNotSaved as error: - pywikibot.error(u'putting page: %s' % (error.args,)) - except pywikibot.ServerError as e: - pywikibot.output(u'Server Error : %s' % e) + self.userPut(page, page.text, new_text, comment=self.msg, + ignore_save_related_errors=True, + ignore_server_errors=True)
if new_text == page.text: continue
pywikibot-commits@lists.wikimedia.org