A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 line
4.9 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 wiki
  23. __all__ = ["BaseTask"]
  24. class BaseTask(object):
  25. """
  26. EarwigBot's Base Bot Task
  27. This package provides built-in wiki bot "tasks" EarwigBot runs. Additional
  28. tasks can be installed as plugins in the bot's working directory.
  29. This class (import with `from earwigbot.tasks import BaseTask`) can be
  30. subclassed to create custom bot tasks.
  31. To run a task, use :py:meth:`bot.tasks.start(name, **kwargs)
  32. <earwigbot.managers.TaskManager.start>`. ``**kwargs`` get passed to the
  33. Task's run() function.
  34. """
  35. name = None
  36. number = 0
  37. def __init__(self, bot):
  38. """Constructor for new tasks.
  39. This is called once immediately after the task class is loaded by
  40. the task manager (in tasks._load_task()). Don't override this directly
  41. (or if you do, remember super(Task, self).__init()) - use setup().
  42. """
  43. self.bot = bot
  44. self.config = bot.config
  45. self.logger = bot.tasks.logger.getChild(self.name)
  46. self.setup()
  47. def setup(self):
  48. """Hook called immediately after the task is loaded.
  49. Does nothing by default; feel free to override.
  50. """
  51. pass
  52. def run(self, **kwargs):
  53. """Main entry point to run a given task.
  54. This is called directly by tasks.start() and is the main way to make a
  55. task do stuff. kwargs will be any keyword arguments passed to start()
  56. which are entirely optional.
  57. The same task instance is preserved between runs, so you can
  58. theoretically store data in self (e.g.
  59. start('mytask', action='store', data='foo')) and then use it later
  60. (e.g. start('mytask', action='save')).
  61. """
  62. pass
  63. def make_summary(self, comment):
  64. """Makes an edit summary by filling in variables in a config value.
  65. config.wiki["summary"] is used, where $2 is replaced by the main
  66. summary body, given as a method arg, and $1 is replaced by the task
  67. number.
  68. If the config value is not found, we just return the arg as-is.
  69. """
  70. try:
  71. summary = self.bot.config.wiki["summary"]
  72. except KeyError:
  73. return comment
  74. return summary.replace("$1", str(self.number)).replace("$2", comment)
  75. def shutoff_enabled(self, site=None):
  76. """Returns whether on-wiki shutoff is enabled for this task.
  77. We check a certain page for certain content. This is determined by
  78. our config file: config.wiki["shutoff"]["page"] is used as the title,
  79. with $1 replaced by our username and $2 replaced by the task number,
  80. and config.wiki["shutoff"]["disabled"] is used as the content.
  81. If the page has that content or the page does not exist, then shutoff
  82. is "disabled", meaning the bot is supposed to run normally, and we
  83. return False. If the page's content is something other than what we
  84. expect, shutoff is enabled, and we return True.
  85. If a site is not provided, we'll try to use self.site if it's set.
  86. Otherwise, we'll use our default site.
  87. """
  88. if not site:
  89. try:
  90. site = self.site
  91. except AttributeError:
  92. site = self.bot.wiki.get_site()
  93. try:
  94. cfg = self.config.wiki["shutoff"]
  95. except KeyError:
  96. return False
  97. title = cfg.get("page", "User:$1/Shutoff/Task $2")
  98. username = site.get_user().name()
  99. title = title.replace("$1", username).replace("$2", str(self.number))
  100. page = site.get_page(title)
  101. try:
  102. content = page.get()
  103. except wiki.PageNotFoundError:
  104. return False
  105. if content == cfg.get("disabled", "run"):
  106. return False
  107. self.logger.warn("Emergency task shutoff has been enabled!")
  108. return True