Esempio n. 1
0
 def values_changed(self):
     """ Recomputes the cached data based on the underlying enumeration model
         or the values of the factory.
     """
     self._names, self._mapping, self._inverse_mapping = enum_values_changed(
         self._value(), self.string_value
     )
Esempio n. 2
0
    def _values_changed(self):
        """ Recomputes the mappings whenever the **values** trait is changed.
        """
        self._names, self._mapping, self._inverse_mapping = \
            enum_values_changed(self.values)

        self.values_modified = True
Esempio n. 3
0
 def values_changed(self):
     """ Recomputes the cached data based on the underlying enumeration model.
     """
     self._names, self._mapping, self._inverse_mapping = \
         enum_values_changed(self._value(), self.string_value)
Esempio n. 4
0
    def _funcs(self, object, name):
        """Create the evalution and formatting functions for the editor.

        Parameters
        ----------
        object : instance of HasTraits
            This is the object that has the List trait for which we are
            creating an editor.

        name : str
            Name of the List trait on `object`.

        Returns
        -------
        evaluate, fmt_func : callable, callable
            The functions for converting a string to a list (`evaluate`)
            and a list to a string (`fmt_func`).  These are the functions
            that are ultimately given as the keyword arguments 'evaluate'
            and 'format_func' of the TextEditor that will be generated by
            the CSVListEditor editor factory functions.
        """
        t = getattr(object, name)
        # Get the list of inner traits.  Only a single inner trait is allowed.
        it_list = t.trait.inner_traits()
        if len(it_list) > 1:
            raise TraitError("Only one inner trait may be specified when "
                             "using a CSVListEditor.")

        # `it` is the single inner trait.  This will be an instance of
        # traits.traits.CTrait.
        it = it_list[0]
        # The following 'if' statement figures out the appropriate evaluation
        # function (evaluate) and formatting function (fmt_func) for the
        # given inner trait.
        if (it.is_trait_type(Int) or it.is_trait_type(Float)
                or it.is_trait_type(Str)):
            evaluate = lambda s: _eval_list_str(
                s,
                sep=self.sep,
                item_eval=it.trait_type.evaluate,
                ignore_trailing_sep=self.ignore_trailing_sep,
            )
            fmt_func = lambda vals: _format_list_str(vals, sep=self.sep)
        elif it.is_trait_type(Enum):
            values, mapping, inverse_mapping = enum_values_changed(it)
            evaluate = lambda s: _eval_list_str(
                s,
                sep=self.sep,
                item_eval=mapping.__getitem__,
                ignore_trailing_sep=self.ignore_trailing_sep,
            )
            fmt_func = lambda vals: _format_list_str(
                vals, sep=self.sep, item_format=inverse_mapping.__getitem__)
        elif it.is_trait_type(Range):
            # Get the type of the values from the default value.
            # range_object will be an instance of traits.trait_types.Range.
            range_object = it.handler
            if range_object.default_value_type == 8:
                # range_object.default_value is callable.
                defval = range_object.default_value(object)
            else:
                # range_object.default_value *is* the default value.
                defval = range_object.default_value
            typ = type(defval)

            if range_object.validate is None:
                # This will be the case for dynamic ranges.
                item_eval = lambda s: _validate_range_value(
                    range_object, object, name, typ(s))
            else:
                # Static ranges have a validate method.
                item_eval = lambda s: range_object.validate(
                    object, name, typ(s))

            evaluate = lambda s: _eval_list_str(
                s,
                sep=self.sep,
                item_eval=item_eval,
                ignore_trailing_sep=self.ignore_trailing_sep,
            )
            fmt_func = lambda vals: _format_list_str(vals, sep=self.sep)
        else:
            raise TraitError("To use a CSVListEditor, the inner trait of the "
                             "List must be Int, Float, Range, Str or Enum.")

        return evaluate, fmt_func