Beispiel #1
0
 def _assertSerialized(self, field, value, expected):
     element = utils.valueToElement(field, value, 'value')
     sio = StringIO()
     etree.ElementTree(element).write(sio)
     self.assertEquals(sio.getvalue(), expected)
     unserialized = utils.elementToValue(field, element)
     self.assertEquals(value, unserialized)
Beispiel #2
0
 def _assertSerialized(self, field, value, expected):
     element = utils.valueToElement(field, value, 'value')
     sio = StringIO()
     etree.ElementTree(element).write(sio)
     self.assertEquals(sio.getvalue(), expected)
     unserialized = utils.elementToValue(field, element)
     self.assertEquals(value, unserialized)
 def readAttribute(self, element, attributeField):
     if element.tag == 'values':
         if any([child.get('key') for child in element]):
             attributeField = OrderedDictField(
                 key_type=zope.schema.TextLine(),
                 value_type=zope.schema.TextLine(),
                 )
     return elementToValue(attributeField, element)
 def readAttribute(self, element, attributeField):
     if element.tag == 'values':
         if any([child.get('key') for child in element]):
             attributeField = OrderedDictField(
                 key_type=zope.schema.TextLine(),
                 value_type=zope.schema.TextLine(),
                 )
     return elementToValue(attributeField, element)
Beispiel #5
0
 def read(self, widgetNode, params):
     for attributeName, attributeField in self.fieldAttributes.items():
         for node in widgetNode.iterchildren():
             if noNS(node.tag) == attributeName:
                 params[attributeName] = elementToValue(
                     attributeField,
                     node
                 )
Beispiel #6
0
 def readAttribute(self, element, attributeField):
     if (etree.QName(element).localname == 'values'
             and any([child.get('key') for child in element])):
         attributeField = OrderedDictField(
             key_type=zope.schema.TextLine(),
             value_type=zope.schema.TextLine(),
         )
     return elementToValue(attributeField, element)
Beispiel #7
0
 def readAttribute(self, element, attributeField):
     if (
         etree.QName(element).localname == 'values' and
         any([child.get('key') for child in element])
     ):
         attributeField = OrderedDictField(
             key_type=zope.schema.TextLine(),
             value_type=zope.schema.TextLine(),
         )
     return elementToValue(attributeField, element)
Beispiel #8
0
    def import_node(self, interface, child):
        """Import a single <property /> node
        """
        property_name = child.getAttribute('name')

        field = interface.get(property_name, None)
        if field is None:
            return

        field = field.bind(self.element)
        # child is minidom but supermodel needs an etree node so we need to convert it
        child = etree.fromstring(child.toxml())

        value = elementToValue(field, child)

        field.validate(value)
        field.set(self.element, value)
 def readAttribute(self, element, attributeField):
     """Read a single attribute from the given element. The attribute is of
     a type described by the given Field object.
     """
     return elementToValue(attributeField, element)
Beispiel #10
0
    def importRecord(self, node):
        name = node.get('name', '')
        if node.get('delete') is not None:
            self.logger.warning(
                u"The 'delete' attribute of <record /> nodes is deprecated, "
                u"it should be replaced with 'remove'."
            )
        remove = node.get('remove', node.get('delete', 'false'))
        interfaceName = node.get('interface', None)
        fieldName = node.get('field', None)

        if not name and (interfaceName and fieldName):
            prefix = node.get('prefix', None)
            if prefix is None:
                prefix = interfaceName

            name = "%s.%s" % (prefix, fieldName)

        if not name:
            raise NameError("No name given for <record /> node!")

        # Unicode is not supported
        name = str(name)
        __traceback_info__ = 'record name: {0}'.format(name)

        # Handle deletion and quit
        if remove.lower() == 'true':
            if name in self.context.records:
                del self.context.records[name]
                self.logger.info("Removed record %s." % name)
            else:
                self.logger.warning(
                    "Record {0} was marked for deletion, but was not "
                    "found.".format(name)
                )
            return

        # See if we have an existing record
        existing_record = self.context.records.get(name, None)

        interface = None
        field = None
        value = _marker
        value_purge = True

        # If we are given an interface and field name, try to resolve them
        if interfaceName and fieldName:
            try:
                interface = resolve(interfaceName)
                field = IPersistentField(interface[fieldName])
            except ImportError:
                self.logger.warning(
                    'Failed to import interface {0} for '
                    'record {1}'.format(interfaceName, name)
                )
                interface = None
                field = None
            except KeyError:
                self.logger.warning(
                    'Interface {0} specified for record %s has '
                    'no field {1}.'.format(interfaceName, name, fieldName)
                )
                interface = None
                field = None
            except TypeError:
                self.logger.warning(
                    "Field {0} in interface {1} specified for record {2} "
                    "cannot be used as a persistent field.".format(
                        fieldName,
                        interfaceName,
                        name
                    )
                )
                interface = None
                field = None

        # Find field and value nodes

        field_node = None
        value_node = None

        for child in node:
            if not isinstance(child.tag, str):
                continue
            elif child.tag.lower() == 'field':
                field_node = child
            elif child.tag.lower() == 'value':
                value_node = child

        # Let field not potentially override interface[fieldName]
        if field_node is not None:
            field_ref = field_node.attrib.get('ref', None)
            if field_ref is not None:
                # We have a field reference
                if field_ref not in self.context:
                    raise KeyError(
                        u'Record {0} references field for record {1}, '
                        u'which does not exist'.format(name, field_ref)
                    )
                ref_record = self.context.records[field_ref]
                field = FieldRef(field_ref, ref_record.field)
            else:
                # We have a standard field
                field_type = field_node.attrib.get('type', None)
                field_type_handler = queryUtility(
                    IFieldExportImportHandler,
                    name=field_type
                )
                if field_type_handler is None:
                    raise TypeError(
                        "Field of type {0} used for record {1} is not "
                        "supported.".format(field_type, name)
                    )
                else:
                    field = field_type_handler.read(field_node)
                    if not IPersistentField.providedBy(field):
                        raise TypeError(
                            "Only persistent fields may be imported. {0} used "
                            "for record {1} is invalid.".format(
                                field_type,
                                name
                            )
                        )

        if field is not None and not IFieldRef.providedBy(field):
            # Set interface name and fieldName, if applicable
            field.interfaceName = interfaceName
            field.fieldName = fieldName

        # Fall back to existing record if neither a field node nor the
        # interface yielded a field

        change_field = True

        if field is None and existing_record is not None:
            change_field = False
            field = existing_record.field

        if field is None:
            raise ValueError(
                "Cannot find a field for the record {0}. Add a <field /> "
                "element or reference an interface and field name.".format(
                    name
                )
            )

        # Extract the value

        if value_node is not None:
            value_purge = value_node.attrib.get('purge', '').lower() != 'false'
            value = elementToValue(field, value_node, default=_marker)

        # Now either construct or update the record

        if value is _marker:
            value = field.default
            value_purge = True

        if existing_record is not None:
            if change_field:
                existing_record.field = field
            existing_value = existing_record.value
            if change_field or value != existing_value:

                if not value_purge and type(value) == type(existing_value):
                    if isinstance(value, list):
                        value = (
                            existing_value +
                            [v for v in value if v not in existing_value]
                        )
                    elif isinstance(value, tuple):
                        value = (
                            existing_value +
                            tuple(
                                [v for v in value if v not in existing_value]
                            )
                        )
                    elif isinstance(value, (set, frozenset, )):
                        value = existing_value.union(value)
                    elif isinstance(value, dict):
                        for key, value in value.items():
                            # check if value is list, if so, let's add
                            # instead of overridding
                            if (
                                type(value) == list and
                                key in existing_value and
                                not shouldPurgeList(value_node, key)
                            ):
                                existing = existing_value[key]
                                for item in existing:
                                    # here, we'll remove existing items
                                    # point is that we don't want
                                    # duplicates and don't want to reorder
                                    if item in value:
                                        value.remove(item)
                                existing.extend(value)
                                value = existing
                            existing_value[key] = value
                        value = existing_value

                existing_record.value = value
        else:
            self.context.records[name] = Record(field, value)
Beispiel #11
0
    def importRecord(self, node):
        name = node.get('name', '')
        if node.get('delete') is not None:
            self.logger.warning(u"The 'delete' attribute of <record /> nodes "
                                u"is deprecated, it should be replaced with "
                                u"'remove'.")
        remove = node.get('remove', node.get('delete', 'false'))

        interfaceName = node.get('interface', None)
        fieldName = node.get('field', None)

        if not name and (interfaceName and fieldName):
            prefix = node.get('prefix', None)
            if prefix is None:
                prefix = interfaceName

            name = "%s.%s" % (prefix, fieldName)

        if not name:
            raise NameError("No name given for <record /> node!")

        # Unicode is not supported
        name = str(name)

        # Handle deletion and quit
        if remove.lower() == 'true':
            if name in self.context.records:
                del self.context.records[name]
                self.logger.info("Removed record %s." % name)
            else:
                self.logger.warning(
                    "Record %s was marked for deletion, but was not found." %
                    name)
            return

        # See if we have an existing record
        existing_record = self.context.records.get(name, None)

        interface = None
        field = None
        value = _marker
        value_purge = True

        # If we are given an interface and field name, try to resolve them
        if interfaceName and fieldName:
            try:
                interface = resolve(interfaceName)
                field = IPersistentField(interface[fieldName])
            except ImportError:
                self.logger.warning("Failed to import interface %s for \
                    record %s" % (interfaceName, name))
                interface = None
                field = None
            except KeyError:
                self.logger.warning("Interface %s specified for record %s has \
                    no field %s." % (interfaceName, name, fieldName))
                interface = None
                field = None
            except TypeError:
                self.logger.warning("Field %s in interface %s specified for \
                    record %s cannot be used as a persistent field." %
                                    (fieldName, interfaceName, name))
                interface = None
                field = None

        # Find field and value nodes

        field_node = None
        value_node = None

        for child in node:
            if not isinstance(child.tag, str):
                continue
            elif child.tag.lower() == 'field':
                field_node = child
            elif child.tag.lower() == 'value':
                value_node = child

        # Let field not potentially override interface[fieldName]
        if field_node is not None:
            field_ref = field_node.attrib.get('ref', None)
            if field_ref is not None:
                # We have a field reference
                if field_ref not in self.context:
                    raise KeyError(
                        u"Record %s references field for record %s, \
                        which does not exist" % (name, field_ref))
                ref_record = self.context.records[field_ref]
                field = FieldRef(field_ref, ref_record.field)
            else:
                # We have a standard field
                field_type = field_node.attrib.get('type', None)
                field_type_handler = queryUtility(IFieldExportImportHandler,
                                                  name=field_type)
                if field_type_handler is None:
                    raise TypeError(
                        "Field of type %s used for record %s is not supported."
                        % (field_type, name))
                else:
                    field = field_type_handler.read(field_node)
                    if not IPersistentField.providedBy(field):
                        raise TypeError(
                            "Only persistent fields may be imported. \
                            %s used for record %s is invalid." %
                            (field_type, name))

        if field is not None and not IFieldRef.providedBy(field):

            # Set interface name and fieldName, if applicable
            field.interfaceName = interfaceName
            field.fieldName = fieldName

        # Fall back to existing record if neither a field node nor the
        # interface yielded a field

        change_field = True

        if field is None and existing_record is not None:
            change_field = False
            field = existing_record.field

        if field is None:
            raise ValueError("Cannot find a field for the record %s. Add a \
                <field /> element or reference an interface and field name." %
                             name)

        # Extract the value

        if value_node is not None:
            value_purge = value_node.attrib.get('purge', '').lower() != 'false'
            value = elementToValue(field, value_node, default=_marker)

        # Now either construct or update the record

        if value is _marker:
            value = field.default
            value_purge = True

        if existing_record is not None:
            if change_field:
                existing_record.field = field
            existing_value = existing_record.value
            if change_field or value != existing_value:

                if not value_purge and type(value) == type(existing_value):
                    if isinstance(value, list):
                        value = existing_value + [
                            v for v in value if v not in existing_value
                        ]
                    elif isinstance(value, tuple):
                        value = existing_value + tuple(
                            [v for v in value if v not in existing_value])
                    elif isinstance(value, (
                            set,
                            frozenset,
                    )):
                        value = existing_value.union(value)
                    elif isinstance(value, dict):
                        for key, value in value.items():
                            # check if value is list, if so, let's add
                            # instead of overridding
                            if type(value) == list:
                                if key in existing_value and \
                                        not shouldPurgeList(value_node, key):
                                    existing = existing_value[key]
                                    for item in existing:
                                        # here, we'll remove existing items
                                        # point is that we don't want duplicates
                                        # and don't want to reorder
                                        if item in value:
                                            value.remove(item)
                                    existing.extend(value)
                                    value = existing
                            existing_value[key] = value

                        value = existing_value

                existing_record.value = value
        else:
            self.context.records[name] = Record(field, value)
 def readAttribute(self, element, attributeField):
     """Read a single attribute from the given element. The attribute is of
     a type described by the given Field object.
     """
     return elementToValue(attributeField, element)
Beispiel #13
0
    def importRecord(self, node):
        
        name = node.get('name', '')
        delete = node.get('delete', 'false')
        
        interfaceName = node.get('interface', None)
        fieldName = node.get('field', None)

        if not name and (interfaceName and fieldName):
            prefix = node.get('prefix', None)
            if prefix is None:
                prefix = interfaceName
            
            name = "%s.%s" % (prefix, fieldName,)
        
        if not name:
            raise NameError("No name given for <record /> node!")
        
        # Unicode is not supported
        name = str(name)
        
        # Handle deletion and quit
        if delete.lower() == 'true':
            if name in self.context.records:
                del self.context.records[name]
                self.logger.info("Deleted record %s." % name)
            else:
                self.logger.warning("Record %s was marked for deletion, but was not found." % name)
            return
        
        # See if we have an existing record
        existing_record = self.context.records.get(name, None)
        
        interface = None
        field = None
        value = _marker
        value_purge = True
        
        # If we are given an interface and field name, try to resolve them
        if interfaceName and fieldName:
            try:
                interface = resolve(interfaceName)
                field = IPersistentField(interface[fieldName])
            except ImportError:
                self.logger.warning("Failed to import interface %s for record %s" % (interfaceName, name))
                interface = None
                field = None
            except KeyError:
                self.logger.warning("Interface %s specified for record %s has no field %s." % (interfaceName, name, fieldName,))
                interface = None
                field = None
            except TypeError:
                self.logger.warning("Field %s in interface %s specified for record %s cannot be used as a persistent field." % (fieldName, interfaceName, name,))            
                interface = None
                field = None
        
        # Find field and value nodes
        
        field_node = None
        value_node = None
         
        for child in node:
            if child.tag.lower() == 'field':
                field_node = child
            elif child.tag.lower() == 'value':
                value_node = child
        
        # Let field not potentially override interface[fieldName]
        if field_node is not None:
            field_ref = field_node.attrib.get('ref', None)
            if field_ref is not None:
                # We have a field reference
                if field_ref not in self.context:
                    raise KeyError(u"Record %s references field for record %s, which does not exist" % (name, field_ref,))
                ref_record = self.context.records[field_ref]
                field = FieldRef(field_ref, ref_record.field)
            else:
                # We have a standard field
                field_type = field_node.attrib.get('type', None)
                field_type_handler = queryUtility(IFieldExportImportHandler, name=field_type)
                if field_type_handler is None:
                    raise TypeError("Field of type %s used for record %s is not supported." % (field_type, name,))
                else:
                    field = field_type_handler.read(field_node)
                    if not IPersistentField.providedBy(field):
                        raise TypeError("Only persistent fields may be imported. %s used for record %s is invalid." % (field_type, name,))
        
        if field is not None and not IFieldRef.providedBy(field):
            
            # Set interface name and fieldName, if applicable
            field.interfaceName = interfaceName
            field.fieldName = fieldName
        
        # Fall back to existing record if neither a field node nor the
        # interface yielded a field
        
        change_field = True
        
        if field is None and existing_record is not None:
            change_field = False
            field = existing_record.field
        
        if field is None:
            raise ValueError("Cannot find a field for the record %s. Add a <field /> element or reference an interface and field name." % name)
        
        # Extract the value

        if value_node is not None:
            value_purge = value_node.attrib.get('purge', '').lower() != 'false'
            value = elementToValue(field, value_node, default=_marker)

        # Now either construct or update the record
        
        if value is _marker:
            value = field.default
            value_purge = True
        
        if existing_record is not None:
            if change_field:
                existing_record.field = field
            existing_value = existing_record.value
            if change_field or value != existing_value:
                
                if not value_purge and type(value) == type(existing_value):
                    if isinstance(value, list):
                        value = existing_value + [v for v in value if v not in existing_value]
                    elif isinstance(value, tuple):
                        value = existing_value + tuple([v for v in value if v not in existing_value])
                    elif isinstance(value, (set, frozenset,)):
                        value = existing_value.union(value)
                    elif isinstance(value, dict):
                        existing_value.update(value)
                        value = existing_value
                
                existing_record.value = value
        else:
            self.context.records[name] = Record(field, value)