#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # reddiscparser.py # # Copyright 2017 Jonathan Golder # # 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 locale import pywikibot import jogobot def prepare_bot( task_slug, subtask, genFactory, subtask_args ): """ Handles importing subtask Bot class and prepares specific args Throws exception if bot not exists @param task_slug Task slug, needed for logging @type task_slug str @param subtask Slug of given subtask @type subtask str @param genFactory GenFactory with parsed pagegenerator args @type genFactory pagegenerators.GeneratorFactory @param subtask_args Additional args for subtasks @type subtask_args dict\ @returns The following tuple @return 1 Subtask slug (replaced None for default) @rtype str @return 2 Botclass of given subtask (Arg "-task") @rtype Class @return 3 GenFactory with parsed pagegenerator args @rtype pagegenerators.GeneratorFactory @return 4 Additional args for subtasks @rtype dict @rtype tuple """ # kwargs are passed to selected bot as **kwargs kwargs = subtask_args if not subtask or subtask == "discparser": # Default case: discparser subtask = "discparser" # Import related bot from bots.reddiscparser import DiscussionParserBot as Bot elif subtask == "markpages": # Import related bot from bots.markpages import MarkPagesBot as Bot elif subtask == "missingnotice": # Import related bot from bots.missingnotice import MissingNoticeBot as Bot # Subtask error else: jogobot.output( ( "\03{{red}} Given subtask \"{subtask} \"" + "is not existing!" ).format( subtask=subtask ), "ERROR" ) raise Exception return ( subtask, Bot, genFactory, kwargs ) def parse_red_args( argkey, value ): """ Process additional args for red.py @param argkey The arguments key @type argkey str @param value The arguments value @type value str @return Tuple with (key, value) if given pair is relevant, else None @rtype tuple or None """ if argkey.startswith("-famhash"): return ( "famhash", value ) return None 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 """ # Make sure locale is set to 'de_DE.UTF-8' to prevent problems # with wrong month abreviations in strptime locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') # 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")] # Disabled until [FS#86] is done # Before run, we need to check wether we are currently active or not if not jogobot.bot.active( task_slug ): return # Parse local Args to get information about subtask ( subtask, genFactory, subtask_args ) = jogobot.bot.parse_local_args( local_args, parse_red_args ) # select subtask and prepare args ( subtask, Bot, genFactory, kwargs ) = prepare_bot( task_slug, subtask, genFactory, subtask_args ) # Init Bot bot = jogobot.bot.init_bot( task_slug, subtask, Bot, genFactory, **kwargs) # Run bot jogobot.bot.run_bot( task_slug, subtask, bot ) if( __name__ == "__main__" ): main()