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.

246 lines
9.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. """
  23. EarwigBot's Wiki Task Manager
  24. This package provides the wiki bot "tasks" EarwigBot runs. This module contains
  25. the BaseTask class (import with `from earwigbot.tasks import BaseTask`) and an
  26. internal TaskManager class. This can be accessed through `bot.tasks`.
  27. """
  28. import imp
  29. from os import listdir, path
  30. from threading import Lock, Thread
  31. from time import gmtime, strftime
  32. from earwigbot import wiki
  33. __all__ = ["BaseTask", "TaskManager"]
  34. class BaseTask(object):
  35. """A base class for bot tasks that edit Wikipedia."""
  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 = wiki.get_site()
  94. try:
  95. cfg = self.bot.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 wiki.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
  110. class TaskManager(object):
  111. def __init__(self, bot):
  112. self.bot = bot
  113. self.logger = bot.logger.getChild("tasks")
  114. self._tasks = {}
  115. self._task_access_lock = Lock()
  116. def __iter__(self):
  117. for name in self._tasks:
  118. yield name
  119. def _wrapper(self, task, **kwargs):
  120. """Wrapper for task classes: run the task and catch any errors."""
  121. try:
  122. task.run(**kwargs)
  123. except Exception:
  124. msg = "Task '{0}' raised an exception and had to stop:"
  125. self.logger.exception(msg.format(task.name))
  126. else:
  127. msg = "Task '{0}' finished without error"
  128. self.logger.info(msg.format(task.name))
  129. def _load_task(self, name, path):
  130. """Load a specific task from a module, identified by name and path.
  131. We'll first try to import it using imp magic, and if that works, make
  132. an instance of the 'Task' class inside (assuming it is an instance of
  133. BaseTask), add it to self._tasks, and log the addition. Any problems
  134. along the way will either be ignored or logged.
  135. """
  136. f, path, desc = imp.find_module(name, [path])
  137. try:
  138. module = imp.load_module(name, f, path, desc)
  139. except Exception:
  140. e = "Couldn't load module {0} from {1}"
  141. self.logger.exception(e.format(name, path))
  142. return
  143. finally:
  144. f.close()
  145. try:
  146. task_class = module.Task
  147. except AttributeError:
  148. return # No task in this module
  149. try:
  150. task = task_class(self.bot)
  151. except Exception:
  152. e = "Error initializing Task() class in {0} (from {1})"
  153. self.logger.exception(e.format(name, path))
  154. return
  155. if not isinstance(task, BaseTask):
  156. return
  157. self._tasks[task.name] = task
  158. self.logger.debug("Added task {0}".format(task.name))
  159. def load(self):
  160. """Load (or reload) all valid tasks into self._tasks."""
  161. with self._task_access_lock:
  162. self._tasks.clear()
  163. dirs = [path.join(path.dirname(__file__), "tasks"),
  164. path.join(self.bot.config.root_dir, "tasks")]
  165. for dir in dirs:
  166. files = listdir(dir)
  167. files = [sub("\.pyc?$", "", f) for f in files if f[0] != "_"]
  168. files = list(set(files)) # Remove duplicates
  169. for filename in sorted(files):
  170. self._load_task(filename)
  171. msg = "Found {0} tasks: {1}"
  172. tasks = ', '.join(self._tasks.keys())
  173. self.logger.info(msg.format(len(self._tasks), tasks))
  174. def start(self, task_name, **kwargs):
  175. """Start a given task in a new thread. kwargs are passed to task.run"""
  176. msg = "Starting task '{0}' in a new thread"
  177. self.logger.info(msg.format(task_name))
  178. with self._task_access_lock:
  179. try:
  180. task = self._tasks[task_name]
  181. except KeyError:
  182. e = "Couldn't find task '{0}':"
  183. self.logger.error(e.format(task_name))
  184. return
  185. task_thread = Thread(target=self._wrapper, args=(task,), kwargs=kwargs)
  186. start_time = strftime("%b %d %H:%M:%S")
  187. task_thread.name = "{0} ({1})".format(task_name, start_time)
  188. task_thread.start()
  189. def schedule(self, now=None):
  190. """Start all tasks that are supposed to be run at a given time."""
  191. if not now:
  192. now = gmtime()
  193. # Get list of tasks to run this turn:
  194. tasks = self.bot.config.schedule(now.tm_min, now.tm_hour, now.tm_mday,
  195. now.tm_mon, now.tm_wday)
  196. for task in tasks:
  197. if isinstance(task, list): # They've specified kwargs,
  198. self.start(task[0], **task[1]) # so pass those to start
  199. else: # Otherwise, just pass task_name
  200. self.start(task)
  201. def get(self, task_name):
  202. """Return the class instance associated with a certain task name.
  203. Will raise KeyError if the task is not found.
  204. """
  205. return self._tasks[task_name]