3
8ßL]RÕ ã               @   sz  d Z ddlmZ ddlZddlZddlZddlZddljZ	ddl
mZmZ eZddlmZ ddlmZmZmZmZmZmZmZmZmZ ddlmZmZ dd	lmZ dd
lm Z  d Z!Z"ddl#Z#e#j$e%ƒZ&dZ'dZ(dZ)e*e e+fZ,eefZ-dbZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6dZ7dZ8dddœZ9dZ:dd„ Z;dd„ Z<dd„ Z=dd„ Z>dd„ Z?G dd„ de@ƒZAG d d!„ d!e@ƒZBeBƒ ZBG d"d#„ d#eAƒZCG d$d%„ d%eAƒZDG d&d'„ d'eDƒZEG d(d)„ d)eDƒZFG d*d+„ d+eDƒZGG d,d-„ d-eGƒZHG d.d/„ d/eDƒZIdcddd0œZJG d1d2„ d2eIƒZKG d3d4„ d4eKƒZLd5d6„ ZMG d7d8„ d8e@ƒZNG d9d:„ d:eDƒZOG d;d<„ d<eDƒZPG d=d>„ d>eDƒZQG d?d@„ d@eDƒZRG dAdB„ dBeDƒZSG dCdD„ dDeSƒZTG dEdF„ dFeDƒZUG dGdH„ dHeDƒZVG dIdJ„ dJeDƒZWG dKdL„ dLeDƒZXG dMdN„ dNe@ƒZYG dOdP„ dPeDƒZZdQdR„ Z[G dSdT„ dTe\ƒZ]G dUdV„ dVe@ƒZ^G dWdX„ dXe_ƒZ`G dYdZ„ dZe@ƒZaG d[d\„ d\eDƒZbG d]d^„ d^ecƒZddd_lmeZe eejfe]e`edƒ d`da„ Zgeejhegƒ dS )da^  
Defines the BaseTraitHandler class and a standard set of BaseTraitHandler
subclasses for use with the Traits package.

A trait handler mediates the assignment of values to object traits. It
verifies (via its validate() method) that a specified value is consistent
with the object trait, and generates a TraitError exception if it is not
consistent.
é    )Úabsolute_importN)ÚFunctionTypeÚ
MethodType)Úrefé   )	ÚstrxÚSequenceTypesÚ	UndefinedÚ	TypeTypesÚ
ClassTypesÚCoercableTypesÚTraitsCacheÚclass_ofÚMissing)Ú
TraitErrorÚ	repr_type)Ú_py2to3)Ú	LONG_TYPEé   é   é   é   é   é   é   é	   )ÚpythonÚeventc             C   s*   t | ƒtkr"| jdk	r"| jjd S | jjS )zL Returns the correct argument count for a specified function or method.
    Nr   )Útyper   Ú__self__Ú__code__Úco_argcount)Úfunc© r#   ú9/tmp/pip-install-5sebvq2j/traits/traits/trait_handlers.pyÚ
_arg_countˆ   s    r%   c             C   s   t d|t| ƒf ƒ‚d S )Nz.The '%s' trait of %s instance is 'write only'.)r   r   )ÚobjectÚnamer#   r#   r$   Ú_write_only•   s    r(   c             C   s   t d|t| ƒf ƒ‚d S )Nz-The '%s' trait of %s instance is 'read only'.)r   r   )r&   r'   Úvaluer#   r#   r$   Ú
_read_onlyœ   s    r*   c             C   s   t d|t| ƒf ƒ‚d S )NzMThe '%s' trait of %s instance is a property that has no 'get' or 'set' method)r   r   )r&   r'   r#   r#   r$   Ú_undefined_get£   s    r+   c             C   s   t | |ƒ d S )N)r+   )r&   r'   r)   r#   r#   r$   Ú_undefined_set­   s    r,   c               @   sf   e Zd ZdZeZdZdZdZdZ	dd„ Z
dd„ Zd	d
„ Zdd„ Zdd„ Zddd„Zdd„ Zdd„ ZdS )ÚBaseTraitHandleraÔ   The task of this class and its subclasses is to verify the correctness
    of values assigned to object trait attributes.

    This class is an alternative to trait validator functions. A trait handler
    has several advantages over a trait validator function, due to being an
    object:

        * Trait handlers have constructors and state. Therefore, you can use
          them to create *parametrized types*.
        * Trait handlers can have multiple methods, whereas validator functions
          can have only one callable interface. This feature allows more
          flexibility in their implementation, and allows them to handle a
          wider range of cases, such as interactions with other components.
    FNza legal valuec             C   s6   y&| j }y||||ƒ dS    dS W n
   dS d S )NTF)Úvalidate)Úselfr&   r'   r)   r.   r#   r#   r$   Úis_validÍ   s    zBaseTraitHandler.is_validc             C   s   t ||| j|||ƒ|ƒ‚dS )aŒ  Raises a TraitError exception.

        Parameters
        ----------
        object : object
            The object whose attribute is being assigned.
        name : str
            The name of the attribute being assigned.
        value : object
            The proposed new value for the attribute.

        Description
        -----------
        This method is called by the validate() method when an assigned value
        is not valid. Raising a TraitError exception either notifies the user of
        the problem, or, in the case of compound traits, provides a chance for
        another trait handler to handle to validate the value.
        N)r   Ú	full_info)r/   r&   r'   r)   r#   r#   r$   ÚerrorØ   s    zBaseTraitHandler.errorc             C   s   | j ƒ S )a_  Returns a string describing the type of value accepted by the
        trait handler.

        Parameters
        ----------
        object : object
            The object whose attribute is being assigned.
        name : str
            The name of the attribute being assigned.
        value :
            The proposed new value for the attribute.

        Description
        -----------
        The string should be a phrase describing the type defined by the
        TraitHandler subclass, rather than a complete sentence. For example, use
        the phrase, "a square sprocket" instead of the sentence, "The value must
        be a square sprocket." The value returned by full_info() is combined
        with other information whenever an error occurs and therefore makes more
        sense to the user if the result is a phrase. The full_info() method is
        similar in purpose and use to the **info** attribute of a validator
        function.

        Note that the result can include information specific to the particular
        trait handler instance. For example, TraitRange instances return a
        string indicating the range of values acceptable to the handler (e.g.,
        "an integer in the range from 1 to 9"). If the full_info() method is not
        overridden, the default method returns the value of calling the info()
        method.
        )Úinfo)r/   r&   r'   r)   r#   r#   r$   r1   ï   s    zBaseTraitHandler.full_infoc             C   s   | j S )a$  Must return a string describing the type of value accepted by the
        trait handler.

        The string should be a phrase describing the type defined by the
        TraitHandler subclass, rather than a complete sentence. For example, use
        the phrase, "a square sprocket" instead of the sentence, "The value must
        be a square sprocket." The value returned by info() is combined with
        other information whenever an error occurs and therefore makes more
        sense to the user if the result is a phrase. The info() method is
        similar in purpose and use to the **info** attribute of a validator
        function.

        Note that the result can include information specific to the particular
        trait handler instance. For example, TraitRange instances return a
        string indicating the range of values acceptable to the handler (e.g.,
        "an integer in the range from 1 to 9"). If the info() method is not
        overridden, the default method returns the value of the 'info_text'
        attribute.
        )Ú	info_text)r/   r#   r#   r$   r3     s    zBaseTraitHandler.infoc             C   s   ddl }|jdtƒ t|ƒS )ay   Returns a printable representation of a value along with its type.

        .. deprecated :: 3.0.3
            This functionality was only used to provide readable error
            messages. This functionality has been incorporated into
            TraitError itself.

        Parameters
        ----------
        value : object
            The value to be printed.
        r   NzKthis functionality has been merged into TraitError; just pass the raw value)ÚwarningsÚwarnÚDeprecationWarningr   )r/   r)   r5   r#   r#   r$   Úrepr&  s
    zBaseTraitHandler.reprc             C   s   | j dkr| jƒ | _ | j S )ax   Returns a trait editor that allows the user to modify the *trait*
        trait.

        Parameters
        ----------
        trait : Trait
            The trait to be edited.

        Description
        -----------
        This method only needs to be specified if traits defined using this
        trait handler require a non-default trait editor in trait user
        interfaces. The default implementation of this method returns a trait
        editor that allows the user to type an arbitrary string as the value.

        For more information on trait user interfaces, refer to the *Traits UI
        User Guide*.
        N)ÚeditorÚcreate_editor)r/   Útraitr#   r#   r$   Ú
get_editor<  s    

zBaseTraitHandler.get_editorc             C   s   ddl m} |ƒ S )zB Returns the default traits UI editor to use for a trait.
        r   )Ú
TextEditor)Útraitsui.apir=   )r/   r=   r#   r#   r$   r:   T  s    zBaseTraitHandler.create_editorc             C   s   f S )aª   Returns a tuple containing the *inner traits* for this trait. Most
            trait handlers do not have any inner traits, and so will return an
            empty tuple. The exceptions are **List** and **Dict** trait types,
            which have inner traits used to validate the values assigned to the
            trait. For example, in *List( Int )*, the *inner traits* for
            **List** are ( **Int**, ).
        r#   )r/   r#   r#   r$   Úinner_traits[  s    zBaseTraitHandler.inner_traits)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚUNSPECIFIED_DEFAULT_VALUEÚdefault_value_typeÚ	has_itemsÚ	is_mappedr9   r4   r0   r2   r1   r3   r8   r<   r:   r?   r#   r#   r#   r$   r-   ·   s   !
r-   c               @   s   e Zd ZdS )ÚNoDefaultSpecifiedN)r@   rA   rB   r#   r#   r#   r$   rH   k  s   rH   c               @   sz   e Zd ZdZeZi Zefdd„Zdd„ Z	dd„ Z
efdd	„Zddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd
S )Ú	TraitTypea’   Base class for new trait types.

        This class enables you to define new traits using a class-based
        approach, instead of by calling the Trait() factory function with an
        instance of a TraitHandler derived object.

        When subclassing this class, you can implement one or more of the
        method signatures below. Note that these methods are defined only as
        comments, because the absence of method definitions in the subclass
        definition implicitly provides information about how the trait should
        operate.

        The optional methods are as follows:

        * **get ( self, object, name ):**

          This is the getter method of a trait that behaves like a property.

          :Parameters:
            **object** (*object*) -- The object that the property applies to.

            **name** (str) -- The name of the property on *object* property.

          *Description*

          If neither this method nor the set() method is defined, the value
          of the trait is handled like a normal object attribute. If this
          method is not defined, but the set() method is defined, the trait
          behaves like a write-only property. This method should return the
          value of the *name* property for the *object* object.

        * **set ( self, object, name, value )**

          This is the setter method of a trait that behaves like a property.

          :Parameters:
            **object** (*object*) -- The object that the property applies to.

            **name** (str) -- The name of the property on *object*.

            **value** -- The value being assigned as the value of the property.

          *Description*

          If neither this method nor the get() method is implemented, the
          trait behaves like a normal trait attribute. If this method is not
          defined, but the get() method is defined, the trait behaves like a
          read-only property. This method does not need to return a value,
          but it should raise a TraitError exception if the specified *value*
          is not valid and cannot be coerced or adapted to a valid value.

        * **validate ( self, object, name, value )**

          This method validates, coerces, or adapts the specified *value* as
          the value of the *name* trait of the *object* object. This method
          is called when a value is assigned to an object trait that is
          based on this subclass of *TraitType* and the class does not
          contain a definition for either the get() or set() methods. This
          method must return the original *value* or any suitably coerced or
          adapted value that is a legal value for the trait. If *value* is
          not a legal value for the trait, and cannot be coerced or adapted
          to a legal value, the method should either raise a **TraitError** or
          call the **error** method to raise the **TraitError** on its behalf.

        * **is_valid_for ( self, value )**

          As an alternative to implementing the **validate** method, you can
          instead implement the **is_valid_for** method, which receives only
          the *value* being assigned. It should return **True** if the value is
          valid, and **False** otherwise.

        * **value_for ( self, value )**

          As another alternative to implementing the **validate** method, you
          can instead implement the **value_for** method, which receives only
          the *value* being assigned. It should return the validated form of
          *value* if it is valid, or raise a **TraitError** if the value is not
          valid.

        * **post_setattr ( self, object, name, value )**

          This method allows the trait to do additional processing after
          *value* has been successfully assigned to the *name* trait of the
          *object* object. For most traits there is no additional processing
          that needs to be done, and this method need not be defined. It is
          normally used for creating "shadow" (i.e., "mapped" traits), but
          other uses may arise as well. This method does not need to return
          a value, and should normally not raise any exceptions.
    c             K   sˆ   |t k	r|| _t|ƒdkrpt| jƒdkrB| jjƒ | _| jj|ƒ n|| _| jjdƒr|| jjdƒdkr|d| jd< n| jjƒ | _| jƒ  dS )a‡   This constructor method is the only method normally called
            directly by client code. It defines the trait. The
            default implementation accepts an optional, untype-checked default
            value, and caller-supplied trait metadata. Override this method
            whenever a different method signature or a type-checked
            default value is needed.
        r   ÚprivateZvisibleNF)	rH   Údefault_valueÚlenÚmetadataÚcopyÚ	_metadataÚupdateÚgetÚinit)r/   rK   rM   r#   r#   r$   Ú__init__Ð  s    zTraitType.__init__c             C   s   dS )zK Allows the trait to perform any additional initialization needed.
        Nr#   )r/   r#   r#   r$   rR   ì  s    zTraitType.initc             C   st   | j }| j}|dk rlt}t|tƒr(t}n>t|tƒr8t}n.t|tƒrHt	}nt|t
ƒrXt}nt|tƒrft}|| _||fS )aÉ  Returns a tuple of the form: (*default_value_type*, *default_value*)
            which describes the default value for this trait. The default
            implementation analyzes the value of the trait's **default_value**
            attribute and determines an appropriate *default_value_type* for
            *default_value*. If you need to override this method to provide a
            different result tuple, the following values are valid values for
            *default_value_type*:

                - 0, 1: The *default_value* item of the tuple is the default
                  value.
                - 2: The object containing the trait is the default value.
                - 3: A new copy of the list specified by *default_value* is
                  the default value.
                - 4: A new copy of the dictionary specified by *default_value*
                  is the default value.
                - 5: A new instance of TraitListObject constructed using the
                  *default_value* list is the default value.
                - 6: A new instance of TraitDictObject constructed using the
                  *default_value* dictionary is the default value.
                - 7: *default_value* is a tuple of the form: (*callable*, *args*,
                  *kw*), where *callable* is a callable, *args* is a tuple, and
                  *kw* is either a dictionary or None. The default value is the
                  result obtained by invoking callable(\*args, \*\*kw).
                - 8: *default_value* is a callable. The default value is the
                  result obtained by invoking *default_value*(*object*), where
                  *object* is the object containing the trait. If the trait has
                  a validate() method, the validate() method is also called to
                  validate the result.
                - 9: A new instance of TraitSetObject constructed using the
                  *default_value* set is the default value.
        r   )rK   rE   ÚCONSTANT_DEFAULT_VALUEÚ
isinstanceÚTraitListObjectÚTRAIT_LIST_OBJECT_DEFAULT_VALUEÚlistÚLIST_COPY_DEFAULT_VALUEÚTraitDictObjectÚTRAIT_DICT_OBJECT_DEFAULT_VALUEÚdictÚDICT_COPY_DEFAULT_VALUEÚTraitSetObjectÚTRAIT_SET_OBJECT_DEFAULT_VALUE)r/   ZdvZdvtr#   r#   r$   Úget_default_valueñ  s      




zTraitType.get_default_valuec          
   K   s¤   d|kr| |d< | j j| j ƒ}|j}|j| jƒ d|kr>|d= d|krT|jjƒ |_ni |_|jj|ƒ |tk	r ||_| jdk	r y| jdd|ƒ|_W n   Y nX |S )aŽ   Clones the contents of this object into a new instance of the same
            class, and then modifies the cloned copy using the specified
            *default_value* and *metadata*. Returns the cloned object as the
            result.

            Note that subclasses can change the signature of this method if
            needed, but should always call the 'super' method if possible.
        Úparentr9   rO   N)	Ú	__class__Ú__new__Ú__dict__rP   rO   rN   r   rK   r.   )r/   rK   rM   ÚnewZnew_dictr#   r#   r$   Úclone$  s(    	
zTraitType.cloneNc             C   sJ   t | }|jj|tƒ}|tkrF|dkr0|j|ƒ}|j||ƒ |j|< }|S )z> Returns the current value of a property-based trait.
        N)r   rd   rQ   r	   r;   Zdefault_value_for)r/   r&   r'   r;   Úcnamer)   r#   r#   r$   Ú	get_valueJ  s    
zTraitType.get_valuec             C   s:   t | }|jj|tƒ}||kr6||j|< |j|||ƒ dS )zs Sets the cached value of a property-based trait and fires the
            appropriate trait change event.
        N)r   rd   rQ   r	   Útrait_property_changed)r/   r&   r'   r)   rg   Úoldr#   r#   r$   Ú	set_valueY  s
    
zTraitType.set_valuec             O   s   | j ||Žjƒ S )z@ Allows a derivative trait to be defined from this one.
        )rf   Ú	as_ctrait)r/   ÚargsÚkwr#   r#   r$   Ú__call__e  s    zTraitType.__call__c             C   s    | j |ƒr|S | j|||ƒ dS )zr Handles a simplified validator that only returns whether or not the
            original value is valid.
        N)Úis_valid_forr2   )r/   r&   r'   r)   r#   r#   r$   Ú_is_valid_forj  s    
zTraitType._is_valid_forc             C   s2   y
| j |ƒS  tk
r,   | j|||ƒ Y nX dS )z[ Handles a simplified validator that only receives the value
            argument.
        N)Ú	value_forr   r2   )r/   r&   r'   r)   r#   r#   r$   Ú
_value_fors  s    
zTraitType._value_forc             C   s  ddl m} t| di ƒ}t| ddƒ}t| ddƒ}|dk	s@|dk	rÆ|dkrZt}|jddƒ n|dkrrt}|jddƒ |d	ƒ}d
}t| ddƒ}|dk	ršt|ƒ}|j|t|ƒ|t|ƒ||ƒ |jddƒ �nt| ddƒ}|dkrìtj	|j	dƒd
ƒ}||ƒ}t| ddƒ}|dk�rZt| ddƒ}|dk�rZt| ddƒ}|dk	�r>| j
}nt| ddƒ}|dk	�rZ| j}|dk	�rn|j|ƒ t| ddƒ}	|	dk	�r–|	|_|j| jƒ |j	dƒ}
|
dk	�r¸|j|
dkƒ |j	dƒ}|dk	�rÖ|j|ƒ |jddƒ |j| jƒ Ž  |j|j	ddƒdkƒ | |_|jƒ |_|S )zL Returns a CTrait corresponding to the trait defined by this class.
        r   )ÚCTraitrO   rQ   NÚsetZ	transientTr   r   r.   r   ÚpropertyZctrait_typeÚfast_validaterp   rr   Úpost_setattrÚrich_compareÚcomparison_moder;   Ztrait_valueF)Ztraitsrt   Úgetattrr(   Ú
setdefaultr*   r%   rv   Útrait_typesrQ   rq   rs   Úset_validaterx   rG   Zrich_comparisonrz   rK   r`   Zvalue_allowedÚhandlerrN   rd   )r/   rt   rM   ÚgetterÚsetterr;   Únr.   r   rx   ry   rz   r#   r#   r$   rl   |  sp    












zTraitType.as_ctraitc             C   sH   |d d… dkr4|dd … dkr4t d| jj|f ƒ‚t| di ƒj|d ƒS )Nr   Ú__z!'%s' object has no attribute '%s'rO   éþÿÿÿ)ÚAttributeErrorrb   r@   r{   rQ   )r/   r'   r#   r#   r$   Ú__getattr__É  s
     zTraitType.__getattr__)N)r@   rA   rB   rC   r	   rK   rM   rH   rS   rR   r`   r   rf   rh   rk   ro   rq   rs   rl   r†   r#   r#   r#   r$   rI   r  s   Y3&
		MrI   c               @   s   e Zd ZdZdd„ ZdS )ÚTraitHandlera5   The task of this class and its subclasses is to verify the correctness
    of values assigned to object trait attributes.

    This class is an alternative to trait validator functions. A trait handler
    has several advantages over a trait validator function, due to being an
    object:

        * Trait handlers have constructors and state. Therefore, you can use
          them to create *parametrized types*.
        * Trait handlers can have multiple methods, whereas validator functions
          can have only one callable interface. This feature allows more
          flexibility in their implementation, and allows them to handle a
          wider range of cases, such as interactions with other components.

    The only method of TraitHandler that *must* be implemented by subclasses
    is validate().
    c             C   s   t d|t|ƒf ƒ‚dS )a)   Verifies whether a new value assigned to a trait attribute is valid.

        Parameters
        ----------
        object : object
            The object whose attribute is being assigned.
        name : str
            The name of the attribute being assigned.
        value :
            The proposed new value for the attribute.

        Returns
        -------
        If the new value is valid, this method must return either the original
        value passed to it, or an alternate value to be assigned in place of the
        original value. Whatever value this method returns is the actual value
        assigned to *object.name*.

        Description
        -----------
        This method *must* be implemented by subclasses of TraitHandler. It is
        called whenever a new value is assigned to a trait attribute defined
        using this trait handler.

        If the value received by validate() is not valid for the trait
        attribute, the method must called the predefined error() method to
        raise a TraitError exception

        z`The '%s' trait of %s instance has an unknown type. Contact the developer to correct the problem.N)r   r   )r/   r&   r'   r)   r#   r#   r$   r.   ë  s    zTraitHandler.validateN)r@   rA   rB   rC   r.   r#   r#   r#   r$   r‡   Ø  s   r‡   c               @   sB   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dS )Ú
TraitRangeaÝ  Ensures that a trait attribute lies within a specified numeric range.

    TraitRange is the underlying handler for the predefined Range() trait
    factory.

    Any value assigned to a trait containing a TraitRange handler must be of the
    correct type and in the numeric range defined by the TraitRange instance.
    No automatic coercion takes place. For example::

        class Person(HasTraits):
            age = Trait(0, TraitRange(0, 150))
            weight = Trait(0.0, TraitRange(0.0, None))

    This example defines a Person class, which has an **age** trait
    attribute, which must be an integer/long in the range from 0 to 150, and a
    **weight** trait attribute, which must be a non-negative float value.
    NFc             C   s*  t |ƒ}|dk	r |tk	r t |ƒ}|tkr4td| ƒ‚|tkrp| j| _d}d| _|dk	r^t|ƒ}|dk	rÚt|ƒ}nj|tkr¨| j| _d| _|dk	r–t|ƒ}|dk	rÚt|ƒ}n2| j	| _d}d| _|dk	rÊt
|ƒ}|dk	rÚt
|ƒ}d}|rê|d	O }|rö|d
O }|tk	�r||||f| _|| _|| _|| _|| _dS )aZ   Creates a TraitRange handler.

        Parameters
        ----------
        low : number
            The minimum value that the trait can accept.
        high : number
            The maximum value that the trait can accept.
        exclude_low : bool
            Should the *low* value be exclusive (or inclusive).
        exclude_high : bool
            Should the *high* value be exclusive (or inclusive).

        Description
        -----------
        The *low* and *high* values must be of the same Python numeric type,
        either ``int``, ``long`` or ``float``. Alternatively, one of the values
        may be None, to indicate that that portion of the range is
        unbounded. The *exclude_low* and *exclude_high* values can be used to
        specify whether the *low* and *high* values should be exclusive (or
        inclusive).
        Nz_TraitRange can only be use for int, long or float values, but a value of type %s was specified.r   za floating point numberza long integerr   z
an integerr   r   r   )r   ÚfloatÚ
RangeTypesr   Úfloat_validater.   Ú
_type_descr   Úlong_validateÚint_validateÚintrw   Ú_lowÚ_highÚ_exclude_lowÚ_exclude_high)r/   ÚlowÚhighZexclude_lowZexclude_highZvtypeÚkindZexclude_maskr#   r#   r$   rS   (  sP    


zTraitRange.__init__c          
   C   sŽ   ynt |tƒrl| jd ks8| jr&| j|k s8| j rl| j|krl| jd ksd| jrR| j|ksd| j rl| j|krlt|ƒS W n   Y nX | j|||ƒ d S )N)rU   rŠ   r�   r’   r‘   r“   r‰   r2   )r/   r&   r'   r)   r#   r#   r$   r‹   o  s    


zTraitRange.float_validatec          
   C   sŠ   yjt |tƒrh| jd ks8| jr&| j|k s8| j rh| j|krh| jd ksd| jrR| j|ksd| j rh| j|krh|S W n   Y nX | j|||ƒ d S )N)rU   r�   r�   r’   r‘   r“   r2   )r/   r&   r'   r)   r#   r#   r$   rŽ   ƒ  s    


zTraitRange.int_validatec          
   C   sŒ   ylt |tjƒrj| jd ks:| jr(| j|k s:| j rj| j|krj| jd ksf| jrT| j|ksf| j rj| j|krj|S W n   Y nX | j|||ƒ d S )N)rU   ÚsixÚinteger_typesr�   r’   r‘   r“   r2   )r/   r&   r'   r)   r#   r#   r$   r�   —  s    

zTraitRange.long_validatec             C   sˆ   | j d kr6| jd kr| jS d| jd| jd … | jf S | jd kr\d| jd| jd … | j f S d| j d| jd … | jd| jd … | jf S )Nz	%s <%s %sú=z	%s >%s %sz%s <%s %s <%s %s)r�   r‘   rŒ   r“   r’   )r/   r#   r#   r$   r3   «  s$    




zTraitRange.infoc          	   C   sR   ddl m} |j}|d krd}|| |jp*d|jp2d||jp<d|jpDd|jpLdd�S )	Nr   )ÚRangeEditorTÚautor   FÚ )ÚmodeÚcolsÚauto_setÚ	enter_setZ	low_labelZ
high_label)r>   rš   rŸ   r�   rž   r    r”   r•   )r/   r;   rš   rŸ   r#   r#   r$   r<   Â  s    zTraitRange.get_editor)NNFF)
r@   rA   rB   rC   rS   r‹   rŽ   r�   r3   r<   r#   r#   r#   r$   rˆ     s   
Frˆ   c               @   sb   e Zd ZdZdejdfdd„Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚTraitStringaŠ   Ensures that a trait attribute value is a string that satisfied some
    additional, optional constraints.

    The optional constraints include minimum and maximum lengths, and a regular
    expression that the string must match.

    If the value assigned to the trait attribute is a Python numeric type, the
    TraitString handler first coerces the value to a string. Values of other
    non-string types result in a TraitError being raised. The handler then
    makes sure that the resulting string is within the specified length range
    and that it matches the regular expression.

    Example
    -------

    class Person(HasTraits):
        name = Trait('', TraitString(maxlen=50, regex=r'^[A-Za-z]*$'))


    This example defines a **Person** class with a **name** attribute, which
    must be a string of between 0 and 50 characters that consist of only
    upper and lower case letters.
    r   rœ   c             C   s,   t d|ƒ| _t | j|ƒ| _|| _| jƒ  dS )a@   Creates a TraitString handler.

        Parameters
        ----------
        minlen : int
            The minimum length allowed for the string.
        maxlen : int
            The maximum length allowed for the string.
        regex : str
            A Python regular expression that the string must match.

        r   N)ÚmaxÚminlenÚmaxlenÚregexÚ_init)r/   r£   r¤   r¥   r#   r#   r$   rS   ò  s    zTraitString.__init__c             C   sf   | j dkr:tj| j ƒj| _| jdkrb| jtjkrb| j| _	n(| jdkrZ| jtjkrZ| j
| _	n| j| _	d S )Nrœ   r   )r¥   ÚreÚcompileÚmatchr£   r¤   r—   ÚMAXSIZEÚvalidate_regexr.   Úvalidate_strÚvalidate_len)r/   r#   r#   r$   r¦     s    


zTraitString._initc          
   C   s^   y>t |ƒ}| jt|ƒ  ko$| jkn  r<| j|ƒd k	r<|S W n   Y nX | j|||ƒ d S )N)r   r£   rL   r¤   r©   r2   )r/   r&   r'   r)   r#   r#   r$   r.     s     zTraitString.validatec          
   C   s(   yt |ƒS    Y nX | j|||ƒ d S )N)r   r2   )r/   r&   r'   r)   r#   r#   r$   r¬     s
    zTraitString.validate_strc          
   C   sP   y0t |ƒ}| jt|ƒ  ko$| jkn  r.|S W n   Y nX | j|||ƒ d S )N)r   r£   rL   r¤   r2   )r/   r&   r'   r)   r#   r#   r$   r­      s     zTraitString.validate_lenc          
   C   s>   yt |ƒ}| j|ƒd k	r|S W n   Y nX | j|||ƒ d S )N)r   r©   r2   )r/   r&   r'   r)   r#   r#   r$   r«   )  s    zTraitString.validate_regexc             C   sˆ   d}| j dkr,| jtjkr,d| j | jf }n,| jtjkrDd| j }n| j dkrXd| j  }| jdkr€|dkrr|d7 }|d| j 7 }d| S )	Nrœ   r   z" between %d and %d characters longz <= %d characters longz >= %d characters longz andz matching the pattern '%s'za string)r£   r¤   r—   rª   r¥   )r/   Úmsgr#   r#   r$   r3   2  s    


zTraitString.infoc             C   s*   | j jƒ }xdD ]}||kr||= qW |S )Nr.   r©   )r.   r©   )rd   rN   )r/   Úresultr'   r#   r#   r$   Ú__getstate__C  s
    


zTraitString.__getstate__c             C   s   | j j|ƒ | jƒ  d S )N)rd   rP   r¦   )r/   Ústater#   r#   r$   Ú__setstate__J  s    zTraitString.__setstate__N)r@   rA   rB   rC   r—   rª   rS   r¦   r.   r¬   r­   r«   r3   r°   r²   r#   r#   r#   r$   r¡   Ù  s   
		r¡   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚTraitCoerceTypea  Ensures that a value assigned to a trait attribute is of a specified
    Python type, or can be coerced to the specified type.

    TraitCoerceType is the underlying handler for the predefined traits and
    factories for Python simple types. The TraitCoerceType class is also an
    example of a parametrized type, because the single TraitCoerceType class
    allows creating instances that check for totally different sets of values.
    For example::

        class Person(HasTraits):
            name = Trait('', TraitCoerceType(''))
            weight = Trait(0.0, TraitCoerceType(float))

    In this example, the **name** attribute must be of type ``str`` (string),
    while the **weight** attribute must be of type ``float``, although both are
    based on instances of the TraitCoerceType class. Note that this example is
    essentially the same as writing::

        class Person(HasTraits):
            name = Trait('')
            weight = Trait(0.0)

    This simpler form is automatically changed by the Trait() function into
    the first form, based on TraitCoerceType instances, when the trait
    attributes are defined.

    For attributes based on TraitCoerceType instances, if a value that is
    assigned is not of the type defined for the trait, a TraitError exception
    is raised. However, in certain cases, if the value can be coerced to the
    required type, then the coerced value is assigned to the attribute. Only
    *widening* coercions are allowed, to avoid any possible loss of precision.
    The following table lists the allowed coercions.

    ============ =================
     Trait Type   Coercible Types
    ============ =================
    complex      float, int
    float        int
    long         int
    unicode      str
    ============ =================
    c          	   C   sB   t |tƒst|ƒ}|| _yt| | _W n   d|f| _Y nX dS )aµ   Creates a TraitCoerceType handler.

        Parameters
        ----------
        aType : type
            Either a Python type (e.g., ``str`` or types.StringType) or a
            Python value (e.g., 'cat').

        Description
        -----------
        If *aType* is a value, it is mapped to its corresponding type. For
        example, the string 'cat' is automatically mapped to ``str`` (i.e.,
        types.StringType).
        é   N)rU   ÚTypeTyper   ÚaTyper   rw   )r/   r¶   r#   r#   r$   rS   €  s    
zTraitCoerceType.__init__c             C   sZ   | j }t|ƒ}||d kr|S x(|dd … D ]}||kr,|d |ƒS q,W | j|||ƒ d S )Nr   r   )rw   r   r2   )r/   r&   r'   r)   ÚfvÚtvZtypeir#   r#   r$   r.   —  s    zTraitCoerceType.validatec             C   s   dt | jƒdd…  S )Nza value of %sr   éÿÿÿÿ)Ústrr¶   )r/   r#   r#   r$   r3   ¨  s    zTraitCoerceType.infoc             C   sf   | j tkr.| jd kr(ddlm} |ƒ | _| jS |j}|d kr@d}ddlm} |||jpXd| jd d�S )Nr   )ÚBooleanEditorT)r=   Fr   )rŸ   r    Úevaluate)	r¶   Úboolr9   r>   r»   rŸ   r=   r    rw   )r/   r;   r»   rŸ   r=   r#   r#   r$   r<   «  s    

zTraitCoerceType.get_editorN)r@   rA   rB   rC   rS   r.   r3   r<   r#   r#   r#   r$   r³   T  s
   *r³   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTraitCastTypeaã  Ensures that a value assigned to a trait attribute is of a specified
    Python type, or can be cast to the specified type.

    This class is similar to TraitCoerceType, but uses casting rather than
    coercion. Values are cast by calling the type with the value to be assigned
    as an argument. When casting is performed, the result of the cast is the
    value assigned to the trait attribute.

    Any trait that uses a TraitCastType instance in its definition ensures that
    its value is of the type associated with the TraitCastType instance. For
    example::

        class Person(HasTraits):
            name = Trait('', TraitCastType(''))
            weight = Trait(0.0, TraitCastType(float))

    In this example, the **name** trait must be of type ``str`` (string), while
    the **weight** trait must be of type ``float``. Note that this example is
    essentially the same as writing::

        class Person(HasTraits):
            name = CStr
            weight = CFloat

    To understand the difference between TraitCoerceType and TraitCastType (and
    also between Float and CFloat), consider the following example::

        >>>class Person(HasTraits):
        ...    weight = Float
        ...    cweight = CFloat
        >>>
        >>>bill = Person()
        >>>bill.weight = 180    # OK, coerced to 180.0
        >>>bill.cweight = 180   # OK, cast to 180.0
        >>>bill.weight = '180'  # Error, invalid coercion
        >>>bill.cweight = '180' # OK, cast to float('180')
    c             C   s&   t |tƒst|ƒ}|| _d|f| _dS )aÓ   Creates a TraitCastType handler.

        Parameters
        ----------
        aType : type
            Either a Python type (e.g., ``str`` or types.StringType) or a
            Python value (e.g., ``'cat``).

        Description
        -----------
        If *aType* is a Python value, it is automatically mapped to its
        corresponding Python type. For example, the string 'cat' is
        automatically mapped to ``str`` (i.e., types.StringType).

        é   N)rU   rµ   r   r¶   rw   )r/   r¶   r#   r#   r$   rS   ð  s    
zTraitCastType.__init__c          
   C   s<   t |ƒ| jkr|S y
| j|ƒS    | j|||ƒ Y nX d S )N)r   r¶   r2   )r/   r&   r'   r)   r#   r#   r$   r.     s    
zTraitCastType.validateN)r@   rA   rB   rC   rS   r.   r#   r#   r#   r$   r¾   É  s   %r¾   c               @   sJ   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )Ú	ThisClassa  Ensures that the trait attribute values belong to the same class (or
       a subclass) as the object containing the trait attribute.

       ThisClass is the underlying handler for the predefined traits **This**
       and **self**, and the elements of ListThis.
    Fc             C   s&   |r| j | _| j| _d| _nd| _dS )zàCreates a ThisClass handler.

        Parameters
        ----------
        allow_none : bool
            Flag indicating whether None is accepted as a valid value
            (True or non-zero) or not (False or 0).
        r   N)r   N)r   )Úvalidate_noner.   Ú	info_noner3   rw   )r/   Ú
allow_noner#   r#   r$   rS     s
    	zThisClass.__init__c             C   s"   t ||jƒr|S | j|||ƒ d S )N)rU   rb   Úvalidate_failed)r/   r&   r'   r)   r#   r#   r$   r.   /  s    zThisClass.validatec             C   s*   t ||jƒs|d kr|S | j|||ƒ d S )N)rU   rb   rÄ   )r/   r&   r'   r)   r#   r#   r$   rÁ   5  s    zThisClass.validate_nonec             C   s   dS )Nz,an instance of the same type as the receiverr#   )r/   r#   r#   r$   r3   ;  s    zThisClass.infoc             C   s   dS )Nz4an instance of the same type as the receiver or Noner#   )r/   r#   r#   r$   rÂ   >  s    zThisClass.info_nonec             C   s   | j |||ƒ d S )N)r2   )r/   r&   r'   r)   r#   r#   r$   rÄ   A  s    zThisClass.validate_failedc             C   s>   | j d kr8ddlm} ||jp d|jp(d|jp0dd�| _ | j S )Nr   )ÚInstanceEditorrœ   Zlive)ÚlabelÚviewr–   )r9   r>   rÅ   rÆ   rÇ   r–   )r/   r;   rÅ   r#   r#   r$   r<   D  s    
zThisClass.get_editorN)F)r@   rA   rB   rC   rS   r.   rÁ   r3   rÂ   rÄ   r<   r#   r#   r#   r$   rÀ     s   
rÀ   )ÚnoÚyesÚdefaultc               @   sZ   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚTraitInstanceaï  Ensures that trait attribute values belong to a specified Python class
    or type.

    TraitInstance is the underlying handler for the predefined trait
    **Instance** and the elements of List( Instance ).

    Any trait that uses a TraitInstance handler ensures that its values belong
    to the specified type or class (or one of its subclasses). For example::

        class Employee(HasTraits):
            manager = Trait(None, TraitInstance(Employee, True))

    This example defines a class Employee, which has a **manager** trait
    attribute, which accepts either None or an instance of Employee
    as its value.

    TraitInstance ensures that assigned values are exactly of the type specified
    (i.e., no coercion is performed).
    TrÈ   rœ   c             C   sL   || _ t| | _|| _t|tjƒr*|| _nt|tƒs:|j	}|| _| j
ƒ  dS )a�  Creates a TraitInstance handler.

        Parameters
        ----------
        aClass : class or type
            A Python class, an instance of a Python class, or a Python type.
        allow_none : bool
            Flag indicating whether None is accepted as a valid value.
            (True or non-zero) or not (False or 0)
        adapt : str
            Value indicating how adaptation should be handled:

            - 'no' (-1): Adaptation is not allowed.
            - 'yes' (0): Adaptation is allowed and should raise an exception if
              adaptation fails.
            - 'default' (1): Adaption is allowed and should return the default
              value if adaptation fails.
        module : module
            The module that the class belongs to.

        Description
        -----------
        If *aClass* is an instance, it is mapped to the class it is an instance
        of.
        N)Ú_allow_noneÚAdaptMapÚadaptÚmodulerU   r—   Ústring_typesÚaClassr   rb   Úset_fast_validate)r/   rÑ   rÃ   rÎ   rÏ   r#   r#   r$   rS   m  s    

zTraitInstance.__init__c             C   s   d| _ t| dƒr| jƒ  d S )NTrw   )rÌ   ÚhasattrrÒ   )r/   r#   r#   r$   rÃ   ’  s    
zTraitInstance.allow_nonec             C   s\   | j dk rDd| jg}| jr&dd | jg}| jtkr8d|d< t|ƒ| _nd| j| j | jf| _d S )Nr   r   é   )rÎ   rÑ   rÌ   r
   Útuplerw   )r/   rw   r#   r#   r$   rÒ   —  s    


zTraitInstance.set_fast_validatec          
   C   sª   ddl m} |d kr,| jr|S | j|||ƒ t| jtjƒrH| j|||ƒ | jdk rdt|| jƒr˜|S n4| jdkrŠy||| jƒS    Y q˜X n||| jd ƒS | j|||ƒ d S )Nr   )rÎ   )	Útraits.adaptation.apirÎ   rÌ   rÄ   rU   rÑ   r—   rÐ   Úresolve_class)r/   r&   r'   r)   rÎ   r#   r#   r$   r.   §  s"    

zTraitInstance.validatec             C   sF   | j }t|ƒtk	r|j}| jdk r,t|ƒ}nd| }| jrB|d S |S )Nr   z5an implementor of, or can be adapted to implement, %sz or None)rÑ   r   rº   r@   rÎ   r   rÌ   )r/   rÑ   r¯   r#   r#   r$   r3   Å  s    

zTraitInstance.infoc             C   sn   | j | j| jƒƒ}|d kr(| j|||ƒ || _| jƒ  |j|ƒ}|j}|| k	r^t|dƒr^|j}|j	| j
ƒ d S )NÚ
item_trait)Úvalidate_classÚ
find_classrÑ   rÄ   rÒ   Z
base_traitr   rÓ   rØ   r~   rw   )r/   r&   r'   r)   rÑ   r;   r   r#   r#   r$   r×   ×  s    

zTraitInstance.resolve_classc             C   s°   | j }|jdƒ}|dkr4|d |… }||d d … }ttjj|ƒ|d ƒ}|d kr¬|dkr¬yFt|tƒ dd�}x$|jdƒdd … D ]}t||ƒ}q~W t||d ƒ}W n   Y nX |S )NÚ.r   r   )ÚglobalsÚlevel)	rÏ   Úrfindr{   ÚsysÚmodulesrQ   Ú
__import__rÜ   Úsplit)r/   rÑ   rÏ   ÚcolZtheClassÚmodÚ	componentr#   r#   r$   rÚ   ì  s    
zTraitInstance.find_classc             C   s   |S )Nr#   )r/   rÑ   r#   r#   r$   rÙ   ÿ  s    zTraitInstance.validate_classc             O   sN   |d }t |tjƒr<| j| j|ƒƒ}|d kr<td|d  ƒ‚||dd … |ŽS )Nr   zUnable to locate class: r   )rU   r—   rÐ   rÙ   rÚ   r   )r/   rm   rn   rÑ   r#   r#   r$   Úcreate_default_value  s    z"TraitInstance.create_default_valueN)TrÈ   rœ   )r@   rA   rB   rC   rS   rÃ   rÒ   r.   r3   r×   rÚ   rÙ   ræ   r#   r#   r#   r$   rË   X  s   
%rË   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTraitWeakRefc             C   s$   t ||d d ƒ}|d k	r |jƒ S d S )NÚ_)r{   r)   )r/   r&   r'   r)   r#   r#   r$   Ú_get  s    zTraitWeakRef._getc             C   s&   |d k	rt |||ƒ}||j|d < d S )Nrè   )ÚHandleWeakRefrd   )r/   r&   r'   r)   r#   r#   r$   Ú_set  s    zTraitWeakRef._setc             C   s,   | j | jƒ}|d kr"| j|||ƒ || _d S )N)rÚ   rÑ   rÄ   )r/   r&   r'   r)   rÑ   r#   r#   r$   r×     s    zTraitWeakRef.resolve_classN)r@   rA   rB   ré   rë   r×   r#   r#   r#   r$   rç     s   rç   c                s   ‡ ‡fdd„}|S )Nc                s    ˆƒ }|d k	r|j ˆ td ƒ d S )N)ri   r	   )Z	value_refr&   )r'   Ú
object_refr#   r$   Ú_value_freed,  s    z0_make_value_freed_callback.<locals>._value_freedr#   )rì   r'   rí   r#   )r'   rì   r$   Ú_make_value_freed_callback+  s    rî   c               @   s   e Zd Zdd„ ZdS )rê   c             C   s.   t |ƒ}t||ƒ}|| _|| _t ||ƒ| _d S )N)r   rî   r&   r'   r)   )r/   r&   r'   r)   rì   rí   r#   r#   r$   rS   5  s
    
zHandleWeakRef.__init__N)r@   rA   rB   rS   r#   r#   r#   r$   rê   4  s   rê   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú
TraitClassa  Ensures that trait attribute values are subclasses of a specified class
    (or the class itself).

    A value is valid if it is a subclass of the specified class (including the
    class itself), or it is a string that is equivalent to the name of a valid
    class.
    c             C   s   t j|ƒr|j}|| _dS )a  Creates a TraitClass handler.

        Parameters
        ----------
        aClass : class
            A Python class.

        Description
        -----------
        If *aClass* is an instance, it is mapped to the class it is an instance
        of.
        N)r   Zis_old_style_instancerb   rÑ   )r/   rÑ   r#   r#   r$   rS   K  s    
zTraitClass.__init__c          
   C   s¸   y˜t |tjƒr†|jƒ }|jdƒ}|dkrz|d |… }||d d … }tjj|ƒ}|d krntd| ƒ tj| }t	||ƒ}nt
ƒ j|ƒ}t|| jƒr–|S W n   Y nX | j|||ƒ d S )NrÛ   r   r   zimport )rU   r—   rÐ   ÚstriprÞ   rß   rà   rQ   Úexecr{   rÜ   Ú
issubclassrÑ   r2   )r/   r&   r'   r)   rã   Úmodule_nameÚ
class_namerÏ   r#   r#   r$   r.   \  s$    

zTraitClass.validatec             C   s   d| j j S )Nza subclass of )rÑ   r@   )r/   r#   r#   r$   r3   s  s    zTraitClass.infoN)r@   rA   rB   rC   rS   r.   r3   r#   r#   r#   r$   rï   B  s   rï   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚTraitFunctiona  Ensures that assigned trait attribute values are acceptable to a
    specified validator function.

    TraitFunction is the underlying handler for the predefined trait
    **Function**, and for the use of function references as arguments to the
    Trait() function.
    c             C   s&   t |tƒstdƒ‚|| _d|f| _dS )as   Creates a TraitFunction handler.

        Parameters
        ----------
        aFunc : function
            A function to validate trait attribute values.

        Description
        -----------
        The signature of the function passed as an argument must be of the
        form *function* ( *object*, *name*, *value* ). The function must
        verify that *value* is a legal value for the *name* trait attribute
        of *object*. If it is, the value returned by the function is the
        actual value assigned to the trait attribute. If it is not, the
        function must raise a TraitError exception.
        zArgument must be callable.é   N)rU   ÚCallableTypesr   ÚaFuncrw   )r/   rø   r#   r#   r$   rS   …  s    
zTraitFunction.__init__c             C   s6   y| j |||ƒS  tk
r0   | j|||ƒ Y nX d S )N)rø   r   r2   )r/   r&   r'   r)   r#   r#   r$   r.   ›  s    zTraitFunction.validatec          	   C   s(   y| j jS    | j jr | j jS dS d S )Nza legal value)rø   r3   rC   )r/   r#   r#   r$   r3   ¡  s    zTraitFunction.infoN)r@   rA   rB   rC   rS   r.   r3   r#   r#   r#   r$   rõ   |  s   rõ   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú	TraitEnumzÞ Ensures that a value assigned to a trait attribute is a member of a
    specified list of values.

    TraitEnum is the underlying handler for the forms of the Trait() function
    that take a list of possible values
    c             G   s>   t |ƒdkr$t|d ƒtkr$|d }t|ƒ| _d| jf| _dS )a   Creates a TraitEnum handler.

        Parameters
        ----------
        values : list or tuple
            Enumeration of all legal values for a trait.

        Description
        -----------
        The list of legal values can be provided as a list of values. That is,
        ``TraitEnum([1, 2, 3])`` and ``TraitEnum(1, 2, 3)`` are equivalent. For
        example::

            class Flower(HasTraits):
                color = Trait('white', TraitEnum(['white', 'yellow', 'red']))
                kind  = Trait('annual', TraitEnum('annual', 'perennial'))

        This example defines a Flower class, which has a **color** trait
        attribute, which can have as its value, one of the three strings,
        'white', 'yellow', or 'red', and a **kind** trait attribute, which can
        have as its value, either of the strings 'annual' or 'perennial'. This
        is equivalent to the following class definition::

            class Flower(HasTraits):
                color = Trait(['white', 'yellow', 'red'])
                kind  = Trait('annual', 'perennial')

        The Trait() function automatically maps traits of the form shown in
        this example to the form shown in the preceding example whenever it
        encounters them in a trait definition.
        r   r   r   N)rL   r   r   rÕ   Úvaluesrw   )r/   rú   r#   r#   r$   rS   ·  s     
zTraitEnum.__init__c             C   s    || j kr|S | j|||ƒ d S )N)rú   r2   )r/   r&   r'   r)   r#   r#   r$   r.   Ü  s    
zTraitEnum.validatec             C   s   dj dd„ | jD ƒƒS )Nz or c             S   s   g | ]}t |ƒ‘qS r#   )r8   )Ú.0Úxr#   r#   r$   ú
<listcomp>â  s    z"TraitEnum.info.<locals>.<listcomp>)Újoinrú   )r/   r#   r#   r$   r3   á  s    zTraitEnum.infoc             C   s*   ddl m} || |jpd|j|jp$dd�S )Nr   )Ú
EnumEditorr   Zradio)rú   rž   r¼   r�   )r>   rÿ   rž   r¼   r�   )r/   r;   rÿ   r#   r#   r$   r<   ä  s    zTraitEnum.get_editorN)r@   rA   rB   rC   rS   r.   r3   r<   r#   r#   r#   r$   rù   ¯  s
   %rù   c               @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚTraitPrefixListaþ  Ensures that a value assigned to a trait attribute is a member of a list
    of specified string values, or is a unique prefix of one of those values.

    TraitPrefixList is a variation on TraitEnum. The values that can be
    assigned to a trait attribute defined using a TraitPrefixList handler is the
    set of all strings supplied to the TraitPrefixList constructor, as well as
    any unique prefix of those strings. That is, if the set of strings supplied
    to the constructor is described by [*s*\ :sub:`1`\ , *s*\ :sub:`2`\ , ...,
    *s*\ :sub:`n`\ ], then the string *v* is a valid value for the trait if
    *v* == *s*\ :sub:`i[:j]` for one and only one pair of values (i, j). If *v*
    is a valid value, then the actual value assigned to the trait attribute is
    the corresponding *s*\ :sub:`i` value that *v* matched.

    Example
    -------

    class Person(HasTraits):
        married = Trait('no', TraitPrefixList('yes', 'no')

    The Person class has a **married** trait that accepts any of the
    strings 'y', 'ye', 'yes', 'n', or 'no' as valid values. However, the actual
    values assigned as the value of the trait attribute are limited to either
    'yes' or 'no'. That is, if the value 'y' is assigned to the **married**
    attribute, the actual value assigned will be 'yes'.

    Note that the algorithm used by TraitPrefixList in determining whether a
    string is a valid value is fairly efficient in terms of both time and space,
    and is not based on a brute force set of comparisons.

    c             G   sd   t |ƒdkr$t|d ƒtkr$|d }|dd… | _i  | _}x|D ]}|||< qBW d|| jf| _dS )a¾   Creates a TraitPrefixList handler.

        Parameters
        ----------
        values : list or tuple of strings
            Enumeration of all legal values for a trait.

        Description
        -----------
        As with TraitEnum, the list of legal values can be provided as a list
        of values.  That is, ``TraitPrefixList(['one', 'two', 'three'])`` and
        ``TraitPrefixList('one', 'two', 'three')`` are equivalent.
        r   r   Né
   )rL   r   r   rú   Úvalues_r.   rw   )r/   rú   r  Úkeyr#   r#   r$   rS     s    

zTraitPrefixList.__init__c             C   s’   yr|| j krjd }t|ƒ}x0| jD ]&}||d |… kr |d k	rBd }P |}q W |d kr`| j|||ƒ || j |< | j | S    | j|||ƒ Y nX d S )N)r  rL   rú   r2   )r/   r&   r'   r)   r©   r‚   r  r#   r#   r$   r.   *  s     


zTraitPrefixList.validatec             C   s   dj dd„ | jD ƒƒd S )Nz or c             S   s   g | ]}t |ƒ‘qS r#   )r8   )rû   rü   r#   r#   r$   rý   >  s    z(TraitPrefixList.info.<locals>.<listcomp>z (or any unique prefix))rþ   rú   )r/   r#   r#   r$   r3   <  s    zTraitPrefixList.infoc             C   s   ddl m} || |jpdd�S )Nr   )rÿ   r   )rú   rž   )r>   rÿ   rž   )r/   r;   rÿ   r#   r#   r$   r<   B  s    zTraitPrefixList.get_editorc             C   s   | j jƒ }d|kr|d= |S )Nrw   )rd   rN   )r/   r¯   r#   r#   r$   r°   G  s    
zTraitPrefixList.__getstate__N)	r@   rA   rB   rC   rS   r.   r3   r<   r°   r#   r#   r#   r$   r   ô  s   r   c               @   sD   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dS )ÚTraitMapa1  Checks that the value assigned to a trait attribute is a key of a
    specified dictionary, and also assigns the dictionary value corresponding
    to that key to a *shadow* attribute.

    A trait attribute that uses a TraitMap handler is called *mapped* trait
    attribute. In practice, this means that the resulting object actually
    contains two attributes: one whose value is a key of the TraitMap
    dictionary, and the other whose value is the corresponding value of the
    TraitMap dictionary. The name of the shadow attribute is simply the base
    attribute name with an underscore ('_') appended. Mapped trait attributes
    can be used to allow a variety of user-friendly input values to be mapped to
    a set of internal, program-friendly values.

    Example
    -------

        >>>class Person(HasTraits):
        ...    married = Trait('yes', TraitMap({'yes': 1, 'no': 0 })
        >>>
        >>>bob = Person()
        >>>print bob.married
        yes
        >>>print bob.married_
        1

    In this example, the default value of the **married** attribute of the
    Person class is 'yes'. Because this attribute is defined using
    TraitPrefixList, instances of Person have another attribute,
    **married_**, whose default value is 1, the dictionary value corresponding
    to the key 'yes'.
    Tc             C   s   || _ d|f| _dS )a   Creates a TraitMap handler.

        Parameters
        ----------
        map : dict
            A dictionary whose keys are valid values for the trait attribute,
            and whose corresponding values are the values for the shadow
            trait attribute.
        r   N)Úmaprw   )r/   r  r#   r#   r$   rS   w  s    
zTraitMap.__init__c          
   C   s2   y|| j kr|S W n   Y nX | j|||ƒ d S )N)r  r2   )r/   r&   r'   r)   r#   r#   r$   r.   „  s    
zTraitMap.validatec             C   s
   | j | S )N)r  )r/   r)   r#   r#   r$   Úmapped_value�  s    zTraitMap.mapped_valuec             C   s4   yt ||d | j|ƒƒ W n   tdƒ‚Y nX d S )Nrè   Z
Unmappable)Úsetattrr  r   )r/   r&   r'   r)   r#   r#   r$   rx   �  s    zTraitMap.post_setattrc             C   s"   t dd„ | jjƒ D ƒƒ}dj|ƒS )Nc             s   s   | ]}t |ƒV  qd S )N)r8   )rû   rü   r#   r#   r$   ú	<genexpr>™  s    z TraitMap.info.<locals>.<genexpr>z or )Úsortedr  Úkeysrþ   )r/   r
  r#   r#   r$   r3   ˜  s    zTraitMap.infoc             C   s   ddl m} || |jpdd�S )Nr   )rÿ   r   )rú   rž   )r>   rÿ   rž   )r/   r;   rÿ   r#   r#   r$   r<   œ  s    zTraitMap.get_editorN)r@   rA   rB   rC   rG   rS   r.   r  rx   r3   r<   r#   r#   r#   r$   r  T  s   	r  c                   s0   e Zd ZdZdd„ Zdd„ Z‡ fdd„Z‡  ZS )ÚTraitPrefixMapaç  A cross between the TraitPrefixList and TraitMap classes.

    Like TraitMap, TraitPrefixMap is created using a dictionary, but in this
    case, the keys of the dictionary must be strings. Like TraitPrefixList,
    a string *v* is a valid value for the trait attribute if it is a prefix of
    one and only one key *k* in the dictionary. The actual values assigned to
    the trait attribute is *k*, and its corresponding mapped attribute is
    *map*[*k*].

    Example
    -------

        mapping = {'true': 1, 'yes': 1, 'false': 0, 'no': 0 }
        boolean_map = Trait('true', TraitPrefixMap(mapping))

    This example defines a Boolean trait that accepts any prefix of 'true',
    'yes', 'false', or 'no', and maps them to 1 or 0.
    c             C   s<   || _ i  | _}x|jƒ D ]}|||< qW d|| jf| _dS )a)  Creates a TraitPrefixMap handler.

        Parameters
        ----------
        map : dict
            A dictionary whose keys are strings that are valid values for the
            trait attribute, and whose corresponding values are the values for
            the shadow trait attribute.
        r  N)r  Ú_mapr
  r.   rw   )r/   r  r  r  r#   r#   r$   rS   »  s
    

zTraitPrefixMap.__init__c             C   s–   yv|| j krnd }t|ƒ}x4| jjƒ D ]&}||d |… kr$|d k	rFd }P |}q$W |d krd| j|||ƒ || j |< | j | S    | j|||ƒ Y nX d S )N)r  rL   r  r
  r2   )r/   r&   r'   r)   r©   r‚   r  r#   r#   r$   r.   Ë  s     


zTraitPrefixMap.validatec                s   t t| ƒjƒ d S )Nz (or any unique prefix))Úsuperr  r3   )r/   )rb   r#   r$   r3   Ý  s    zTraitPrefixMap.info)r@   rA   rB   rC   rS   r.   r3   Ú__classcell__r#   r#   )rb   r$   r  §  s   r  c               @   s4   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTraitExpressionzº Ensures that a value assigned to a trait attribute is a valid Python
        expression. The compiled form of a valid expression is stored as the
        mapped value of the trait.
    Tc          
   C   s0   yt |ddƒ |S    | j|||ƒ Y nX d S )Nz<string>Úeval)r¨   r2   )r/   r&   r'   r)   r#   r#   r$   r.   î  s
    zTraitExpression.validatec             C   s   | j |ƒ|j|d < d S )Nrè   )r  rd   )r/   r&   r'   r)   r#   r#   r$   rx   õ  s    zTraitExpression.post_setattrc             C   s   dS )Nza valid Python expressionr#   )r/   r#   r#   r$   r3   ø  s    zTraitExpression.infoc             C   s   t |ddƒS )Nz<string>r  )r¨   )r/   r)   r#   r#   r$   r  û  s    zTraitExpression.mapped_valueN)	r@   rA   rB   rC   rG   r.   rx   r3   r  r#   r#   r#   r$   r  æ  s   r  c               @   s`   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚTraitCompoundaá   Provides a logical-OR combination of other trait handlers.

    This class provides a means of creating complex trait definitions by
    combining several simpler trait definitions. TraitCompound is the underlying
    handler for the general forms of the Trait() function.

    A value is a valid value for a trait attribute based on a TraitCompound
    instance if the value is valid for at least one of the TraitHandler or
    trait objects supplied to the constructor. In addition, if at least one of
    the TraitHandler or trait objects is mapped (e.g., based on a TraitMap or
    TraitPrefixMap instance), then the TraitCompound is also mapped. In this
    case, any non-mapped traits or trait handlers use identity mapping.

    c             G   s6   t |ƒdkr$t|d ƒtkr$|d }|| _| jƒ  dS )z± Creates a TraitCompound handler.

        Parameters
        ----------
        *handlers :
            list or tuple of TraitHandler or trait objects to be combined.

        r   r   N)rL   r   r   Úhandlersr~   )r/   r  r#   r#   r$   rS     s    	zTraitCompound.__init__c       	      C   sv  d| _ d| _d| _g }g }g }g }g }xª| jD ] }t|dd ƒ}|d k	rz|j|jƒ |d dkrn|j|d ƒ q†|j|ƒ n|j|jƒ t|dd ƒ}|d k	r¤|j|ƒ |j r¼d| _ |j|ƒ nd| _|jr.d| _q.W || _|| _	| j rì|| _
nt| dƒrú| `
t|ƒdk�r4t|ƒdk�r$|jd	| fƒ dt|ƒf| _nt| dƒ�rD| `t|ƒdk�rb|| _| j| _nt| dƒ�rr| `d S )
NFTrw   r   r   r   rx   Úmapped_handlersr   )rG   rF   Z
reversabler  r{   Úappendr.   ÚextendÚ	validatesÚslow_validatesr  rÓ   rL   rÕ   rw   Úpost_setattrsÚ_post_setattrrx   )	r/   r  r  r  Zfast_validatesr  r   r·   rx   r#   r#   r$   r~   "  sT    



zTraitCompound.set_validatec             C   s@   x0| j D ]&}y||||ƒS  tk
r,   Y qX qW | j|||ƒS )N)r  r   Úslow_validate)r/   r&   r'   r)   r.   r#   r#   r$   r.   a  s    
zTraitCompound.validatec             C   sD   x0| j D ]&}y||||ƒS  tk
r,   Y qX qW | j|||ƒ d S )N)r  r   r2   )r/   r&   r'   r)   r.   r#   r#   r$   r  i  s    
zTraitCompound.slow_validatec                s   dj ‡ ‡‡fdd„| jD ƒƒS )Nz or c                s   g | ]}|j ˆˆ ˆƒ‘qS r#   )r1   )rû   rü   )r'   r&   r)   r#   r$   rý   s  s    z+TraitCompound.full_info.<locals>.<listcomp>)rþ   r  )r/   r&   r'   r)   r#   )r'   r&   r)   r$   r1   q  s    zTraitCompound.full_infoc             C   s   dj dd„ | jD ƒƒS )Nz or c             S   s   g | ]}|j ƒ ‘qS r#   )r3   )rû   rü   r#   r#   r$   rý   w  s    z&TraitCompound.info.<locals>.<listcomp>)rþ   r  )r/   r#   r#   r$   r3   v  s    zTraitCompound.infoc          
   C   s,   x&| j D ]}y
|j|ƒS    Y qX qW |S )N)r  r  )r/   r)   r   r#   r#   r$   r  y  s    

zTraitCompound.mapped_valuec             C   sJ   x4| j D ]*}y||||ƒ d S  tk
r0   Y qX qW t||d |ƒ d S )Nrè   )r  r   r  )r/   r&   r'   r)   rx   r#   r#   r$   r  �  s    
zTraitCompound._post_setattrc       	         sr   ddl m}m} ‡ fdd„| jD ƒ}|ƒ }d}g }x4|D ],}t||jƒrZ|d7 }|dkrZq8|j|ƒ q8W ||d�S )Nr   )r=   ÚCompoundEditorc                s   g | ]}|j ˆ ƒ‘qS r#   )r<   )rû   rü   )r;   r#   r$   rý   �  s    z,TraitCompound.get_editor.<locals>.<listcomp>r   )Úeditors)r>   r=   r  r  rU   rb   r  )	r/   r;   r=   r  Zthe_editorsZtext_editorÚcountr  r9   r#   )r;   r$   r<   Š  s    
zTraitCompound.get_editorc             C   s   t ƒ S )N)Úitems_event)r/   r#   r#   r$   r  š  s    zTraitCompound.items_eventN)r@   rA   rB   rC   rS   r~   r.   r  r1   r3   r  r  r<   r  r#   r#   r#   r$   r    s   ?	r  c               @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú
TraitTupleaã   Ensures that values assigned to a trait attribute are tuples of a
    specified length, with elements that are of specified types.

    TraitTuple is the underlying handler for the predefined trait **Tuple**,
    and the trait factory Tuple().

    Example
    -------

        rank = Range(1, 13)
        suit = Trait('Hearts', 'Diamonds', 'Spades', 'Clubs')
        class Card(HasTraits):
            value = Trait(TraitTuple(rank, suit))

    This example defines a Card class, which has a **value** trait attribute,
    which must be a tuple of two elments. The first element must be an integer
    in the range from 1 to 13, and the second element must be one of the four
    strings, 'Hearts', 'Diamonds', 'Spades', or 'Clubs'.
    c             G   s$   t dd„ |D ƒƒ| _d| jf| _dS )aI   Creates a TraitTuple handler.

        Parameters
        ----------
        *args :
            A list of traits, each *trait*\ :sub:`i` specifies the type that
            the *i*\ th element of a tuple must be.

        Description
        -----------
        Each *trait*\ :sub:`i` must be either a trait, or a value that can be
        converted to a trait using the Trait() function. The resulting
        trait handler accepts values that are tuples of the same length as
        *args*, and whose *i*\ th element is of the type specified by
        *trait*\ :sub:`i`.
        c             S   s   g | ]}t |ƒ‘qS r#   )Ú
trait_from)rû   Úargr#   r#   r$   rý   É  s    z'TraitTuple.__init__.<locals>.<listcomp>r   N)rÕ   Útypesrw   )r/   rm   r#   r#   r$   rS   ¸  s    zTraitTuple.__init__c             C   s€   y`t |tƒr^| j}t|ƒt|ƒkr^g }x.t|ƒD ]"\}}|j|jj|||| ƒƒ q0W t|ƒS W n   Y nX | j|||ƒ d S )N)	rU   rÕ   r"  rL   Ú	enumerater  r   r.   r2   )r/   r&   r'   r)   r"  rú   Úir   r#   r#   r$   r.   Ì  s    
zTraitTuple.validatec                s$   ddj ‡ ‡‡‡fdd„ˆjD ƒƒ S )Nza tuple of the form: (%s)z, c                s   g | ]}ˆj |ˆˆ ˆƒ‘qS r#   )Ú_trait_info)rû   r   )r'   r&   r/   r)   r#   r$   rý   à  s   z(TraitTuple.full_info.<locals>.<listcomp>)rþ   r"  )r/   r&   r'   r)   r#   )r'   r&   r/   r)   r$   r1   Ü  s    zTraitTuple.full_infoc             C   s    |j }|d krdS |j|||ƒS )Nz	any value)r   r1   )r/   r   r&   r'   r)   r   r#   r#   r$   r%  æ  s    zTraitTuple._trait_infoc             C   s(   ddl m} || j|jpg |jp"dd�S )Nr   )ÚTupleEditorr   )r"  Úlabelsrž   )r>   r&  r"  r'  rž   )r/   r;   r&  r#   r#   r$   r<   í  s    zTraitTuple.get_editorN)	r@   rA   rB   rC   rS   r.   r1   r%  r<   r#   r#   r#   r$   r  £  s   
r  c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚTraitCallablezpEnsures that the value of a trait attribute is a callable Python object
    (usually a function or method).
    c             C   s&   |d kst |ƒr|S | j|||ƒ d S )N)Úcallabler2   )r/   r&   r'   r)   r#   r#   r$   r.   ÿ  s    zTraitCallable.validatec             C   s   dS )Nza callable valuer#   )r/   r#   r#   r$   r3   	  s    zTraitCallable.infoN)r@   rA   rB   rC   r.   r3   r#   r#   r#   r$   r(  ú  s   r(  c               @   s   e Zd Zddd„ZdS )ÚTraitListEventr   Nc             C   s.   || _ |d krg }|| _|d kr$g }|| _d S )N)ÚindexÚremovedÚadded)r/   r+  r,  r-  r#   r#   r$   rS   	  s    zTraitListEvent.__init__)r   NN)r@   rA   rB   rS   r#   r#   r#   r$   r*  	  s   r*  c               @   sX   e Zd ZdZdZeZdZddej	dfdd„Z
dd„ Zd	d
„ Zdd„ Zdd„ Zdd„ ZdS )Ú	TraitListaî   Ensures that a value assigned to a trait attribute is a list containing
    elements of a specified type, and that the length of the list is also
    within a specified range.

    TraitList also makes sure that any changes made to the list after it is
    assigned to the trait attribute do not violate the list's type and length
    constraints. TraitList is the underlying handler for the predefined
    list-based traits.

    Example
    -------

    class Card(HasTraits):
        pass
    class Hand(HasTraits):
        cards = Trait([], TraitList(Trait(Card), maxlen=52))


    This example defines a Hand class, which has a **cards** trait attribute,
    which is a list of Card objects and can have from 0 to 52 items in the
    list.
    Nr   Tc             C   s,   t |ƒ| _td|ƒ| _t||ƒ| _|| _dS )aƒ   Creates a TraitList handler.

        Parameters
        ----------
        trait : Trait
            The type of items the list can contain.
        minlen : int
            The minimum length of the list.
        maxlen : int
            The maximum length of the list.
        has_items : bool
            Flag indicating whether the list contains elements.

        Description
        -----------
        If *trait* is None or omitted, then no type checking is performed
        on any items in the list; otherwise, *trait* must be either a trait, or
        a value that can be converted to a trait using the Trait() function.

        r   N)r   rØ   r¢   r£   r¤   rF   )r/   r;   r£   r¤   rF   r#   r#   r$   rS   @	  s    
zTraitList.__init__c             C   s   t | j| j| j| jƒS )N)r.  rØ   r£   r¤   rF   )r/   r#   r#   r$   rf   \	  s    zTraitList.clonec             C   sJ   t |tƒr8| jt|ƒ  ko$| jkn  r8t| |||ƒS | j|||ƒ d S )N)rU   rX   r£   rL   r¤   rV   r2   )r/   r&   r'   r)   r#   r#   r$   r.   a	  s    
 zTraitList.validatec             C   s„   | j dkr(| jtjkrd}qPd| j }n(| jtjkr@d| j  }nd| j | jf }| jj}|d krfd}nd|j|||ƒ }d||f S )	Nr   Úitemszat most %d itemszat least %d itemszfrom %s to %s itemsrœ   z which are %sza list of %s%s)r£   r¤   r—   rª   rØ   r   r1   )r/   r&   r'   r)   Úsizer   r3   r#   r#   r$   r1   i	  s    
zTraitList.full_infoc             C   s   ddl m} ||| ƒS )Nr   )Úlist_editor)Ztraits.traitsr1  )r/   r;   r1  r#   r#   r$   r<   |	  s    zTraitList.get_editorc             C   s   t ƒ S )N)r  )r/   r#   r#   r$   r  �	  s    zTraitList.items_event)r@   rA   rB   rC   Ú
info_traitrW   rE   Ú_items_eventr—   rª   rS   rf   r.   r1   r<   r  r#   r#   r#   r$   r.  $	  s   r.  c               C   s"   t jd krttdd�jƒ t _t jS )NF)Úis_base)r.  r3  ÚEventr*  rl   r#   r#   r#   r$   r  …	  s    
r  c               @   sÖ   e Zd Zdd„ Zd.dd„Zdd„ Zdd	„ Zejr8d
d„ Z	dd„ Z
ejrNdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zejr�d/dd„Zn
d0dd„Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ ZdS )1rV   c                sÐ   || _ tˆƒ| _ˆ | _d | _|jr,ˆ d | _|jt|ƒ  koF|jkn  r¾y@|j	j
j‰ˆd k	rv‡ ‡‡fdd„|D ƒ}tj| tddƒ|ƒ d S  tk
r¼ } z|jdƒ |‚W Y d d }~X nX | jt|ƒƒ d S )NÚ_itemsc                s   g | ]}ˆˆˆ |ƒ‘qS r#   r#   )rû   Úval)r'   r&   r.   r#   r$   rý   ¢	  s    z,TraitListObject.__init__.<locals>.<listcomp>r   zEach element of the)r;   r   r&   r'   Ú
name_itemsrF   r£   rL   r¤   rØ   r   r.   rX   Ú__setitem__Úslicer   Ú
set_prefixÚ	len_error)r/   r;   r&   r'   r)   Úexcpr#   )r'   r&   r.   r$   rS   ”	  s"    

 

zTraitListObject.__init__Nc             C   s>   | j ƒ }|dk	r:|dkr,t| dƒr,| jjƒ }|j|||ƒ dS )zE Send a TraitListEvent to the owning object if there is one.
        Nr;   )r&   rÓ   r;   r  Útrait_items_event)r/   r'   r   r  r&   r#   r#   r$   Ú_send_trait_items_event®	  s
    
z'TraitListObject._send_trait_items_eventc                sH   t | ƒ}|ˆ krˆ | S t| jdd„ | j‡ fdd„| D ƒƒ ˆ |< }|S )Nc               S   s   d S )Nr#   r#   r#   r#   r$   Ú<lambda>¾	  s    z.TraitListObject.__deepcopy__.<locals>.<lambda>c                s   g | ]}t j|ˆ ƒ‘qS r#   )rN   Údeepcopy)rû   rü   )Úmemor#   r$   rý   À	  s    z0TraitListObject.__deepcopy__.<locals>.<listcomp>)ÚidrV   r;   r'   )r/   rB  Úid_selfr¯   r#   )rB  r$   Ú__deepcopy__·	  s    zTraitListObject.__deepcopy__c                s  t | dd ƒ}|d kr"tj| ||ƒS y| | }W n   g }Y nX �y’| jƒ ‰| jjjj‰| j‰ t	|t
ƒ�r8|}t|ƒ}t|ƒ| }|jd kr�dn|j}|dkrº|dkrºtdt|ƒ|f ƒ‚t| ƒ| }	|j|	  koÜ|jkn  sð| j|	ƒ d S ˆd k	�r‡ ‡‡fdd„|D ƒ}|}|dk�r&|j}
n|}
|g}|g}n@ˆd k	�rNˆˆˆ |ƒ}|g}|g}d}|dk �rtt| ƒ| n|}
tj| ||ƒ | jd k	�rÒ|dk�r¼y||k�r¬d S W n   Y nX | j| jt|
||ƒƒ W n2 tk
�r } z|jdƒ |‚W Y d d }~X nX d S )Nr;   r   r   zBattempt to assign sequence of size %d to extended slice of size %dc                s   g | ]}ˆˆˆ |ƒ‘qS r#   r#   )rû   r)   )r'   r&   r.   r#   r$   rý   ä	  s    z/TraitListObject.__setitem__.<locals>.<listcomp>zEach element of the)r{   rX   r9  r&   r;   rØ   r   r.   r'   rU   r:  rL   ÚstepÚ
ValueErrorr£   r¤   r<  Ústartr8  r?  r*  r   r;  )r/   r  r)   Z
self_traitr,  rú   Ú	slice_lenÚdeltarF  Znewlenr+  r=  r#   )r'   r&   r.   r$   r9  Å	  sf    







zTraitListObject.__setitem__c             C   s   | j t||ƒ|ƒ d S )N)r9  r:  )r/   r$  Újrú   r#   r#   r$   Ú__setslice__
  s    zTraitListObject.__setslice__c             C   s   t | dd ƒ}|d kr tj| |ƒS y| | }W n   g }Y nX t|tƒr„t|ƒ}|}|jd krbdn|j}|dkrx|j}qª|}|g}n&d}|dk r t| ƒ| d n|}|g}|jt| ƒ| ksÒ| j	t| ƒ| ƒ d S tj| |ƒ | j
d k	rü| j| j
t||ƒƒ d S )Nr;   r   r   )r{   rX   Ú__delitem__rU   r:  rL   rF  rH  r£   r<  r8  r?  r*  )r/   r  r;   r,  rI  rJ  rF  r+  r#   r#   r$   rM  
  s2    


zTraitListObject.__delitem__c             C   s   | j t||ƒƒ d S )N)rM  r:  )r/   r$  rK  r#   r#   r$   Ú__delslice__;
  s    zTraitListObject.__delslice__c             C   s   | j |ƒ | S )N)r  )r/   Úotherr#   r#   r$   Ú__iadd__>
  s    
zTraitListObject.__iadd__c             C   sÈ   t | dd ƒ}|d kr tj| |ƒS t| ƒ}|j||   koB|jkn  r¶| jd k	rf|rZd n
| d d … }tj| |ƒ}| jd k	r²|rŒ| |d … nd }|r˜|nd}| j| jt|||ƒƒ |S | j	|| ƒ d S )Nr;   r   )
r{   rX   Ú__imul__rL   r£   r¤   r8  r?  r*  r<  )r/   r  r;   Úoriginal_lenr,  r¯   r-  r+  r#   r#   r$   rQ  B
  s     

zTraitListObject.__imul__c             C   sø   t | dd ƒ}|d kr$tj| |ƒ d S |jt| ƒd   koB|jkn  râyh|jjj}| j	ƒ }|d k	rr||| j
|ƒ}tj| |ƒ | jd k	r®| j| jtt| ƒd d |gƒ|jƒ ƒ d S  tk
rà } z|jdƒ |‚W Y d d }~X nX | jt| ƒd ƒ d S )Nr;   r   zEach element of the)r{   rX   r  r£   rL   r¤   rØ   r   r.   r&   r'   r8  r?  r*  r  r   r;  r<  )r/   r)   r;   r.   r&   r=  r#   r#   r$   r  Z
  s*    $



zTraitListObject.appendc             C   s*  t | dd ƒ}|d kr"tj| ||ƒS |jt| ƒd   ko@|jkn  �ry˜|jjj}| j	ƒ }|d k	rr||| j
|ƒ}tj| ||ƒ | jd k	rÞt| ƒd }|dk r´||7 }|dk rÀd}n||krÀ|}| j| jt|d |gƒ|jƒ ƒ d S  tk
�r } z|jdƒ |‚W Y d d }~X nX | jt| ƒd ƒ d S )Nr;   r   r   zEach element of the)r{   rX   Úinsertr£   rL   r¤   rØ   r   r.   r&   r'   r8  r?  r*  r  r   r;  r<  )r/   r+  r)   r;   r.   r&   rR  r=  r#   r#   r$   rS  u
  s6    &



zTraitListObject.insertc                s>  t | dd ƒ}|d kr$tj| |ƒ d S yt|ƒ}W n   tdƒ‚Y nX |jt| ƒ|   kod|jkn  �r$| jƒ ‰| j‰ |j	j
j‰ylˆd k	r¤‡ ‡‡fdd„|D ƒ}tj| |ƒ | jd k	rît|ƒdkrî| j| jtt| ƒt|ƒ d |ƒ|jƒ ƒ d S  tk
�r" } z|jdƒ |‚W Y d d }~X nX | jt| ƒt|ƒ ƒ d S )Nr;   z'list.extend() argument must be iterablec                s   g | ]}ˆˆˆ |ƒ‘qS r#   r#   )rû   r)   )r'   r&   r.   r#   r$   rý   °
  s    z*TraitListObject.extend.<locals>.<listcomp>r   zThe elements of the)r{   rX   r  rL   Ú	TypeErrorr£   r¤   r&   r'   rØ   r   r.   r8  r?  r*  r  r   r;  r<  )r/   Zxlistr;   Z	len_xlistr=  r#   )r'   r&   r.   r$   r  ž
  s4    &


zTraitListObject.extendc             C   s´   t | dd ƒ}|d kr$tj| |ƒ d S |jt| ƒk r„y| j|ƒ}| | g}W n   Y nX tj| |ƒ | jd k	r°| j| jt||ƒƒ n,t| ƒdkržtj| |ƒ n| j	t| ƒd ƒ d S )Nr;   r   r   )
r{   rX   Úremover£   rL   r+  r8  r?  r*  r<  )r/   r)   r;   r+  r,  r#   r#   r$   rU  Ã
  s"    

zTraitListObject.removeFc             C   s,   | d d … }t j| |||d� | j|ƒ d S )N)Úcmpr  Úreverse)rX   ÚsortÚ_sort_common)r/   rV  r  rW  r,  r#   r#   r$   rX  Ý
  s    zTraitListObject.sortc             C   s*   | d d … }t j| ||d� | j|ƒ d S )N)r  rW  )rX   rX  rY  )r/   r  rW  r,  r#   r#   r$   rX  ä
  s    c             C   sB   t | dd ƒd k	r>t | dd ƒd k	r>| j| jtd|| d d … ƒƒ d S )Nr8  r;   r   )r{   r?  r8  r*  )r/   r,  r#   r#   r$   rY  é
  s    zTraitListObject._sort_commonc             C   sN   | d d … }t | ƒdkrJtj| ƒ | jd k	rJ| j| jtd|| d d … ƒƒ d S )Nr   r   )rL   rX   rW  r8  r?  r*  )r/   r,  r#   r#   r$   rW  ò
  s    

zTraitListObject.reversec             G   sÀ   t | dƒstj| f|žŽ S | jjt| ƒk rªt|ƒdkr@|d }nd}y| | g}W n   Y nX tj| f|žŽ }| jd k	r¦|dk r’t| ƒ| d }| j| jt||ƒƒ |S | j	t| ƒd ƒ d S )Nr;   r   r   r¹   )
rÓ   rX   Úpopr;   r£   rL   r8  r?  r*  r<  )r/   rm   r+  r,  r¯   r#   r#   r$   rZ  û
  s$    


zTraitListObject.popc             C   s*   | j ƒ j|dƒ}|d k	r&|| _|j| _d S )Nr   )r&   Ú_traitr'   r   r;   )r/   r'   r;   r#   r#   r$   Úrename  s    zTraitListObject.renamec          	   C   sB   t d| jt| jƒ ƒ| jj| jƒ | jtƒ|d|dkd … f ƒ‚d S )NzaThe '%s' trait of %s instance must be %s, but you attempted to change its length to %d element%s.Úsr   )r   r'   r   r&   r;   r1   r	   )r/   rL   r#   r#   r$   r<    s    
zTraitListObject.len_errorc             C   s&   | j jƒ }|jdd ƒ |jdd ƒ |S )Nr&   r;   )rd   rN   rZ  )r/   r¯   r#   r#   r$   r°   +  s    
zTraitListObject.__getstate__c             C   sP   |j ddƒ}|jdd ƒ}|d k	r6t|ƒ| _| j|ƒ n
dd„ | _| jj|ƒ d S )Nr'   rœ   r&   c               S   s   d S )Nr#   r#   r#   r#   r$   r@  9  s    z.TraitListObject.__setstate__.<locals>.<lambda>)r|   rZ  r   r&   r\  rd   rP   )r/   r±   r'   r&   r#   r#   r$   r²   2  s    

zTraitListObject.__setstate__)N)NNF)NF)r@   rA   rB   rS   r?  rE  r9  r—   ÚPY2rL  rM  rN  rP  rQ  r  rS  r  rU  rX  rY  rW  rZ  r\  r<  r°   r²   r#   r#   r#   r$   rV   “	  s2   
	J%)%
		rV   c               @   s   e Zd Zddd„ZdS )ÚTraitSetEventNc             C   s,   |d krt ƒ }|| _|d kr"t ƒ }|| _d S )N)ru   r,  r-  )r/   r,  r-  r#   r#   r$   rS   I  s    zTraitSetEvent.__init__)NN)r@   rA   rB   rS   r#   r#   r#   r$   r_  C  s   r_  c                   s¸   e Zd Z‡ fdd„Zd*dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd+dd„Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Z‡  ZS ),r^   c                sž   || _ tˆƒ| _ˆ | _d | _|jr,ˆ d | _y<|jjj‰ˆd k	rV‡ ‡‡fdd„|D ƒ}t	t
| ƒj|ƒ d S  tk
r˜ } z|jdƒ |‚W Y d d }~X nX d S )Nr6  c                s   g | ]}ˆˆˆ |ƒ‘qS r#   r#   )rû   r7  )r'   r&   r.   r#   r$   rý   e  s    z+TraitSetObject.__init__.<locals>.<listcomp>zEach element of the)r;   r   r&   r'   r8  rF   rØ   r   r.   r  r^   rS   r   r;  )r/   r;   r&   r'   r)   r=  )rb   )r'   r&   r.   r$   rS   Y  s    



zTraitSetObject.__init__Nc             C   s>   | j ƒ }|dk	r:|dkr,t| dƒr,| jjƒ }|j|||ƒ dS )zE Send a TraitDictEvent to the owning object if there is one.
        Nr;   )r&   rÓ   r;   r  r>  )r/   r'   r   r  r&   r#   r#   r$   r?  o  s
    
z&TraitSetObject._send_trait_items_eventc                sH   t | ƒ}|ˆ krˆ | S t| jdd„ | j‡ fdd„| D ƒƒ ˆ |< }|S )Nc               S   s   d S )Nr#   r#   r#   r#   r$   r@    s    z-TraitSetObject.__deepcopy__.<locals>.<lambda>c                s   g | ]}t j|ˆ ƒ‘qS r#   )rN   rA  )rû   rü   )rB  r#   r$   rý   �  s    z/TraitSetObject.__deepcopy__.<locals>.<listcomp>)rC  r^   r;   r'   )r/   rB  rD  r¯   r#   )rB  r$   rE  x  s    zTraitSetObject.__deepcopy__c                sÞ   t | dƒstj| |ƒS y’t|tƒs*t|ƒ}|j| ƒ}t|ƒdkr¦| jƒ ‰| jjj	j
‰ˆd k	r|| j‰ t‡ ‡‡fdd„|D ƒƒ}tj| |ƒ | jd k	r¦| j| jtd |ƒƒ W n0 tk
rØ } z|jdƒ |‚W Y d d }~X nX d S )Nr;   r   c                s   g | ]}ˆˆˆ |ƒ‘qS r#   r#   )rû   Úitem)r'   r&   r.   r#   r$   rý   “  s    z)TraitSetObject.update.<locals>.<listcomp>zEach element of the)rÓ   ru   rP   rU   Ú
differencerL   r&   r;   rØ   r   r.   r'   r8  r?  r_  r   r;  )r/   r)   r-  r=  r#   )r'   r&   r.   r$   rP   †  s(    




zTraitSetObject.updatec             C   sB   | j |ƒ}t|ƒdkr>tj| |ƒ | jd k	r>| j| jt|ƒƒ d S )Nr   )ra  rL   ru   Údifference_updater8  r?  r_  )r/   r)   r,  r#   r#   r$   Úintersection_update   s    

z"TraitSetObject.intersection_updatec             C   sB   | j |ƒ}t|ƒdkr>tj| |ƒ | jd k	r>| j| jt|ƒƒ d S )Nr   )ÚintersectionrL   ru   rb  r8  r?  r_  )r/   r)   r,  r#   r#   r$   rb  ª  s    

z TraitSetObject.difference_updatec                sÖ   t | dƒstj| |ƒS t|tƒs(t|ƒ}| j|ƒ}|j| ƒ}t|ƒdksTt|ƒdkrÒ| jƒ ‰tj| |ƒ t|ƒdkr´| j	j
jj‰ˆd k	r¨| j‰ t‡ ‡‡fdd„|D ƒƒ}tj| |ƒ | jd k	rÒ| j| jt||ƒƒ d S )Nr;   r   c                s   g | ]}ˆˆˆ |ƒ‘qS r#   r#   )rû   r`  )r'   r&   r.   r#   r$   rý   Ä  s    z>TraitSetObject.symmetric_difference_update.<locals>.<listcomp>)rÓ   ru   Úsymmetric_difference_updaterU   rd  ra  rL   r&   rb  r;   rØ   r   r.   r'   rP   r8  r?  r_  )r/   r)   r,  r-  r#   )r'   r&   r.   r$   re  ´  s&    




z*TraitSetObject.symmetric_difference_updatec             C   s²   t | dƒstj| |ƒS || kr®y^| jƒ }| jjjj}|d k	rJ||| j|ƒ}tj| |ƒ | j	d k	rz| j
| j	td t|gƒƒƒ W n0 tk
r¬ } z|jdƒ |‚W Y d d }~X nX d S )Nr;   zEach element of the)rÓ   ru   Úaddr&   r;   rØ   r   r.   r'   r8  r?  r_  r   r;  )r/   r)   r&   r.   r=  r#   r#   r$   rf  Î  s    


zTraitSetObject.addc             C   s2   t j| |ƒ | jd k	r.| j| jtt |gƒƒƒ d S )N)ru   rU  r8  r?  r_  )r/   r)   r#   r#   r$   rU  â  s    
zTraitSetObject.removec             C   s   || kr| j |ƒ d S )N)rU  )r/   r)   r#   r#   r$   Údiscardê  s    zTraitSetObject.discardc             C   s0   t j| ƒ}| jd k	r,| j| jtt |gƒƒƒ |S )N)ru   rZ  r8  r?  r_  )r/   r)   r#   r#   r$   rZ  î  s
    

zTraitSetObject.popc             C   s2   t | ƒ}t j| ƒ | jd k	r.| j| jt|ƒƒ d S )N)ru   Úclearr8  r?  r_  )r/   r,  r#   r#   r$   rh  ø  s
    

zTraitSetObject.clearc             C   s   t | ƒS )z? Return a true ``set`` object with a copy of the data.
        )ru   )r/   r#   r#   r$   rN     s    zTraitSetObject.copyc             C   s    t jjt| ƒtt| ƒf| jƒ fS )zB Overridden to make sure we call our custom __getstate__.
        )ÚsmÚcopyregÚ_reconstructorr   ru   rX   r°   )r/   Úprotocolr#   r#   r$   Ú__reduce_ex__  s    zTraitSetObject.__reduce_ex__c             C   s&   | j jƒ }|jdd ƒ |jdd ƒ |S )Nr&   r;   )rd   rN   rZ  )r/   r¯   r#   r#   r$   r°     s    
zTraitSetObject.__getstate__c             C   sP   |j ddƒ}|jdd ƒ}|d k	r6t|ƒ| _| j|ƒ n
dd„ | _| jj|ƒ d S )Nr'   rœ   r&   c               S   s   d S )Nr#   r#   r#   r#   r$   r@    s    z-TraitSetObject.__setstate__.<locals>.<lambda>)r|   rZ  r   r&   r\  rd   rP   )r/   r±   r'   r&   r#   r#   r$   r²     s    

zTraitSetObject.__setstate__c             C   s   | j |ƒ | S )N)rP   )r/   r)   r#   r#   r$   Ú__ior__   s    
zTraitSetObject.__ior__c             C   s   | j |ƒ | S )N)rc  )r/   r)   r#   r#   r$   Ú__iand__$  s    
zTraitSetObject.__iand__c             C   s   | j |ƒ | S )N)re  )r/   r)   r#   r#   r$   Ú__ixor__(  s    
zTraitSetObject.__ixor__c             C   s   | j |ƒ | S )N)rb  )r/   r)   r#   r#   r$   Ú__isub__,  s    
zTraitSetObject.__isub__)N)N)r@   rA   rB   rS   r?  rE  rP   rc  rb  re  rf  rU  rg  rZ  rh  rN   rm  r°   r²   rn  ro  rp  rq  r  r#   r#   )rb   r$   r^   X  s(   
	


	
	r^   c               @   s   e Zd Zddd„ZdS )ÚTraitDictEventNc             C   s:   |dkri }|| _ |dkri }|| _|dkr0i }|| _dS )zÿ
        Parameters
        ----------
        added : dict
            New keys and values.
        changed : dict
            Updated keys and their previous values.
        removed : dict
            Old keys and values that were just removed.
        N)r-  Úchangedr,  )r/   r-  rs  r,  r#   r#   r$   rS   7  s    zTraitDictEvent.__init__)NNN)r@   rA   rB   rS   r#   r#   r#   r$   rr  6  s   rr  c               @   sN   e Zd ZdZdZeZdZddd„Zdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ ZdS )Ú	TraitDicta5   Ensures that values assigned to a trait attribute are dictionaries whose
    keys and values are of specified types.

    TraitDict also makes sure that any changes to keys or values made that are
    made after the dictionary is assigned to the trait attribute satisfy the
    type constraints. TraitDict is the underlying handler for the
    dictionary-based predefined traits, and the Dict() trait factory.

    Example
    -------

    class WorkoutClass(HasTraits):
        member_weights = Trait({}, TraitDict(str, float))


    This example defines a WorkoutClass class containing a *member_weights*
    trait attribute whose value must be a dictionary containing keys that
    are strings (i.e., the members' names) and whose associated values must
    be floats (i.e., their most recently recorded weight).
    NTc             C   s@   t |ƒ| _t |ƒ| _|| _| jj}|jr6|jƒ }d|_|| _dS )a.   Creates a TraitDict handler.

        Parameters
        ----------
        key_trait : trait
            The type for the dictionary keys.
        value_trait : trait
            The type for the dictionary values.
        has_items : bool
            Flag indicating whether the dictionary contains entries.

        Description
        -----------
        If *key_trait* is None or omitted, the keys in the dictionary can
        be of any type. Otherwise, *key_trait* must be either a trait, or a
        value that can be converted to a trait using the Trait() function. In
        this case, all dictionary keys are checked to ensure that they are of
        the type specified by *key_trait*.

        If *value_trait* is None or omitted, the values in the dictionary
        can be of any type. Otherwise, *value_trait* must be either a trait, or
        a value that can be converted to a trait using the Trait() function.
        In this case, all dictionary values are checked to ensure that they are
        of the type specified by *value_trait*.

        FN)r   Ú	key_traitÚvalue_traitrF   r   rf   Úvalue_handler)r/   ru  rv  rF   r   r#   r#   r$   rS   q  s    

zTraitDict.__init__c             C   s   t | j| j| jƒS )N)rt  ru  rv  rF   )r/   r#   r#   r$   rf   •  s    zTraitDict.clonec             C   s*   t |tƒrt| |||ƒS | j|||ƒ d S )N)rU   r\   rZ   r2   )r/   r&   r'   r)   r#   r#   r$   r.   ˜  s    
zTraitDict.validatec             C   sh   d}| j j}|d k	r&d|j|||ƒ }| j}|d k	r`|dkrBd}n|d7 }|d|j|||ƒ 7 }d| S )Nrœ   z with keys which are %sz withz andz values which are %sza dictionary%s)ru  r   r1   rw  )r/   r&   r'   r)   Úextrar   r#   r#   r$   r1   �  s    zTraitDict.full_infoc             C   s(   | j d kr"ddlm} |td�| _ | j S )Nr   )r=   )r¼   )r9   r>   r=   r  )r/   r;   r=   r#   r#   r$   r<   ¯  s    
zTraitDict.get_editorc             C   s"   t jd krttdd�jƒ t _t jS )NF)r4  )rt  r3  r5  rr  rl   )r/   r#   r#   r$   r  ·  s    
zTraitDict.items_event)NNT)r@   rA   rB   rC   r2  r[   rE   r3  rS   rf   r.   r1   r<   r  r#   r#   r#   r$   rt  W  s   
$rt  c               @   s„   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	ddd„Z
efdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) rZ   c             C   sN   || _ t|ƒ| _|| _d | _|jr,|d | _t|ƒdkrJtj| | j	|ƒƒ d S )Nr6  r   )
r;   r   r&   r'   r8  rF   rL   r\   rP   Ú_validate_dic)r/   r;   r&   r'   r)   r#   r#   r$   rS   Æ  s    

zTraitDictObject.__init__Nc             C   s>   | j ƒ }|dk	r:|dkr,t| dƒr,| jjƒ }|j|||ƒ dS )zE Send a TraitDictEvent to the owning object if there is one.
        Nr;   )r&   rÓ   r;   r  r>  )r/   r'   r   r  r&   r#   r#   r$   r?  Ñ  s
    
z'TraitDictObject._send_trait_items_eventc                sR   t | ƒ}|ˆ krˆ | S t| jdd„ | jt‡ fdd„tj| ƒD ƒƒƒ ˆ |< }|S )Nc               S   s   d S )Nr#   r#   r#   r#   r$   r@  á  s    z.TraitDictObject.__deepcopy__.<locals>.<lambda>c                s   g | ]}t j|ˆ ƒ‘qS r#   )rN   rA  )rû   rü   )rB  r#   r$   rý   ã  s    z0TraitDictObject.__deepcopy__.<locals>.<listcomp>)rC  rZ   r;   r'   r\   r—   Ú	iteritems)r/   rB  rD  r¯   r#   )rB  r$   rE  Ú  s    &zTraitDictObject.__deepcopy__c       
      C   sp  t | dd ƒ}|d kr&tj| ||ƒ d S | jƒ }y$|jjj}|d k	rP||| j|ƒ}W n0 tk
r‚ } z|j	dƒ |‚W Y d d }~X nX y´|j
j}|d k	r¤||| j|ƒ}| jd k	rØ|| krÌd }| | }||i}	n||i}d }	tj| ||ƒ | jd k	�r6|d k�ry||k�rd S W n   Y nX | j| jt||	ƒ|jƒ ƒ W n2 tk
�rj } z|j	dƒ |‚W Y d d }~X nX d S )Nr;   zEach key of thezEach value of the)r{   r\   r9  r&   ru  r   r.   r'   r   r;  rw  r8  r?  rr  r  )
r/   r  r)   r;   r&   r.   r=  r-  rj   rs  r#   r#   r$   r9  è  sL    






zTraitDictObject.__setitem__c             C   sD   | j d k	r|| | i}tj| |ƒ | j d k	r@| j| j t|d�ƒ d S )N)r,  )r8  r\   rM  r?  rr  )r/   r  r,  r#   r#   r$   rM    s    

zTraitDictObject.__delitem__c             C   sJ   t | ƒdkrF| jd k	r| jƒ }tj| ƒ | jd k	rF| j| jt|d�ƒ d S )Nr   )r,  )rL   r8  rN   r\   rh  r?  rr  )r/   r,  r#   r#   r$   rh  %  s    


zTraitDictObject.clearc             C   s¶   t | dd ƒ}|d kr$tj| |ƒ d S t|ƒdkr²| j|ƒ}| jd k	r¦i }i }x4tj|ƒD ]&\}}|| krv| | ||< qX|||< qXW tj| |ƒ | j| jt	||d�ƒ ntj| |ƒ d S )Nr;   r   )r-  rs  )
r{   r\   rP   rL   ry  r8  r—   rz  r?  rr  )r/   Údicr;   Únew_dicr-  rs  r  r)   r#   r#   r$   rP   1  s$    

zTraitDictObject.updatec             C   sF   || kr| | S || |< | | }| j d k	rB| j| j t||id�ƒ |S )N)r-  )r8  r?  rr  )r/   r  r)   r¯   r#   r#   r$   r|   L  s    
zTraitDictObject.setdefaultc             C   sF   |t ks|| krBtj| |ƒ}| jd k	r>| j| jt||id�ƒ |S |S )N)r,  )r	   r\   rZ  r8  r?  rr  )r/   r  r)   r¯   r#   r#   r$   rZ  Z  s    
zTraitDictObject.popc             C   s8   t j| ƒ}| jd k	r4| j| jt|d |d id�ƒ |S )Nr   r   )r,  )r\   Úpopitemr8  r?  rr  )r/   r¯   r#   r#   r$   r}  g  s
    

zTraitDictObject.popitemc             C   sB   | j ƒ j|dƒ}|d k	r(|| _|j| _ntjd| j ƒ |f ƒ d S )Nr   z!rename: No 'trait' in %s for '%s')r&   r[  r'   r   r;   ÚloggerÚdebug)r/   r'   r;   r#   r#   r$   r\  q  s    
zTraitDictObject.renamec             C   s&   | j jƒ }|jdd ƒ |jdd ƒ |S )Nr&   r;   )rd   rN   rZ  )r/   r¯   r#   r#   r$   r°   {  s    
zTraitDictObject.__getstate__c             C   sP   |j ddƒ}|jdd ƒ}|d k	r6t|ƒ| _| j|ƒ n
dd„ | _| jj|ƒ d S )Nr'   rœ   r&   c               S   s   d S )Nr#   r#   r#   r#   r$   r@  ˆ  s    z.TraitDictObject.__setstate__.<locals>.<lambda>)r|   rZ  r   r&   r\  rd   rP   )r/   r±   r'   r&   r#   r#   r$   r²   �  s    

zTraitDictObject.__setstate__c       
       C   sò   | j }i }| jjjj}|d kr&dd„ }| jjjj}|d krBdd„ }| jƒ }x¢tj|ƒD ]”\}}y||||ƒ}W n0 t	k
rž }	 z|	j
dƒ |	‚W Y d d }	~	X nX y||||ƒ}W n0 t	k
rà }	 z|	j
dƒ |	‚W Y d d }	~	X nX |||< qVW |S )Nc             S   s   |S )Nr#   )r&   r'   r  r#   r#   r$   r@  ”  s    z/TraitDictObject._validate_dic.<locals>.<lambda>c             S   s   |S )Nr#   )r&   r'   r)   r#   r#   r$   r@  ˜  s    zEach key of thezEach value of the)r'   r;   ru  r   r.   rv  r&   r—   rz  r   r;  )
r/   r{  r'   r|  Zkey_validateZvalue_validater&   r  r)   r=  r#   r#   r$   ry  Ž  s,    

zTraitDictObject._validate_dic)N)N)r@   rA   rB   rS   r?  rE  r9  rM  rh  rP   r|   r	   rZ  r}  r\  r°   r²   ry  r#   r#   r#   r$   rZ   Å  s   
	2


rZ   )Úctraitsc              O   s    ddl m} tj|ƒ || |ŽS )Nr   )rÎ   )rÖ   rÎ   r€  Ú_adapt)rm   rn   rÎ   r#   r#   r$   Ú_adapt_wrapper·  s    
r‚  r¹   r¹   )irC   Ú
__future__r   rß   r§   rN   r—   Z	six.movesÚmovesri  r"  r   r   r   rµ   Úweakrefr   Z
trait_baser   r   r	   r
   r   r   r   r   r   Ztrait_errorsr   r   rœ   r   r   ZTraitr5  ÚloggingÚ	getLoggerr@   r~  Z
NO_COMPAREZOBJECT_IDENTITY_COMPAREZRICH_COMPAREr�   r‰   rŠ   r÷   rD   rT   ZMISSING_DEFAULT_VALUEZOBJECT_DEFAULT_VALUErY   r]   rW   r[   ZCALLABLE_AND_ARGS_DEFAULT_VALUEZCALLABLE_DEFAULT_VALUEr_   r}   r   r%   r(   r*   r+   r,   r&   r-   rH   rI   r‡   rˆ   r¡   r³   r¾   rÀ   rÍ   rË   rç   rî   rê   rï   rõ   rù   r   r  r  r  r  r  r(  r*  r.  r  rX   rV   r_  ru   r^   rr  rt  r\   rZ   r€  Z_list_classesr‚  r�  r#   r#   r#   r$   Ú<module>   s°   
,




 5  h= E{uN> :	:3E`S?  Wa   3 _!n n