Beispiel #1
0
    def from_element(self, ctx, cls, element):
        cls_attrs = self.get_cls_attrs(cls)

        if bool(element.get(XSI('nil'))):
            if self.validator is self.SOFT_VALIDATION and not \
                                                             cls_attrs.nillable:
                raise ValidationError(None)

            if self.replace_null_with_default:
                return cls_attrs.default

            return None

        # if present, use the xsi:type="ns0:ObjectName"
        # attribute to instantiate subclass objects
        if self.parse_xsi_type:
            xsi_type = element.get(XSI_TYPE, None)
            if xsi_type is not None:
                if ":" in xsi_type:
                    prefix, objtype = xsi_type.split(':', 1)
                else:
                    prefix, objtype = None, xsi_type

                ns = element.nsmap.get(prefix)
                if ns is not None:
                    classkey = "{%s}%s" % (ns, objtype)

                else:
                    logger.error(
                        "xsi:type namespace prefix "
                        "'%s' in '%s' not found in %r", ns, xsi_type,
                        element.nsmap)

                    raise ValidationError(xsi_type)

                newclass = ctx.app.interface.classes.get(classkey, None)
                if newclass is None:
                    logger.error(
                        "xsi:type '%s' interpreted as class key '%s' "
                        "is not recognized", xsi_type, classkey)
                    raise ValidationError(xsi_type)

                cls = newclass
                logger.debug("xsi:type '%s' overrides %r to %r", xsi_type, cls,
                             newclass)

        handler = self.deserialization_handlers[cls]
        return handler(ctx, cls, element)
Beispiel #2
0
    AnyHtml, AnyXml, AnyDict, Unicode, PushBase, File, ByteArray, XmlData, \
    XmlAttribute
from spyne.model.binary import BINARY_ENCODING_BASE64
from spyne.model.enum import EnumBase

from spyne.protocol import ProtocolBase

from spyne.util import six

if six.PY2:
    STR_TYPES = (str, unicode)
else:
    STR_TYPES = (str, bytes)


NIL_ATTR = {XSI('nil'): 'true'}
XSI_TYPE = XSI('type')


def _append(parent, child_elt):
    if hasattr(parent, 'append'):
        parent.append(child_elt)
    else:
        parent.write(child_elt)


def _gen_tagname(ns, name):
    if ns is not None:
        name = "{%s}%s" % (ns, name)
    return name
 def from_element(self, ctx, cls, element):
     if bool(element.get(XSI('nil'))):
         attributes = dict([(key[key.find('}') + 1:], value)
                            for key, value in element.attrib.items()])
         return Nil(**attributes)
     return super().from_element(ctx, cls, element)