basemodule.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from event import Event
  2. class BaseModule(object):
  3. """
  4. A base module class for deriving modules (anything you fine folk write, probably) to inherit from.
  5. The nice this is this allows you to define your own post_init and handle functions.
  6. In your module's post_init, define and register your own events, and pass your module in.
  7. .. code-block:: python
  8. def MyModule(BaseModule):
  9. def post_init(self):
  10. e = Event("__wee__")
  11. e.define("foo")
  12. self.bot.register_event(e,self)
  13. Bam, you've got the things you need (a bot handle, mostly) and by extending BaseModuleyou implement the right things to be called without error.
  14. Elzar.
  15. """
  16. def __init__(self, events=None, printer_handle=None, bot=None, say=None):
  17. """
  18. This is called by load_modules for every .py in the modules directory. Your module must implement this, one way or the other, either directly,
  19. by implementing this function signature, or indirectly, by inheriting from BaseModule. I suggest the latter.
  20. """
  21. self.events = events
  22. self.printer = printer_handle
  23. self.interests = []
  24. self.bot = bot
  25. self.say = say
  26. # IMPORTANT: you must subscribe to events before you add your own below, or you'll subscribe twice.
  27. # register ourself for any events that we're interested in that exist already
  28. for event in events:
  29. if event._type in self.interests:
  30. event.subscribe(self)
  31. self.help = None
  32. self.post_init()
  33. def handle(self, event):
  34. pass
  35. def post_init(self):
  36. """
  37. Called after init is set up and builds out our basic module's needs. Allows you to do your own post-processing when inheriting from BaseModule.
  38. """
  39. pass