Ejemplo n.º 1
0
    def protect_parameter(self, param, state=True):
        """ Protect the named parameter.

        Protecting is not a real lock, it just marks the parameter a list of
        "protected" parameters. This is typically used to mark values that have
        been set manually by the user (using the ControllerWidget for instance)
        and that should not be later modified by automatic parameters tweaking
        (such as completion systems).

        Protected parameters are listed in an additional trait,
        "protected_parameters".

        If the "state" parameter is False, then we will unprotect it
        (calling unprotect_parameter())
        """
        if not state:
            return self.unprotect_parameter(param)
        if not self.trait('protected_parameters'):
            # add a 'protected_parameters' trait bypassing the
            # Controller.add_trait mechanism (it will not be a "user_trait")
            HasTraits.add_trait(self, 'protected_parameters',
                                traits.List(traits.Str(), default=[],
                                            hidden=True))
            #self.locked_parameters = []
        protected = set(self.protected_parameters)
        protected.update([param])
        self.protected_parameters = sorted(protected)
Ejemplo n.º 2
0
    def import_from_dict(self, state_dict, clear=False):
        """ Set Controller variables from a dictionary. When setting values on
        Controller instances (in the Controller sub-tree), replace dictionaries
        by Controller instances appropriately.

        Parameters
        ----------
        state_dict: dict, sorted_dictionary or OrderedDict
            dict containing the variables to set
        clear: bool (optional, default: False)
            if True, older values (in keys not listed in state_dict) will be
            cleared, otherwise they are left in place.
        """
        if clear:
            for trait_name in self.user_traits():
                if trait_name not in state_dict:
                    delattr(self, trait_name)
        for trait_name, value in six.iteritems(state_dict):
            trait = self.trait(trait_name)
            if trait_name == 'protected_parameters' and trait is None:
                HasTraits.add_trait(self, 'protected_parameters',
                                    traits.List(traits.Str(), default=[],
                                                hidden=True))
                trait = self.trait('protected_parameters')
            if trait is None and not isinstance(self, OpenKeyController):
                raise KeyError(
                    "item %s is not a trait in the Controller" % trait_name)
            if isinstance(trait.trait_type, Instance) \
                    and issubclass(trait.trait_type.klass, Controller):
                controller = trait.trait_type.create_default_value(
                    trait.trait_type.klass)
                controller.import_from_dict(value)
            else:
                if value in (None, Undefined):
                    # None / Undefined may be an acceptable value for many
                    # traits types
                    try:
                        setattr(self, trait_name, value)
                    except traits.TraitError:
                        if value is not Undefined:
                            setattr(self, trait_name, Undefined)
                else:
                    # check trait type for conversions
                    tr = self.trait(trait_name)
                    if tr and isinstance(tr.trait_type, Set):
                        setattr(self, trait_name, set(value))
                    elif tr and isinstance(tr.trait_type, Tuple):
                        setattr(self, trait_name, tuple(value))
                    else:
                        setattr(self, trait_name, value)
Ejemplo n.º 3
0
    def copy(self, with_values=True):
        """ Copy traits definitions to a new Controller object

        Parameters
        ----------
        with_values: bool (optional, default: False)
            if True, traits values will be copied, otherwise the defaut trait
            value will be left in the copy.

        Returns
        -------
        copied: Controller instance
            the returned copy will have the same class as the copied object
            (which may be a derived class from Controller). Traits definitions
            will be copied. Traits values will only be copied if with_values is
            True.
        """
        import copy

        initargs = ()
        if hasattr(self, '__getinitargs__'):
            # if the Controller class is subclassed and needs init parameters
            initargs = self.__getinitargs__()
        copied = self.__class__(*initargs)
        for name, trait in six.iteritems(self.user_traits()):
            copied.add_trait(name, self._clone_trait(trait))
            if with_values:
                setattr(copied, name, getattr(self, name))
        if self.trait('protected_parameters'):
            trait = self.trait('protected_parameters')
            HasTraits.add_trait(copied, 'protected_parameters',
                                self._clone_trait(trait))
            if with_values:
                setattr(copied, 'protected_parameters',
                        getattr(self, 'protected_parameters'))
        return copied
Ejemplo n.º 4
0
#class TestClass(HasTraits):
#    b1 = Bool
#    b2 = Bool
#    b3 = Bool
#    _updated = Bool(False)

#
#for e in dir(view1):
#    print e

#for attr in ['b1', "title", "handler", "buttons"]:
#    print attr, getattr(view1, attr, None)

#tc = TestClass()
#tc.add_trait( 'b4',Bool)

t = HasTraits()
nameL = []
for i in range(4):
    name = 'r%d' % i
    t.add_trait(name, Range(1, 10, i))
    nameL.append(name)

view1 = View(nameL,
             title="Alter Title",
             handler=TC_Handler(),
             buttons=['OK', 'Cancel'])

t.configure_traits(view=view1)
Ejemplo n.º 5
0
#class TestClass(HasTraits):
#    b1 = Bool
#    b2 = Bool
#    b3 = Bool
#    _updated = Bool(False)


#
#for e in dir(view1):
#    print e

#for attr in ['b1', "title", "handler", "buttons"]:
#    print attr, getattr(view1, attr, None)

#tc = TestClass()
#tc.add_trait( 'b4',Bool)

t = HasTraits()
nameL = []
for i in range(4):
    name = 'r%d'%i
    t.add_trait(name, Range(1,10,i) )
    nameL.append(name)

view1 = View(nameL,
             title="Alter Title",
             handler=TC_Handler(),
             buttons = ['OK', 'Cancel'])

t.configure_traits(view=view1)