Skip to content

Commit 10bd079

Browse files
committed
invoke addons via context menu
1 parent 606d283 commit 10bd079

11 files changed

Lines changed: 76 additions & 58 deletions

File tree

pyload/AddonManager.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import __builtin__
1919

2020
from gettext import gettext
21-
from copy import copy
2221
from thread import start_new_thread
2322
from threading import RLock
2423

@@ -27,7 +26,7 @@
2726

2827
from types import MethodType
2928

30-
from pyload.Api import AddonService, AddonInfo
29+
from pyload.Api import AddonService, AddonInfo, ServiceException, ServiceDoesNotExist
3130
from pyload.threads.AddonThread import AddonThread
3231
from utils import lock, to_string
3332

@@ -70,14 +69,27 @@ def callInHooks(self, event, eventName, *args):
7069
self.call(inst, event, *args)
7170
self.dispatchEvent(eventName, *args)
7271

73-
def call(self, addon, f, *args):
72+
def call(self, plugin, f, *args):
7473
try:
75-
func = getattr(addon, f)
74+
func = getattr(plugin, f)
7675
return func(*args)
7776
except Exception, e:
78-
addon.logError(_("Error when executing %s" % f), e)
77+
plugin.logError(_("Error when executing %s" % f), e)
7978
self.core.print_exc()
8079

80+
def invoke(self, plugin, func_name, args):
81+
""" Invokes a registered method """
82+
83+
if plugin not in self.plugins and func_name not in self.plugins[plugin].handler:
84+
raise ServiceDoesNotExist(plugin, func_name)
85+
86+
# TODO: choose correct instance
87+
try:
88+
func = getattr(self.plugins[plugin].instances[0], func_name)
89+
return func(*args)
90+
except Exception, e:
91+
raise ServiceException(e.message)
92+
8193
@lock
8294
def createIndex(self):
8395
active = []
@@ -159,7 +171,7 @@ def deactivateAddon(self, plugin):
159171
self.log.debug("Removed callback %s" % self.core.scheduler.removeJob(addon.cb))
160172

161173
# todo: only delete instances, meta data is lost otherwise
162-
del self.plugins[addon.__name__]
174+
del self.plugins[addon.__name__].instances[:]
163175

164176
# TODO: could be improved
165177
#remove event listener

pyload/FileManager.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from pyload.utils import lock, read_lock
2323

24-
from Api import PackageStatus, DownloadStatus as DS, TreeCollection, PackageDoesNotExists
24+
from Api import PackageStatus, DownloadStatus as DS, TreeCollection, PackageDoesNotExist
2525
from datatypes.PyFile import PyFile
2626
from datatypes.PyPackage import PyPackage, RootPackage
2727

@@ -529,8 +529,8 @@ def movePackage(self, pid, root):
529529

530530
p = self.getPackageInfo(pid)
531531
dest = self.getPackageInfo(root)
532-
if not p: raise PackageDoesNotExists(pid)
533-
if not dest: raise PackageDoesNotExists(root)
532+
if not p: raise PackageDoesNotExist(pid)
533+
if not dest: raise PackageDoesNotExist(root)
534534

535535
# cantor won't be happy if we put the package in itself
536536
if pid == root or p.root == root: return False
@@ -552,7 +552,7 @@ def moveFiles(self, fids, pid):
552552
if not f or f.package == pid:
553553
return False
554554
if not self.getPackageInfo(pid):
555-
raise PackageDoesNotExists(pid)
555+
raise PackageDoesNotExist(pid)
556556

557557
# TODO move real files
558558

pyload/api/AddonApi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ def getAddonHandler(self):
3939
return handler
4040

4141
@RequirePerm(Permission.Interaction)
42-
def callAddon(self, plugin, func, arguments):
42+
def invokeAddon(self, plugin, func, func_args):
4343
""" Calls any function exposed by an addon """
44-
pass
44+
return self.core.addonManager.invoke(plugin, func, func_args)
4545

4646
@RequirePerm(Permission.Interaction)
47-
def callAddonHandler(self, plugin, func, pid_or_fid):
47+
def invokeAddonHandler(self, plugin, func, pid_or_fid):
4848
""" Calls an addon handler registered to work with packages or files """
49-
pass
49+
return self.invokeAddon(plugin, func, (pid_or_fid, ))
5050

5151

5252
if Api.extend(AddonApi):

pyload/api/FileApi.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
from pyload.Api import Api, RequirePerm, Permission, DownloadState, PackageDoesNotExists, FileDoesNotExists
4+
from pyload.Api import Api, RequirePerm, Permission, DownloadState, PackageDoesNotExist, FileDoesNotExist
55
from pyload.utils import uniqify
66

77
from ApiComponent import ApiComponent
@@ -52,26 +52,26 @@ def getPackageInfo(self, pid):
5252
"""Returns information about package, without detailed information about containing files
5353
5454
:param pid: package id
55-
:raises PackageDoesNotExists:
55+
:raises PackageDoesNotExist:
5656
:return: :class:`PackageInfo`
5757
"""
5858
info = self.core.files.getPackageInfo(pid)
5959
if not info:
60-
raise PackageDoesNotExists(pid)
60+
raise PackageDoesNotExist(pid)
6161
return info
6262

6363
@RequirePerm(Permission.All)
6464
def getFileInfo(self, fid):
6565
""" Info for specific file
6666
6767
:param fid: file id
68-
:raises FileDoesNotExists:
68+
:raises FileDoesNotExist:
6969
:return: :class:`FileInfo`
7070
7171
"""
7272
info = self.core.files.getFileInfo(fid)
7373
if not info:
74-
raise FileDoesNotExists(fid)
74+
raise FileDoesNotExist(fid)
7575
return info
7676

7777
def getFilePath(self, fid):
@@ -103,7 +103,7 @@ def updatePackage(self, pack):
103103
"""
104104
pid = pack.pid
105105
p = self.core.files.getPackage(pid)
106-
if not p: raise PackageDoesNotExists(pid)
106+
if not p: raise PackageDoesNotExist(pid)
107107

108108
#TODO: fix
109109
for key, value in data.iteritems():
@@ -124,7 +124,7 @@ def movePackage(self, pid, root):
124124
125125
:param pid: package id
126126
:param root: package id of new root
127-
:raises PackageDoesNotExists: When pid or root is missing
127+
:raises PackageDoesNotExist: When pid or root is missing
128128
:return: False if package can't be moved
129129
"""
130130
return self.core.files.movePackage(pid, root)

pyload/datatypes/PyPackage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def toInfoData(self):
6666
self.comment, self.password, self.added, self.tags, self.status, self.shared, self.packageorder
6767
)
6868

69-
def getChildren(self):
70-
"""get fids of container files"""
71-
return self.m.getPackageInfo(self.pid).fids
69+
def getFiles(self):
70+
"""get contaied files data"""
71+
return self.m.core.db.getAllFiles(package=self.pid)
7272

7373
def getPath(self, name=""):
7474
self.timestamp = time()

pyload/plugins/addons/ExtractArchive.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ def wait(self, timeout=0):
5151

5252
from pyload.utils.fs import safe_join as save_join, fs_encode
5353

54-
from pyload.plugins.Addon import Addon, threaded, AddonHandler, AddonProperty
54+
from pyload.plugins.Addon import Addon, threaded, AddonHandler
5555
from pyload.plugins.internal.AbstractExtractor import ArchiveError, CRCError, WrongPassword
5656

57+
# TODO: plugin needs a rewrite to work on unfinished packages
5758

5859
class ExtractArchive(Addon):
5960
"""
@@ -161,7 +162,7 @@ def extract(self, ids, thread=None):
161162
if not exists(out):
162163
makedirs(out)
163164

164-
files_ids = [(save_join(dl, p.folder, x["name"]), x["id"]) for x in p.getChildren().itervalues()]
165+
files_ids = [(save_join(dl, p.folder, f.name), f.fid) for f in p.getFiles().itervalues()]
165166
matched = False
166167

167168
# check as long there are unseen files

pyload/remote/apitypes.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def __init__(self, eventname=None, event_args=None):
194194
self.eventname = eventname
195195
self.event_args = event_args
196196

197-
class FileDoesNotExists(ExceptionObject):
197+
class FileDoesNotExist(ExceptionObject):
198198
__slots__ = ['fid']
199199

200200
def __init__(self, fid=None):
@@ -261,7 +261,7 @@ def __init__(self, rid=None, data=None):
261261
self.rid = rid
262262
self.data = data
263263

264-
class PackageDoesNotExists(ExceptionObject):
264+
class PackageDoesNotExist(ExceptionObject):
265265
__slots__ = ['pid']
266266

267267
def __init__(self, pid=None):
@@ -323,7 +323,7 @@ def __init__(self, speed=None, linkstotal=None, linksqueue=None, sizetotal=None,
323323
self.download = download
324324
self.reconnect = reconnect
325325

326-
class ServiceDoesNotExists(ExceptionObject):
326+
class ServiceDoesNotExist(ExceptionObject):
327327
__slots__ = ['plugin', 'func']
328328

329329
def __init__(self, plugin=None, func=None):
@@ -364,7 +364,7 @@ def __init__(self, uid=None, name=None, email=None, role=None, permission=None,
364364
self.user = user
365365
self.templateName = templateName
366366

367-
class UserDoesNotExists(ExceptionObject):
367+
class UserDoesNotExist(ExceptionObject):
368368
__slots__ = ['user']
369369

370370
def __init__(self, user=None):
@@ -383,10 +383,6 @@ def addPackageP(self, name, links, password, paused):
383383
pass
384384
def addUser(self, username, password):
385385
pass
386-
def callAddon(self, plugin, func, arguments):
387-
pass
388-
def callAddonHandler(self, plugin, func, pid_or_fid):
389-
pass
390386
def checkContainer(self, filename, data):
391387
pass
392388
def checkHTML(self, html, url):
@@ -463,6 +459,10 @@ def getUserData(self):
463459
pass
464460
def getWSAddress(self):
465461
pass
462+
def invokeAddon(self, plugin, func, func_args):
463+
pass
464+
def invokeAddonHandler(self, plugin, func, pid_or_fid):
465+
pass
466466
def isInteractionWaiting(self, mode):
467467
pass
468468
def loadConfig(self, name):

pyload/remote/apitypes_debug.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@
2727
'DownloadInfo' : [basestring, basestring, basestring, int, basestring, basestring],
2828
'DownloadProgress' : [int, int, int, int],
2929
'EventInfo' : [basestring, (list, basestring)],
30-
'FileDoesNotExists' : [int],
30+
'FileDoesNotExist' : [int],
3131
'FileInfo' : [int, basestring, int, int, int, int, int, int, int, (None, DownloadInfo)],
3232
'Input' : [int, (None, basestring), (None, basestring)],
3333
'InteractionTask' : [int, int, Input, basestring, basestring, basestring],
3434
'InvalidConfigSection' : [basestring],
3535
'LinkStatus' : [basestring, basestring, int, int, (None, basestring), (None, basestring)],
3636
'OnlineCheck' : [int, (dict, basestring, LinkStatus)],
37-
'PackageDoesNotExists' : [int],
37+
'PackageDoesNotExist' : [int],
3838
'PackageInfo' : [int, basestring, basestring, int, int, basestring, basestring, basestring, int, (list, basestring), int, bool, int, PackageStats, (list, int), (list, int)],
3939
'PackageStats' : [int, int, int, int],
4040
'ProgressInfo' : [basestring, basestring, basestring, int, int, int, (None, DownloadProgress)],
4141
'ServerStatus' : [int, int, int, int, int, bool, bool, bool, bool],
42-
'ServiceDoesNotExists' : [basestring, basestring],
42+
'ServiceDoesNotExist' : [basestring, basestring],
4343
'ServiceException' : [basestring],
4444
'TreeCollection' : [PackageInfo, (dict, int, FileInfo), (dict, int, PackageInfo)],
4545
'UserData' : [int, basestring, basestring, int, int, basestring, int, int, basestring, int, int, basestring],
46-
'UserDoesNotExists' : [basestring],
46+
'UserDoesNotExist' : [basestring],
4747
}
4848

4949
methods = {
@@ -53,8 +53,6 @@
5353
'addPackageChild': int,
5454
'addPackageP': int,
5555
'addUser': UserData,
56-
'callAddon': basestring,
57-
'callAddonHandler': basestring,
5856
'checkContainer': OnlineCheck,
5957
'checkHTML': OnlineCheck,
6058
'checkLinks': OnlineCheck,
@@ -93,6 +91,8 @@
9391
'getServerVersion': basestring,
9492
'getUserData': UserData,
9593
'getWSAddress': basestring,
94+
'invokeAddon': basestring,
95+
'invokeAddonHandler': basestring,
9696
'isInteractionWaiting': bool,
9797
'loadConfig': ConfigHolder,
9898
'login': bool,

pyload/remote/pyload.thrift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,19 @@ struct OnlineCheck {
306306

307307
// exceptions
308308

309-
exception PackageDoesNotExists {
309+
exception PackageDoesNotExist {
310310
1: PackageID pid
311311
}
312312

313-
exception FileDoesNotExists {
313+
exception FileDoesNotExist {
314314
1: FileID fid
315315
}
316316

317-
exception UserDoesNotExists {
317+
exception UserDoesNotExist {
318318
1: string user
319319
}
320320

321-
exception ServiceDoesNotExists {
321+
exception ServiceDoesNotExist {
322322
1: string plugin
323323
2: string func
324324
}
@@ -414,8 +414,8 @@ service Pyload {
414414

415415
PackageID uploadContainer(1: string filename, 2: binary data),
416416

417-
void addLinks(1: PackageID pid, 2: LinkList links) throws (1: PackageDoesNotExists e),
418-
void addLocalFile(1: PackageID pid, 2: string name, 3: string path) throws (1: PackageDoesNotExists e)
417+
void addLinks(1: PackageID pid, 2: LinkList links) throws (1: PackageDoesNotExist e),
418+
void addLocalFile(1: PackageID pid, 2: string name, 3: string path) throws (1: PackageDoesNotExist e)
419419

420420
// these are real file operations and WILL delete files on disk
421421
void deleteFiles(1: list<FileID> fids),
@@ -444,8 +444,8 @@ service Pyload {
444444
// same as above with full=False
445445
TreeCollection getPackageContent(1: PackageID pid),
446446

447-
PackageInfo getPackageInfo(1: PackageID pid) throws (1: PackageDoesNotExists e),
448-
FileInfo getFileInfo(1: FileID fid) throws (1: FileDoesNotExists e),
447+
PackageInfo getPackageInfo(1: PackageID pid) throws (1: PackageDoesNotExist e),
448+
FileInfo getFileInfo(1: FileID fid) throws (1: FileDoesNotExist e),
449449

450450
TreeCollection findFiles(1: string pattern),
451451
TreeCollection findPackages(1: list<string> tags),
@@ -454,12 +454,12 @@ service Pyload {
454454
// Modify Files/Packages
455455

456456
// moving package while downloading is not possible, so they will return bool to indicate success
457-
void updatePackage(1: PackageInfo pack) throws (1: PackageDoesNotExists e),
458-
bool setPackageFolder(1: PackageID pid, 2: string path) throws (1: PackageDoesNotExists e),
457+
void updatePackage(1: PackageInfo pack) throws (1: PackageDoesNotExist e),
458+
bool setPackageFolder(1: PackageID pid, 2: string path) throws (1: PackageDoesNotExist e),
459459

460460
// as above, this will move files on disk
461-
bool movePackage(1: PackageID pid, 2: PackageID root) throws (1: PackageDoesNotExists e),
462-
bool moveFiles(1: list<FileID> fids, 2: PackageID pid) throws (1: PackageDoesNotExists e),
461+
bool movePackage(1: PackageID pid, 2: PackageID root) throws (1: PackageDoesNotExist e),
462+
bool moveFiles(1: list<FileID> fids, 2: PackageID pid) throws (1: PackageDoesNotExist e),
463463

464464
void orderPackage(1: list<PackageID> pids, 2: i16 position),
465465
void orderFiles(1: list<FileID> fids, 2: PackageID pid, 3: i16 position),
@@ -518,12 +518,12 @@ service Pyload {
518518

519519
map<PluginName, list<AddonService>> getAddonHandler(),
520520

521-
JSONString callAddon(1: PluginName plugin, 2: string func, 3: list<JSONString> arguments)
522-
throws (1: ServiceDoesNotExists e, 2: ServiceException ex),
521+
JSONString invokeAddon(1: PluginName plugin, 2: string func, 3: list<JSONString> func_args)
522+
throws (1: ServiceDoesNotExist e, 2: ServiceException ex),
523523

524524
// special variant of callAddon that works on the media types, acccepting integer
525-
JSONString callAddonHandler(1: PluginName plugin, 2: string func, 3: PackageID pid_or_fid)
526-
throws (1: ServiceDoesNotExists e, 2: ServiceException ex),
525+
JSONString invokeAddonHandler(1: PluginName plugin, 2: string func, 3: PackageID pid_or_fid)
526+
throws (1: ServiceDoesNotExist e, 2: ServiceException ex),
527527

528528

529529
//scheduler

pyload/web/app/scripts/models/AddonHandler.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ define(['jquery', 'backbone', 'underscore', 'app', 'utils/apitypes'],
4949

5050
// dispatches call to the plugin
5151
invoke: function(plugin, func, args, success) {
52-
console.log(plugin, func, args);
52+
console.log('Invoking addon', plugin, func, args);
53+
return $.ajax(App.apiRequest('invokeAddon', {
54+
plugin: plugin,
55+
func: func,
56+
func_args: args
57+
}, {sucess: success}));
5358
}
5459
});
5560
});

0 commit comments

Comments
 (0)