initial commit
[synarere.git] / core / module.py
blob7ce92b91c921ddb80d7810fe33ed8fd2c8b27a27
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
13 def load(module):
14 '''Load a module.'''
16 mod = None
18 try:
19 mod = load_source(module, module)
20 except ImportError, e:
21 logger.error('load(): unable to load module %s: %s' % (module, e))
22 return
24 # Check to make sure the module has init/fini functions.
25 if not hasattr(mod, 'module_init'):
26 logger.error('load(): unable to use module %s: no entry point has been defined' % mod.__name__)
27 return
29 if not hasattr(mod, 'module_fini'):
30 logger.error('load(): unable to use module %s: no exit point has been defined' % mod.__name__)
31 return
33 mod.module_init()
34 logger.info('load(): module %s loaded' % mod.__name__)
36 # Add the module to the loaded modules list.
37 var.modules_loaded.append(mod)
39 def unload(module):
40 '''Unload a module.'''
42 # Make sure it is in the modules loaded list.
43 if module not in var.modules_loaded:
44 logger.warning('unload(): %s is not in the loaded modules list' % module)
45 return
47 module.module_fini()
49 # Remove the module from the loaded modules list.
50 var.modules_loaded.remove(module)
52 def load_all():
53 '''Load all modules listed in the configuration.'''
55 try:
56 for i in var.conf.get('module'):
57 name = i.get('name')
59 if name != None:
60 load(name)
61 except:
62 pass # Means they dont want modules.
64 def unload_all():
65 '''Unload all loaded modules.'''
67 for i in var.modules_loaded:
68 unload(i)