a
    Df)                     @  s   d Z ddlmZ ddlZddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZ ddlZddlmZ ddlmZ ddlmZmZ d	d
lmZ d	dlmZ d	dlmZ erddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% G dd deZdS )zL
Defines the Location  widget which allows changing the href of the window.
    )annotationsN)TYPE_CHECKINGAnyCallableClassVarDictListMappingOptional   )Location)Syncable)edit_readonlyparse_query   )is_equal)create_doc_if_none_exists)state)Document)Model)BokehSessionContext)Commc                      s|  e Zd ZU dZejdddZejdddZejdddZejdd	dZ	ejdd
dZ
ejdddZejdddZejdddZddiZded< edd Z fddZdKdddddddd ZdLd!d"d#dd$d%d&Zd'd(d) fd*d+ZdMd,d(d- fd.d/ZdNd0d(d1d2d3Zdd4d0d5d(d6d7d8Zed9d:d;d<Zd=d(d>d?d@ZdOdAdBdCd(dDdEdFZdPdAdGd(dHdIdJZ  ZS )Qr   z
    The Location component can be made available in a server context
    to provide read and write access to the URL components in the
    browser.
    TzF
        The full url, e.g. 'https://localhost:80?color=blue#interact')readonlydocz=
        hostname in window.location e.g. 'panel.holoviz.org'z^$|[\/]|srcdoc$zE
        pathname in window.location e.g. '/user_guide/Interact.html')regexr   z=
        protocol in window.location e.g. 'http:' or 'https:'z*
        port in window.location e.g. '80'z^$|\?z5
        search in window.location e.g. '?color=blue'z^$|#z1
        hash in window.location e.g. '#interact'Fz
        Reload the page when the location is updated. For multipage
        apps this should be set to True, For single page apps this
        should be set to False)defaultr   nameNz"ClassVar[Mapping[str, str | None]]_renamec           	      C  s  z,ddl m} t||r"|jd u r*|  W S W n tyD   |   Y S 0 i }d}|jrh|j d |d< }|jr|d|j 7 }d|jv r|jd\|d< |d< n
|j|d< |jrxd  }}||j7 }d	|jv rd
|jv r|jd	\|d< }|d
\}}nNd	|jv r$|jd	\|d< }n,d
|jv rF|jd
\|d< }n
|j|d< |rdd	| |d< |rxd
| |d< ||d< |  }t	| |j
| W d    n1 s0    Y  |S )Nr   )_RequestProxy :protocolz//hostnameport?#pathnamesearchhashhref)bokeh.server.contextsr   
isinstance_requestImportErrorr!   hostspliturir   paramupdate)	clsrequestr   paramsr)   r'   r(   queryloc r8   ^/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/io/location.pyfrom_request@   sF    




,zLocation.from_requestc                   s4   t  jf i | g | _d| _| j| jdg d S )NFr'   )super__init___synced_syncingr1   watch_update_synced)selfr5   	__class__r8   r9   r<   i   s    zLocation.__init__z
'Document'zOptional['Model']zOptional['Comm']z'Model')r   rootparentcommreturnc                 C  sL   t f i | |  }|p|}||f| j|jd < | || j||| |S Nid)_BkLocationZ_process_param_changeZ_init_paramsZ_modelsrefZ_link_propsZ_linked_properties)rA   r   rD   rE   rF   modelr8   r8   r9   
_get_modelo   s
    zLocation._get_modelzOptional[Document]zOptional[Comm]bool)r   rF   
preprocessrG   c                 C  s@   t |}| j||d}|jd }| |||ftj|< || j|< |S )N)rF   rI   )r   rM   rK   r   _views
_documents)rA   r   rF   rO   rD   rK   r8   r8   r9   get_rooty   s    

zLocation.get_rootr   None)session_contextrG   c              	     sH   | j D ]0\}}}}z| || W q ty4   Y q0 qt | d S N)r=   unsync	Exceptionr;   _server_destroy)rA   rT   pps_rB   r8   r9   rX      s    zLocation._server_destroyzModel | None)rD   rG   c                   sL   |r&|j | jv r| j|j = |jd }nd }t | |tjv rHtj|= d S rH   )documentrQ   rK   r;   _cleanupr   rP   )rA   rD   rK   rB   r8   r9   r]      s    

zLocation._cleanupzparam.parameterized.Event)eventrG   c              
   C  s   | j r
d S | j}| jD ]\}}}}dd | D }i }| D ]z\}	}
|	|vrRq@||	 }z|j| |
}
W n ty   Y n0 zt|
t||}W n ty   d}Y n0 |s@|
||< q@z|jj	f i | W q ty   |r|| Y q0 qd S )Nc                 S  s   i | ]\}}||qS r8   r8   .0kvr8   r8   r9   
<dictcomp>       z+Location._update_synced.<locals>.<dictcomp>F)
r>   query_paramsr=   itemsr1   ZdeserializerW   r   getattrr2   )rA   r^   re   rY   
parametersr[   on_errormappingZmappedra   rb   Zpnameequalr8   r8   r9   r@      s2    

zLocation._update_syncedr6   zOptional[Dict[str, Any]])eventsr6   rG   c             	     s   | j r
d S |pi }|D ]  fdd| jD }|s4q jd u rD jn j}z|j j  j}W n tyz    j}Y n0 t	|t
st|}|||d  j < qd| _ z(| jf i dd | D  W d| _ nd| _ 0 d S )Nc                   s(   g | ] \}}}}| j  jfv r|qS r8   )r3   obj)r`   orZ   r[   er8   r9   
<listcomp>   rd   z*Location._update_query.<locals>.<listcomp>r   Tc                 S  s   i | ]\}}|d ur||qS rU   r8   r_   r8   r8   r9   rc      rd   z*Location._update_query.<locals>.<dictcomp>F)r>   r=   rn   r3   r1   r   	serializenewrW   r+   strjsondumpsupdate_queryrf   )rA   r6   rm   Z
serializedmatchesownervalr8   rp   r9   _update_query   s&    

 zLocation._update_queryzDict[str, Any])rG   c                 C  s
   t | jS rU   )r   r'   )rA   r8   r8   r9   re      s    zLocation.query_paramszMapping[str, Any])kwargsrG   c                 K  s$   | j }|| dt| | _d S )Nr$   )re   r2   urlparse	urlencoder'   )rA   r}   r6   r8   r8   r9   rx      s    
zLocation.update_queryzparam.Parameterizedz$Optional[List[str] | Dict[str, str]]z*Optional[Callable[[Dict[str, Any]], None]])parameterizedrh   ri   rG   c           	   	   C  s   |pdd |j D }t|ts,tt||}|j | jt|}| j||||f | 	  i }|
 D ]`\}}t||}|du rqhz|j | |}W n ty   Y n0 t|tst|}|||< qh| j|d dS )a"  
        Syncs the parameters of a Parameterized object with the query
        parameters in the URL. If no parameters are supplied all
        parameters except the name are synced.

        Arguments
        ---------
        parameterized (param.Parameterized):
          The Parameterized object to sync query parameters with
        parameters (list or dict):
          A list or dictionary specifying parameters to sync.
          If a dictionary is supplied it should define a mapping from
          the Parameterized's parameters to the names of the query
          parameters.
        on_error: (callable):
          Callback when syncing Parameterized with URL parameters
          fails. The callback is passed a dictionary of parameter
          values, which could not be applied.
        c                 S  s   g | ]}|d kr|qS )r   r8   )r`   rY   r8   r8   r9   rr      rd   z!Location.sync.<locals>.<listcomp>Nrl   )r1   r+   dictzipr?   r|   listr=   appendr@   rf   rg   rs   rW   ru   rv   rw   )	rA   r   rh   ri   watcherr6   rY   r   rb   r8   r8   r9   sync   s&    




zLocation.synczOptional[List[str]])r   rh   rG   c                   s&   fdd| j D }|s0t }td| dg g  }| j D ]\}}}}	 |urh|||||	f q@ j| du rt|  q@fdd|	 D  fdd|	 D }
 j
|jt|
}|||
||	f q@|| _ fd	d| j	 D }|rd
t| nd| _dS )a  
        Unsyncs the parameters of the Parameterized with the query
        params in the URL. If no parameters are supplied all
        parameters except the name are unsynced.

        Arguments
        ---------
        parameterized (param.Parameterized):
          The Parameterized object to unsync query parameters with
        parameters (list):
          A list of parameters to unsync.
        c                   s   g | ]}|d   u r|qS )r   r8   )r`   s)r   r8   r9   rr     rd   z#Location.unsync.<locals>.<listcomp>zCannot unsync z5 object since it was never synced in the first place.Nc                   s   g | ]\}}| v r|qS r8   r8   r`   rY   qrh   r8   r9   rr     rd   c                   s   i | ]\}}| vr||qS r8   r8   r   r   r8   r9   rc     s   z#Location.unsync.<locals>.<dictcomp>c                   s   i | ]\}}| vr||qS r8   r8   r_   )unsyncedr8   r9   rc      rd   r$   r   )r=   type
ValueErrorr   r1   Zunwatchextendr   valuesrf   r?   fnre   r~   r   r'   )rA   r   rh   ry   ptypeZsyncedrY   r5   r   ri   
new_paramsZnew_watcherr6   r8   )r   rh   r   r9   rV      s&    
zLocation.unsync)NNN)NNT)N)N)NN)N) __name__
__module____qualname____doc__r1   Stringr)   r"   r&   r!   r#   r'   r(   Booleanreloadr   __annotations__classmethodr:   r<   rM   rR   rX   r]   r@   r|   propertyre   rx   r   rV   __classcell__r8   r8   rB   r9   r      s>   

(      +r   )&r   
__future__r   rv   urllib.parseparser~   typingr   r   r   r   r   r   r	   r
   r1   Zmodels.locationr   rJ   Zreactiver   utilr   r   cacher   r\   r   r   Zbokeh.documentr   Zbokeh.modelr   r*   r   Zpyviz_commsr   r8   r8   r8   r9   <module>   s"   (