forked from ammaraskar/pyCraft
/
pluginloader.py
63 lines (53 loc) · 1.81 KB
/
pluginloader.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
import os
import imp
class PluginLoader():
path = ""
plugins = {}
listeners = []
def __init__(self, path):
self.path = path
def loadPlugins(self, parser):
for item in os.listdir(self.path):
split = os.path.splitext(item)
if (split[1] == '.py'):
name = split[0]
full_name = split[0].replace(os.path.sep, '.')
m = imp.load_module(full_name, *imp.find_module(name, [self.path]))
pluginClass = None
try:
pluginClass = getattr(m, name)()
self.plugins[name] = pluginClass
try:
pluginClass.onEnable(parser, self)
except AttributeError:
pass
try:
self.listeners.append(pluginClass.packetReceive)
except AttributeError:
pass
except AttributeError:
print "Plugin " + name + " is malformed"
def disablePlugins(self):
for plugin in self.plugins.values():
try:
plugin.onDisable()
except AttributeError:
pass
def notifyOptions(self, options):
for plugin in self.plugins.values():
try:
plugin.optionsParsed(options)
except AttributeError:
pass
def notify(self, methodName):
for plugin in self.plugins.values():
try:
getattr(plugin, methodName)()
except AttributeError:
pass
def getPlugins(self):
return self.plugins.values()
def getPlugin(self, name):
return self.plugins[name]
def getPacketListeners(self):
return self.listeners