A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # A class to store data from an individual line received on IRC.
  3. import re
  4. class KwargParseException(Exception):
  5. """Couldn't parse a certain keyword argument in self.args, probably because
  6. it was given incorrectly: e.g., no value (abc), just a value (=xyz), just
  7. an equal sign (=), instead of the correct (abc=xyz)."""
  8. pass
  9. class Data(object):
  10. def __init__(self, line):
  11. """Store data from an individual line received on IRC."""
  12. self.line = line
  13. self.chan = str()
  14. self.nick = str()
  15. self.ident = str()
  16. self.host = str()
  17. self.msg = str()
  18. def parse_args(self):
  19. """parse command arguments from self.msg into self.command and self.args"""
  20. args = self.msg.strip().split(' ') # strip out extra whitespace and split the message into a list
  21. while '' in args: # remove any empty arguments
  22. args.remove('')
  23. self.args = args[1:] # the command arguments
  24. self.is_command = False # whether this is a real command or not
  25. try:
  26. self.command = args[0] # the command itself
  27. except IndexError:
  28. self.command = None
  29. try:
  30. if self.command.startswith('!') or self.command.startswith('.'):
  31. self.is_command = True
  32. self.command = self.command[1:] # strip '!' or '.'
  33. self.command = self.command.lower() # lowercase command name
  34. except AttributeError:
  35. pass
  36. def parse_kwargs(self):
  37. """parse command arguments from self.args, given as !command key1=value1 key2=value2..., into a dict self.kwargs: {'key1': 'value2', 'key2': 'value2'...}"""
  38. self.kwargs = {}
  39. for arg in self.args[2:]:
  40. try:
  41. key, value = re.findall("^(.*?)\=(.*?)$", arg)[0]
  42. except IndexError:
  43. raise KwargParseException(arg)
  44. if not key or not value:
  45. raise KwargParseException(arg)
  46. self.kwargs[key] = value