예제 #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()
예제 #2
0
파일: _base.py 프로젝트: plq/spyne
    def _incgen(self, ctx, cls, inst, name):
        """Entry point to the (stack of) XmlCloth-based protocols.

        Not supposed to be overridden.
        """

        if name is None:
            name = cls.get_type_name()

        try:
            with self.docfile(ctx.out_stream, encoding=self.encoding) as xf:
                ctx.outprot_ctx.doctype_written = False
                ctx.protocol.prot_stack = tlist([], ProtocolMixin)
                ret = self.subserialize(ctx, cls, inst, xf, name)

                if isgenerator(ret):  # Poor man's yield from
                    try:
                        while True:
                            sv2 = (yield)
                            ret.send(sv2)

                    except Break as b:
                        try:
                            ret.throw(b)
                        except StopIteration:
                            pass

        except LxmlSyntaxError as e:
            if e.msg == 'no content written':
                pass
            else:
                raise
예제 #3
0
파일: xml.py 프로젝트: 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()
예제 #4
0
    def _incgen(self, ctx, cls, inst, name):
        """Entry point to the (stack of) XmlCloth-based protocols.

        Not supposed to be overridden.
        """

        if name is None:
            name = cls.get_type_name()

        try:
            with self.docfile(ctx.out_stream, encoding=self.encoding) as xf:
                ctx.outprot_ctx.doctype_written = False
                ctx.protocol.prot_stack = tlist([], ProtocolMixin)
                ret = self.subserialize(ctx, cls, inst, xf, name)

                if isgenerator(ret):  # Poor man's yield from
                    try:
                        while True:
                            sv2 = (yield)
                            ret.send(sv2)

                    except Break as b:
                        try:
                            ret.throw(b)
                        except StopIteration:
                            pass

        except LxmlSyntaxError as e:
            if e.msg == 'no content written':
                pass
            else:
                raise
예제 #5
0
    def test_tlist(self):
        tlist([], int)

        a = tlist([1, 2], int)
        a.append(3)
        a += [4]
        a = [5] + [a]
        a = a + [6]
        a[0] = 1
        a[5:] = [5]

        try:
            tlist([1, 2, 'a'], int)
            a.append('a')
            a += ['a']
            _ = ['a'] + a
            _ = a + ['a']
            a[0] = 'a'
            a[0:] = 'a'

        except TypeError:
            pass
        else:
            raise Exception("Must fail")
예제 #6
0
파일: test_util.py 프로젝트: plq/spyne
    def test_tlist(self):
        tlist([], int)

        a = tlist([1, 2], int)
        a.append(3)
        a += [4]
        a = [5] + [a]
        a = a + [6]
        a[0] = 1
        a[5:] = [5]

        try:
            tlist([1, 2, 'a'], int)
            a.append('a')
            a += ['a']
            _ = ['a'] + a
            _ = a + ['a']
            a[0] = 'a'
            a[0:] = 'a'

        except TypeError:
            pass
        else:
            raise Exception("Must fail")
예제 #7
0
파일: _base.py 프로젝트: plq/spyne
    def __init__(self, parent, transport, type=None):
        super(XmlClothProtocolContext, self).__init__(parent, transport, type)

        self.inst_stack = tlist([], tuple)
        self.prot_stack = tlist([], ProtocolMixin)
        self.doctype_written = False
예제 #8
0
    def __init__(self, parent, transport, type=None):
        super(XmlClothProtocolContext, self).__init__(parent, transport, type)

        self.inst_stack = tlist([], tuple)
        self.prot_stack = tlist([], ProtocolMixin)
        self.doctype_written = False