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