3
d                 @   s\   d Z ddlmZ ddlmZ dd Zdd Zd	d
 ZeZdd Z	dd Z
dd Zdd ZdS )a   Utility routines that convert objects to CTrait instances

Most of these functions depend on objects having an informal ``as_ctrait``
interface if they know how to turn themselves into a CTrait instance.

We need to special-case ``TraitType`` subclasses so that we can use them
in trait definitions without needing to call them, eg.::

    x = Float

rather than::

    x = Float()

To handle this, there is another informal interface with the deliberately
unwieldy classmethod name ``instantiate_and_get_ctrait``.
    )partial   )DefaultValuec             C   s"   yt | S  tk
r   dS X dS )zD Convert to a CTrait if the object knows how, else return None.
    N)	as_ctrait	TypeError)obj r   9/tmp/pip-build-7vycvbft/traits/traits/trait_converters.py
trait_cast"   s    r
   c             C   sL   t | trt| dr| j S t | t r:t| dr:| j S tdj| dS )a   Convert to CTrait if the object knows how, else raise TraitError.

    Parameters
    ----------
    obj
        An object that supports conversion to `CTrait`. Refer
        documentation for when the object supports this conversion.

    Returns
    -------
    ctrait.CTrait
        A CTrait object.

    Raises
    ------
    TypeError
        If the object does not support conversion to CTrait.
    instantiate_and_get_ctraitr   z1Object {!r} does not support conversion to CTraitN)
isinstancetypehasattrr   r   r   format)r   r   r   r	   r   +   s    r   c             C   s"   yt | S  tk
r   | S X dS )zl Returns either the original value or a valid CTrait if the value can be
        converted to a CTrait.
    N)r   r   )traitr   r   r	   check_traitG   s    r   c             C   sP   ddl m} ddlm} | dkr*| j S yt| S  tk
rJ   || S X dS )z- Returns a trait derived from its input.
    r   )Any)TraitN)trait_typesr   traitsr   r   r   )r   r   r   r   r   r	   
trait_fromU   s    
r   c             C   s2   ddl m} yt| S  tk
r,   || S X dS )z; Returns the trait corresponding to a specified value.
    r   )r   N)r   r   r   r   )r   r   r   r   r	   	trait_fore   s
    r   c             C   s   t ||}| jj|S )a   Callable providing default for a shadow trait of a mapped trait pair.

    Parameters
    ----------
    trait : CTrait
        The principal trait of the mapped trait pair.
    name : str
        The name of the trait on the relevant HasTraits object.
    instance : HasTraits
        The HasTraits object on which the mapped trait lives.

    Returns
    -------
    default : object
        The default value for the shadow trait.
    )getattrhandlerZmapped_value)r   nameinstancevaluer   r   r	   _mapped_trait_defaultt   s    
r   c             C   s8   ddl m} |ddddj }|jtjtt| | |S )a   Returns the 'mapped trait' definition for a mapped trait, the default
        value of which is a callable that maps the value of the original trait.

        Parameters
        ----------
        trait : ctrait.CTrait
            A trait for which the 'mapped trait' definition is being created.
        name : str
            The name of the trait for which the 'mapped trait' definition is
            being created.

        Returns
        -------
        trait_types.Any
            A definition of the 'mapped trait'
    r   )r   FT)is_baseZ	transienteditable)r   r   r   Zset_default_valuer   callabler   r   )r   r   r   Zmapped_traitr   r   r	   mapped_trait_for   s    r!   N)__doc__	functoolsr   	constantsr   r
   r   r   Ztry_trait_castr   r   r   r!   r   r   r   r	   <module>   s   	