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

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