Exemplo n.º 1
0
def get_object_as_xml_cloth(inst,
                            cls=None,
                            no_namespace=False,
                            encoding='utf8'):
    """Returns an ElementTree representation of a
    :class:`spyne.model.complex.ComplexModel` subclass.

    :param inst: The instance of the class to be serialized.
    :param cls: The class to be serialized. Optional.
    :param root_tag_name: The root tag string to use. Defaults to the output of
        ``value.__class__.get_type_name_ns()``.
    :param no_namespace: When true, namespace information is discarded.
    """

    if cls is None:
        cls = inst.__class__

    if cls.get_namespace() is None and no_namespace is None:
        no_namespace = True

    if no_namespace is None:
        no_namespace = False

    ostr = BytesIO()
    xml_cloth = XmlCloth(use_ns=(not no_namespace))
    ctx = FakeContext()
    with etree.xmlfile(ostr, encoding=encoding) as xf:
        ctx.outprot_ctx.doctype_written = False
        ctx.protocol.prot_stack = tlist([], ProtocolMixin)
        tn = cls.get_type_name()
        ret = xml_cloth.subserialize(ctx, cls, inst, xf, tn)

        assert not isgenerator(ret)

    return ostr.getvalue()
Exemplo n.º 2
0
Arquivo: xml.py Projeto: plq/spyne
def get_object_as_xml_cloth(inst, cls=None, no_namespace=False, encoding='utf8'):
    """Returns an ElementTree representation of a
    :class:`spyne.model.complex.ComplexModel` subclass.

    :param inst: The instance of the class to be serialized.
    :param cls: The class to be serialized. Optional.
    :param root_tag_name: The root tag string to use. Defaults to the output of
        ``value.__class__.get_type_name_ns()``.
    :param no_namespace: When true, namespace information is discarded.
    """

    if cls is None:
        cls = inst.__class__

    if cls.get_namespace() is None and no_namespace is None:
        no_namespace = True

    if no_namespace is None:
        no_namespace = False

    ostr = BytesIO()
    xml_cloth = XmlCloth(use_ns=(not no_namespace))
    ctx = FakeContext()
    with etree.xmlfile(ostr, encoding=encoding) as xf:
        ctx.outprot_ctx.doctype_written = False
        ctx.protocol.prot_stack = tlist([], ProtocolMixin)
        tn = cls.get_type_name()
        ret = xml_cloth.subserialize(ctx, cls, inst, xf, tn)

        assert not isgenerator(ret)

    return ostr.getvalue()
Exemplo n.º 3
0
    def _run(self, inst, spid=None, cloth=None):
        cls = inst.__class__
        if cloth is None:
            assert spid is not None
            cloth = etree.fromstring("""<a><b spyne_id="%s"></b></a>""" % spid)
        else:
            assert spid is None

        with etree.xmlfile(self.stream) as parent:
            XmlCloth(cloth=cloth).subserialize(self.ctx, cls, inst, parent)
        elt = etree.fromstring(self.stream.getvalue())
        print etree.tostring(elt, pretty_print=True)
        return elt
Exemplo n.º 4
0
    def _run(self, inst, cls=None):
        if cls is None:
            cls = inst.__class__

        with etree.xmlfile(self.stream) as parent:
            XmlCloth().subserialize(self.ctx,
                                    cls,
                                    inst,
                                    parent,
                                    name=cls.__name__)

        elt = etree.fromstring(self.stream.getvalue())

        print(etree.tostring(elt, pretty_print=True))
        return elt
Exemplo n.º 5
0
from spyne.model.primitive import UnsignedInteger
from spyne.model.primitive import String
from spyne.server.wsgi import WsgiApplication


class HelloWorldService(Service):
    @rpc(String, UnsignedInteger, _returns=Iterable(String))
    def say_hello(ctx, name, times):
        def cb(ret):
            for i in range(times):
                ret.append('Hello, %s' % name)
        return Iterable.Push(cb)

if __name__=='__main__':
    from wsgiref.simple_server import make_server
    logging.basicConfig(level=logging.DEBUG)

    application = Application([HelloWorldService], 'spyne.examples.hello.http',
        in_protocol=HttpRpc(validator='soft'),
        out_protocol=XmlCloth(),
    )

    wsgi_application = WsgiApplication(application)

    server = make_server('127.0.0.1', 8000, wsgi_application)

    logging.info("listening to http://127.0.0.1:8000")
    logging.info("wsdl is at: http://localhost:8000/?wsdl")

    server.serve_forever()