proxy for plugin methods

This commit is contained in:
Martijn Voncken 2008-02-20 19:36:24 +00:00
commit 4fa375bd6c
2 changed files with 53 additions and 16 deletions

View file

@ -228,7 +228,8 @@ class torrent_queue_up:
torrent_list.sort(lambda x, y : x.queue - y.queue) torrent_list.sort(lambda x, y : x.queue - y.queue)
torrent_ids = [t.id for t in torrent_list] torrent_ids = [t.id for t in torrent_list]
for torrent_id in torrent_ids: 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() do_redirect()
class torrent_queue_down: class torrent_queue_down:
@ -239,7 +240,8 @@ class torrent_queue_down:
torrent_list.sort(lambda x, y : x.queue - y.queue) torrent_list.sort(lambda x, y : x.queue - y.queue)
torrent_ids = [t.id for t in torrent_list] torrent_ids = [t.id for t in torrent_list]
for torrent_id in reversed(torrent_ids): 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() do_redirect()
class torrent_files: class torrent_files:

View file

@ -108,42 +108,77 @@ CONFIG_DEFAULTS = {
#/constants #/constants
#some magic to transform the async-proxy back to sync: #some magic to transform the async-proxy back to sync:
class SyncProxyMethod: class BlockingMethod:
""" """
helper class for SyncProxy helper class for SyncProxy
""" """
def __init__(self, func_name): def __init__(self, method_name):
self.func_name = func_name self.method_name = method_name
self.result = None
def __call__(self,*args,**kwargs): def __call__(self, *args, **kwargs):
func = getattr(client,self.func_name) func = getattr(client, self.method_name)
if self.has_callback(func): if self.has_callback(func):
#(ab)using list.append as a builtin callback method func(self.callback ,*args, **kwargs)
sync_result = []
func(sync_result.append,*args, **kwargs)
client.force_call(block=True) client.force_call(block=True)
if not sync_result: return self.result
return None
return sync_result[0]
else: else:
func(*args, **kwargs) func(*args, **kwargs)
client.force_call(block=True) client.force_call(block=True)
return return
def callback(self, result):
self.result = result
@staticmethod @staticmethod
def has_callback(func): def has_callback(func):
return "callback" in inspect.getargspec(func)[0] 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): class SyncProxy(object):
"""acts like the old synchonous proxy""" """acts like the old synchonous proxy"""
def __getattr__(self, attr): def __getattr__(self, method_name):
return SyncProxyMethod(attr) 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 #moving stuff from WS to module
#goal: eliminate WS, because the 05 compatiblilty is not needed anymore #goal: eliminate WS, because the 05 compatiblilty is not needed anymore
proxy = SyncProxy() proxy = SyncProxy()
async_proxy = client async_proxy = ASyncProxy()
#log is already imported. #log is already imported.