We want to pop the first item off of the deque instead of the last so we don't end...
[synarere.git] / core / module.py
blob5e6a57aea03f37027ca4996422d1b9d53db61d51
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 '''Module operations.'''
7 # Import required Python function.
8 from imp import load_source
10 # Import required core modules.
11 import logger, var, event
13 def load(module):
14 '''Load a module.'''
16 try:
17 mod = load_source(module, module)
18 except ImportError, e:
19 logger.error('Unable to load module %s: %s' % (module, e))
20 return
22 # Check to make sure the module has init/fini functions.
23 if not hasattr(mod, 'module_init'):
24 logger.error('Unable to use module %s: No entry point has been defined.' % mod.__name__)
25 return
27 if not hasattr(mod, 'module_fini'):
28 logger.error('Unable to use module %s: No exit point has been defined.' % mod.__name__)
29 return
31 mod.module_init()
32 logger.info('Module %s loaded.' % mod.__name__)
34 # Add the module to the loaded modules list.
35 var.modules_loaded.append(mod)
36 event.dispatch('OnModuleLoad', module)
38 def unload(module):
39 '''Unload a module.'''
41 # Make sure it is in the modules loaded list.
42 if module not in var.modules_loaded:
43 logger.warning('%s is not in the loaded modules list.' % module)
44 return
46 module.module_fini()
48 # Remove the module from the loaded modules list.
49 var.modules_loaded.remove(module)
50 event.dispatch('OnModuleUnload', module)
52 def load_all():
53 '''Load all modules listed in the configuration.'''
55 for i in var.conf.get('module'):
56 name = i.get('name')
58 if name != None:
59 load(name)
61 event.dispatch('OnLoadAllModules', name)
63 def unload_all():
64 '''Unload all loaded modules.'''
66 logger.info('Unloading all modules.')
68 for i in var.modules_loaded:
69 unload(i)
71 event.dispatch('OnUnloadAllModules')