A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

342 rindas
12 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 YAML Config File Parser
  24. This handles all tasks involving reading and writing to our config file,
  25. including encrypting and decrypting passwords and making a new config file from
  26. scratch at the inital bot run.
  27. Usually you'll just want to do "from earwigbot.config import config", which
  28. returns a singleton _BotConfig object, with data accessible from various
  29. attributes and functions:
  30. * config.components - enabled components
  31. * config.wiki - information about wiki-editing
  32. * config.tasks - information for bot tasks
  33. * config.irc - information about IRC
  34. * config.metadata - miscellaneous information
  35. * config.schedule() - tasks scheduled to run at a given time
  36. Additionally, _BotConfig has some functions used in config loading:
  37. * config.load() - loads and parses our config file, returning True if
  38. passwords are stored encrypted or False otherwise
  39. * config.decrypt() - given a key, decrypts passwords inside our config
  40. variables; won't work if passwords aren't encrypted
  41. """
  42. import logging
  43. import logging.handlers
  44. from os import mkdir, path
  45. import yaml
  46. from earwigbot import blowfish
  47. __all__ = ["config"]
  48. class _ConfigNode(object):
  49. def __iter__(self):
  50. for key in self.__dict__.iterkeys():
  51. yield key
  52. def __getitem__(self, item):
  53. return self.__dict__.__getitem__(item)
  54. def _dump(self):
  55. data = self.__dict__.copy()
  56. for key, val in data.iteritems():
  57. if isinstance(val, _ConfigNode):
  58. data[key] = val._dump()
  59. return data
  60. def _load(self, data):
  61. self.__dict__ = data.copy()
  62. def _decrypt(self, key, intermediates, item):
  63. base = self.__dict__
  64. try:
  65. for inter in intermediates:
  66. base = base[inter]
  67. except KeyError:
  68. return
  69. if item in base:
  70. base[item] = blowfish.decrypt(key, base[item])
  71. def get(self, *args, **kwargs):
  72. return self.__dict__.get(*args, **kwargs)
  73. class _BotConfig(object):
  74. def __init__(self):
  75. self._script_dir = path.dirname(path.abspath(__file__))
  76. self._root_dir = path.split(self._script_dir)[0]
  77. self._config_path = path.join(self._root_dir, "config.yml")
  78. self._log_dir = path.join(self._root_dir, "logs")
  79. self._decryption_key = None
  80. self._data = None
  81. self._components = _ConfigNode()
  82. self._wiki = _ConfigNode()
  83. self._tasks = _ConfigNode()
  84. self._irc = _ConfigNode()
  85. self._metadata = _ConfigNode()
  86. self._nodes = [self._components, self._wiki, self._tasks, self._irc,
  87. self._metadata]
  88. def _load(self):
  89. """Load data from our JSON config file (config.yml) into _config."""
  90. filename = self._config_path
  91. with open(filename, 'r') as fp:
  92. try:
  93. self._data = yaml.load(fp)
  94. except yaml.YAMLError as error:
  95. print "Error parsing config file {0}:".format(filename)
  96. print error
  97. exit(1)
  98. def _setup_logging(self):
  99. """Configures the logging module so it works the way we want it to."""
  100. log_dir = self._log_dir
  101. logger = logging.getLogger("earwigbot")
  102. logger.setLevel(logging.DEBUG)
  103. if self.metadata.get("enableLogging"):
  104. hand = logging.handlers.TimedRotatingFileHandler
  105. formatter = _BotFormatter()
  106. color_formatter = _BotFormatter(color=True)
  107. logfile = lambda f: path.join(log_dir, f)
  108. if not path.isdir(log_dir):
  109. if not path.exists(log_dir):
  110. mkdir(log_dir, 0700)
  111. else:
  112. msg = "log_dir ({0}) exists but is not a directory!"
  113. print msg.format(log_dir)
  114. exit(1)
  115. main_handler = hand(logfile("bot.log"), "midnight", 1, 7)
  116. error_handler = hand(logfile("error.log"), "W6", 1, 4)
  117. debug_handler = hand(logfile("debug.log"), "H", 1, 6)
  118. main_handler.setLevel(logging.INFO)
  119. error_handler.setLevel(logging.WARNING)
  120. debug_handler.setLevel(logging.DEBUG)
  121. for h in (main_handler, error_handler, debug_handler):
  122. h.setFormatter(formatter)
  123. logger.addHandler(h)
  124. stream_handler = logging.StreamHandler()
  125. stream_handler.setLevel(logging.DEBUG)
  126. stream_handler.setFormatter(color_formatter)
  127. logger.addHandler(stream_handler)
  128. else:
  129. logger.addHandler(logging.NullHandler())
  130. def _make_new(self):
  131. """Make a new config file based on the user's input."""
  132. encrypt = raw_input("Would you like to encrypt passwords stored in config.yml? [y/n] ")
  133. if encrypt.lower().startswith("y"):
  134. is_encrypted = True
  135. else:
  136. is_encrypted = False
  137. return is_encrypted
  138. @property
  139. def script_dir(self):
  140. return self._script_dir
  141. @property
  142. def root_dir(self):
  143. return self._root_dir
  144. @property
  145. def path(self):
  146. return self._config_path
  147. @property
  148. def log_dir(self):
  149. return self._log_dir
  150. @property
  151. def data(self):
  152. """The entire config file."""
  153. return self._data
  154. @property
  155. def components(self):
  156. """A dict of enabled components."""
  157. return self._components
  158. @property
  159. def wiki(self):
  160. """A dict of information about wiki-editing."""
  161. return self._wiki
  162. @property
  163. def tasks(self):
  164. """A dict of information for bot tasks."""
  165. return self._tasks
  166. @property
  167. def irc(self):
  168. """A dict of information about IRC."""
  169. return self._irc
  170. @property
  171. def metadata(self):
  172. """A dict of miscellaneous information."""
  173. return self._metadata
  174. def is_loaded(self):
  175. """Return True if our config file has been loaded, otherwise False."""
  176. return self._data is not None
  177. def is_encrypted(self):
  178. """Return True if passwords are encrypted, otherwise False."""
  179. return self.metadata.get("encryptPasswords", False)
  180. def load(self, config_path=None, log_dir=None):
  181. """Load, or reload, our config file.
  182. First, check if we have a valid config file, and if not, notify the
  183. user. If there is no config file at all, offer to make one, otherwise
  184. exit.
  185. Store data from our config file in five _ConfigNodes (components,
  186. wiki, tasks, irc, metadata) for easy access (as well as the internal
  187. _data variable).
  188. If everything goes well, return True if stored passwords are
  189. encrypted in the file, or False if they are not.
  190. """
  191. if config_path:
  192. self._config_path = config_path
  193. if log_dir:
  194. self._log_dir = log_dir
  195. if not path.exists(self._config_path):
  196. print "You haven't configured the bot yet!"
  197. choice = raw_input("Would you like to do this now? [y/n] ")
  198. if choice.lower().startswith("y"):
  199. return self._make_new()
  200. else:
  201. exit(1)
  202. self._load()
  203. data = self._data
  204. self.components._load(data.get("components", {}))
  205. self.wiki._load(data.get("wiki", {}))
  206. self.tasks._load(data.get("tasks", {}))
  207. self.irc._load(data.get("irc", {}))
  208. self.metadata._load(data.get("metadata", {}))
  209. self._setup_logging()
  210. return self.is_encrypted()
  211. def decrypt(self, node, *nodes):
  212. """Use self._decryption_key to decrypt an object in our config tree.
  213. If this is called when passwords are not encrypted (check with
  214. config.is_encrypted()), nothing will happen.
  215. An example usage would be:
  216. config.decrypt(config.irc, "frontend", "nickservPassword")
  217. """
  218. if not self.is_encrypted():
  219. return
  220. try:
  221. node._decrypt(self._decryption_key, nodes[:-1], nodes[-1])
  222. except blowfish.BlowfishError as error:
  223. print "\nError decrypting passwords:"
  224. print "{0}: {1}.".format(error.__class__.__name__, error)
  225. exit(1)
  226. def schedule(self, minute, hour, month_day, month, week_day):
  227. """Return a list of tasks scheduled to run at the specified time.
  228. The schedule data comes from our config file's 'schedule' field, which
  229. is stored as self._data["schedule"]. Call this function as
  230. config.schedule(args).
  231. """
  232. # Tasks to run this turn, each as a list of either [task_name, kwargs],
  233. # or just the task_name:
  234. tasks = []
  235. now = {"minute": minute, "hour": hour, "month_day": month_day,
  236. "month": month, "week_day": week_day}
  237. data = self._data.get("schedule", [])
  238. for event in data:
  239. do = True
  240. for key, value in now.items():
  241. try:
  242. requirement = event[key]
  243. except KeyError:
  244. continue
  245. if requirement != value:
  246. do = False
  247. break
  248. if do:
  249. try:
  250. tasks.extend(event["tasks"])
  251. except KeyError:
  252. pass
  253. return tasks
  254. class _BotFormatter(logging.Formatter):
  255. def __init__(self, color=False):
  256. self._format = super(_BotFormatter, self).format
  257. if color:
  258. fmt = "[%(asctime)s %(lvl)s] %(name)s: %(message)s"
  259. self.format = lambda record: self._format(self.format_color(record))
  260. else:
  261. fmt = "[%(asctime)s %(levelname)-8s] %(name)s: %(message)s"
  262. self.format = self._format
  263. datefmt = "%Y-%m-%d %H:%M:%S"
  264. super(_BotFormatter, self).__init__(fmt=fmt, datefmt=datefmt)
  265. def format_color(self, record):
  266. l = record.levelname.ljust(8)
  267. if record.levelno == logging.DEBUG:
  268. record.lvl = l.join(("\x1b[34m", "\x1b[0m")) # Blue
  269. if record.levelno == logging.INFO:
  270. record.lvl = l.join(("\x1b[32m", "\x1b[0m")) # Green
  271. if record.levelno == logging.WARNING:
  272. record.lvl = l.join(("\x1b[33m", "\x1b[0m")) # Yellow
  273. if record.levelno == logging.ERROR:
  274. record.lvl = l.join(("\x1b[31m", "\x1b[0m")) # Red
  275. if record.levelno == logging.CRITICAL:
  276. record.lvl = l.join(("\x1b[1m\x1b[31m", "\x1b[0m")) # Bold red
  277. return record
  278. config = _BotConfig()