Files
jogobot-red/red.py
GOLDERWEB – Jonathan Golder 156f117b18 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]
2016-08-27 15:40:09 +02:00

141 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# reddiscparser.py
#
# Copyright 2016 GOLDERWEB Jonathan Golder <jonathan@golderweb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
"""
Wrapper script to invoke all redundances bot tasks
"""
import os
import sys
import pywikibot
from pywikibot import pagegenerators
import jogobot
def main(*args):
"""
Process command line arguments and invoke bot.
If args is an empty list, sys.argv is used.
@param args: command line arguments
@type args: list of unicode
"""
# Process global arguments to determine desired site
local_args = pywikibot.handle_args(args)
# Get the jogobot-task_slug (basename of current file without ending)
task_slug = os.path.basename(__file__)[:-len(".py")]
# Before run, we need to check wether we are currently active or not
try:
# Will throw Exception if disabled/blocked
# jogobot.is_active( task_slug )
pass
except jogobot.jogobot.Blocked:
(type, value, traceback) = sys.exc_info()
jogobot.output( "\03{lightpurple} %s (%s)" % (value, type ),
"CRITICAL" )
except jogobot.jogobot.Disabled:
(type, value, traceback) = sys.exc_info()
jogobot.output( "\03{red} %s (%s)" % (value, type ),
"ERROR" )
# Bot/Task is active
else:
# This factory is responsible for processing command line arguments
# that are also used by other scripts and that determine on which pages
# to work on.
genFactory = pagegenerators.GeneratorFactory()
# If always is True, bot won't ask for confirmation of edit (automode)
# always = False
# If force_reload is True, bot will always parse Countrylist regardless
# if parsing is needed or not
# force_reload = False
# Subtask selects the specific bot to run
# Default is reddiscparser
subtask = None
# kwargs are passed to selected bot as **kwargs
kwargs = dict()
# Parse command line arguments
for arg in local_args:
# Split args
arg, sep, value = arg.partition(':')
if arg.startswith("-always"):
# always = True
pass
elif arg.startswith("-task"):
subtask = value
else:
genFactory.handleArg(arg)
# After parsing args we can select bot to run
if not subtask or subtask == "discparser":
# Default case: discparser
subtask = "discparser"
# Import related bot
from bots.reddiscparser import DiscussionParserBot as Bot
#
else:
jogobot.output( (
"\03{{red}} Given subtask \"{subtask} \"" +
"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__" ):
main()