No Description

red.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # reddiscparser.py
  5. #
  6. # Copyright 2017 Jonathan Golder <jonathan@golderweb.de>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. # MA 02110-1301, USA.
  22. #
  23. #
  24. """
  25. Wrapper script to invoke all redundances bot tasks
  26. """
  27. import os
  28. import locale
  29. import pywikibot
  30. import jogobot
  31. def prepare_bot( task_slug, subtask, genFactory, subtask_args ):
  32. """
  33. Handles importing subtask Bot class and prepares specific args
  34. Throws exception if bot not exists
  35. @param task_slug Task slug, needed for logging
  36. @type task_slug str
  37. @param subtask Slug of given subtask
  38. @type subtask str
  39. @param genFactory GenFactory with parsed pagegenerator args
  40. @type genFactory pagegenerators.GeneratorFactory
  41. @param subtask_args Additional args for subtasks
  42. @type subtask_args dict\
  43. @returns The following tuple
  44. @return 1 Subtask slug (replaced None for default)
  45. @rtype str
  46. @return 2 Botclass of given subtask (Arg "-task")
  47. @rtype Class
  48. @return 3 GenFactory with parsed pagegenerator args
  49. @rtype pagegenerators.GeneratorFactory
  50. @return 4 Additional args for subtasks
  51. @rtype dict
  52. @rtype tuple
  53. """
  54. # kwargs are passed to selected bot as **kwargs
  55. kwargs = subtask_args
  56. if not subtask or subtask == "discparser":
  57. # Default case: discparser
  58. subtask = "discparser"
  59. # Import related bot
  60. from bots.reddiscparser import DiscussionParserBot as Bot
  61. elif subtask == "markpages":
  62. # Import related bot
  63. from bots.markpages import MarkPagesBot as Bot
  64. elif subtask == "missingnotice":
  65. # Import related bot
  66. from bots.missingnotice import MissingNoticeBot as Bot
  67. # Subtask error
  68. else:
  69. jogobot.output( (
  70. "\03{{red}} Given subtask \"{subtask} \"" +
  71. "is not existing!" ).format( subtask=subtask ), "ERROR" )
  72. raise Exception
  73. return ( subtask, Bot, genFactory, kwargs )
  74. def parse_red_args( argkey, value ):
  75. """
  76. Process additional args for red.py
  77. @param argkey The arguments key
  78. @type argkey str
  79. @param value The arguments value
  80. @type value str
  81. @return Tuple with (key, value) if given pair is relevant, else None
  82. @rtype tuple or None
  83. """
  84. if argkey.startswith("-famhash"):
  85. return ( "famhash", value )
  86. return None
  87. def main(*args):
  88. """
  89. Process command line arguments and invoke bot.
  90. If args is an empty list, sys.argv is used.
  91. @param args: command line arguments
  92. @type args: list of unicode
  93. """
  94. # Make sure locale is set to 'de_DE.UTF-8' to prevent problems
  95. # with wrong month abreviations in strptime
  96. locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
  97. # Process global arguments to determine desired site
  98. local_args = pywikibot.handle_args(args)
  99. # Get the jogobot-task_slug (basename of current file without ending)
  100. task_slug = os.path.basename(__file__)[:-len(".py")]
  101. # Disabled until [FS#86] is done
  102. # Before run, we need to check wether we are currently active or not
  103. if not jogobot.bot.active( task_slug ):
  104. return
  105. # Parse local Args to get information about subtask
  106. ( subtask, genFactory, subtask_args ) = jogobot.bot.parse_local_args(
  107. local_args, parse_red_args )
  108. # select subtask and prepare args
  109. ( subtask, Bot, genFactory, kwargs ) = prepare_bot(
  110. task_slug, subtask, genFactory, subtask_args )
  111. # Init Bot
  112. bot = jogobot.bot.init_bot( task_slug, subtask, Bot, genFactory, **kwargs)
  113. # Run bot
  114. jogobot.bot.run_bot( task_slug, subtask, bot )
  115. if( __name__ == "__main__" ):
  116. main()