Beispiel #1
0
 def __delitem__(self, name):
     """
     Remove a field from the field set. May raise an MissingField exception
     if the field has already been deleted.
     """
     if name in self._deleted:
         raise MissingField(self, name)
     self._deleted.add(name)
     if name in self._fields:
         del self._fields[name]
Beispiel #2
0
 def getField(self, key, const=True):
     if isinstance(key, (int, long)):
         if key < 0:
             raise KeyError("Key must be positive!")
         if not const:
             self.readFirstFields(key + 1)
         if len(self._fields.values) <= key:
             raise MissingField(self, key)
         return self._fields.values[key]
     return Field.getField(self, key, const)
Beispiel #3
0
 def _getFieldAddress(self, name):
     """
     Compute relative address of a field. The operation takes care of
     deleted and resized fields.
     """
     #assert name not in self._deleted
     addr = 0
     for field in self:
         if field.name == name:
             return addr
         addr += field.size
     raise MissingField(self, name)
Beispiel #4
0
 def __delitem__(self, name):
     """
     Remove a field from the field set. May raise an MissingField exception
     if the field has already been deleted.
     """
     parts = name.partition('/')
     if parts[2]:
         fieldset = self[parts[0]]
         del fieldset[parts[2]]
         return
     if name in self._deleted:
         raise MissingField(self, name)
     self._deleted.add(name)
     if name in self._fields:
         del self._fields[name]
Beispiel #5
0
    def __getitem__(self, key):
        """
        Create a weak reference to an editable field (EditableField) for the
        field with specified name. If the field is removed later, using the
        editable field will raise a weakref.ReferenceError exception.

        May raise a MissingField error if the field doesn't exist in original
        field set or it has been deleted.
        """
        if "/" in key:
            return self._getItemByPath(key.split("/"))
        if isinstance(key, (int, long)):
            raise EditorError("Integer index are not supported")

        if (key in self._deleted) or (key not in self.input):
            raise MissingField(self, key)
        if key not in self._fields:
            field = self.input[key]
            if field.is_field_set:
                self._fields[key] = createEditableFieldSet(self, field)
            else:
                self._fields[key] = createEditableField(self, field)
        return weakref.proxy(self._fields[key])
Beispiel #6
0
    def _insert(self, key, new_fields, next):
        """
        key is the name of the field before which new_fields
        will be inserted. If next is True, the fields will be inserted
        _after_ this field.
        """
        # Set unique field name
        for field in new_fields:
            if field._name.endswith("[]"):
                self.input.setUniqueFieldName(field)

        # Check that there is no duplicate in inserted fields
        new_names = list(field.name for field in new_fields)
        names_set = set(new_names)
        if len(names_set) != len(new_fields):
            duplicates = (name for name in names_set
                          if 1 < new_names.count(name))
            raise UniqKeyError(
                _("Duplicates in inserted fields: %s") % ", ".join(duplicates))

        # Check that field names are not in input
        if self.input:  # Write special version for NewFieldSet?
            for name in new_names:
                if name in self.input and name not in self._deleted:
                    raise UniqKeyError(
                        _("Field name '%s' already exists") % name)

        # Check that field names are not in inserted fields
        for fields in self._inserted.itervalues():
            for field in fields:
                if field.name in new_names:
                    raise UniqKeyError(
                        _("Field name '%s' already exists") % field.name)

        # Input have already inserted field?
        if key in self._inserted:
            if next:
                self._inserted[key].extend(reversed(new_fields))
            else:
                self._inserted[key].extendleft(reversed(new_fields))
            return

        # Whould like to insert in inserted fields?
        if key:
            for fields in self._inserted.itervalues():
                names = [item.name for item in fields]
                try:
                    pos = names.index(key)
                except ValueError:
                    continue
                if 0 <= pos:
                    if next:
                        pos += 1
                    fields.rotate(-pos)
                    fields.extendleft(reversed(new_fields))
                    fields.rotate(pos)
                    return

            # Get next field. Use None if we are at the end.
            if next:
                index = self.input[key].index + 1
                try:
                    key = self.input[index].name
                except IndexError:
                    key = None

            # Check that field names are not in input
            if key not in self.input:
                raise MissingField(self, key)

        # Insert in original input
        self._inserted[key] = deque(new_fields)
 def __getitem__(self, key):
     # TODO: Implement this function!
     raise MissingField(self, key)