proxy for plugin methods

This commit is contained in:
Martijn Voncken 2008-02-20 19:36:24 +00:00
parent 86eeef4b92
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_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:

View File

@ -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.