a
    ¦ˆc  ã                   @   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$   z
t | ƒW S  ty   Y dS 0 dS )zD Convert to a CTrait if the object knows how, else return None.
    N©Ú	as_ctraitÚ	TypeError©Úobj© r
   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/trait_converters.pyÚ
trait_cast"   s    
r   c                 C   sJ   t | tƒrt| dƒr|  ¡ S t | tƒs8t| dƒr8|  ¡ S td | ¡ƒ‚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   Úformatr   r
   r
   r   r   +   s    ÿr   c                 C   s&   z
t | ƒW S  ty    |  Y S 0 dS )zl Returns either the original value or a valid CTrait if the value can be
        converted to a CTrait.
    Nr   )Útraitr
   r
   r   Úcheck_traitG   s    
r   c                 C   sT   ddl m} ddlm} | du r*|ƒ  ¡ S z
t| ƒW S  tyN   || ƒ Y S 0 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   s6   ddl m} z
t| ƒW S  ty0   || ƒ Y S 0 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 |¡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� ¡ }| 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)Z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   	