U
    /e                     @  s   d Z ddlmZ ddlZee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
ZG dd deZeedd ZdS )z Provide the Either property.

The Either property is used to construct properties that an accept any of
multiple possible types.

    )annotationsN   )	nice_join   )property_linkregister_type_link	type_link)DeserializationErrorParameterizedProperty)	Intrinsic)Eitherc                      sx   e Zd ZdZedddddd fddZd	dd
dZedd ZddddZ	dd Z
d fdd	Zdd Z  ZS )r   a   Accept values according to a sequence of other property types.

        Example:

        .. code-block:: python

            >>> class EitherModel(HasProps):
            ...     prop = Either(Bool, Int, Auto)
            ...

            >>> m = EitherModel()

            >>> m.prop = True

            >>> m.prop = 10

            >>> m.prop = "auto"

            >>> m.prop = 10.3   # ValueError !!

            >>> m.prop = "foo"  # ValueError !!

    NFdefaulthelp
serializedreadonlyNone)returnc          	        sh   t t| j||f| }|tk	r$|n
|d  }t j||||d || _| jD ]}| j	|j qPd S )Nr   r   )
listmapZ_validate_type_paramr   Z_raw_defaultsuper__init___type_paramsZalternativesextend)	selfZtp1Ztp2r   r   r   r   type_paramstp	__class__ >/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/core/property/either.pyr   E   s    
zEither.__init__strc                 C  s.   | j j}ddd | jD }| d| dS )N, c                 s  s   | ]}t |V  qd S N)r!   .0xr   r   r    	<genexpr>O   s     z!Either.__str__.<locals>.<genexpr>())r   __name__joinr   )r   
class_nameZ
item_typesr   r   r    __str__M   s    zEither.__str__c                 C  s   | j S r#   )r   )r   r   r   r    r   R   s    zEither.type_paramsmodelsc             	   C  sL   | j D ].}z|j||dW   S  tk
r2   Y qX qt|  d| d S )Nr.   z couldn't deserialize )r   	from_jsonr	   )r   jsonr/   r   r   r   r    r0   V   s    
zEither.from_jsonc              	   C  sD   | j D ]*}z||W   S  tk
r.   Y qX qtd|d S )NzCould not transform )r   	transform
ValueError)r   valueparamr   r   r    r2   ^   s    
zEither.transformTc                   sT   t   | t fdd| jD r*d S |s2dndt| j d }t|d S )Nc                 3  s   | ]}|  V  qd S r#   )Zis_valid)r%   r5   r4   r   r    r'   j   s     z"Either.validate.<locals>.<genexpr> zexpected an element of either z, got )r   validateanyr   r   r3   )r   r4   Zdetailmsgr   r6   r    r8   g   s
    zEither.validatec                 C  s   | j D ]}||}q|S r#   )r   wrap)r   r4   r   r   r   r    r;   p   s    
zEither.wrap)T)r*   
__module____qualname____doc__r   r   r-   propertyr   r0   r2   r8   r;   __classcell__r   r   r   r    r   ,   s   
		r   c                 C  s*   d dd | jD }t|  d| dS )Nr"   c                 s  s   | ]}t |V  qd S r#   )r   r$   r   r   r    r'      s     z$_sphinx_type_link.<locals>.<genexpr>r(   r)   )r+   r   r   )objZsubtypesr   r   r    _sphinx_type_link   s    rB   )r>   
__future__r   logging	getLoggerr*   logZutil.stringr   Z_sphinxr   r   r   basesr	   r
   Z
singletonsr   __all__r   rB   r   r   r   r    <module>   s   

Y