Example #1
0
    def __init__(self, services, tns, interface, in_protocol, out_protocol,
                                        name=None, supports_fanout_methods=False):

        self.services = services
        self.tns = tns
        self.name = name
        self.supports_fanout_methods = supports_fanout_methods

        if self.name is None:
            self.name = self.__class__.__name__.split('.')[-1]

        self.in_protocol = in_protocol
        self.in_protocol.set_app(self)

        self.out_protocol = out_protocol
        self.out_protocol.set_app(self)

        self.interface = interface
        self.interface.set_app(self)

        self.__public_methods = {}
        self.__classes = {}

        self.event_manager = EventManager(self)
        self.error_handler = None
Example #2
0
File: _base.py Project: rch/rpclib
    def __init__(self, app):
        self.app = app
        self.app.transport = self.transport
        if app.supports_fanout_methods and not self.supports_fanout_methods:
            logger.warning("""Your application is in fanout mode.
            note that in fanout mode, only the response from the last
            call will be returned.""")

        self.event_manager = EventManager(self)
Example #3
0
    def __init__(self, cls_name, cls_bases, cls_dict):
        super(ServiceBaseMeta, self).__init__(cls_name, cls_bases, cls_dict)

        self.public_methods = {}
        self.event_manager = EventManager(
            self, self.__get_base_event_handlers(cls_bases))

        for k, v in cls_dict.items():
            if hasattr(v, '_is_rpc'):
                # these three lines are needed for staticmethod wrapping to work
                descriptor = v(_default_function_name=k)
                setattr(self, k, staticmethod(descriptor.function))
                descriptor.reset_function(getattr(self, k))

                self.public_methods[k] = descriptor
Example #4
0
    def __init__(self, services, tns, in_protocol, out_protocol, interface=None,
                                                                     name=None):

        self.services = services
        self.tns = tns
        self.name = name

        if self.name is None:
            self.name = self.__class__.__name__.split('.')[-1]

        self.event_manager = EventManager(self)
        self.error_handler = None

        self.interface = Interface(self)
        self.in_protocol = in_protocol
        self.out_protocol = out_protocol

        self.in_protocol.set_app(self)
        self.out_protocol.set_app(self)

        self.reinitialize()
Example #5
0
    def __init__(self, services, interface_class, in_protocol_class,
                                      out_protocol_class=None, *args, **kwargs):
        '''Constructor.

        @param An iterable of ServiceBase subclasses that define the exposed
               services.
        @param The targetNamespace attribute of the exposed service.
        @param The name attribute of the exposed service.
        '''

        if out_protocol_class is None:
            out_protocol_class = in_protocol_class

        # interface should be initialized before protocols.
        self.interface = interface_class(self, services, *args, **kwargs)

        self.in_protocol = in_protocol_class(self)
        self.out_protocol = out_protocol_class(self)
        self.services = services

        self.__public_methods = {}
        self.__classes = {}

        self.event_manager = EventManager(self)
Example #6
0
class Application(object):
    '''This class is the glue between one or more service definitions,
    interface and protocol choices.

    :param services:     An iterable of ServiceBase subclasses that define
                         the exposed services.
    :param tns:          The targetNamespace attribute of the exposed
                         service.
    :param interface:    An InterfaceBase instance that sets the service
                         definition document standard.
    :param in_protocol:  A ProtocolBase instance that defines the input
                         protocol.
    :param out_protocol: A ProtocolBase instance that defines the output
                         protocol.
    :param name:         The optional name attribute of the exposed service.
                         The default is the name of the application class
                         which is, by default, 'Application'.

    Supported events:
        * method_call
            Called right before the service method is executed

        * method_return_object
            Called right after the service method is executed

        * method_exception_object
            Called when an exception occurred in a service method, before the
            exception is serialized.
    '''

    transport = None

    def __init__(self, services, tns, interface, in_protocol, out_protocol,
                                                                    name=None):

        self.services = services
        self.tns = tns
        self.name = name
        if self.name is None:
            self.name = self.__class__.__name__.split('.')[-1]

        self.interface = interface
        self.interface.set_app(self)

        self.in_protocol = in_protocol
        self.in_protocol.set_app(self)

        self.out_protocol = out_protocol
        self.out_protocol.set_app(self)

        self.__public_methods = {}
        self.__classes = {}

        self.event_manager = EventManager(self)
        self.error_handler = None

    def process_request(self, ctx):
        """Takes a MethodContext instance. Returns the response to the request
        as a native python object. If the function throws an exception, it
        returns None and sets the exception object to ctx.out_error.

        Overriding this method would break event management. So this is not
        meant to be overridden unless you know what you're doing.
        """

        try:
            # fire events
            self.event_manager.fire_event('method_call', ctx)
            ctx.service_class.event_manager.fire_event('method_call', ctx)

            # call the method
            ctx.out_object = self.call_wrapper(ctx)

            # out object is always an iterable of return values. see
            # MethodContext docstrings for more info
            if len(ctx.descriptor.out_message._type_info) == 0:
                ctx.out_object = [None]
            elif len(ctx.descriptor.out_message._type_info) == 1:
                ctx.out_object = [ctx.out_object]
            # otherwise, the return value should already be wrapped in an
            # iterable.

            # fire events
            self.event_manager.fire_event('method_return_object', ctx)
            ctx.service_class.event_manager.fire_event(
                                                    'method_return_object', ctx)

        except Fault, e:
            logger.exception(e)

            ctx.out_error = e

            # fire events
            self.event_manager.fire_event('method_exception_object', ctx)
            if ctx.service_class != None:
                ctx.service_class.event_manager.fire_event(
                                                'method_exception_object', ctx)

        except Exception, e:
            logger.exception(e)

            ctx.out_error = Fault('Server', str(e))

            # fire events
            self.event_manager.fire_event('method_exception_object', ctx)
            if ctx.service_class != None:
                ctx.service_class.event_manager.fire_event(
                                                'method_exception_object', ctx)
Example #7
0
    def __init__(self, app=None, validator=None):
        self.__app = None

        self.set_app(app)
        self.event_manager = EventManager(self)
        self.set_validator(validator)
Example #8
0
class Application(object):
    '''This class is the glue between one or more service definitions,
    interface and protocol choices.

    :param services:     An iterable of ServiceBase subclasses that define
                         the exposed services.
    :param tns:          The targetNamespace attribute of the exposed
                         service.
    :param interface:    An InterfaceBase instance that sets the service
                         definition document standard.
    :param in_protocol:  A ProtocolBase instance that defines the input
                         protocol.
    :param out_protocol: A ProtocolBase instance that defines the output
                         protocol.
    :param name:         The optional name attribute of the exposed service.
                         The default is the name of the application class
                         which is, by default, 'Application'.

    Supported events:
        * method_call
            Called right before the service method is executed

        * method_return_object
            Called right after the service method is executed

        * method_exception_object
            Called when an exception occurred in a service method, before the
            exception is serialized.

        * method_context_constructed
            Called from the constructor of the MethodContext instance.

        * method_context_destroyed
            Called from the destructor of the MethodContext instance.
    '''

    transport = None

    def __init__(self, services, tns, interface, in_protocol, out_protocol,
                                        name=None, supports_fanout_methods=False):

        self.services = services
        self.tns = tns
        self.name = name
        self.supports_fanout_methods = supports_fanout_methods

        if self.name is None:
            self.name = self.__class__.__name__.split('.')[-1]

        self.in_protocol = in_protocol
        self.in_protocol.set_app(self)

        self.out_protocol = out_protocol
        self.out_protocol.set_app(self)

        self.interface = interface
        self.interface.set_app(self)

        self.__public_methods = {}
        self.__classes = {}

        self.event_manager = EventManager(self)
        self.error_handler = None

    def process_request(self, ctx):
        """Takes a MethodContext instance. Returns the response to the request
        as a native python object. If the function throws an exception, it
        returns None and sets the exception object to ctx.out_error.

        Overriding this method would break event management. So this is not
        meant to be overridden unless you know what you're doing.
        """

        try:
            # fire events
            self.event_manager.fire_event('method_call', ctx)
            ctx.service_class.event_manager.fire_event('method_call', ctx)

            # call the method
            ctx.out_object = self.call_wrapper(ctx)

            # out object is always an iterable of return values. see
            # MethodContext docstrings for more info
            if len(ctx.descriptor.out_message._type_info) == 0:
                ctx.out_object = [None]
            elif len(ctx.descriptor.out_message._type_info) == 1:
                ctx.out_object = [ctx.out_object]
            # otherwise, the return value should already be wrapped in an
            # iterable.

            # fire events
            self.event_manager.fire_event('method_return_object', ctx)
            ctx.service_class.event_manager.fire_event(
                                                    'method_return_object', ctx)

        except Fault, e:
            logger.exception(e)

            ctx.out_error = e

            # fire events
            self.event_manager.fire_event('method_exception_object', ctx)
            if ctx.service_class != None:
                ctx.service_class.event_manager.fire_event(
                                                'method_exception_object', ctx)

        except Exception, e:
            logger.exception(e)

            ctx.out_error = Fault('Server', str(e))

            # fire events
            self.event_manager.fire_event('method_exception_object', ctx)
            if ctx.service_class != None:
                ctx.service_class.event_manager.fire_event(
                                                'method_exception_object', ctx)
Example #9
0
class Application(object):
    transport = None

    def __init__(self, services, interface_class, in_protocol_class,
                                      out_protocol_class=None, *args, **kwargs):
        '''Constructor.

        @param An iterable of ServiceBase subclasses that define the exposed
               services.
        @param The targetNamespace attribute of the exposed service.
        @param The name attribute of the exposed service.
        '''

        if out_protocol_class is None:
            out_protocol_class = in_protocol_class

        # interface should be initialized before protocols.
        self.interface = interface_class(self, services, *args, **kwargs)

        self.in_protocol = in_protocol_class(self)
        self.out_protocol = out_protocol_class(self)
        self.services = services

        self.__public_methods = {}
        self.__classes = {}

        self.event_manager = EventManager(self)

    def process_request(self, ctx, req_obj):
        """Takes a MethodContext instance and the native request object.
        Returns the response to the request as a native python object.

        Not meant to be overridden.
        """

        try:
            # implementation hook
            ctx.service_class.event_manager.fire_event('method_call',ctx)

            # retrieve the method
            func = getattr(ctx.service_class, ctx.descriptor.name)

            # call the method
            ctx.out_object = ctx.service_class.call_wrapper(ctx, func, req_obj)

            # fire events
            self.event_manager.fire_event('method_return_object', ctx)
            ctx.service_class.event_manager.fire_event(
                                                    'method_return_object', ctx)

        except Fault, e:
            stacktrace=traceback.format_exc()
            logger.error(stacktrace)

            ctx.out_error = e

            # fire events
            self.event_manager.fire_event('method_exception_object', ctx)
            if ctx.service_class != None:
                ctx.service_class.event_manager.fire_event(
                                                    'method_return_object', ctx)

        except Exception, e:
            stacktrace=traceback.format_exc()
            logger.error(stacktrace)

            ctx.out_error = Fault('Server', str(e))

            # fire events
            self.event_manager.fire_event('method_exception_object', ctx)
            if ctx.service_class != None:
                ctx.service_class.event_manager.fire_event(
                                                    'method_return_object', ctx)