jenkins-bot has submitted this change and it was merged.

Change subject: [FEAT] Invoke an exit method when Bot.run() is done.
......................................................................


[FEAT] Invoke an exit method when Bot.run() is done.

The current implementation prints treat and save counters and informs
whether the script terminated gracefully or was halted by exception.

This method may be overridden by subclasses for final processing,
e.g. printing some statistics or save values to a file.

Change-Id: I83f1ada472f5164fadf3df7cfea320be33ee919f
---
M pywikibot/bot.py
1 file changed, 23 insertions(+), 0 deletions(-)

Approvals:
  Ladsgroup: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 4d8e5c9..cb97020 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -1200,6 +1200,27 @@
         """Cleanup and quit processing."""
         raise QuitKeyboardInterrupt
 
+    def exit(self):
+        """
+        Cleanup and exit processing.
+
+        Invoked when Bot.run() is finished.
+        Prints treat and save counters and informs whether the script
+        terminated gracefully or was halted by exception.
+        May be overridden by subclasses.
+        """
+        pywikibot.output("\n%i pages read"
+                         "\n%i pages written"
+                         % (self._treat_counter, self._save_counter))
+
+        # exc_info contains exception from self.run() while terminating
+        exc_info = sys.exc_info()
+        if exc_info[0] is None:
+            pywikibot.output("Script terminated successfully.")
+        else:
+            pywikibot.output("Script terminated by exception:\n")
+            pywikibot.exception()
+
     def treat(self, page):
         """Process one page (Abstract method)."""
         raise NotImplementedError('Method %s.treat() not implemented.'
@@ -1291,6 +1312,8 @@
             else:
                 pywikibot.output('\nKeyboardInterrupt during %s bot run...' %
                                  self.__class__.__name__)
+        finally:
+            self.exit()
 
 
 class CurrentPageBot(Bot):

-- 
To view, visit https://gerrit.wikimedia.org/r/216432
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I83f1ada472f5164fadf3df7cfea320be33ee919f
Gerrit-PatchSet: 6
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl>
Gerrit-Reviewer: XZise <commodorefabia...@gmx.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
Pywikibot-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to