Prepare environment for starting subtasks

Before init and run bot we need to provide a environment for it,
like parsed args

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

65
red.py
View File

@@ -32,26 +32,6 @@ import pywikibot
from pywikibot import pagegenerators from pywikibot import pagegenerators
import jogobot import jogobot
from bots.reddiscparser import DiscussionParserBot
def apply_conf_cat_generators( genFactory ):
"""
Builds generators for categories which are read from jogobot.config
Parameters:
@param genFactory: The GeneratorFactory to which the builded generators
should be added.
@type genFactory: pagegenerators.GeneratorFactory
"""
# Create Generators for configured Categories
for category in jogobot.config["redundances"]["redpage_cats"]:
cgen = genFactory.getCategoryGen(
category, gen_func=pagegenerators.CategorizedPageGenerator)
# If there is one, append to genFactory
if cgen:
genFactory.gens.append(cgen)
def main(*args): def main(*args):
@@ -68,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")] task_slug = os.path.basename(__file__)[:-len(".py")] # noqa (temp)
# 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:
@@ -93,8 +73,6 @@ def main(*args):
# that are also used by other scripts and that determine on which pages # that are also used by other scripts and that determine on which pages
# to work on. # to work on.
genFactory = pagegenerators.GeneratorFactory() genFactory = pagegenerators.GeneratorFactory()
# The generator gives the pages that should be worked upon.
gen = None
# If always is True, bot won't ask for confirmation of edit (automode) # If always is True, bot won't ask for confirmation of edit (automode)
# always = False # always = False
@@ -103,34 +81,41 @@ def main(*args):
# if parsing is needed or not # if parsing is needed or not
# force_reload = False # 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() # noqa (temp)
# Parse command line arguments # Parse command line arguments
for arg in local_args: for arg in local_args:
# Split args
arg, sep, value = arg.partition(':')
if arg.startswith("-always"): if arg.startswith("-always"):
# always = True # always = True
pass pass
elif arg.startswith("-task"):
subtask = value
else: else:
genFactory.handleArg(arg) genFactory.handleArg(arg)
if not gen: # After parsing args we can select bot to run
if not subtask or subtask == "discparser":
# Default case: discparser
subtask = "discparser"
# Check wether there are generators waiting for factoring, if not # Import related bot
# use configured categories from bots.reddiscparser import DiscussionParserBot as Bot # noqa (temp)
if not genFactory.gens:
apply_conf_cat_generators( genFactory )
# Create combined Generator (Union of all Generators) #
gen = genFactory.getCombinedGenerator()
if gen:
# Log beginning of parsing
jogobot.output( "{task_slug} invoked".format(task_slug=task_slug) )
# The preloading generator is responsible for downloading multiple
# pages from the wiki simultaneously.
gen = pagegenerators.PreloadingGenerator(gen)
DiscussionParserBot( gen ).run()
else: else:
pywikibot.showHelp() jogobot.output( (
"\03{{red}} Given subtask \"{subtask} \"" +
"is not existing!" ).format( subtask=subtask ), "ERROR" )
if( __name__ == "__main__" ): if( __name__ == "__main__" ):
main() main()