def _deprecated_warn(self, obj, name, old, new): """Checks if a user assigns a value to a deprecated trait """ if isdefined(new): trait_spec = self.traits()[name] msg1 = "Input %s in interface %s is deprecated." % ( name, self.__class__.__name__.split("InputSpec")[0], ) msg2 = ("Will be removed or raise an error as of release %s" % trait_spec.deprecated) if trait_spec.new_name: if trait_spec.new_name not in self.copyable_trait_names(): raise TraitError(msg1 + " Replacement trait %s not found" % trait_spec.new_name) msg3 = "It has been replaced by %s." % trait_spec.new_name else: msg3 = "" msg = " ".join((msg1, msg2, msg3)) if Version(str(trait_spec.deprecated)) < self.package_version: raise TraitError(msg) else: if trait_spec.new_name: msg += "Unsetting old value %s; setting new value %s." % ( name, trait_spec.new_name, ) warn(msg) if trait_spec.new_name: self.trait_set(trait_change_notify=False, **{ "%s" % name: Undefined, "%s" % trait_spec.new_name: new })
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", ))
def _set_increment(self, increment): if isscalar(increment): try: self._increment = absolute(float(increment)) except: raise TraitError(args=self, name='increment', info='Float or CArray(3,)', value=increment) elif len(increment) == 3: self._increment = array(increment,dtype=float) else: raise(TraitError(args=self, name='increment', info='Float or CArray(3,)', value=increment))
def _set_increment3D(self, inc): if not isscalar(inc) and len(inc) == 3: self._increment = array(inc,dtype=float) else: raise(TraitError(args=self, name='increment3D', info='CArray(3,)', value=inc))
def validate(self, object, name, value): """ Validates that a specified value is valid for this trait. """ validated_value = super(ImageFile, self).validate(object, name, value) if validated_value and self.types: _exts = self.grab_exts() if not any(validated_value.endswith(x) for x in _exts): raise TraitError( args="{} is not included in allowed types: {}".format( validated_value, ', '.join(_exts))) return validated_value
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)
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)
def _set_steer(self, steer): if type(steer) == SteeringVector: # This condition may be replaced at a later time by: isinstance(steer, SteeringVector): -- (derived classes allowed) self._steer_obj = steer elif steer in ('true level', 'true location', 'classic', 'inverse'): # Type of steering vectors, see also :ref:`Sarradj, 2012<Sarradj2012>`. warn("Deprecated use of 'steer' trait. " "Please use object of class 'SteeringVector' in the future.", Warning, stacklevel = 2) self._steer_obj = SteeringVector(steer_type = steer) else: raise(TraitError(args=self, name='steer', info='SteeringVector', value=steer))
def list_item_validator(item): """ An item_validator for TraitList that checks that the item is a list. Parameters ---------- item : object Proposed item to add to the list. Returns ------- validated_item : object Actual item to add to the list. Raises ------ TraitError If the item is not valid. """ if isinstance(item, list): return item else: raise TraitError("Value {} is not a list instance".format(item))
def int_item_validator(item): """ An item_validator for TraitList that checks that the item is an int or integer-like object (e.g., any object whose type provides __index__). Parameters ---------- item : object Proposed item to add to the list. Returns ------- validated_item : object Actual item to add to the list. Raises ------ TraitError If the item is not valid. """ try: return int(operator.index(item)) except TypeError: raise TraitError("Value {} is not a valid integer".format(item))
def string_validator(value): if isinstance(value, str): return value else: raise TraitError("string_validator error")
def int_validator(value): try: return _validate_int(value) except TypeError: raise TraitError("int_validator error")