Add Bot initiation with exception handling

Bot initiation needs to catch errors by Bot to enforce at least a basic
logging.
And also to be sure Init was successfull before starting bot.

Related Task: [https://fs.golderweb.de/index.php?do=details&task_id=82 FS#82]
This commit is contained in:
2016-08-27 13:49:19 +02:00
parent 1679e2ad6a
commit 156f117b18

25
red.py
View File

@@ -48,7 +48,7 @@ def main(*args):
local_args = pywikibot.handle_args(args) local_args = pywikibot.handle_args(args)
# Get the jogobot-task_slug (basename of current file without ending) # Get the jogobot-task_slug (basename of current file without ending)
task_slug = os.path.basename(__file__)[:-len(".py")] # noqa (temp) task_slug = os.path.basename(__file__)[:-len(".py")]
# Before run, we need to check wether we are currently active or not # Before run, we need to check wether we are currently active or not
try: try:
@@ -86,7 +86,7 @@ def main(*args):
subtask = None subtask = None
# kwargs are passed to selected bot as **kwargs # kwargs are passed to selected bot as **kwargs
kwargs = dict() # noqa (temp) kwargs = dict()
# Parse command line arguments # Parse command line arguments
for arg in local_args: for arg in local_args:
@@ -108,7 +108,7 @@ def main(*args):
subtask = "discparser" subtask = "discparser"
# Import related bot # Import related bot
from bots.reddiscparser import DiscussionParserBot as Bot # noqa (temp) from bots.reddiscparser import DiscussionParserBot as Bot
# #
else: else:
@@ -116,6 +116,25 @@ def main(*args):
"\03{{red}} Given subtask \"{subtask} \"" + "\03{{red}} Given subtask \"{subtask} \"" +
"is not existing!" ).format( subtask=subtask ), "ERROR" ) "is not existing!" ).format( subtask=subtask ), "ERROR" )
# Bot gets prepared genFactory as first param and possible kwargs dict
# It has to threw an exception if something does not work properly
try:
# Init bot with genFactory and **kwargs
bot = Bot( genFactory, **kwargs ) # noqa (temp)
except:
# Catch Errors while initiation
jogobot.output( (
"\03{{red}} Error while trying to init " +
"subtask \"{task_slug}-{subtask} \"!" ).
format( task_slug=task_slug, subtask=subtask ), "ERROR" )
raise
else:
# Init successfull
jogobot.output( (
"{task_slug}-{subtask} init successfull" ).
format(task_slug=task_slug, subtask=subtask) )
if( __name__ == "__main__" ): if( __name__ == "__main__" ):
main() main()