diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2013-01-02 22:03:02 +0100 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2013-01-02 22:03:02 +0100 |
commit | a786fef06639da439f9edd367db36388dc1c2658 (patch) | |
tree | 84b7d7acf23c300bef4b0fd5ee744e408ffc1152 | |
parent | New naming: pyLoadCore -> pyload (diff) | |
download | pyload-a786fef06639da439f9edd367db36388dc1c2658.tar.xz |
little cleanup, improved handling of custom exceptions via api
-rw-r--r-- | module/Api.py | 2 | ||||
-rw-r--r-- | module/remote/JSONClient.py | 2 | ||||
-rw-r--r-- | module/remote/WSClient.py | 2 | ||||
-rw-r--r-- | module/remote/create_ttypes.py | 5 | ||||
-rw-r--r-- | module/remote/json_converter.py | 3 | ||||
-rw-r--r-- | module/remote/pyload.thrift | 2 | ||||
-rw-r--r-- | module/remote/ttypes.py | 23 | ||||
-rw-r--r-- | module/remote/ttypes_debug.py | 2 | ||||
-rw-r--r-- | module/remote/wsbackend/AbstractHandler.py | 1 | ||||
-rw-r--r-- | module/remote/wsbackend/ApiHandler.py | 6 | ||||
-rw-r--r-- | module/web/api_app.py | 5 | ||||
-rwxr-xr-x | pyload.py | 28 | ||||
-rw-r--r-- | tests/api/test_JSONBackend.py | 2 | ||||
-rw-r--r-- | tests/api/test_noargs.py | 2 |
14 files changed, 39 insertions, 46 deletions
diff --git a/module/Api.py b/module/Api.py index a96f35ab6..dc500dabe 100644 --- a/module/Api.py +++ b/module/Api.py @@ -228,7 +228,7 @@ class Api(Iface): return free_space(self.core.config["general"]["download_folder"]) - def stop(self): + def quit(self): """Clean way to quit pyLoad""" self.core.do_kill = True diff --git a/module/remote/JSONClient.py b/module/remote/JSONClient.py index 9f678f5bd..469d40317 100644 --- a/module/remote/JSONClient.py +++ b/module/remote/JSONClient.py @@ -16,6 +16,8 @@ class JSONClient: def request(self, path, data): ret = urlopen(self.url + path, urlencode(data)) + if ret.code == 400: + raise loads(ret.read()) if ret.code == 404: raise AttributeError("Unknown Method") if ret.code == 500: diff --git a/module/remote/WSClient.py b/module/remote/WSClient.py index 23b5fc3ca..fc7590edb 100644 --- a/module/remote/WSClient.py +++ b/module/remote/WSClient.py @@ -34,6 +34,8 @@ class WSClient: self.ws.send(dumps([func, args])) code, result = loads(self.ws.recv()) + if code == 400: + raise result if code == 404: raise AttributeError("Unknown Method") elif code == 500: diff --git a/module/remote/create_ttypes.py b/module/remote/create_ttypes.py index c50142322..544b00ee8 100644 --- a/module/remote/create_ttypes.py +++ b/module/remote/create_ttypes.py @@ -86,6 +86,9 @@ class BaseObject(object): \tdef __str__(self): \t\treturn "<%s %s>" % (self.__class__.__name__, ", ".join("%s=%s" % (k,getattr(self,k)) for k in self.__slots__)) +class ExceptionObject(Exception): +\t__slots__ = [] + """) dev = open(join(path, "ttypes_debug.py"), "wb") @@ -112,7 +115,7 @@ from ttypes import *\n for klass in classes: name = klass.__name__ - base = "Exception" if issubclass(klass, ttypes.TExceptionBase) else "BaseObject" + base = "ExceptionObject" if issubclass(klass, ttypes.TExceptionBase) else "BaseObject" f.write("class %s(%s):\n" % (name, base)) # No attributes, don't write further info diff --git a/module/remote/json_converter.py b/module/remote/json_converter.py index 76a0d5935..ad7645ccc 100644 --- a/module/remote/json_converter.py +++ b/module/remote/json_converter.py @@ -9,12 +9,13 @@ except ImportError: import ttypes from ttypes import BaseObject +from ttypes import ExceptionObject # json encoder that accepts TBase objects class BaseEncoder(json.JSONEncoder): def default(self, o): - if isinstance(o, BaseObject): + if isinstance(o, BaseObject) or isinstance(o, ExceptionObject): ret = {"@class" : o.__class__.__name__} for att in o.__slots__: ret[att] = getattr(o, att) diff --git a/module/remote/pyload.thrift b/module/remote/pyload.thrift index 47c371904..af4b81cd6 100644 --- a/module/remote/pyload.thrift +++ b/module/remote/pyload.thrift @@ -345,7 +345,7 @@ service Pyload { bool togglePause(), bool toggleReconnect(), - void stop(), + void quit(), void restart(), /////////////////////// diff --git a/module/remote/ttypes.py b/module/remote/ttypes.py index d661c684a..0b9faea98 100644 --- a/module/remote/ttypes.py +++ b/module/remote/ttypes.py @@ -9,6 +9,9 @@ class BaseObject(object): def __str__(self): return "<%s %s>" % (self.__class__.__name__, ", ".join("%s=%s" % (k,getattr(self,k)) for k in self.__slots__)) +class ExceptionObject(Exception): + __slots__ = [] + class DownloadState: All = 0 Finished = 1 @@ -183,7 +186,7 @@ class EventInfo(BaseObject): self.eventname = eventname self.event_args = event_args -class FileDoesNotExists(Exception): +class FileDoesNotExists(ExceptionObject): __slots__ = ['fid'] def __init__(self, fid=None): @@ -204,7 +207,7 @@ class FileInfo(BaseObject): self.fileorder = fileorder self.download = download -class Forbidden(Exception): +class Forbidden(ExceptionObject): pass class InteractionTask(BaseObject): @@ -220,7 +223,7 @@ class InteractionTask(BaseObject): self.description = description self.plugin = plugin -class InvalidConfigSection(Exception): +class InvalidConfigSection(ExceptionObject): __slots__ = ['section'] def __init__(self, section=None): @@ -244,7 +247,7 @@ class OnlineCheck(BaseObject): self.rid = rid self.data = data -class PackageDoesNotExists(Exception): +class PackageDoesNotExists(ExceptionObject): __slots__ = ['pid'] def __init__(self, pid=None): @@ -302,14 +305,14 @@ class ServerStatus(BaseObject): self.download = download self.reconnect = reconnect -class ServiceDoesNotExists(Exception): +class ServiceDoesNotExists(ExceptionObject): __slots__ = ['plugin', 'func'] def __init__(self, plugin=None, func=None): self.plugin = plugin self.func = func -class ServiceException(Exception): +class ServiceException(ExceptionObject): __slots__ = ['msg'] def __init__(self, msg=None): @@ -323,7 +326,7 @@ class TreeCollection(BaseObject): self.files = files self.packages = packages -class Unauthorized(Exception): +class Unauthorized(ExceptionObject): pass class UserData(BaseObject): @@ -343,7 +346,7 @@ class UserData(BaseObject): self.user = user self.templateName = templateName -class UserDoesNotExists(Exception): +class UserDoesNotExists(ExceptionObject): __slots__ = ['user'] def __init__(self, user=None): @@ -474,6 +477,8 @@ class Iface(object): pass def pollResults(self, rid): pass + def quit(self): + pass def recheckPackage(self, pid): pass def removeAccount(self, plugin, account): @@ -500,8 +505,6 @@ class Iface(object): pass def setPassword(self, username, old_password, new_password): pass - def stop(self): - pass def stopAllDownloads(self): pass def stopDownloads(self, fids): diff --git a/module/remote/ttypes_debug.py b/module/remote/ttypes_debug.py index 1e04624d9..03d619dd4 100644 --- a/module/remote/ttypes_debug.py +++ b/module/remote/ttypes_debug.py @@ -96,6 +96,7 @@ methods = { 'parseURLs': (dict, basestring, list), 'pauseServer': None, 'pollResults': OnlineCheck, + 'quit': None, 'recheckPackage': None, 'removeAccount': None, 'removeUser': None, @@ -109,7 +110,6 @@ methods = { 'setInteractionResult': None, 'setPackageFolder': bool, 'setPassword': bool, - 'stop': None, 'stopAllDownloads': None, 'stopDownloads': None, 'togglePause': bool, diff --git a/module/remote/wsbackend/AbstractHandler.py b/module/remote/wsbackend/AbstractHandler.py index 276f6fa38..f843fc278 100644 --- a/module/remote/wsbackend/AbstractHandler.py +++ b/module/remote/wsbackend/AbstractHandler.py @@ -28,6 +28,7 @@ class AbstractHandler: PATH = "/" OK = 200 + BAD_REQUEST = 400 UNAUTHORIZED = 401 FORBIDDEN = 403 NOT_FOUND = 404 diff --git a/module/remote/wsbackend/ApiHandler.py b/module/remote/wsbackend/ApiHandler.py index 52dd05b9f..eec546d47 100644 --- a/module/remote/wsbackend/ApiHandler.py +++ b/module/remote/wsbackend/ApiHandler.py @@ -18,6 +18,8 @@ from mod_pywebsocket.msgutil import receive_message +from module.Api import ExceptionObject + from AbstractHandler import AbstractHandler class ApiHandler(AbstractHandler): @@ -50,7 +52,7 @@ class ApiHandler(AbstractHandler): func, args, kwargs = self.handle_call(msg, req) if not func: - return # Result was already sent + return # handle_call already sent the result if func == 'login': user = self.api.checkAuth(*args, **kwargs) @@ -74,6 +76,8 @@ class ApiHandler(AbstractHandler): try: result = getattr(req.api, func)(*args, **kwargs) + except ExceptionObject, e: + return self.send_result(req, self.BAD_REQUEST, e) except AttributeError: return self.send_result(req, self.NOT_FOUND, "Not Found") except Exception, e: diff --git a/module/web/api_app.py b/module/web/api_app.py index 4a84c85ca..ff8d0134c 100644 --- a/module/web/api_app.py +++ b/module/web/api_app.py @@ -10,6 +10,7 @@ from bottle import route, request, response, HTTPError from utils import set_session, get_user_api from webinterface import PYLOAD +from module.Api import ExceptionObject from module.remote.json_converter import loads, dumps from module.utils import remove_chars @@ -46,11 +47,13 @@ def call_api(func, args=""): try: return callApi(func, *args, **kwargs) + except ExceptionObject, e: + return HTTPError(400, dumps(e)) except Exception, e: print_exc() return HTTPError(500, dumps({"error": e.message, "traceback": format_exc()})) -# Better error codes on invalid input +# TODO Better error codes on invalid input def callApi(func, *args, **kwargs): if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"): @@ -187,13 +187,6 @@ class Core(object): print " -h, --help", " " * 13, "Display this help screen" print "" - def toggle_pause(self): - if self.threadManager.pause: - self.threadManager.pause = False - return False - elif not self.threadManager.pause: - self.threadManager.pause = True - return True def quit(self, a, b): self.shutdown() @@ -366,13 +359,8 @@ class Core(object): if not tests: self.writePidFile() - self.check_install("Crypto", _("pycrypto to decode container files")) - self.captcha = True # checks seems to fail, although tesseract is available - if self.config['ssl']['activated']: - self.check_install("OpenSSL", _("OpenSSL for secure connection")) - self.eventManager = self.evm = EventManager(self) self.setupDB() @@ -536,22 +524,6 @@ class Core(object): self.log.removeHandler(h) h.close() - def check_install(self, check_name, legend, python=True, essential=False): - """check whether needed tools are installed""" - try: - if python: - find_module(check_name) - else: - pipe = subprocess.PIPE - subprocess.Popen(check_name, stdout=pipe, stderr=pipe) - - return True - except: - if essential: - self.log.info(_("Install %s") % legend) - exit() - - return False def restart(self): self.shutdown() diff --git a/tests/api/test_JSONBackend.py b/tests/api/test_JSONBackend.py index 8d3373f65..76077b67b 100644 --- a/tests/api/test_JSONBackend.py +++ b/tests/api/test_JSONBackend.py @@ -27,3 +27,5 @@ class TestJSONBackend: def test_unknown_method(self): self.client.login("User", "test") self.client.sdfdsg() + + # TODO: test raising of exceptions, also in WSBackend
\ No newline at end of file diff --git a/tests/api/test_noargs.py b/tests/api/test_noargs.py index aa010f293..3635b9355 100644 --- a/tests/api/test_noargs.py +++ b/tests/api/test_noargs.py @@ -6,7 +6,7 @@ from ApiTester import ApiTester from module.remote.ttypes import Iface -IGNORE = ('stop', 'restart') +IGNORE = ('quit', 'restart') class TestNoArgs(ApiTester): def setUp(self): |