jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/428144 )
Change subject: [IMPR] Introduce setup and teardown methods ......................................................................
[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(-)
Approvals: JJMC89: Looks good to me, but someone else must approve Framawiki: Looks good to me, approved Dvorapa: Looks good to me, approved jenkins-bot: Verified
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:
pywikibot-commits@lists.wikimedia.org