-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimporter.py
58 lines (46 loc) · 1.85 KB
/
importer.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
import inspect
import logging
from os import listdir
from os.path import isfile, join, abspath, dirname, sep
import sys
from traceback import format_exception
def moduleQualifies(dir, file, excludes, includes):
return isfile(join(dir, file)) \
and file.endswith(".py") \
and file not in excludes \
and file[0] != "_" \
and (file in includes or len(includes) == 0)
def getPluginList(plugindir, excludes, includes):
basedir = dirname(abspath(inspect.getsourcefile(lambda _: None)))
dir = basedir + sep + plugindir
return [f[:-3]
for f in listdir(dir)
if moduleQualifies(dir, f, excludes, includes)]
def ImportPlugins(dir, excludes, includes, progress, matrix, config):
output = {}
excludes.append("__init__.py")
plugins = getPluginList(dir, excludes, includes)
total = len(plugins)
for index, plugin in enumerate(plugins):
try:
module = __import__(
dir+'.'+plugin,
globals(),
locals(),
["Art"],
0)
obj = module.Art(matrix, config)
desc = getattr(obj, "description", None)
if desc is not None:
logging.info("%s: %s" % (plugin, desc))
else:
logging.info("%s: [None]" % (plugin))
if progress is not None:
progress(index, total)
output[plugin] = obj
except Exception: # yes, we want to catch everything
logging.error("%s: import failed, details follow: " % (plugin))
etype, evalue, etraceback = sys.exc_info()
for line in format_exception(etype, evalue, etraceback):
logging.error(' Exception: '+line.rstrip('\n'))
return output