Exemple #1
0
    def test_attribute_of_multi(self):
        class C(ComplexModel):
            e = Unicode(max_occurs='unbounded')
            a = XmlAttribute(Unicode, attribute_of="e")

        class SomeService(ServiceBase):
            @srpc(C, _returns=C)
            def some_call(c):
                assert c.e == ['e0', 'e1']
                assert c.a == ['a0', 'a1']
                return c

        app = Application([SomeService],
                          "tns",
                          name="test_attribute_of",
                          in_protocol=XmlDocument(),
                          out_protocol=XmlDocument())
        server = ServerBase(app)

        initial_ctx = MethodContext(server)
        initial_ctx.method_request_string = '{test_attribute_of}some_call'
        initial_ctx.in_string = [
            '<some_call xmlns="tns">'
            '<c>'
            '<e a="a0">e0</e>'
            '<e a="a1">e1</e>'
            '</c>'
            '</some_call>'
        ]

        ctx, = server.generate_contexts(initial_ctx)
        server.get_in_object(ctx)
        server.get_out_object(ctx)
        server.get_out_string(ctx)

        ret = etree.fromstring(''.join(ctx.out_string)).xpath(
            '//s0:e', namespaces=app.interface.nsmap)

        print(etree.tostring(ret[0], pretty_print=True))
        print(etree.tostring(ret[1], pretty_print=True))

        assert ret[0].text == "e0"
        assert ret[0].attrib['a'] == "a0"
        assert ret[1].text == "e1"
        assert ret[1].attrib['a'] == "a1"
Exemple #2
0
    def test_attribute_of_multi(self):
        class C(ComplexModel):
            __namespace__ = 'tns'
            e = Unicode(max_occurs='unbounded')
            a = XmlAttribute(Unicode, attribute_of="e")

        class SomeService(ServiceBase):
            @srpc(C, _returns=C)
            def some_call(c):
                assert c.e == ['e0', 'e1']
                assert c.a == ['a0', 'a1']
                return c

        app = Application([SomeService], "tns", name="test_attribute_of",
                          in_protocol=XmlDocument(), out_protocol=XmlDocument())
        server = ServerBase(app)

        initial_ctx = MethodContext(server)
        initial_ctx.method_request_string = '{test_attribute_of}some_call'
        initial_ctx.in_string = [
            '<some_call xmlns="tns">'
                '<c>'
                    '<e a="a0">e0</e>'
                    '<e a="a1">e1</e>'
                '</c>'
            '</some_call>'
        ]

        ctx, = server.generate_contexts(initial_ctx)
        server.get_in_object(ctx)
        server.get_out_object(ctx)
        server.get_out_string(ctx)

        ret = etree.fromstring(''.join(ctx.out_string)).xpath('//tns:e',
                                                 namespaces=app.interface.nsmap)

        print(etree.tostring(ret[0], pretty_print=True))
        print(etree.tostring(ret[1], pretty_print=True))

        assert ret[0].text == "e0"
        assert ret[0].attrib['a'] == "a0"
        assert ret[1].text == "e1"
        assert ret[1].attrib['a'] == "a1"
Exemple #3
0
    def __call__(self, *args, **kwargs):
        initial_ctx = MethodContext(self, MethodContext.SERVER)
        initial_ctx.method_request_string = self._key
        initial_ctx.in_header = self._in_header
        initial_ctx.transport.type = NullServer.transport
        initial_ctx.locale = self._locale

        contexts = self.app.in_protocol.generate_method_contexts(initial_ctx)

        retval = None
        logger.warning("%s start request %s" % (_big_header, _big_footer))

        if self._async:
            from twisted.internet.defer import Deferred

        for cnt, ctx in enumerate(contexts):
            # this reconstruction is quite costly. I wonder whether it's a
            # problem though.

            _type_info = ctx.descriptor.in_message._type_info
            ctx.in_object = [None] * len(_type_info)
            for i in range(len(args)):
                ctx.in_object[i] = args[i]

            for i, k in enumerate(_type_info.keys()):
                val = kwargs.get(k, None)
                if val is not None:
                    ctx.in_object[i] = val

            if ctx.descriptor.body_style == BODY_STYLE_BARE:
                ctx.in_object = ctx.descriptor.in_message \
                                      .get_serialization_instance(ctx.in_object)

            if cnt == 0:
                p_ctx = ctx
            else:
                ctx.descriptor.aux.initialize_context(ctx, p_ctx, error=None)

            # do logging.getLogger('spyne.server.null').setLevel(logging.CRITICAL)
            # to hide the following
            logger.warning("%s start context %s" %
                           (_small_header, _small_footer))
            logger.warning("%r.%r" %
                           (ctx.service_class, ctx.descriptor.function))
            try:
                self.app.process_request(ctx)
            finally:
                logger.warning("%s  end context  %s" %
                               (_small_header, _small_footer))

            if cnt == 0:
                if self._async and isinstance(ctx.out_object[0], Deferred):
                    retval = ctx.out_object[0]
                    retval.addCallback(_cb_async, ctx, cnt, self)

                else:
                    retval = _cb_sync(ctx, cnt, self)

        if not self._async:
            p_ctx.close()

        logger.warning("%s  end request  %s" % (_big_header, _big_footer))

        return retval
Exemple #4
0
    def __call__(self, *args, **kwargs):
        initial_ctx = MethodContext(self)
        initial_ctx.method_request_string = self.__key
        initial_ctx.in_header = self.__in_header
        initial_ctx.transport.type = NullServer.transport

        contexts = self.app.in_protocol.generate_method_contexts(initial_ctx)

        cnt = 0
        retval = None
        logger.warning("%s start request %s" % (_big_header, _big_footer))

        for ctx in contexts:
            # this reconstruction is quite costly. I wonder whether it's a
            # problem though.

            _type_info = ctx.descriptor.in_message._type_info
            ctx.in_object = [None] * len(_type_info)
            for i in range(len(args)):
                ctx.in_object[i] = args[i]

            for i, k in enumerate(_type_info.keys()):
                val = kwargs.get(k, None)
                if val is not None:
                    ctx.in_object[i] = val

            if cnt == 0:
                p_ctx = ctx
            else:
                ctx.descriptor.aux.initialize_context(ctx, p_ctx, error=None)

            # do logging.getLogger('spyne.server.null').setLevel(logging.CRITICAL)
            # to hide the following
            logger.warning("%s start context %s" %
                           (_small_header, _small_footer))
            logger.warning("%r.%r" %
                           (ctx.service_class, ctx.descriptor.function))
            self.app.process_request(ctx)
            logger.warning("%s  end context  %s" %
                           (_small_header, _small_footer))

            if ctx.out_error:
                raise ctx.out_error

            else:
                if len(ctx.descriptor.out_message._type_info) == 0:
                    _retval = None

                elif len(ctx.descriptor.out_message._type_info) == 1:
                    _retval = ctx.out_object[0]

                    # workaround to have the context disposed of when the caller
                    # is done with the return value. the context is sometimes
                    # needed to fully construct the return object (e.g. when the
                    # object is a sqlalchemy object bound to a session that's
                    # defined in the context object).
                    try:
                        _retval.__ctx__ = ctx
                    except AttributeError:
                        # not all objects let this happen. (eg. built-in types
                        # like str, but they don't need the context anyway).
                        pass

                else:
                    _retval = ctx.out_object

                    # same as above
                    try:
                        _retval[0].__ctx__ = ctx
                    except AttributeError:
                        pass

            if cnt == 0:
                retval = _retval
            cnt += 1

            if ctx != p_ctx:
                ctx.close()

        p_ctx.close()

        logger.warning("%s  end request  %s" % (_big_header, _big_footer))

        return retval
Exemple #5
0
    def __call__(self, *args, **kwargs):
        initial_ctx = MethodContext(self, MethodContext.SERVER)
        initial_ctx.method_request_string = self._key
        initial_ctx.in_header = self._in_header
        initial_ctx.transport.type = NullServer.transport
        initial_ctx.locale = self._locale

        contexts = self.app.in_protocol.generate_method_contexts(initial_ctx)

        retval = None
        logger.warning("%s start request %s" % (_big_header, _big_footer))

        if self._async:
            from twisted.internet.defer import Deferred

        for cnt, ctx in enumerate(contexts):
            # this reconstruction is quite costly. I wonder whether it's a
            # problem though.

            _type_info = ctx.descriptor.in_message._type_info
            ctx.in_object = [None] * len(_type_info)
            for i in range(len(args)):
                ctx.in_object[i] = args[i]

            for i, k in enumerate(_type_info.keys()):
                val = kwargs.get(k, None)
                if val is not None:
                    ctx.in_object[i] = val

            if ctx.descriptor.body_style == BODY_STYLE_BARE:
                ctx.in_object = ctx.descriptor.in_message \
                                      .get_serialization_instance(ctx.in_object)

            if cnt == 0:
                p_ctx = ctx
            else:
                ctx.descriptor.aux.initialize_context(ctx, p_ctx, error=None)

            # do logging.getLogger('spyne.server.null').setLevel(logging.CRITICAL)
            # to hide the following
            logger.warning("%s start context %s" % (_small_header,
                                                                 _small_footer))
            logger.warning("%r.%r" % (ctx.service_class,
                                                       ctx.descriptor.function))
            try:
                self.app.process_request(ctx)
            finally:
                logger.warning("%s  end context  %s" % (_small_header,
                                                                 _small_footer))

            if cnt == 0:
                if self._async and isinstance(ctx.out_object[0], Deferred):
                    retval = ctx.out_object[0]
                    retval.addCallback(_cb_async, ctx, cnt, self)

                else:
                    retval = _cb_sync(ctx, cnt, self)

        if not self._async:
            p_ctx.close()

        logger.warning("%s  end request  %s" % (_big_header, _big_footer))

        return retval
Exemple #6
0
    def __call__(self, *args, **kwargs):
        initial_ctx = MethodContext(self)
        initial_ctx.method_request_string = self.__key
        initial_ctx.in_header = self.__in_header
        initial_ctx.transport.type = NullServer.transport
        initial_ctx.locale = self.__locale

        contexts = self.app.in_protocol.generate_method_contexts(initial_ctx)

        cnt = 0
        retval = None
        logger.warning( "%s start request %s" % (_big_header, _big_footer)  )

        for ctx in contexts:
            # this reconstruction is quite costly. I wonder whether it's a
            # problem though.

            _type_info = ctx.descriptor.in_message._type_info
            ctx.in_object = [None] * len(_type_info)
            for i in range(len(args)):
                ctx.in_object[i] = args[i]

            for i,k in enumerate(_type_info.keys()):
                val = kwargs.get(k, None)
                if val is not None:
                    ctx.in_object[i] = val

            if cnt == 0:
                p_ctx = ctx
            else:
                ctx.descriptor.aux.initialize_context(ctx, p_ctx, error=None)

            # do logging.getLogger('spyne.server.null').setLevel(logging.CRITICAL)
            # to hide the following
            logger.warning( "%s start context %s" % (_small_header, _small_footer) )
            logger.warning( "%r.%r" % (ctx.service_class, ctx.descriptor.function) )
            try:
                self.app.process_request(ctx)
            finally:
                logger.warning( "%s  end context  %s" % (_small_header, _small_footer) )

            if ctx.out_error:
                raise ctx.out_error

            else:
                if len(ctx.descriptor.out_message._type_info) == 0:
                    _retval = None

                elif len(ctx.descriptor.out_message._type_info) == 1:
                    _retval = ctx.out_object[0]

                else:
                    _retval = ctx.out_object

                if cnt == 0 and self.__ostr:
                    self.__server.get_out_string(ctx)
                    _retval = ctx.out_string

            if cnt == 0:
                retval = _retval
            else:
                ctx.close()

            cnt += 1

        p_ctx.close()

        logger.warning( "%s  end request  %s" % (_big_header, _big_footer)  )

        return retval
Exemple #7
0
    def __call__(self, *args, **kwargs):
        initial_ctx = MethodContext(self)
        initial_ctx.method_request_string = self.__key
        initial_ctx.in_header = self.__in_header
        initial_ctx.transport.type = NullServer.transport

        contexts = self.app.in_protocol.generate_method_contexts(initial_ctx)

        cnt = 0
        retval = None
        logger.warning( "%s start request %s" % (_big_header, _big_footer)  )

        for ctx in contexts:
            # this reconstruction is quite costly. I wonder whether it's a
            # problem though.

            _type_info = ctx.descriptor.in_message._type_info
            ctx.in_object = [None] * len(_type_info)
            for i in range(len(args)):
                ctx.in_object[i] = args[i]

            for i,k in enumerate(_type_info.keys()):
                val = kwargs.get(k, None)
                if val is not None:
                    ctx.in_object[i] = val

            if cnt == 0:
                p_ctx = ctx
            else:
                ctx.descriptor.aux.initialize_context(ctx, p_ctx, error=None)

            # do logging.getLogger('spyne.server.null').setLevel(logging.CRITICAL)
            # to hide the following
            logger.warning( "%s start context %s" % (_small_header, _small_footer) )
            self.app.process_request(ctx)
            logger.warning( "%s  end context  %s" % (_small_header, _small_footer) )

            if ctx.out_error:
                raise ctx.out_error

            else:
                if len(ctx.descriptor.out_message._type_info) == 0:
                    _retval = None

                elif len(ctx.descriptor.out_message._type_info) == 1:
                    _retval = ctx.out_object[0]

                    # workaround to have the context disposed of when the caller is
                    # done with the return value. the context is sometimes needed to
                    # fully construct the return object (e.g. when the object is a
                    # sqlalchemy object bound to a session that's defined in the
                    # context object).
                    try:
                        _retval.__ctx__ = ctx
                    except AttributeError:
                        # not all objects let this happen. (eg. built-in types
                        # like str, but they don't need the context anyway).
                        pass

                else:
                    _retval = ctx.out_object

                    # same as above
                    try:
                        _retval[0].__ctx__ = ctx
                    except AttributeError:
                        pass

            if cnt == 0:
                retval = _retval
            cnt += 1

        logger.warning( "%s  end request  %s" % (_big_header, _big_footer)  )

        return retval
Exemple #8
0
    def __call__(self, *args, **kwargs):
        initial_ctx = MethodContext(self)
        initial_ctx.method_request_string = self.__key
        initial_ctx.in_header = self.__in_header
        initial_ctx.transport.type = NullServer.transport
        initial_ctx.locale = self.__locale

        contexts = self.app.in_protocol.generate_method_contexts(initial_ctx)

        cnt = 0
        retval = None
        logger.warning("%s start request %s" % (_big_header, _big_footer))

        for ctx in contexts:
            # this reconstruction is quite costly. I wonder whether it's a
            # problem though.

            _type_info = ctx.descriptor.in_message._type_info
            ctx.in_object = [None] * len(_type_info)
            for i in range(len(args)):
                ctx.in_object[i] = args[i]

            for i, k in enumerate(_type_info.keys()):
                val = kwargs.get(k, None)
                if val is not None:
                    ctx.in_object[i] = val

            if cnt == 0:
                p_ctx = ctx
            else:
                ctx.descriptor.aux.initialize_context(ctx, p_ctx, error=None)

            # do logging.getLogger('spyne.server.null').setLevel(logging.CRITICAL)
            # to hide the following
            logger.warning("%s start context %s" %
                           (_small_header, _small_footer))
            logger.warning("%r.%r" %
                           (ctx.service_class, ctx.descriptor.function))
            try:
                self.app.process_request(ctx)
            finally:
                logger.warning("%s  end context  %s" %
                               (_small_header, _small_footer))

            if ctx.out_error:
                raise ctx.out_error

            else:
                if len(ctx.descriptor.out_message._type_info) == 0:
                    _retval = None

                elif len(ctx.descriptor.out_message._type_info) == 1:
                    _retval = ctx.out_object[0]

                else:
                    _retval = ctx.out_object

                if cnt == 0 and self.__ostr:
                    self.__server.get_out_string(ctx)
                    _retval = ctx.out_string

            if cnt == 0:
                retval = _retval
            else:
                ctx.close()

            cnt += 1

        p_ctx.close()

        logger.warning("%s  end request  %s" % (_big_header, _big_footer))

        return retval