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 # Import confparse Exception
14 from confparse
import Exception as confparseException
20 mod
= load_source(module
, module
)
21 except ImportError, e
:
22 logger
.error('load(): unable to load module %s: %s' % (module
, e
))
25 # Check to make sure the module has init/fini functions.
26 if not hasattr(mod
, 'module_init'):
27 logger
.error('load(): unable to use module %s: no entry point has been defined' % mod
.__name
__)
30 if not hasattr(mod
, 'module_fini'):
31 logger
.error('load(): unable to use module %s: no exit point has been defined' % mod
.__name
__)
35 logger
.info('load(): module %s loaded' % mod
.__name
__)
37 # Add the module to the loaded modules list.
38 var
.modules_loaded
.append(mod
)
39 event
.dispatch('OnModuleLoad', module
)
42 '''Unload a module.'''
44 # Make sure it is in the modules loaded list.
45 if module
not in var
.modules_loaded
:
46 logger
.warning('unload(): %s is not in the loaded modules list' % module
)
51 # Remove the module from the loaded modules list.
52 var
.modules_loaded
.remove(module
)
53 event
.dispatch('OnModuleUnload', module
)
56 '''Load all modules listed in the configuration.'''
59 for i
in var
.conf
.get('module'):
65 event
.dispatch('OnLoadAllModules', name
)
66 except confparseException
:
67 pass # They don't have any modules in the configuration.
70 '''Unload all loaded modules.'''
72 for i
in var
.modules_loaded
:
75 event
.dispatch('OnUnloadAllModules')