A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

command_handler.py 2.3 KiB

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