A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

220 строки
9.1 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  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 threading import Lock, Thread, enumerate as enumerate_threads
  24. from time import gmtime, sleep
  25. from earwigbot import __version__
  26. from earwigbot.config import BotConfig
  27. from earwigbot.irc import Frontend, Watcher
  28. from earwigbot.managers import CommandManager, TaskManager
  29. from earwigbot.wiki import SitesDB
  30. __all__ = ["Bot"]
  31. class Bot(object):
  32. """
  33. **EarwigBot: Main Bot Class**
  34. The :py:class:`Bot` class is the core of EarwigBot, essentially responsible
  35. for starting the various bot components and making sure they are all happy.
  36. EarwigBot has three components that can run independently of each other: an
  37. IRC front-end, an IRC watcher, and a wiki scheduler.
  38. - The IRC front-end runs on a normal IRC server and expects users to
  39. interact with it/give it commands.
  40. - The IRC watcher runs on a wiki recent-changes server and listens for
  41. edits. Users cannot interact with this part of the bot.
  42. - The wiki scheduler runs wiki-editing bot tasks in separate threads at
  43. user-defined times through a cron-like interface.
  44. The :py:class:`Bot` object is accessible from within commands and tasks as
  45. :py:attr:`self.bot`. This is the primary way to access data from other
  46. components of the bot. For example, our
  47. :py:class:`~earwigbot.config.BotConfig` object is accessable from
  48. :py:attr:`bot.config`, tasks can be started with
  49. :py:meth:`bot.tasks.start() <earwigbot.managers.TaskManager.start>`, and
  50. sites can be loaded from the wiki toolset with
  51. :py:meth:`bot.wiki.get_site() <earwigbot.wiki.sitesdb.SitesDB.get_site>`.
  52. """
  53. def __init__(self, root_dir, level=logging.INFO):
  54. self.config = BotConfig(self, root_dir, level)
  55. self.logger = logging.getLogger("earwigbot")
  56. self.commands = CommandManager(self)
  57. self.tasks = TaskManager(self)
  58. self.wiki = SitesDB(self)
  59. self.frontend = None
  60. self.watcher = None
  61. self.component_lock = Lock()
  62. self._keep_looping = True
  63. self.config.load()
  64. self.commands.load()
  65. self.tasks.load()
  66. def __repr__(self):
  67. """Return the canonical string representation of the Bot."""
  68. return "Bot(config={0!r})".format(self.config)
  69. def __str__(self):
  70. """Return a nice string representation of the Bot."""
  71. return "<Bot at {0}>".format(self.config.root_dir)
  72. def _dispatch_irc_component(self, name, klass):
  73. """Create a new IRC component, record it internally, and start it."""
  74. component = klass(self)
  75. setattr(self, name, component)
  76. Thread(name="irc_" + name, target=component.loop).start()
  77. def _start_irc_components(self):
  78. """Start the IRC frontend/watcher in separate threads if enabled."""
  79. if self.config.components.get("irc_frontend"):
  80. self.logger.info("Starting IRC frontend")
  81. self._dispatch_irc_component("frontend", Frontend)
  82. if self.config.components.get("irc_watcher"):
  83. self.logger.info("Starting IRC watcher")
  84. self._dispatch_irc_component("watcher", Watcher)
  85. def _start_wiki_scheduler(self):
  86. """Start the wiki scheduler in a separate thread if enabled."""
  87. def wiki_scheduler():
  88. run_at = 15
  89. while self._keep_looping:
  90. self.tasks.schedule()
  91. sleep(60 + run_at - gmtime().tm_sec)
  92. if self.config.components.get("wiki_scheduler"):
  93. self.logger.info("Starting wiki scheduler")
  94. thread = Thread(name="wiki_scheduler", target=wiki_scheduler)
  95. thread.daemon = True # Stop if other threads stop
  96. thread.start()
  97. def _keep_irc_component_alive(self, name, klass):
  98. """Ensure that IRC components stay connected, else restart them."""
  99. component = getattr(self, name)
  100. if component:
  101. component.keep_alive()
  102. if component.is_stopped():
  103. log = "IRC {0} has stopped; restarting".format(name)
  104. self.logger.warn(log)
  105. self._dispatch_irc_component(name, klass)
  106. def _stop_irc_components(self, msg):
  107. """Request the IRC frontend and watcher to stop if enabled."""
  108. if self.frontend:
  109. self.frontend.stop(msg)
  110. if self.watcher:
  111. self.watcher.stop(msg)
  112. def _stop_daemon_threads(self):
  113. """Notify the user of which threads are going to be killed.
  114. Unfortunately, there is no method right now of stopping command and
  115. task threads safely. This is because there is no way to tell them to
  116. stop like the IRC components can be told; furthermore, they are run as
  117. daemons, and daemon threads automatically stop without calling any
  118. __exit__ or try/finally code when all non-daemon threads stop. They
  119. were originally implemented as regular non-daemon threads, but this
  120. meant there was no way to completely stop the bot if tasks were
  121. running, because all other threads would exit and threading would
  122. absorb KeyboardInterrupts.
  123. The advantage of this is that stopping the bot is truly guarenteed to
  124. *stop* the bot, while the disadvantage is that the threads are given no
  125. advance warning of their forced shutdown.
  126. """
  127. tasks = []
  128. component_names = self.config.components.keys()
  129. skips = component_names + ["MainThread", "reminder", "irc:quit"]
  130. for thread in enumerate_threads():
  131. if thread.name not in skips and thread.is_alive():
  132. tasks.append(thread.name)
  133. if tasks:
  134. log = "The following commands or tasks will be killed: {0}"
  135. self.logger.warn(log.format(" ".join(tasks)))
  136. @property
  137. def is_running(self):
  138. """Whether or not the bot is currently running.
  139. This may return ``False`` even if the bot is still technically active,
  140. but in the process of shutting down.
  141. """
  142. return self._keep_looping
  143. def run(self):
  144. """Main entry point into running the bot.
  145. Starts all config-enabled components and then enters an idle loop,
  146. ensuring that all components remain online and restarting components
  147. that get disconnected from their servers.
  148. """
  149. self.logger.info("Starting bot (EarwigBot {0})".format(__version__))
  150. self._start_irc_components()
  151. self._start_wiki_scheduler()
  152. while self._keep_looping:
  153. with self.component_lock:
  154. self._keep_irc_component_alive("frontend", Frontend)
  155. self._keep_irc_component_alive("watcher", Watcher)
  156. sleep(2)
  157. def restart(self, msg=None):
  158. """Reload config, commands, tasks, and safely restart IRC components.
  159. This is thread-safe, and it will gracefully stop IRC components before
  160. reloading anything. Note that you can safely reload commands or tasks
  161. without restarting the bot with :py:meth:`bot.commands.load()
  162. <earwigbot.managers._ResourceManager.load>` or
  163. :py:meth:`bot.tasks.load() <earwigbot.managers._ResourceManager.load>`.
  164. These should not interfere with running components or tasks.
  165. If given, *msg* will be used as our quit message.
  166. """
  167. if msg:
  168. self.logger.info('Restarting bot ("{0}")'.format(msg))
  169. else:
  170. self.logger.info("Restarting bot")
  171. with self.component_lock:
  172. self._stop_irc_components(msg)
  173. self.config.load()
  174. self.commands.load()
  175. self.tasks.load()
  176. self._start_irc_components()
  177. def stop(self, msg=None):
  178. """Gracefully stop all bot components.
  179. If given, *msg* will be used as our quit message.
  180. """
  181. if msg:
  182. self.logger.info('Stopping bot ("{0}")'.format(msg))
  183. else:
  184. self.logger.info("Stopping bot")
  185. with self.component_lock:
  186. self._stop_irc_components(msg)
  187. self._keep_looping = False
  188. self._stop_daemon_threads()