U
    Zf->                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZddlmZmZm	Z	 edZ
ed ZG dd dZG dd de jZd	d
 Zdd ZG dd dedZdd ZdS )    N   )patch_collections_abcstringify_id
OrderedSetMutableSequencec                   @   s2   e Zd ZdZe ZeeZ	i Z
edddZdS )ComponentRegistryz6Holds a registry of the namespaces used by components.Nc                 C   sB   g }| j D ]2}|d k	r ||kr q
tj| }|t||g  q
|S N)registrysysmodulesextendgetattr)clsresource_nameZincludes	resourcesmodule_namemodule r   C/tmp/pip-unpacked-wheel-47crqvv_/dash/development/base_component.pyget_resources   s    

zComponentRegistry.get_resources)N)__name__
__module____qualname____doc__r   r	   collectionsdefaultdictdictchildren_propsnamespace_to_packageclassmethodr   r   r   r   r   r      s   
r   c                   @   s   e Zd Zdd ZdS )ComponentMetac                 C   sr   t j| |||}|d dd }|dks4|dkr8|S |d|}|tj|< tj| |dtj	| |< |S )Nr   .r   	Componentbuiltins
_namespace_children_props)
abcABCMeta__new__splitgetr   r   r	   addr   )Zmcsnamebases
attributes	componentr   r$   r   r   r   r(   '   s    
zComponentMeta.__new__N)r   r   r   r(   r   r   r   r   r    $   s   r    c                 C   s*   zt |  W dS  tk
r$   Y dS X d S )NTF)float
ValueError)sr   r   r   	is_number:   s
    r3   c                 C   s.   t | dr&t| jts*t| jttfs*td S )Nchildren)hasattr
isinstancer4   r"   tupler   KeyError)itemr   r   r    _check_if_has_indexable_childrenB   s    

r:   c                   @   s   e Zd Zg ZdgZG dd dZe ZG dd dZe 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edd Zdd Zdd Zdd Zdd  Zd!d" ZdS )$r"   r4   c                   @   s   e Zd Zdd Zdd ZdS )zComponent._UNDEFINEDc                 C   s   dS NZ	undefinedr   selfr   r   r   __repr__P   s    zComponent._UNDEFINED.__repr__c                 C   s   dS r;   r   r<   r   r   r   __str__S   s    zComponent._UNDEFINED.__str__Nr   r   r   r>   r?   r   r   r   r   
_UNDEFINEDO   s   rA   c                   @   s   e Zd Zdd Zdd ZdS )zComponent._REQUIREDc                 C   s   dS Nrequiredr   r<   r   r   r   r>   Y   s    zComponent._REQUIRED.__repr__c                 C   s   dS rB   r   r<   r   r   r   r?   \   s    zComponent._REQUIRED.__str__Nr@   r   r   r   r   	_REQUIREDX   s   rD   c              	      s  dd l }t| D ]\ } | jk}t fdd| jD }d|krXd|d  dnd}z\d}d	d
dd}| j|kr||| j | j|j	|}n"|| j| jt
t| jdd|}W n& tk
r   d| j d| }Y nX |s|sdt| j}	t| d  d|	  | jkrRt|trRt|d d  d|d d d  dkrt|tr| D ]Z\}
}t|
tstdd|
d| t|ttttfsptdd|d| qpnt|tstd|t|  | qd S )Nr   c                 3   s   | ]}  |V  qd S r   
startswith.0wkr   r   	<genexpr>h   s    z%Component.__init__.<locals>.<genexpr>idz with the ID "" z$The `{}.{}` component (version {}){}htmlZdcc
dash_table)Zdash_html_componentsZdash_core_componentsrQ   __version__unknownzThe `z` component, z+ received an unexpected keyword argument: `z`
Allowed arguments: z7 detected a Component for a prop other than `children`
zProp z has value z

z8Did you forget to wrap multiple `children` in an array?
zNFor example, it must be html.Div(["a", "b", "c"]) not html.Div("a", "b", "c")
zdict id keys must be strings,
zfound z in id z2dict id values must be strings, numbers or bools,
z(`id` prop must be a string or dict, not )dashlistitems_prop_namesany_valid_wildcard_attributesr$   format_typerR   r   
__import__ImportErrorjoinsorted	TypeError_base_nodesr6   r"   r   strintr0   boolsetattr)r=   kwargsrU   vZk_in_propnamesZk_in_wildcardsZ	id_suffixZerror_string_prefixZdash_packagesZallowed_argsZid_keyZid_valr   rJ   r   __init__a   s    



zComponent.__init__c                 C   s   t | drt| dS d| j d| j d}t| ddrFtd| ddtjkr`td	| dttj	t
d
dd d}t| d| |S )NrM   `r!   ZpersistenceFa  
                Attempting to use an auto-generated ID with the `persistence` prop.
                This is prohibited because persistence is tied to component IDs and
                auto-generated IDs can easily change.

                Please assign an explicit ID to this z component.
                Zdash_snapshotsa  
                Attempting to use an auto-generated ID in an app with `dash_snapshots`.
                This is prohibited because snapshots saves the whole app layout,
                including component IDs, and auto-generated IDs can easily change.
                Callbacks referencing the new IDs will not work with old snapshots.

                Please assign an explicit ID to this r   r      )rd   )r5   r   r$   r\   RuntimeErrorr
   r   rc   uuidUUIDrdrandintrf   )r=   kindrh   r   r   r   _set_random_id   s$    

	
zComponent._set_random_idc                    sB    fdd j D }| fdd jD  | j jd}|S )Nc                    s"   i | ]}t  |r|t |qS r   )r5   r   rH   pr<   r   r   
<dictcomp>   s   
 z,Component.to_plotly_json.<locals>.<dictcomp>c                    s0   i | ]( t  fd djD r t qS )c                 3   s   | ]}  |V  qd S r   rE   rG   rJ   r   r   rL      s   z6Component.to_plotly_json.<locals>.<dictcomp>.<genexpr>)rY   rZ   r   rH   r<   rJ   r   ru      s    )propstype	namespace)rX   update__dict__r\   r$   )r=   rw   Zas_jsonr   r<   r   to_plotly_json   s    

zComponent.to_plotly_jsonNc              	   C   s  t |  t| jtrt| jdd d k	rd| jj|krd|dkr@| jS |dkrR|| _d S |dkrdd | _d S zP|dkr|| j|W S |dkr| j|| W d S |dkr| j| W d S W n t	k
r   Y nX t| jt
tfrt| jD ]\}}t|dd |krF|dkr|  S |dkr,|| j|<  d S |dkr| j|=  d S qt|trzX|dkrl||W   S |dkr||| W  d S |dkr|| W  d S W q t	k
r   Y qX qt	|d S )NrM   r*   setdelete)r:   r6   r4   r"   r   rM   __getitem____setitem____delitem__r8   r7   r   	enumerate)r=   rM   Z	operationZnew_itemir9   r   r   r   _get_set_or_delete   s^    









zComponent._get_set_or_deletec                 C   s   |  |dS )zTRecursively find the element with the given ID through the tree of
        children.r*   r   r=   rM   r   r   r   r   4  s    zComponent.__getitem__c                 C   s   |  |d|S )zSet an element by its ID.r}   r   )r=   rM   r9   r   r   r   r   <  s    zComponent.__setitem__c                 C   s   |  |dS )z+Delete items by ID in the tree of children.r~   r   r   r   r   r   r   @  s    zComponent.__delitem__c                 c   s   |   D ]}|d V  qdS )zYield each item in the tree.   N)_traverse_with_pathsr=   tr   r   r   	_traverseD  s    zComponent._traversec                 C   s"   t t| dd}|o d|ddS )NrM   rO   z (id=r2   ))r   r   )r/   Zid_r   r   r   _id_strI  s    zComponent._id_strc           	      c   s   t | dd}t|j}|| | }t|trhd| |fV  | D ] \}}dd| |g|fV  qDn~t|tt	frt
|D ]f\}}d|ddt|jd| | }||fV  t|tr~| D ]\}}d||g|fV  qq~dS )	z*Yield each item with its path in the tree.r4   Nz[*] 
[dz] r2   )r   rx   r   r   r6   r"   r   r_   r7   r   r   )	r=   r4   Zchildren_typeZchildren_stringrt   r   idxr   	list_pathr   r   r   r   N  s    

$

zComponent._traverse_with_pathsc                 c   s2   |   D ]$}t|trt|dddk	r|V  qdS )z2Yield components with IDs in the tree of children.rM   N)r   r6   r"   r   r   r   r   r   _traverse_idsf  s    zComponent._traverse_idsc                 c   s   |   D ]}|jV  qdS )z"Yield IDs in the tree of children.N)r   rM   r   r   r   r   __iter__l  s    zComponent.__iter__c                 C   s~   d}t | dddkrd}n`t| jtr:d}|t| j7 }n@t| jttfrv| jD ]"}|d7 }t|trP|t|7 }qPnd}|S )z'Return the number of items in the tree.r   r4   Nr   )r   r6   r4   r"   lenr7   r   )r=   lengthcr   r   r   __len__q  s    

zComponent.__len__c                    sv    fdd j D  fdd jD  }tdd |D rTd fdd|D }ntt dd } j d	| d
S )Nc                    s    g | ]}t  |d d k	r|qS r   r   )rH   r   r<   r   r   
<listcomp>  s     z&Component.__repr__.<locals>.<listcomp>c                    s(   g | ]  t  fd djD r qS )c                 3   s   | ]}  |V  qd S r   rE   )rH   Zwc_attrr   r   r   rL     s     z0Component.__repr__.<locals>.<listcomp>.<genexpr>)rY   rZ   rv   r<   r   r   r     s   c                 s   s   | ]}|d kV  qdS )r4   Nr   rs   r   r   r   rL     s     z%Component.__repr__.<locals>.<genexpr>rT   c                 3   s"   | ]}| d t  |V  qdS )=Nr   rs   r<   r   r   rL     s    r4   (r   )rX   r{   rY   r_   reprr   r\   )r=   Zprops_with_valuesZprops_stringr   r<   r   r>     s    


zComponent.__repr__)N)r   r   r   r%   rb   rA   	UNDEFINEDrD   ZREQUIREDri   rr   r|   r   r   r   r   r   staticmethodr   r   r   r   r   r>   r   r   r   r   r"   K   s*   N!
G
r"   )	metaclassc                    s\   t  dr jjn jj fdd}t tdrXt|jt j d}||_	|S )N	func_codec                     sf   d|krt dtttd t|  dd | D  |d< d|d kr\|d d  | |S )NZ_explicit_argsz*Variable _explicit_args should not be set.c                 S   s   g | ]\}}|qS r   r   )rH   rK   _r   r   r   r     s     z6_explicitize_args.<locals>.wrapper.<locals>.<listcomp>r=   )	ExceptionrV   r}   r   rW   remove)argsrg   funcvarnamesr   r   wrapper  s    (z"_explicitize_args.<locals>.wrapper	signature)
parameters)
r5   r   co_varnames__code__inspectr   replacer   values__signature__)r   r   Znew_sigr   r   r   _explicitize_args  s    



r   )r&   r   r   r
   rm   random_utilsr   r   r   r   Randomro   r   r'   r    r3   r:   r"   r   r   r   r   r   <module>   s    
	  P