3
d i                 @   s  d Z ddlmZmZ ddlZddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZmZmZ dd	lmZmZ dd
lmZ ddlmZmZ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& ddl'm(Z( e)dZ*e*e+e,e-e.fZ/e.e+e,e-e0e1e2eee)e*fZ3eefZ4ee
fZ5e.de+de,de-de0g e1f e2i e6diZ7dd Z8G dd de9Z:G dd de9Z;dd Z<G dd de9Z=d*ddZ>e&e>Z>G dd  d e9Z?e
ej@ZAe(d!d"d# ZBe&eBZBe(d$d%d& ZCe&eCZCe(d'd(d) ZDe&eDZDdS )+a  
Defines the 'core' traits for the Traits package. A trait is a type definition
that can be used for normal Python object attributes, giving the attributes
some additional characteristics:

Initialization:
    Traits have predefined values that do not need to be explicitly
    initialized in the class constructor or elsewhere.
Validation:
    Trait attributes have flexible, type-checked values.
Delegation:
    Trait attributes' values can be delegated to other objects.
Notification:
    Trait attributes can automatically notify interested parties when
    their values change.
Visualization:
    Trait attributes can automatically construct (automatic or
    programmer-defined) user interfaces that allow their values to be
    edited or displayed)

.. note:: 'trait' is a synonym for 'property', but is used instead of the
    word 'property' to differentiate it from the Python language 'property'
    feature.
    )FunctionType
MethodTypeN   )ComparisonModeDefaultValue	TraitKind)CTrait)
TraitError)SequenceTypes	TypeTypesadd_article)
trait_castcheck_trait)TraitHandler)_infer_default_value_type
_read_only_write_only)	TraitInstanceTraitFunctionTraitCoerceTypeTraitCastType	TraitEnumTraitCompoundTraitMap_undefined_get_undefined_set)TraitFactory)
deprecated g        y                Fc             G   s   | j | f| S )z" Unpickles new-style objects.
    )__new__)clsargs r"   //tmp/pip-build-7vycvbft/traits/traits/traits.py
__newobj__w   s    r$   c               @   s   e Zd Zdd ZdS )_InstanceArgsc             C   s   |f| | _ || _d S )N)r!   kw)selffactoryr!   r&   r"   r"   r#   __init__   s    z_InstanceArgs.__init__N)__name__
__module____qualname__r)   r"   r"   r"   r#   r%      s   r%   c               @   s    e Zd ZdZdf dfddZdS )Defaultz Generates a value the first time it is accessed.

    A Default object can be used anywhere a default trait value would normally
    be specified, to generate a default value dynamically.
    Nc             C   s   |||f| _ d S )N)default_value)r'   funcr!   r&   r"   r"   r#   r)      s    zDefault.__init__)r*   r+   r,   __doc__r)   r"   r"   r"   r#   r-      s   r-   c              O   s   t | |j S )aq   Creates a trait definition.

    .. note::

       The :func:`~.Trait` function is not recommended for use in new code,
       and may eventually be deprecated and removed. Consider using
       :class:`~.Union` instead.

    This function accepts a variety of forms of parameter lists:

    +-------------------+---------------+-------------------------------------+
    | Format            | Example       | Description                         |
    +===================+===============+=====================================+
    | Trait(*default*)  | Trait(150.0)  | The type of the trait is inferred   |
    |                   |               | from the type of the default value, |
    |                   |               | which must be in *ConstantTypes*.   |
    +-------------------+---------------+-------------------------------------+
    | Trait(*default*,  | Trait(None,   | The trait accepts any of the        |
    | *other1*,         | 0, 1, 2,      | enumerated values, with the first   |
    | *other2*, ...)    | 'many')       | value being the default value. The  |
    |                   |               | values must be of types in          |
    |                   |               | *ConstantTypes*, but they need not  |
    |                   |               | be of the same type. The *default*  |
    |                   |               | value is not valid for assignment   |
    |                   |               | unless it is repeated later in the  |
    |                   |               | list.                               |
    +-------------------+---------------+-------------------------------------+
    | Trait([*default*, | Trait([None,  | Similar to the previous format, but |
    | *other1*,         | 0, 1, 2,      | takes an explicit list or a list    |
    | *other2*, ...])   | 'many'])      | variable.                           |
    +-------------------+---------------+-------------------------------------+
    | Trait(*type*)     | Trait(Int)    | The *type* parameter must be a name |
    |                   |               | of a Python type (see               |
    |                   |               | *PythonTypes*). Assigned values     |
    |                   |               | must be of exactly the specified    |
    |                   |               | type; no casting or coercion is     |
    |                   |               | performed. The default value is the |
    |                   |               | appropriate form of zero, False,    |
    |                   |               | or emtpy string, set or sequence.   |
    +-------------------+---------------+-------------------------------------+
    | Trait(*class*)    |::             | Values must be instances of *class* |
    |                   |               | or of a subclass of *class*. The    |
    |                   | class MyClass:| default value is None, but None     |
    |                   |    pass       | cannot be assigned as a value.      |
    |                   | foo = Trait(  |                                     |
    |                   | MyClass)      |                                     |
    +-------------------+---------------+-------------------------------------+
    | Trait(None,       |::             | Similar to the previous format, but |
    | *class*)          |               | None *can* be assigned as a value.  |
    |                   | class MyClass:|                                     |
    |                   |   pass        |                                     |
    |                   | foo = Trait(  |                                     |
    |                   | None, MyClass)|                                     |
    +-------------------+---------------+-------------------------------------+
    | Trait(*instance*) |::             | Values must be instances of the     |
    |                   |               | same class as *instance*, or of a   |
    |                   | class MyClass:| subclass of that class. The         |
    |                   |    pass       | specified instance is the default   |
    |                   | i = MyClass() | value.                              |
    |                   | foo =         |                                     |
    |                   |   Trait(i)    |                                     |
    +-------------------+---------------+-------------------------------------+
    | Trait(*handler*)  | Trait(        | Assignment to this trait is         |
    |                   | TraitEnum )   | validated by an object derived from |
    |                   |               | **traits.TraitHandler**.            |
    +-------------------+---------------+-------------------------------------+
    | Trait(*default*,  | Trait(0.0, 0.0| This is the most general form of    |
    | { *type* |        | 'stuff',      | the function. The notation:         |
    | *constant* |      | TupleType)    | ``{...|...|...}+`` means a list of  |
    | *dict* | *class* ||               | one or more of any of the items     |
    | *function* |      |               | listed between the braces. Thus, the|
    | *handler* |       |               | most general form of the function   |
    | *trait* }+ )      |               | consists of a default value,        |
    |                   |               | followed by one or more of several  |
    |                   |               | possible items. A trait defined by  |
    |                   |               | multiple items is called a          |
    |                   |               | "compound" trait.                   |
    +-------------------+---------------+-------------------------------------+

    All forms of the Trait function accept both predefined and arbitrary
    keyword arguments. The value of each keyword argument becomes bound to the
    resulting trait object as the value of an attribute having the same name
    as the keyword. This feature lets you associate metadata with a trait.

    The following predefined keywords are accepted:

    desc : str
        Describes the intended meaning of the trait. It is used in
        exception messages and fly-over help in user interfaces.
    label : str
        Provides a human-readable name for the trait. It is used to label user
        interface editors for traits.
    editor : traits.api.Editor
        Instance of a subclass Editor object to use when creating a user
        interface editor for the trait. See the "Traits UI User Guide" for
        more information on trait editors.
    comparison_mode : int
        Indicates when trait change notifications should be generated based
        upon the result of comparing the old and new values of a trait
        assignment. Possible values come from the ``ComparisonMode`` enum:

        * 0 (none): The values are not compared and a trait change
          notification is generated on each assignment.
        * 1 (identity): A trait change notification is
          generated if the old and new values are not the same object.
        * 2 (equality): A trait change notification is generated if the
          old and new values are not equal using Python's standard equality
          testing. This is the default.

    )_TraitMaker	as_ctrait)
value_typemetadatar"   r"   r#   Trait   s    or5   c               @   s:   e Zd ZejejdZdd Zdd Zdd Z	dd	 Z
d
S )r1   )eventconstantc             O   s   |j dd | j|| d S )Ntypetrait)
setdefaultdefine)r'   r3   r4   r"   r"   r#   r)     s    z_TraitMaker.__init__c          
   O   s  t j}d } }}t|dkr\|d }|dd }t|dkrZt|tkrZ|d | }}t|dkrt|}|tkrt|}tj	|}n~t
|tr|}|j \}}|j|d< nXt
|tr|}d}nDt|}|tkrt|}n*|jdd t|}||jkr\tj	|}nNg }g }	i }
| j|||
|	 t|dkr|d dkrt|	dkrt
|	d trg }|	d j  |jdd t|dkrt|
t|	 dkr||kr|jd| |	jt| t|
dkr|	jt|
 t|	dkrt }n`t|	dkr|	d }t
|tr4|d }}|j|d< nt
|tr\|jdd |dkr`|j  nt
|trt j}|j|j|jf}nrt|dkr\t|
dkr\|j}t|}|tkrt j}|f |f}n,t
||s\|tk	r|f}t j}||df}ndxbt |	D ]N\}}t
|tr|jdkr4t!dt"|j |j#}|dkrFP ||	|< qW t$|	}|| _#|| _%|dk rt
|t&rt j}|j}nf|dkr|dk	r|j#}|dk	r|j'}|dk ry|j(dd	|}W n   Y nX |dk rt)|}|| _'|| _|j* | _+dS )
z Define the trait. Nr   r   r8   Zinstance_handlerZ_instance_changed_handlerr9   z2Cannot create a complex trait containing %s trait.r   ),r   Zunspecifiedlenr8   r
   try_trait_castPythonTypesr   DefaultValuesget
isinstancer   r.   r   r   r   r:   r   aClassdo_list
allow_noneinsertappendr   r   r%   Zcallable_and_argsZcreate_default_valuer!   r&   dicttuple	enumerater	   r   handlerr   cloner-   default_value_typevalidater   copyr4   )r'   r3   r4   rL   r.   rJ   rK   Z	typeValueenumothermaprB   iitemr"   r"   r#   r;     s    















z_TraitMaker.definec             C   s   x|D ]}|t kr"|jt| qt|}t|}|tkrF|j| q|tkr`| j|||| q|tkrt|j	| q|t
kr|jt| qt|tr|j| q|jt| qW dS )z= Determine the correct TraitHandler for each item in a list. N)r>   rF   r   r=   r8   ConstantTypesr
   rC   rG   updateCallableTypesr   rA   
TraitTypesr   )r'   listrO   rQ   rP   rS   ZtypeItemr"   r"   r#   rC     s     

z_TraitMaker.do_listc       	      C   s>  | j }t| jj|jdtj}| j}|dk	rN|j| |jdk	rN|jj |_|j	| j
| j | j}|dk	r||_t|dd}|dkr|j}|j| t|dd}|dk	r||_|j|_|jd}|dk	rtjdtdd |rtj|_ntj|_|jd	d}|dk	r||_t|d
kr:|jdkr.||_n|jj| |S )z2 Return a properly initialized 'CTrait' instance. r8   NZfast_validatepost_setattrrich_comparezThe 'rich_compare' metadata has been deprecated. Please use the 'comparison_mode' metadata instead. In a future release, rich_compare will have no effect.   )
stacklevelcomparison_moder   )r4   r   type_mapr@   r   r9   rK   __dict__rN   Zset_default_valuerL   r.   rJ   getattrrM   Zset_validaterY   Z	is_mappedwarningswarnDeprecationWarningr   Zequalityr]   identitypopr<   rU   )	r'   r4   r9   rK   rJ   rM   rY   rZ   r]   r"   r"   r#   r2     sJ    





z_TraitMaker.as_ctraitN)r*   r+   r,   r   r6   r7   r^   r)   r;   rC   r2   r"   r"   r"   r#   r1     s    r1   c             K   s  d|d< | r|dkr| dk	|dk	 |dk	 }|dkr|dkrFt |S d}| dk	rV| }|dk	rt|}|dk	r|j }}|dk	r|j}|dk	s|dk	rd|kr|dk	r|jdk	r|j|d< t |||S | dkrd|d< |dkrt} t}nt} n|dkrt}d|d< |dk	rVt|}|j}|dkr6|dk	r6|j}d|krV|jdk	rV|j|d< |j	d	t
| d	d t
| d
dr|j	dd ttj}|j |_| ||f|_||_|S )aU   Returns a trait whose value is a Python property.

    If no getter, setter or validate functions are specified (and **force** is
    not True), it is assumed that they are defined elsewhere on the class whose
    attribute this trait is assigned to. For example::

        class Bar(HasTraits):

            # A float traits Property that should be always positive.
            foo = Property(Float)

            # Shadow trait attribute
            _foo = Float

            def _set_foo(self,x):
                self._foo = x

            def _validate_foo(self, x):
                if x <= 0:
                    raise TraitError(
                        'foo property should be a positive number')
                return x

            def _get_foo(self):
                return self._foo

    You can use the **observe** metadata attribute to indicate that the
    property depends on the value of another trait. The value of **observe**
    follows the same signature as the **expression** parameter in
    ``HasTraits.observe``. The property will fire a trait change notification
    if any of the traits specified by **observe** change. For example::

        class Wheel(Part):
            axle = Instance(Axle)
            position = Property(observe='axle.chassis.position')

    For details of the extended trait name syntax, refer to the
    observe() method of the HasTraits class.

    Parameters
    ----------
    fget : function
        The "getter" function for the property.
    fset : function
        The "setter" function for the property.
    fvalidate : function
        The validation function for the property. The method should return the
        value to set or raise TraitError if the new value is not valid.
    force : bool
        Indicates whether to use only the function definitions specified by
        **fget** and **fset**, and not look elsewhere on the class.
    handler : function
        A trait handler function for the trait.
    trait : Trait or value
        A trait definition or a value that can be converted to a trait that
        constrains the values of the property trait.
    propertyr8   Nr   r   editorTZ	transientZ
depends_oncached_propertyFcached)ForwardPropertyr   rJ   rM   rg   r   r   r   r   r:   r`   r   r   rf   rN   r_   Zproperty_fields)fgetfsetZ	fvalidateforcerJ   r9   r4   sumr"   r"   r#   Property  sX    B






ro   c               @   s   e Zd ZdZdddZdS )rj   zi Used to implement Property traits where accessor functions are defined
    implicitly on the class.
    Nc             C   s   |j  | _|| _|| _d S )N)rN   r4   rM   rJ   )r'   r4   rM   rJ   r"   r"   r#   r)     s    
zForwardProperty.__init__)NN)r*   r+   r,   r0   r)   r"   r"   r"   r#   rj     s   rj   z]'Color' in 'traits' package has been deprecated. Use 'Color' from 'traitsui' package instead.c              O   s   ddl m} || |S )z Returns a trait whose value must be a GUI toolkit-specific color.

    .. deprecated:: 6.1.0
        ``Color`` trait in this package will be removed in the future. It is
        replaced by ``Color`` trait in TraitsUI package.
    r   )
ColorTrait)traitsui.toolkit_traitsrp   )r!   r4   rp   r"   r"   r#   Color  s    	rr   zc'RGBColor' in 'traits' package has been deprecated. Use 'RGBColor' from 'traitsui' package instead.c              O   s   ddl m} || |S )z Returns a trait whose value must be a GUI toolkit-specific RGB-based
    color.

    .. deprecated:: 6.1.0
        ``RGBColor`` trait in this package will be removed in the future. It is
        replaced by ``RGBColor`` trait in TraitsUI package.
    r   )RGBColorTrait)rq   rs   )r!   r4   rs   r"   r"   r#   RGBColor  s    
rt   z['Font' in 'traits' package has been deprecated. Use 'Font' from 'traitsui' package instead.c              O   s   ddl m} || |S )z Returns a trait whose value must be a GUI toolkit-specific font.

    .. deprecated:: 6.1.0
        ``Font`` trait in this package will be removed in the future. It is
        replaced by ``Font`` trait in TraitsUI package.
    r   )	FontTrait)rq   ru   )r!   r4   ru   r"   r"   r#   Font  s    	rv   )NNNFNN)Er0   typesr   r   ra   	constantsr   r   r   Zctraitr   Ztrait_errorsr	   Z
trait_baser
   r   r   Ztrait_convertersr   r   r=   Ztrait_handlerr   Z
trait_typer   r   r   Ztrait_handlersr   r   r   r   r   r   r   r   r   Ztrait_factoryr   Zutil.deprecatedr   r8   ZNoneTypeintfloatcomplexstrrT   rX   rH   rG   r>   rV   rW   boolr?   r$   objectr%   r-   r5   r1   ro   rj   ZgenericZgeneric_traitrr   rt   rv   r"   r"   r"   r#   <module>"   sp   ,			r y     
z
