Newer
Older
#!/usr/bin/env python
#coding:utf8
from sjrpc.core.exceptions import RpcError
class ConnectionProxy(object):
Create a new call proxy for the connection passed in arguments.
Usage example:
>>> proxy = ConnectionProxy(conn)
>>> ret = proxy.existing_function()
If exception is raised by the function, the proxy tries to get the
exception class in *__builtins__* to re-raise it. If exception class
is not found in *__builtins__*, the exception raised is the
original :exc:`RpcError`:
>>> proxy.unknown_function()
[Traceback]
NameError: remote name 'unknown_function' is not defined
'''
def __init__(self, connection):
Antoine Millet
committed
self.connection = connection
def __getattr__(self, name):
def func(*args, **kwargs):
try:
Antoine Millet
committed
returned = self.connection.call(name, *args, **kwargs)
except RpcError as err:
expt = __builtins__.get(err.exception)
if expt is not None:
raise expt(err.message)
else:
raise err
else:
return returned
return func
def __getitem__(self, name):
return self.__getattr__(name)