7 registered_modules = []
9 class ModuleLoadException(Exception): pass
11 def import_class(absolutename):
12 name = absolutename.split('.')
13 assert (len(name) >= 2)
14 modbase = ".".join(name[:-1])
16 m = __import__(modbase, fromlist=[classname])
17 return getattr(m,classname)
19 def register_module(module_name):
20 m = import_class(module_name)
22 if m in registered_modules:
23 logging.exception("Module already registered: "+str(module_name))
24 raise ModuleLoadException('Module already registered: %s' % (module_name))
25 registered_modules.append(m)
28 '''load in all modules and register them
29 returns the names of the modules loaded
31 for module_name in config.installed_modules:
32 module_name = 'modules.'+module_name
33 logging.debug("Loading module "+module_name)
34 register_module(module_name)
35 return registered_modules
37 def get_modules_for_item(item_state, task_metadata):
38 '''return the module classes to automate an item or empty list if one isn't found
39 This only returns the classes, and does not instansiate them. They should not be instansiated
40 until they are about to be run, as the constructor may throw an exception that must be
43 return [mod for mod in registered_modules if mod.can_automate_item(item_state,task_metadata)]
45 # Helper modules not for day to day use
47 def scan_for_mudpuppy_modules(pkgname='modules'):
48 '''returns a list of Automatia subclasses in the modules directory
49 Warning: this actually imports the things
54 for pymodname in [name for _,name,_ in pkgutil.iter_modules([pkgname])]:
56 if pymodname == 'test': continue
57 pymod = __import__(pkgname+'.'+pymodname)
58 pymod = getattr(pymod, pymodname)
59 for key in dir(pymod):
60 if key[:1] == '_': continue
61 obj = getattr(pymod, key)
62 if type(obj) == type and issubclass(obj, modules.base.Automatia):
63 if len(obj.can_handle) == 0: continue ## Abstract class
64 classes.add("%s.%s" % (pymodname,key))
65 except Exception, err:
66 logging.error("! Error importing %s.%s: %s" % (pkgname,str(pymodname).ljust(20), err))
69 def get_uninstalled_mudpuppy_modules():
70 '''get a list of mudpuppy modules not listed in the config'''
71 allmods = scan_for_mudpuppy_modules()
72 ourmods = set(config.installed_modules)
73 return allmods - ourmods