Ejemplo n.º 1
0
    def _validate_length(self, new_length):
        """
        Validate the new length for a proposed operation.

        Parameters
        ----------
        new_length : int
            New length of the list.

        Raises
        ------
        TraitError
            If the proposed new length would violate the length constraints
            of the list.
        """
        trait = getattr(self, "trait", None)
        if trait is None:
            return

        if not trait.minlen <= new_length <= trait.maxlen:
            raise TraitError(
                "The '%s' trait of %s instance must be %s, "
                "but you attempted to change its length to %d %s." % (
                    self.name,
                    class_of(self.object()),
                    self.trait.full_info(self.object(), self.name, Undefined),
                    new_length,
                    "element" if new_length == 1 else "elements",
                ))
Ejemplo n.º 2
0
 def info ( self ):
     aClass = self.aClass
     if aClass is None:
         result = 'path'
     else:
         if type( aClass ) is not str:
             aClass = aClass.__name__
         result = 'path to an instance of ' + class_of( aClass )
     if self.or_none is None:
         return result + ' or an empty string'
     return result
Ejemplo n.º 3
0
 def info(self):
     aClass = self.aClass
     if aClass is None:
         result = "path"
     else:
         if type(aClass) is not str:
             aClass = aClass.__name__
         result = "path to an instance of " + class_of(aClass)
     if self.or_none is None:
         return result + " or an empty string"
     return result
Ejemplo n.º 4
0
    def validate(self, object, name, value):
        """ Validates that a specified value is valid for this trait."""
        if isinstance(value, (str, bytes)):
            if not self.exists:
                return value
            if os.path.isdir(value):
                return value
            else:
                raise TraitError(
                    args='The trait \'{}\' of {} instance is {}, but the path '
                    ' \'{}\' does not exist.'.format(name, class_of(object),
                                                     self.info_text, value))

        self.error(object, name, value)
Ejemplo n.º 5
0
    def validate(self, object, name, value):
        """ Validates that a specified value is valid for this trait."""
        validated_value = super(File, self).validate(object, name, value)
        if not self.exists:
            return validated_value
        elif os.path.isfile(value):
            return validated_value
        else:
            raise TraitError(
                args='The trait \'{}\' of {} instance is {}, but the path '
                ' \'{}\' does not exist.'.format(name, class_of(object),
                                                 self.info_text, value))

        self.error(object, name, value)
Ejemplo n.º 6
0
    def validate(self, object, name, value):
        """ Validates that a specified value is valid for this trait."""
        if isinstance(value, (str, bytes)):
            if not self.exists:
                return value
            if os.path.isdir(value):
                return value
            else:
                raise TraitError(
                    args='The trait \'{}\' of {} instance is {}, but the path '
                    ' \'{}\' does not exist.'.format(name, class_of(object),
                                                     self.info_text, value))

        self.error(object, name, value)
Ejemplo n.º 7
0
    def validate(self, object, name, value):
        """ Validates that a specified value is valid for this trait."""
        validated_value = super(File, self).validate(object, name, value)
        if not self.exists:
            return validated_value
        elif os.path.isfile(value):
            return validated_value
        else:
            raise TraitError(
                args='The trait \'{}\' of {} instance is {}, but the path '
                ' \'{}\' does not exist.'.format(name, class_of(object),
                                                 self.info_text, value))

        self.error(object, name, value)