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

class RpcHandler(object):    
    '''
    Basic RPC functions handler.
Antoine Millet's avatar
Antoine Millet committed

    Derive this call in your handler and define some methods. All the defined
    methods (including privates) are available to your peer.
Antoine Millet's avatar
Antoine Millet committed
    '''

    def __getitem__(self, name):
        if hasattr(self, name):
            return getattr(self, name)
        else:
            raise KeyError(name)

Antoine Millet's avatar
Antoine Millet committed
#
Antoine Millet's avatar
Antoine Millet committed
# Decorators:
Antoine Millet's avatar
Antoine Millet committed
#
Antoine Millet's avatar
Antoine Millet committed

def threadless(func):
    '''
    Function handler decorator -- don't spawn a new thread when function is
    called.
    '''
Antoine Millet's avatar
Antoine Millet committed
    func.__threaded__ = False
    return func

Antoine Millet's avatar
Antoine Millet committed
def pure(func):
    '''
    Function handler decorator -- the function is a pure fonction, caller will
Antoine Millet's avatar
Antoine Millet committed
    not pass :class:`RpcConnection` object as first call parameters.
    .. deprecated:: 14
       This decorator is useless since the default behavior have change. You
       can use :func:`pass_connection` decorator to do the opposite.

       This function is kept for compatibility only, and will be removed latter.
    '''

    return func


def pass_connection(func):
    '''
    Function handler decorator -- pass on first parameter the connection which
    called this function.
    '''

    func.__pass_connection__ = True
    return func

def pass_rpc(func):
Antoine Millet's avatar
Antoine Millet committed
    '''
    Function handler decorator -- pass on first (or after connection) the
    rpc protocol which called this function.
    '''

    func.__pass_rpc__ = True
Antoine Millet's avatar
Antoine Millet committed
    return func