A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

133 lignes
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 by Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from earwigbot import exceptions
  23. from earwigbot import wiki
  24. __all__ = ["BaseTask"]
  25. class BaseTask(object):
  26. """
  27. EarwigBot's Base Bot Task
  28. This package provides built-in wiki bot "tasks" EarwigBot runs. Additional
  29. tasks can be installed as plugins in the bot's working directory.
  30. This class (import with `from earwigbot.tasks import BaseTask`) can be
  31. subclassed to create custom bot tasks.
  32. To run a task, use :py:meth:`bot.tasks.start(name, **kwargs)
  33. <earwigbot.managers.TaskManager.start>`. ``**kwargs`` get passed to the
  34. Task's run() function.
  35. """
  36. name = None
  37. number = 0
  38. def __init__(self, bot):
  39. """Constructor for new tasks.
  40. This is called once immediately after the task class is loaded by
  41. the task manager (in tasks._load_task()). Don't override this directly
  42. (or if you do, remember super(Task, self).__init()) - use setup().
  43. """
  44. self.bot = bot
  45. self.config = bot.config
  46. self.logger = bot.tasks.logger.getChild(self.name)
  47. self.setup()
  48. def setup(self):
  49. """Hook called immediately after the task is loaded.
  50. Does nothing by default; feel free to override.
  51. """
  52. pass
  53. def run(self, **kwargs):
  54. """Main entry point to run a given task.
  55. This is called directly by tasks.start() and is the main way to make a
  56. task do stuff. kwargs will be any keyword arguments passed to start()
  57. which are entirely optional.
  58. The same task instance is preserved between runs, so you can
  59. theoretically store data in self (e.g.
  60. start('mytask', action='store', data='foo')) and then use it later
  61. (e.g. start('mytask', action='save')).
  62. """
  63. pass
  64. def make_summary(self, comment):
  65. """Makes an edit summary by filling in variables in a config value.
  66. config.wiki["summary"] is used, where $2 is replaced by the main
  67. summary body, given as a method arg, and $1 is replaced by the task
  68. number.
  69. If the config value is not found, we just return the arg as-is.
  70. """
  71. try:
  72. summary = self.bot.config.wiki["summary"]
  73. except KeyError:
  74. return comment
  75. return summary.replace("$1", str(self.number)).replace("$2", comment)
  76. def shutoff_enabled(self, site=None):
  77. """Returns whether on-wiki shutoff is enabled for this task.
  78. We check a certain page for certain content. This is determined by
  79. our config file: config.wiki["shutoff"]["page"] is used as the title,
  80. with $1 replaced by our username and $2 replaced by the task number,
  81. and config.wiki["shutoff"]["disabled"] is used as the content.
  82. If the page has that content or the page does not exist, then shutoff
  83. is "disabled", meaning the bot is supposed to run normally, and we
  84. return False. If the page's content is something other than what we
  85. expect, shutoff is enabled, and we return True.
  86. If a site is not provided, we'll try to use self.site if it's set.
  87. Otherwise, we'll use our default site.
  88. """
  89. if not site:
  90. try:
  91. site = self.site
  92. except AttributeError:
  93. site = self.bot.wiki.get_site()
  94. try:
  95. cfg = self.config.wiki["shutoff"]
  96. except KeyError:
  97. return False
  98. title = cfg.get("page", "User:$1/Shutoff/Task $2")
  99. username = site.get_user().name
  100. title = title.replace("$1", username).replace("$2", str(self.number))
  101. page = site.get_page(title)
  102. try:
  103. content = page.get()
  104. except exceptions.PageNotFoundError:
  105. return False
  106. if content == cfg.get("disabled", "run"):
  107. return False
  108. self.logger.warn("Emergency task shutoff has been enabled!")
  109. return True