A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.4 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. import logging
  23. from earwigbot.config import config
  24. from earwigbot import wiki
  25. __all__ = ["BaseTask"]
  26. class BaseTask(object):
  27. """A base class for bot tasks that edit Wikipedia."""
  28. name = None
  29. number = 0
  30. def __init__(self):
  31. """Constructor for new tasks.
  32. This is called once immediately after the task class is loaded by
  33. the task manager (in tasks._load_task()).
  34. """
  35. pass
  36. def _setup_logger(self):
  37. """Set up a basic module-level logger."""
  38. logger_name = ".".join(("earwigbot", "tasks", self.name))
  39. self.logger = logging.getLogger(logger_name)
  40. self.logger.setLevel(logging.DEBUG)
  41. def run(self, **kwargs):
  42. """Main entry point to run a given task.
  43. This is called directly by tasks.start() and is the main way to make a
  44. task do stuff. kwargs will be any keyword arguments passed to start()
  45. which are entirely optional.
  46. The same task instance is preserved between runs, so you can
  47. theoretically store data in self (e.g.
  48. start('mytask', action='store', data='foo')) and then use it later
  49. (e.g. start('mytask', action='save')).
  50. """
  51. pass
  52. def make_summary(self, comment):
  53. """Makes an edit summary by filling in variables in a config value.
  54. config.wiki["summary"] is used, where $2 is replaced by the main
  55. summary body, given as a method arg, and $1 is replaced by the task
  56. number.
  57. If the config value is not found, we just return the arg as-is.
  58. """
  59. try:
  60. summary = config.wiki["summary"]
  61. except KeyError:
  62. return comment
  63. return summary.replace("$1", str(self.number)).replace("$2", comment)
  64. def shutoff_enabled(self, site=None):
  65. """Returns whether on-wiki shutoff is enabled for this task.
  66. We check a certain page for certain content. This is determined by
  67. our config file: config.wiki["shutoff"]["page"] is used as the title,
  68. with $1 replaced by our username and $2 replaced by the task number,
  69. and config.wiki["shutoff"]["disabled"] is used as the content.
  70. If the page has that content or the page does not exist, then shutoff
  71. is "disabled", meaning the bot is supposed to run normally, and we
  72. return False. If the page's content is something other than what we
  73. expect, shutoff is enabled, and we return True.
  74. If a site is not provided, we'll try to use self.site if it's set.
  75. Otherwise, we'll use our default site.
  76. """
  77. if not site:
  78. try:
  79. site = self.site
  80. except AttributeError:
  81. site = wiki.get_site()
  82. try:
  83. cfg = config.wiki["shutoff"]
  84. except KeyError:
  85. return False
  86. title = cfg.get("page", "User:$1/Shutoff/Task $2")
  87. username = site.get_user().name()
  88. title = title.replace("$1", username).replace("$2", str(self.number))
  89. page = site.get_page(title)
  90. try:
  91. content = page.get()
  92. except wiki.PageNotFoundError:
  93. return False
  94. if content == cfg.get("disabled", "run"):
  95. return False
  96. self.logger.warn("Emergency task shutoff has been enabled!")
  97. return True