add misc.py with unicode stripping function.
[synarere.git] / core / module.py
blob11a044125729a88f2efa2cdfab1f07eb1466d62a
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
16 def load(module):
17 '''Load a module.'''
19 try:
20 mod = load_source(module, module)
21 except ImportError, e:
22 logger.error('load(): unable to load module %s: %s' % (module, e))
23 return
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__)
28 return
30 if not hasattr(mod, 'module_fini'):
31 logger.error('load(): unable to use module %s: no exit point has been defined' % mod.__name__)
32 return
34 mod.module_init()
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)
41 def unload(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)
47 return
49 module.module_fini()
51 # Remove the module from the loaded modules list.
52 var.modules_loaded.remove(module)
53 event.dispatch('OnModuleUnload', module)
55 def load_all():
56 '''Load all modules listed in the configuration.'''
58 try:
59 for i in var.conf.get('module'):
60 name = i.get('name')
62 if name != None:
63 load(name)
65 event.dispatch('OnLoadAllModules', name)
66 except confparseException:
67 pass # They don't have any modules in the configuration.
69 def unload_all():
70 '''Unload all loaded modules.'''
72 for i in var.modules_loaded:
73 unload(i)
75 event.dispatch('OnUnloadAllModules')