Beispiel #1
0
def valueToElement(field, value, name=None, force=False):
    """Create and return an element that describes the given value, which is
    assumed to be valid for the given field.

    If name is given, this will be used as the new element name. Otherwise,
    the field's __name__ attribute is consulted.

    If force is True, the value will always be written. Otherwise, it is only
    written if it is not equal to field.missing_value.
    """

    if name is None:
        name = field.__name__

    child = etree.Element(name)

    if value is not None and (force or value != field.missing_value):

        if IDict.providedBy(field):
            key_converter = IToUnicode(field.key_type)
            for k, v in value.items():
                list_element = valueToElement(field.value_type, v, 'element', force)
                list_element.attrib['key'] = key_converter.toUnicode(k)
                child.append(list_element)

        elif ICollection.providedBy(field):
            for v in value:
                list_element = valueToElement(field.value_type, v, 'element', force)
                child.append(list_element)

        else:
            converter = IToUnicode(field)
            child.text = converter.toUnicode(value)

    return child
Beispiel #2
0
def valueToElement(field, value, name=None, force=False):
    """Create and return an element that describes the given value, which is
    assumed to be valid for the given field.

    If name is given, this will be used as the new element name. Otherwise,
    the field's __name__ attribute is consulted.

    If force is True, the value will always be written. Otherwise, it is only
    written if it is not equal to field.missing_value.
    """

    if name is None:
        name = field.__name__

    child = etree.Element(name)

    if value is not None and (force or value != field.missing_value):

        if IDict.providedBy(field):
            key_converter = IToUnicode(field.key_type)
            for k, v in sorted(value.items()):
                list_element = valueToElement(field.value_type, v, 'element',
                                              force)
                list_element.attrib['key'] = key_converter.toUnicode(k)
                child.append(list_element)

        elif ICollection.providedBy(field):
            if ISet.providedBy(field):
                # Serliazation should be consistent even if value was not really a set
                value = sorted(value)
            for v in value:
                list_element = valueToElement(field.value_type, v, 'element',
                                              force)
                child.append(list_element)

        else:
            converter = IToUnicode(field)
            child.text = converter.toUnicode(value)

            # handle i18n
            if isinstance(value, Message):
                child.set(ns('domain', I18N_NAMESPACE), value.domain)
                if not value.default:
                    child.set(ns('translate', I18N_NAMESPACE), '')
                else:
                    child.set(ns('translate', I18N_NAMESPACE), child.text)
                    child.text = converter.toUnicode(value.default)

    return child
Beispiel #3
0
    def __call__(self):
        schema = {'type': self.field_type}
        for attribute_name in sorted(self.field_attributes.keys()):
            attribute_field = self.field_attributes[attribute_name]
            if attribute_name in self.filtered_attributes:
                continue

            element_name = attribute_field.__name__
            attribute_field = attribute_field.bind(self.field)
            force = (element_name in self.forced_fields)
            value = attribute_field.get(self.field)

            # if ignoreDefault and value == attributeField.default:
            #     return None

            # # The value points to another field. Recurse.
            # if IField.providedBy(value):
            #     value_fieldType = IFieldNameExtractor(value)()
            #     handler = queryUtility(
            #         IFieldExportImportHandler,
            #         name=value_fieldType
            #     )
            #     if handler is None:
            #         return None
            #     return handler.write(
            #         value, name=None,
            #         type=value_fieldType,
            #         elementName=elementName
            #     )

            # For 'default', 'missing_value' etc, we want to validate against
            # the imported field type itself, not the field type of the attribute
            if element_name in self.field_type_attributes or \
                    element_name in self.non_validated_field_type_attributes:
                attribute_field = self.field

            if isinstance(value, bytes) and not isinstance(value, str):
                value = value.decode('utf-8')

            if value is not None and (force
                                      or value != self.field.missing_value):
                converter = IToUnicode(self.field)
                text = converter.toUnicode(value)

                # handle i18n
                # if isinstance(value, Message):
                #     child.set(ns('domain', I18N_NAMESPACE), value.domain)
                #     if not value.default:
                #         child.set(ns('translate', I18N_NAMESPACE), '')
                #     else:
                #         child.set(ns('translate', I18N_NAMESPACE), child.text)
                #         child.text = converter.toUnicode(value.default)
                schema[attribute_name] = text

        return schema
Beispiel #4
0
    def __call__(self):
        schema = {'type': self.field_type}
        for attribute_name in sorted(self.field_attributes.keys()):
            attribute_field = self.field_attributes[attribute_name]
            if attribute_name in self.filtered_attributes:
                continue

            element_name = attribute_field.__name__
            attribute_field = attribute_field.bind(self.field)
            force = (element_name in self.forced_fields)
            value = attribute_field.get(self.field)

            # if ignoreDefault and value == attributeField.default:
            #     return None

            # # The value points to another field. Recurse.
            # if IField.providedBy(value):
            #     value_fieldType = IFieldNameExtractor(value)()
            #     handler = queryUtility(
            #         IFieldExportImportHandler,
            #         name=value_fieldType
            #     )
            #     if handler is None:
            #         return None
            #     return handler.write(
            #         value, name=None,
            #         type=value_fieldType,
            #         elementName=elementName
            #     )

            # For 'default', 'missing_value' etc, we want to validate against
            # the imported field type itself, not the field type of the attribute
            if element_name in self.field_type_attributes or \
                    element_name in self.non_validated_field_type_attributes:
                attribute_field = self.field

            if isinstance(value, bytes) and not isinstance(value, str):
                value = value.decode('utf-8')

            if value is not None and (force or value != self.field.missing_value):
                converter = IToUnicode(self.field)
                text = converter.toUnicode(value)

                # handle i18n
                # if isinstance(value, Message):
                #     child.set(ns('domain', I18N_NAMESPACE), value.domain)
                #     if not value.default:
                #         child.set(ns('translate', I18N_NAMESPACE), '')
                #     else:
                #         child.set(ns('translate', I18N_NAMESPACE), child.text)
                #         child.text = converter.toUnicode(value.default)
                schema[attribute_name] = text

        return schema
Beispiel #5
0
def valueToElement(field, value, name=None, force=False):
    """Create and return an element that describes the given value, which is
    assumed to be valid for the given field.

    If name is given, this will be used as the new element name. Otherwise,
    the field's __name__ attribute is consulted.

    If force is True, the value will always be written. Otherwise, it is only
    written if it is not equal to field.missing_value.
    """

    if name is None:
        name = field.__name__

    child = ElementTree.Element(name)

    if value is not None and (force or value != field.missing_value):

        if IDict.providedBy(field):
            key_converter = IToUnicode(field.key_type)
            for k, v in value.items():
                list_element = valueToElement(field.value_type, v, 'element',
                                              force)
                list_element.attrib['key'] = key_converter.toUnicode(k)
                child.append(list_element)

        elif ICollection.providedBy(field):
            for v in value:
                list_element = valueToElement(field.value_type, v, 'element',
                                              force)
                child.append(list_element)

        else:
            converter = IToUnicode(field)
            child.text = converter.toUnicode(value)

    return child