Example #1
0
    def _serializeObject(self, schema, element, obj):
        # build a map of model attributues, index by the attribute
        # name: as attributes are consumed they are removed from the
        # map.
        mas = self._gatherModelAttributes(obj)

        # attributes:
        for attributeNode in schema.attributes:
            model_attribute = self.consumeModelAttribute(mas, 
                stripTag(attributeNode.name))

            # FIXME: should we call __get__ directly?
            attribute_value = getattr(obj, model_attribute.name, None)
            if attribute_value is None:
                continue
            converter = self.getConverter(model_attribute)
            if converter:
                attribute_value = converter.convertFrom(attribute_value)
            element.set(attributeNode.name,
                attributeNode.type.setTypedValue(attribute_value, None))

        # elements:
        for elementNode in schema.elements:
            model_attribute = self.consumeModelAttribute(mas, 
                stripTag(elementNode.name))

            # FIXME: should we call __get__ directly?
            attribute_value = getattr(obj, model_attribute.name, None)
            if attribute_value is None:
                continue
            converter = self.getConverter(model_attribute)
            if converter:
                attribute_value = converter.convertFrom(attribute_value)

            if isinstance(elementNode, Leaf):
                subelement = elementNode.name(
                    elementNode.type.setTypedValue(attribute_value, None))
                element.append(subelement)

            elif isinstance(elementNode, LeafList):
                for item in attribute_value:
                    subelement = elementNode.name(
                        elementNode.type.setTypedValue(item, None))
                    element.append(subelement)

            elif isinstance(elementNode, Container):
                if isinstance(model_attribute, Sequence):
                    for item in attribute_value:
                        subelement = elementNode.name()
                        self._serializeObject(elementNode, subelement, item)
                        element.append(subelement)
                elif isinstance(model_attribute, Reference):
                    if not type(attribute_value) in (list, tuple): #, iterator):
                        attribute_value = [attribute_value]
                    for item in attribute_value:
                        subelement = elementNode.name()
                        self._serializeObject(elementNode, subelement, item)
                        element.append(subelement)

        return element
Example #2
0
    def _deserializeObject(self, schema, element, obj):
        # build a map of model attributues, index by the attribute
        # name: as attributes are consumed they are removed from the
        # map.
        mas = self._gatherModelAttributes(obj)

        for attributeNode in schema.attributes:
            model_attribute = self.consumeModelAttribute(mas, 
                stripTag(attributeNode.name))

            attribute_value = element.get(attributeNode.name, None)
            if attribute_value is None:
                continue

            attribute_value = attributeNode.type.getTypedValue(attribute_value, None)
            converter = self.getConverter(model_attribute)
            if converter:
                attribute_value = converter.convertTo(attribute_value)
            
            setattr(obj, model_attribute.name, attribute_value)

        index = 0
        for elementNode in schema.elements:
            model_attribute = self.consumeModelAttribute(mas, 
                stripTag(elementNode.name))
            converter = self.getConverter(model_attribute)

            elements = []
            while index < len(element) and element[index].tag == elementNode.name:
                elements.append(element[index])
                index += 1

            if isinstance(elementNode, Leaf):
                if not elements:
                    continue
                value = elementNode.type.getTypedValue(elements[0].text, None)
                if converter is not None:
                    value = converter.convertTo(value)
                setattr(obj, model_attribute.name, value)

            elif isinstance(elementNode, LeafList):
                values = []
                for subelement in elements:
                    value = elementNode.type.getTypedValue(subelement.text, None)
                    if converter is not None:
                        value = converter.convertTo(value)
                    values.append(value)
                setattr(obj, model_attribute.name, values)

            elif isinstance(elementNode, Container):
                if isinstance(model_attribute, Reference):
                    if not elements:
                        continue
                    cobj = model_attribute.referenceType(_do_not_finalize=True)
                    self._deserializeObject(elementNode, elements[0], cobj)
                    cobj._finalize()
                    setattr(obj, model_attribute.name, cobj)
                elif isinstance(model_attribute, Sequence):
                    values = []
                    for subelement in elements:
                        cobj = model_attribute.elementType(_do_not_finalize=True)
                        self._deserializeObject(elementNode, subelement, cobj)
                        cobj._finalize()
                        values.append(cobj)
                    setattr(obj, model_attribute.name, values)
                else:
                    assert False, "implement 2"
        return obj