A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

400 wiersze
14 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 getpass import getpass
  23. from hashlib import sha256
  24. import logging
  25. import logging.handlers
  26. from os import mkdir, path
  27. from Crypto.Cipher import Blowfish
  28. import yaml
  29. from earwigbot.exceptions import NoConfigError
  30. __all__ = ["BotConfig"]
  31. class BotConfig(object):
  32. """
  33. **EarwigBot: YAML Config File Manager**
  34. This handles all tasks involving reading and writing to our config file,
  35. including encrypting and decrypting passwords and making a new config file
  36. from scratch at the inital bot run.
  37. BotConfig has a few attributes and methods, including the following:
  38. - :py:attr:`root_dir`: bot's working directory; contains
  39. :file:`config.yml`, :file:`logs/`
  40. - :py:attr:`path`: path to the bot's config file
  41. - :py:attr:`components`: enabled components
  42. - :py:attr:`wiki`: information about wiki-editing
  43. - :py:attr:`irc`: information about IRC
  44. - :py:attr:`commands`: information about IRC commands
  45. - :py:attr:`tasks`: information for bot tasks
  46. - :py:attr:`metadata`: miscellaneous information
  47. - :py:meth:`schedule`: tasks scheduled to run at a given time
  48. BotConfig also has some methods used in config loading:
  49. - :py:meth:`load`: loads (or reloads) and parses our config file
  50. - :py:meth:`decrypt`: decrypts an object in the config tree
  51. """
  52. def __init__(self, root_dir, level):
  53. self._root_dir = root_dir
  54. self._logging_level = level
  55. self._config_path = path.join(self._root_dir, "config.yml")
  56. self._log_dir = path.join(self._root_dir, "logs")
  57. self._decryption_cipher = None
  58. self._data = None
  59. self._components = _ConfigNode()
  60. self._wiki = _ConfigNode()
  61. self._irc = _ConfigNode()
  62. self._commands = _ConfigNode()
  63. self._tasks = _ConfigNode()
  64. self._metadata = _ConfigNode()
  65. self._nodes = [self._components, self._wiki, self._irc, self._commands,
  66. self._tasks, self._metadata]
  67. self._decryptable_nodes = [ # Default nodes to decrypt
  68. (self._wiki, ("password",)),
  69. (self._wiki, ("search", "credentials", "key")),
  70. (self._wiki, ("search", "credentials", "secret")),
  71. (self._irc, ("frontend", "nickservPassword")),
  72. (self._irc, ("watcher", "nickservPassword")),
  73. ]
  74. def _load(self):
  75. """Load data from our JSON config file (config.yml) into self._data."""
  76. filename = self._config_path
  77. with open(filename, 'r') as fp:
  78. try:
  79. self._data = yaml.load(fp)
  80. except yaml.YAMLError:
  81. print "Error parsing config file {0}:".format(filename)
  82. raise
  83. def _setup_logging(self):
  84. """Configures the logging module so it works the way we want it to."""
  85. log_dir = self._log_dir
  86. logger = logging.getLogger("earwigbot")
  87. logger.handlers = [] # Remove any handlers already attached to us
  88. logger.setLevel(logging.DEBUG)
  89. if self.metadata.get("enableLogging"):
  90. hand = logging.handlers.TimedRotatingFileHandler
  91. formatter = _BotFormatter()
  92. color_formatter = _BotFormatter(color=True)
  93. logfile = lambda f: path.join(log_dir, f)
  94. if not path.isdir(log_dir):
  95. if not path.exists(log_dir):
  96. mkdir(log_dir, 0700)
  97. else:
  98. msg = "log_dir ({0}) exists but is not a directory!"
  99. print msg.format(log_dir)
  100. return
  101. main_handler = hand(logfile("bot.log"), "midnight", 1, 7)
  102. error_handler = hand(logfile("error.log"), "W6", 1, 4)
  103. debug_handler = hand(logfile("debug.log"), "H", 1, 6)
  104. main_handler.setLevel(logging.INFO)
  105. error_handler.setLevel(logging.WARNING)
  106. debug_handler.setLevel(logging.DEBUG)
  107. for h in (main_handler, error_handler, debug_handler):
  108. h.setFormatter(formatter)
  109. logger.addHandler(h)
  110. self._stream_handler = stream = logging.StreamHandler()
  111. stream.setLevel(self._logging_level)
  112. stream.setFormatter(color_formatter)
  113. logger.addHandler(stream)
  114. def _decrypt(self, node, nodes):
  115. """Try to decrypt the contents of a config node. Use self.decrypt()."""
  116. try:
  117. node._decrypt(self._decryption_cipher, nodes[:-1], nodes[-1])
  118. except ValueError:
  119. print "Error decrypting passwords:"
  120. raise
  121. def _make_new(self):
  122. """Make a new config file based on the user's input."""
  123. #m = "Would you like to encrypt passwords stored in config.yml? [y/n] "
  124. #encrypt = raw_input(m)
  125. #if encrypt.lower().startswith("y"):
  126. # is_encrypted = True
  127. #else:
  128. # is_encrypted = False
  129. raise NotImplementedError()
  130. # yaml.dumps() config.yml file (self._config_path)
  131. # Create root_dir/, root_dir/commands/, root_dir/tasks/
  132. # Give a reasonable message after config has been created regarding
  133. # what to do next...
  134. @property
  135. def root_dir(self):
  136. """The bot's root directory containing its config file and more."""
  137. return self._root_dir
  138. @property
  139. def logging_level(self):
  140. """The minimum logging level for messages logged via stdout."""
  141. return self._logging_level
  142. @logging_level.setter
  143. def logging_level(self, level):
  144. self._logging_level = level
  145. self._stream_handler.setLevel(level)
  146. @property
  147. def path(self):
  148. """The path to the bot's config file."""
  149. return self._config_path
  150. @property
  151. def log_dir(self):
  152. """The directory containing the bot's logs."""
  153. return self._log_dir
  154. @property
  155. def data(self):
  156. """The entire config file as a decoded JSON object."""
  157. return self._data
  158. @property
  159. def components(self):
  160. """A dict of enabled components."""
  161. return self._components
  162. @property
  163. def wiki(self):
  164. """A dict of information about wiki-editing."""
  165. return self._wiki
  166. @property
  167. def irc(self):
  168. """A dict of information about IRC."""
  169. return self._irc
  170. @property
  171. def commands(self):
  172. """A dict of information for IRC commands."""
  173. return self._commands
  174. @property
  175. def tasks(self):
  176. """A dict of information for bot tasks."""
  177. return self._tasks
  178. @property
  179. def metadata(self):
  180. """A dict of miscellaneous information."""
  181. return self._metadata
  182. def is_loaded(self):
  183. """Return ``True`` if our config file has been loaded, or ``False``."""
  184. return self._data is not None
  185. def is_encrypted(self):
  186. """Return ``True`` if passwords are encrypted, otherwise ``False``."""
  187. return self.metadata.get("encryptPasswords", False)
  188. def load(self):
  189. """Load, or reload, our config file.
  190. First, check if we have a valid config file, and if not, notify the
  191. user. If there is no config file at all, offer to make one, otherwise
  192. exit.
  193. Data from the config file is stored in six
  194. :py:class:`~earwigbot.config._ConfigNode`\ s (:py:attr:`components`,
  195. :py:attr:`wiki`, :py:attr:`irc`, :py:attr:`commands`, :py:attr:`tasks`,
  196. :py:attr:`metadata`) for easy access (as well as the lower-level
  197. :py:attr:`data` attribute). If passwords are encrypted, we'll use
  198. :py:func:`~getpass.getpass` for the key and then decrypt them. If the
  199. config is being reloaded, encrypted items will be automatically
  200. decrypted if they were decrypted earlier.
  201. """
  202. if not path.exists(self._config_path):
  203. print "Config file not found:", self._config_path
  204. choice = raw_input("Would you like to create a config file now? [y/n] ")
  205. if choice.lower().startswith("y"):
  206. self._make_new()
  207. else:
  208. raise NoConfigError()
  209. self._load()
  210. data = self._data
  211. self.components._load(data.get("components", {}))
  212. self.wiki._load(data.get("wiki", {}))
  213. self.irc._load(data.get("irc", {}))
  214. self.commands._load(data.get("commands", {}))
  215. self.tasks._load(data.get("tasks", {}))
  216. self.metadata._load(data.get("metadata", {}))
  217. self._setup_logging()
  218. if self.is_encrypted():
  219. if not self._decryption_cipher:
  220. key = getpass("Enter key to decrypt bot passwords: ")
  221. self._decryption_cipher = Blowfish.new(sha256(key).digest())
  222. for node, nodes in self._decryptable_nodes:
  223. self._decrypt(node, nodes)
  224. def decrypt(self, node, *nodes):
  225. """Decrypt an object in our config tree.
  226. :py:attr:`_decryption_cipher` is used as our key, retrieved using
  227. :py:func:`~getpass.getpass` in :py:meth:`load` if it wasn't already
  228. specified. If this is called when passwords are not encrypted (check
  229. with :py:meth:`is_encrypted`), nothing will happen. We'll also keep
  230. track of this node if :py:meth:`load` is called again (i.e. to reload)
  231. and automatically decrypt it.
  232. Example usage::
  233. >>> config.decrypt(config.irc, "frontend", "nickservPassword")
  234. # decrypts config.irc["frontend"]["nickservPassword"]
  235. """
  236. signature = (node, nodes)
  237. if signature in self._decryptable_nodes:
  238. return # Already decrypted
  239. self._decryptable_nodes.append(signature)
  240. if self.is_encrypted():
  241. self._decrypt(node, nodes)
  242. def schedule(self, minute, hour, month_day, month, week_day):
  243. """Return a list of tasks scheduled to run at the specified time.
  244. The schedule data comes from our config file's ``schedule`` field,
  245. which is stored as :py:attr:`self.data["schedule"] <data>`.
  246. """
  247. # Tasks to run this turn, each as a list of either [task_name, kwargs],
  248. # or just the task_name:
  249. tasks = []
  250. now = {"minute": minute, "hour": hour, "month_day": month_day,
  251. "month": month, "week_day": week_day}
  252. data = self._data.get("schedule", [])
  253. for event in data:
  254. do = True
  255. for key, value in now.items():
  256. try:
  257. requirement = event[key]
  258. except KeyError:
  259. continue
  260. if requirement != value:
  261. do = False
  262. break
  263. if do:
  264. try:
  265. tasks.extend(event["tasks"])
  266. except KeyError:
  267. pass
  268. return tasks
  269. class _ConfigNode(object):
  270. def __iter__(self):
  271. for key in self.__dict__:
  272. yield key
  273. def __getitem__(self, item):
  274. return self.__dict__.__getitem__(item)
  275. def _dump(self):
  276. data = self.__dict__.copy()
  277. for key, val in data.iteritems():
  278. if isinstance(val, _ConfigNode):
  279. data[key] = val._dump()
  280. return data
  281. def _load(self, data):
  282. self.__dict__ = data.copy()
  283. def _decrypt(self, cipher, intermediates, item):
  284. base = self.__dict__
  285. for inter in intermediates:
  286. try:
  287. base = base[inter]
  288. except KeyError:
  289. return
  290. if item in base:
  291. ciphertext = base[item].decode("hex")
  292. base[item] = cipher.decrypt(ciphertext).rstrip("\x00")
  293. def get(self, *args, **kwargs):
  294. return self.__dict__.get(*args, **kwargs)
  295. def keys(self):
  296. return self.__dict__.keys()
  297. def values(self):
  298. return self.__dict__.values()
  299. def items(self):
  300. return self.__dict__.items()
  301. def iterkeys(self):
  302. return self.__dict__.iterkeys()
  303. def itervalues(self):
  304. return self.__dict__.itervalues()
  305. def iteritems(self):
  306. return self.__dict__.iteritems()
  307. class _BotFormatter(logging.Formatter):
  308. def __init__(self, color=False):
  309. self._format = super(_BotFormatter, self).format
  310. if color:
  311. fmt = "[%(asctime)s %(lvl)s] %(name)s: %(message)s"
  312. self.format = lambda record: self._format(self.format_color(record))
  313. else:
  314. fmt = "[%(asctime)s %(levelname)-8s] %(name)s: %(message)s"
  315. self.format = self._format
  316. datefmt = "%Y-%m-%d %H:%M:%S"
  317. super(_BotFormatter, self).__init__(fmt=fmt, datefmt=datefmt)
  318. def format_color(self, record):
  319. l = record.levelname.ljust(8)
  320. if record.levelno == logging.DEBUG:
  321. record.lvl = l.join(("\x1b[34m", "\x1b[0m")) # Blue
  322. if record.levelno == logging.INFO:
  323. record.lvl = l.join(("\x1b[32m", "\x1b[0m")) # Green
  324. if record.levelno == logging.WARNING:
  325. record.lvl = l.join(("\x1b[33m", "\x1b[0m")) # Yellow
  326. if record.levelno == logging.ERROR:
  327. record.lvl = l.join(("\x1b[31m", "\x1b[0m")) # Red
  328. if record.levelno == logging.CRITICAL:
  329. record.lvl = l.join(("\x1b[1m\x1b[31m", "\x1b[0m")) # Bold red
  330. return record