Пример #1
0
    def call_wrapper(self, ctx):
        """This method calls the call_wrapper method in the service definition.
        This can be overridden to make an application-wide custom exception
        management.
        """

        # no function
        if ctx.function is None:
            logger.debug("Skipping user code call as ctx.function is None.")
            return None

        # @rpc inside service class
        if ctx.descriptor.no_self:
            assert ctx.descriptor.service_class is not None
            return ctx.descriptor.service_class.call_wrapper(ctx)

        # from here on it's @mrpc in a (parent) class
        cls = ctx.descriptor.parent_class
        if cls.__orig__ is not None:
            cls = cls.__orig__

        filters = {}
        inst = cls.__respawn__(ctx, filters)
        if inst is None:
            raise RespawnError('{%s}%s with params %r' %
                            (cls.get_namespace(), cls.get_type_name(), filters))

        in_cls = ctx.descriptor.in_message

        args = tuple(ctx.in_object)
        if args is None:
            args = ()

        elif ctx.descriptor.body_style is BODY_STYLE_WRAPPED and \
                                len(in_cls.get_flat_type_info(in_cls)) <= 1:
            args = ()

        else:
            args = args[1:]

        # check whether this is a valid request according to the prerequisite
        # function (the callable that was passed in the _when argument to @mrpc)
        if ctx.descriptor.when is not None:
            if not ctx.descriptor.when(inst, ctx):
                raise InvalidRequestError("Invalid object state for request")

        if ctx.descriptor.no_ctx:
            args = (inst,) + args
        else:
            args = (inst, ctx,) + args

        if ctx.descriptor.service_class is None:
            retval = ctx.function(*args)

        else:
            retval = ctx.descriptor.service_class.call_wrapper(ctx, args=args)

        return retval
Пример #2
0
    def call_wrapper(self, ctx):
        """This method calls the call_wrapper method in the service definition.
        This can be overridden to make an application-wide custom exception
        management.
        """

        if ctx.descriptor.body_style is BODY_STYLE_BARE:
            ctx.in_object = [ctx.in_object]
        elif ctx.descriptor.body_style is BODY_STYLE_EMPTY:
            ctx.in_object = []

        retval = None

        # service rpc
        if ctx.descriptor.no_self:
            retval = ctx.descriptor.service_class.call_wrapper(ctx)

        # class rpc
        else:
            cls = ctx.descriptor.parent_class
            if cls.__orig__ is not None:
                cls = cls.__orig__

            inst = cls.__respawn__(ctx)
            if inst is None:
                raise RespawnError('{%s}%s' %
                                     (cls.get_namespace(), cls.get_type_name()))
            in_cls = ctx.descriptor.in_message

            args = ctx.in_object
            if args is None:
                args = []

            elif ctx.descriptor.body_style is BODY_STYLE_WRAPPED and \
                                        len(in_cls.get_flat_type_info(in_cls)) <= 1:
                args = []

            else:
                args = args[1:]

            if ctx.descriptor.service_class is not None:
                ctx.in_object = [inst, ctx]
                ctx.in_object.extend(args)

                # hack to make sure inst goes first
                ctx.descriptor.no_ctx = True
                retval = ctx.descriptor.service_class.call_wrapper(ctx)

            elif ctx.function is not None:
                if ctx.descriptor.no_ctx:
                    retval = ctx.function(inst, *args)
                else:
                    retval = ctx.function(inst, ctx, *args)

        return retval