forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddon.py
More file actions
303 lines (215 loc) · 9.34 KB
/
Copy pathAddon.py
File metadata and controls
303 lines (215 loc) · 9.34 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- coding: utf-8 -*-
# @author: RaNaN, mkaay
# @interface-version: 0.1
import __builtin__
import threading
import traceback
import types
import SafeEval
from pyload.Thread import AddonThread
from pyload.utils import lock
class AddonManager(object):
"""Manages addons, delegates and handles Events.
Every plugin can define events, \
but some very usefull events are called by the Core.
Contrary to overwriting addon methods you can use event listener,
which provides additional entry point in the control flow.
Only do very short tasks or use threads.
**Known Events:**
Most addon methods exists as events. These are the additional known events.
======================= ============== ==================================
Name Arguments Description
======================= ============== ==================================
download-preparing fid A download was just queued and will be prepared now.
download-start fid A plugin will immediately starts the download afterwards.
links-added links, pid Someone just added links, you are able to modify the links.
all_downloads-processed Every link was handled, pyload would idle afterwards.
all_downloads-finished Every download in queue is finished.
config-changed The config was changed via the api.
pluginConfigChanged The plugin config changed, due to api or internal process.
======================= ============== ==================================
| Notes:
| all_downloads-processed is *always* called before all_downloads-finished.
| config-changed is *always* called before pluginConfigChanged.
"""
def __init__(self, core):
self.core = core
__builtin__.addonManager = self #: needed to let addons register themself
self.plugins = []
self.pluginMap = {}
self.methods = {} #: dict of names and list of methods usable by rpc
self.events = {} #: contains events
# registering callback for config event
self.core.config.pluginCB = types.MethodType(self.dispatchEvent, "pluginConfigChanged", basestring) #@TODO: Rename event pluginConfigChanged
self.addEvent("pluginConfigChanged", self.manageAddon)
self.lock = threading.RLock()
self.createIndex()
def try_catch(func):
def new(*args):
try:
return func(*args)
except Exception, e:
args[0].core.log.error(_("Error executing addon: %s") % e)
if args[0].core.debug:
traceback.print_exc()
return new
def addRPC(self, plugin, func, doc):
plugin = plugin.rpartition(".")[2]
doc = doc.strip() if doc else ""
if plugin in self.methods:
self.methods[plugin][func] = doc
else:
self.methods[plugin] = {func: doc}
def callRPC(self, plugin, func, args, parse):
if not args:
args = ()
if parse:
args = tuple([SafeEval.const_eval(x) for x in args])
plugin = self.pluginMap[plugin]
f = getattr(plugin, func)
return f(*args)
def createIndex(self):
plugins = []
for type in ("addon", "hook"):
active = []
deactive = []
for pluginname in getattr(self.core.pluginManager, "%sPlugins" % type):
try:
if self.core.config.getPlugin("%s_%s" % (pluginname, type), "activated"):
pluginClass = self.core.pluginManager.loadClass(type, pluginname)
if not pluginClass:
continue
plugin = pluginClass(self.core, self)
plugins.append(plugin)
self.pluginMap[pluginClass.__name__] = plugin
if plugin.isActivated():
active.append(pluginClass.__name__)
else:
deactive.append(pluginname)
except Exception:
self.core.log.warning(_("Failed activating %(name)s") % {"name": pluginname})
if self.core.debug:
traceback.print_exc()
self.core.log.info(_("Activated %ss: %s") % (type, ", ".join(sorted(active))))
self.core.log.info(_("Deactivated %ss: %s") % (type, ", ".join(sorted(deactive))))
self.plugins = plugins
def manageAddon(self, plugin, name, value):
if name == "activated" and value:
self.activateAddon(plugin)
elif name == "activated" and not value:
self.deactivateAddon(plugin)
def activateAddon(self, pluginname):
# check if already loaded
for inst in self.plugins:
if inst.__class__.__name__ == pluginname:
return
pluginClass = self.core.pluginManager.loadClass("addon", pluginname)
if not pluginClass:
return
self.core.log.debug("Activate addon: %s" % pluginname)
addon = pluginClass(self.core, self)
self.plugins.append(addon)
self.pluginMap[pluginClass.__name__] = addon
addon.activate()
def deactivateAddon(self, pluginname):
for plugin in self.plugins:
if plugin.__class__.__name__ == pluginname:
addon = plugin
break
else:
return
self.core.log.debug("Deactivate addon: %s" % pluginname)
addon.deactivate()
# remove periodic call
self.core.log.debug("Removed callback: %s" % self.core.scheduler.removeJob(addon.cb))
self.plugins.remove(addon)
del self.pluginMap[addon.__class__.__name__]
@try_catch
def coreReady(self):
for plugin in self.plugins:
if plugin.isActivated():
plugin.activate()
self.dispatchEvent("addon-start")
@try_catch
def coreExiting(self):
for plugin in self.plugins:
if plugin.isActivated():
plugin.exit()
self.dispatchEvent("addon-exit")
@lock
def downloadPreparing(self, pyfile):
for plugin in self.plugins:
if plugin.isActivated():
plugin.downloadPreparing(pyfile)
self.dispatchEvent("download-preparing", pyfile)
@lock
def downloadFinished(self, pyfile):
for plugin in self.plugins:
if plugin.isActivated():
plugin.downloadFinished(pyfile)
self.dispatchEvent("download-finished", pyfile)
@lock
@try_catch
def downloadFailed(self, pyfile):
for plugin in self.plugins:
if plugin.isActivated():
plugin.downloadFailed(pyfile)
self.dispatchEvent("download-failed", pyfile)
@lock
def packageFinished(self, package):
for plugin in self.plugins:
if plugin.isActivated():
plugin.packageFinished(package)
self.dispatchEvent("package-finished", package)
@lock
def beforeReconnecting(self, ip):
for plugin in self.plugins:
plugin.beforeReconnecting(ip)
self.dispatchEvent("beforeReconnecting", ip)
@lock
def afterReconnecting(self, ip):
for plugin in self.plugins:
if plugin.isActivated():
plugin.afterReconnecting(ip)
self.dispatchEvent("afterReconnecting", ip)
def startThread(self, function, *args, **kwargs):
return AddonThread(self.core.threadManager, function, args, kwargs)
def activePlugins(self):
""" returns all active plugins """
return [x for x in self.plugins if x.isActivated()]
def getAllInfo(self):
"""returns info stored by addon plugins"""
info = {}
for name, plugin in self.pluginMap.iteritems():
if plugin.info:
# copy and convert so str
info[name] = dict(
[(x, str(y) if not isinstance(y, basestring) else y) for x, y in plugin.info.iteritems()])
return info
def getInfo(self, plugin):
info = {}
if plugin in self.pluginMap and self.pluginMap[plugin].info:
info = dict((x, str(y) if not isinstance(y, basestring) else y)
for x, y in self.pluginMap[plugin].info.iteritems())
return info
def addEvent(self, event, func):
"""Adds an event listener for event name"""
if event in self.events:
self.events[event].append(func)
else:
self.events[event] = [func]
def removeEvent(self, event, func):
"""removes previously added event listener"""
if event in self.events:
self.events[event].remove(func)
def dispatchEvent(self, event, *args):
"""dispatches event with args"""
if event in self.events:
for f in self.events[event]:
try:
f(*args)
except Exception, e:
self.core.log.warning("Error calling event handler %s: %s, %s, %s"
% (event, f, args, str(e)))
if self.core.debug:
traceback.print_exc()