jenkins-bot merged this change.
[IMPR] Introduce setup and teardown methods
setup and teardown methods can be used to initalize and cleanup the bot.
setup is called in when the bot.run starts working whereas teardown is called
from exit method. This is much more clearer than doing this inside a derived
run method.
setup allows some inital work like reading from live wiki or io stuff
which might be unwanted in the initializer of the instance. teardown allows
some cleanups like saving content to a file when the bots stops running and
there is no need to derive the exit method.
Change-Id: Id18281feb67be8a39f79e409a752f9502e736850
---
M pywikibot/bot.py
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 3007f70..e6a058b 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -1373,6 +1373,7 @@
terminated gracefully or was halted by exception.
May be overridden by subclasses.
"""
+ self.teardown()
pywikibot.output("\n%i pages read"
"\n%i pages written"
% (self._treat_counter, self._save_counter))
@@ -1408,6 +1409,19 @@
"""Return whether treat should be executed for the page."""
pass
+ def setup(self):
+ """Some inital setup before run operation starts.
+
+ This can be used for reading huge parts from life wiki or file
+ operation which is more than just initialize the instance.
+ Invoked by run() before running through generator loop.
+ """
+ pass
+
+ def teardown(self):
+ """Some cleanups after run operation. Invoked by exit()."""
+ pass
+
def run(self):
"""Process all pages in generator."""
self._start_ts = pywikibot.Timestamp.now()
@@ -1422,7 +1436,7 @@
# Python 2 does not clear previous exceptions and method `exit`
# relies on sys.exc_info returning exceptions occurring in `run`.
sys.exc_clear()
-
+ self.setup()
try:
for page in self.generator:
try:
To view, visit change 428144. To unsubscribe, visit settings.