Merge branch 'master' into 1.1
[synarere.git] / core / event.py
blob27f66adf99b209c0648b77ed51cb25dd04de2545
1 # synarere -- a highly modular and stable IRC bot.
2 # Copyright (C) 2010 Michael Rodriguez.
3 # Rights to this code are documented in docs/LICENSE.
5 '''Event system for non-IRC events.'''
7 # Import required core module.
8 import logger
10 # Event data.
11 events = {}
13 def dispatch(name, *args):
14 '''Dispatch the event.'''
16 global events
18 # Call every function attached to 'name'.
19 try:
20 for func in events[name]['funcs']:
21 func(*args)
22 except KeyError:
23 pass
25 def attach(event, func):
26 '''Add a function to an event.'''
28 global events
30 try:
31 test = events[event]
32 except KeyError:
33 events[event] = { 'funcs' : [] }
35 if func in events[event]['funcs']:
36 return True
38 events[event]['funcs'].append(func)
39 return True
41 logger.debug('attach(): attached event %s to %s' % (event, func))
43 def detach(event, func):
44 '''Remove a function from an event.'''
46 global events
48 if func not in events[event]['funcs']:
49 return False
51 events[event]['funcs'].remove(func)
53 logger.debug('detach(): detached event %s from %s' % (event, func))