A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # A module to manage IRC commands.
  3. import os
  4. import traceback
  5. commands = []
  6. def init_commands(connection):
  7. """load all valid command classes from irc/commmands/ into the commands variable"""
  8. files = os.listdir(os.path.join("irc", "commands")) # get all files in irc/commands/
  9. files.sort() # alphabetically sort list of files
  10. for f in files:
  11. if f.startswith("_") or not f.endswith(".py"): # ignore non-python files or files beginning with "_"
  12. continue
  13. module = f[:-3] # strip .py from end
  14. try:
  15. exec "from irc.commands import %s" % module
  16. except: # importing the file failed for some reason...
  17. print "Couldn't load file %s:" % f
  18. traceback.print_exc()
  19. continue
  20. m = eval(module) # 'module' is a string, so get the actual object for processing
  21. process_module(connection, m)
  22. pretty_cmnds = map(lambda c: c.__class__.__name__, commands)
  23. print "Found %s command classes: %s." % (len(commands), ', '.join(pretty_cmnds))
  24. def process_module(connection, module):
  25. """go through all objects in a module and add valid command classes to the commands variable"""
  26. global commands
  27. objects = dir(module)
  28. for this_obj in objects: # go through everything in the file
  29. obj = eval("module.%s" % this_obj) # this_obj is a string, so get the actual object corresponding to that string
  30. try:
  31. bases = obj.__bases__
  32. except AttributeError: # object isn't a valid class, so ignore it
  33. continue
  34. for base in bases:
  35. if base.__name__ == "BaseCommand": # this inherits BaseCommand, so it must be a command class
  36. command = obj(connection) # initialize a new command object
  37. commands.append(command)
  38. print "Added command class %s from %s..." % (this_obj, module.__name__)
  39. continue
  40. def get_commands():
  41. """get our commands"""
  42. return commands
  43. def check(hook, data):
  44. """given an event on IRC, check if there's anything we can respond to by calling each command class"""
  45. data.parse_args() # parse command arguments into data.command and data.args
  46. for command in commands:
  47. if hook in command.get_hooks():
  48. if command.check(data):
  49. command.process(data)
  50. break