-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginmanager.py
270 lines (228 loc) · 8.24 KB
/
pluginmanager.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'''Handles plugin importing'''
import os
import sys
from contextlib import contextmanager
class PluginHandler:
'''Abstraction over a plugin.
Given a filename, will import it and allows to control it.
'''
def __init__(self, filename):
'''constructor'''
self.name = filename.split('.')[0] #TODO basename
self.module = None
self._instance = None
self._do_import()
def _do_import(self):
'''Does the dirty stuff with __import__'''
old_syspath = sys.path
try:
sys.path = [os.curdir, 'plugins']
self.module = __import__(self.name, globals(), locals(), ['plugin'])
except ImportError, reason:
print 'error loading', self.name
print '-->', reason
finally:
sys.path = old_syspath
def instanciate(self):
'''Instanciate (if not already done).
You shouldn't need this, but you can use it for performance tweak.
'''
if self._instance:
return self._instance
try:
self._instance = self.module.Plugin()
except Exception:
self._instance = None
return self._instance
def start(self):
'''Instanciate (if necessary) and starts the plugin.
@return False if something goes wrong, else True.
'''
self.instanciate()
if not self._instance:
return False
try:
self._instance.start()
except Exception, reason:
print "errore nello start di", self.name, reason
return False
return True
def stop(self):
'''Stop the plugin, of course'''
if self._instance and self.is_active():
self._instance.stop()
def is_active(self):
'''@return True if an instance exist and is started. False otherwise'''
if not self._instance:
return False
return self._instance.is_active()
class PackageResource:
def __init__(self, base_dir, directory):
self.path = directory #'''Path to the package'''
self.base_path = base_dir
self._resources = [] #Ope ned resources
def get_resource_path(self, relative_path):
'''get the path to the required resource.
If you can, use get_resource.
@return the path if it exists or an empty string otherwise'''
abs_path = os.path.join(self.base_path, self.path, relative_path)
if os.path.exists(abs_path):
return abs_path
return ''
def use_resource(self, relative_path):
'''A ContextManager that opens a file.
If you can use it, you're reccomended to.
See self.get_resource for more info.
'''
f = get_resource(relative_path)
if not f:
return
try:
yield f
finally:
self.close_resource(relative_path)
def get_resource(self, relative_path):
'''Opens a file.
@param relative_path A path starting from the package dir
@return a file object opening relative_path if it is possible, or None
'''
file_path = self.get_resource_path(relative_path)
if not file_path:
return None
try:
f = open(file_path)
except IOError:
return None
else:
self._resources.append(f)
return f
def close_resource(self, resource):
'''Close a file.
@param resource A resource returned by get_resource
@return
'''
try:
self._resources.remove(resource)
resource.close()
except IOError:
return False
return True
def close(self):
'''everything. to be called when the plugin is stopped'''
for resource in self._resources:
self._resources.remove(resource) #TODO: check if this is buggy
resource.close()
class PackageHandler:
'''Abstraction over a plugin.
Given a directory, will import the plugin.py file inside it and allows to control it.
It will provide the plugin several utilities to work on the package
'''
def __init__(self, base_dir, directory):
'''@param directory The directory containing the package'''
self.name = directory
self.directory = directory
self.base_dir = base_dir
self._instance = None #we are not instancing it
self.module = None
self._do_import()
def _do_import(self):
'''Does the dirty stuff with __import__'''
old_syspath = sys.path
try:
sys.path = ['.', self.base_dir]
self.module = __import__(self.directory, globals(), None, ['plugin'])
self.module = self.module.plugin
except Exception, reason:
print 'error when importing package', self.name
print reason
self.module = None
finally:
sys.path = old_syspath
def instanciate(self):
'''Instanciate (if not already done).
You shouldn't need this, but you can use it for performance tweak.
'''
if self._instance is not None:
return self._instance
try:
self._instance = self.module.Plugin()
except Exception:
self._instance = None
else:
self._instance.resource = PackageResource(self.base_dir, self.directory)
return self._instance
def start(self):
'''Instanciate (if necessary) and starts the plugin.
@return False if something goes wrong, else True.
'''
inst = self.instanciate()
if not inst:
return False
try:
inst.category_register()
inst.start()
inst.extension_register()
except Exception, reason:
print "errore nello start di", self.name, reason
return False
return True
def stop(self):
'''If active, stop the plugin'''
if self.is_active():
self._instance.stop()
self._instance.resource.close()
def is_active(self):
'''@return True if an instance exist and is started. False otherwise'''
if not self._instance:
return False
return self._instance.is_active()
class PluginManager:
'''Scan directories and manage plugins loading/unloading/control'''
def __init__(self):
self._plugins = {} #'name': Plugin/Package
def scan_directory(self, dir_):
'''Find plugins and packages inside dir_'''
dirs = files = []
for root, directories, files in os.walk(dir_):
dirs = directories
files = files
break #sooo ugly
for directory in [x for x in dirs if not x.startswith('.')]:
try:
mod = PackageHandler(dir_, directory)
self._plugins[mod.name] = mod
except Exception, reason:
print 'Exception while importing %s:\n%s' % (directory, reason)
for filename in [x for x in files if x.endswith('.py')]:
try:
mod = PluginHandler(filename)
self._plugins[mod.name] = mod
except Exception, reason:
print 'Exception while importing %s:\n%s' % (filename, reason)
def plugin_start(self, name):
'''Starts a plugin.
@param name The name of the plugin. See plugin_base.PluginBase.name.
'''
if not name in self._plugins:
return False
self._plugins[name].start()
return True
def plugin_stop(self, name):
'''Stops a plugin.
@param name The name of the plugin. See plugin_base.PluginBase.name.
'''
if not name in self._plugins:
return False
self._plugins[name].stop()
return True
def plugin_is_active(self, name):
'''Check if a plugin is active.
@param name The name of the plugin. See plugin_base.PluginBase.name.
@return True if loaded and active, else False.
'''
if not name in self._plugins:
return False
self._plugins[name].is_active()
return True
def get_plugins(self):
return self._plugins.keys()