Skip to content
proxies.py 1.24 KiB
Newer Older
Antoine Millet's avatar
Antoine Millet committed
#!/usr/bin/env python
#coding:utf8

from sjrpc.core.exceptions import RpcError

class ConnectionProxy(object):
Antoine Millet's avatar
Antoine Millet committed
    Create a new call proxy for the connection passed in arguments.
Antoine Millet's avatar
Antoine Millet committed
    Usage example:
    
    >>> proxy = ConnectionProxy(conn)
    >>> ret = proxy.existing_function()
Antoine Millet's avatar
Antoine Millet committed
    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):

    def __getattr__(self, name):
        def func(*args, **kwargs):
            try:
                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)