-
Notifications
You must be signed in to change notification settings - Fork 7
/
plugin_loader.py
36 lines (31 loc) · 1.08 KB
/
plugin_loader.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
import imp
import os
MAIN_MODULE = "__init__"
class PluginNotFoundError(NameError):
pass
def get_plugins(plugin_folder):
plugins = []
possible_plugins = os.listdir(plugin_folder)
for i in possible_plugins:
location = os.path.join(plugin_folder, i)
if os.path.isdir(location) and (MAIN_MODULE + ".py") in os.listdir(location):
name = i
info = imp.find_module(MAIN_MODULE, [location])
elif not os.path.isdir(location) and location.endswith(".py"):
name = i.rpartition(".py")[0]
info = imp.find_module(name, [plugin_folder])
else:
continue
plugins.append({"name": name, "info": info})
return plugins
def get_plugin_by_name(plugin_name, plugin_folder):
for plugin in get_plugins(plugin_folder):
if plugin['name'] == plugin_name:
return load_plugin(plugin)
raise PluginNotFoundError()
def load_plugin(plugin):
try:
return imp.load_module(plugin["name"], *plugin["info"])
finally:
fp = plugin["info"][0]
if fp: fp.close()