From 4fa375bd6c3f0c37b06279054f37d05c227b6391 Mon Sep 17 00:00:00 2001 From: Martijn Voncken Date: Wed, 20 Feb 2008 19:36:24 +0000 Subject: [PATCH] proxy for plugin methods --- deluge/ui/webui/webui_plugin/pages.py | 6 +- .../ui/webui/webui_plugin/webserver_common.py | 63 ++++++++++++++----- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/deluge/ui/webui/webui_plugin/pages.py b/deluge/ui/webui/webui_plugin/pages.py index 750031662..7e58ab71a 100644 --- a/deluge/ui/webui/webui_plugin/pages.py +++ b/deluge/ui/webui/webui_plugin/pages.py @@ -228,7 +228,8 @@ class torrent_queue_up: torrent_list.sort(lambda x, y : x.queue - y.queue) torrent_ids = [t.id for t in torrent_list] for torrent_id in torrent_ids: - async_proxy.get_core().call("queue_queue_up", None, torrent_id) + #async_proxy.get_core().call("queue_queue_up", None, torrent_id) + proxy.queue_queue_up(torrent_id) do_redirect() class torrent_queue_down: @@ -239,7 +240,8 @@ class torrent_queue_down: torrent_list.sort(lambda x, y : x.queue - y.queue) torrent_ids = [t.id for t in torrent_list] for torrent_id in reversed(torrent_ids): - async_proxy.get_core().call("queue_queue_down", None, torrent_id) + #async_proxy.get_core().call("queue_queue_down", None, torrent_id) + proxy.queue_queue_down(torrent_id) do_redirect() class torrent_files: diff --git a/deluge/ui/webui/webui_plugin/webserver_common.py b/deluge/ui/webui/webui_plugin/webserver_common.py index 235e60a94..9c9e3deb1 100644 --- a/deluge/ui/webui/webui_plugin/webserver_common.py +++ b/deluge/ui/webui/webui_plugin/webserver_common.py @@ -108,42 +108,77 @@ CONFIG_DEFAULTS = { #/constants #some magic to transform the async-proxy back to sync: -class SyncProxyMethod: +class BlockingMethod: """ helper class for SyncProxy """ - def __init__(self, func_name): - self.func_name = func_name + def __init__(self, method_name): + self.method_name = method_name + self.result = None - def __call__(self,*args,**kwargs): - func = getattr(client,self.func_name) + def __call__(self, *args, **kwargs): + func = getattr(client, self.method_name) if self.has_callback(func): - #(ab)using list.append as a builtin callback method - sync_result = [] - func(sync_result.append,*args, **kwargs) + func(self.callback ,*args, **kwargs) client.force_call(block=True) - if not sync_result: - return None - return sync_result[0] + return self.result else: func(*args, **kwargs) client.force_call(block=True) return + def callback(self, result): + self.result = result + @staticmethod def has_callback(func): return "callback" in inspect.getargspec(func)[0] +class CoreMethod: + """ + plugins etc are not exposed in client.py + wrapper to make plugin methods behave like the ones in client.py + """ + def __init__(self, method_name): + self.method_name = method_name + self.result = None + + def __call__(self,*args,**kwargs): + client.get_core().call(self.method_name, self.callback ,*args, **kwargs) + return self.result + + def callback(self, result): + self.result = result + +class BlockingCoreMethod(CoreMethod): + """ + for syncProcy + """ + def __call__(self,*args,**kwargs): + client.get_core().call(self.method_name, self.callback ,*args, **kwargs) + client.force_call(block=True) + return self.result + class SyncProxy(object): """acts like the old synchonous proxy""" - def __getattr__(self, attr): - return SyncProxyMethod(attr) + def __getattr__(self, method_name): + if hasattr(client, method_name): + return BlockingMethod(method_name) + else: + return BlockingCoreMethod( method_name ) + +class ASyncProxy(object): + def __getattr__(self, method_name): + if hasattr(client, method_name): + return getattr(client, method_name) + else: + return CoreMethod( method_name ) #moving stuff from WS to module #goal: eliminate WS, because the 05 compatiblilty is not needed anymore proxy = SyncProxy() -async_proxy = client +async_proxy = ASyncProxy() #log is already imported.