-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplugin_engine.py
53 lines (47 loc) · 1.3 KB
/
plugin_engine.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
#!/usr/bin/env python2.4
import os
class Provider(object):
def __init__(self, plugin_location='./plugins'):
self.__plugins = {}
if os.path.isdir(plugin_location):
print 'Loading plugins'
possible = os.listdir(plugin_location)
count = 0
for plugin_file in (filename[:-3] for filename in possible if filename != '__init__.py' and filename.endswith('.py')):
print '\tLoading %s:' % plugin_file,
#f = open(plugin_file,'rb')
#data = f.read()
#f.close()
try:
#plugin = eval(data).root
plugin = __import__('plugins.%s'%plugin_file,globals(),locals(),['plugins']).root
except AttributeError:
print 'Plugin has no root attribute'
continue
except:
print 'Error importing plugin'
raise
continue
else:
print 'Done'
self.register(plugin)
count += 1
print 'Done (%d plugins loaded)\n' % count
def register(self,plugin):
try:
plugin.onRegister()
except AttributeError:
pass
name = plugin.__class__.__name__
self.__plugins[name] = plugin
def API(self):
api = {}
for i,j in self.__plugins.items():
api[i] = dir(j)
return api
def __getattr__(self,name):
if name in self.__plugins:
return self.__plugins[name]
else:
return super(Provider,self).__getattr__(name)
plugin_engine = Provider()