a
    =DfL[                     @  s  U d Z ddlmZ ddlZeeZddlZddlZddl	Z	ddl
m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mZ ddlmZ dd	lmZmZ dd
lmZmZ ddl m!Z! ddl"m#Z#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rddl-m.Z. e"/ Z0e"1 Z2dRdddddddZ3e3 Z4ed Z5de6d< ed Z7de6d< ee5e7f Z8de6d< ed  Z9G d!d" d"eZ:e;e<e<f Z=de6d#< i Z>d$e6d%< d&d'd(d)Z?dd#d*d+d,Z@d-d'd.d/ZAee<ge<f ZBed0 ZCe%G d1d2 d2ZDG d3d4 d4eZEG d5d6 d6eZFe%G d7d8 d8ZGed9 ZHG d:d; d;ZIG d<d= d=ZJe	Kd>ZLdd'd?d@ZMdSddd8dBdCdDZNe4dAdfdddEd8dFdGdHZOdIddJdKdLZPeeIe8f ZQde6dM< eIdNdOZReIdPdOZSdQZTdS )TaK   The resources module provides the Resources class for easily configuring
how BokehJS code and CSS resources should be located, loaded, and embedded in
Bokeh documents.

Additionally, functions for retrieving `Subresource Integrity`_ hashes for
Bokeh JavaScript files are provided here.

Some pre-configured Resources objects are made available as attributes.

Attributes:
    CDN : load minified BokehJS from CDN
    INLINE : provide minified BokehJS from library static directory

.. _Subresource Integrity: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity

    )annotationsN)relpath)Path)	TYPE_CHECKINGCallableClassVarLiteralProtocol	TypedDictUnioncastget_args   )__version__)CSS_RESOURCESJS_RESOURCES)IDPathLike)Model)LogLevelsettings)	dataclassfield)ROOT_DIR)generate_session_id)is_full_release)	TypeAliasF
str | Nonez
int | Noneboolstr)hostportsslreturnc                 C  s*   |rdnd}| d| pt  d|p"t dS )Nhttpshttpz://:/)DEFAULT_SERVER_HOSTDEFAULT_SERVER_PORT)r    r!   r"   protocol r+   \/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/bokeh/resources.py
server_urlL   s    r-   )inlinecdnserverrelativeabsoluter   BaseMode)z
server-devzrelative-devzabsolute-devDevModeResourcesModebokehbokeh-glbokeh-widgetsbokeh-tablesbokeh-mathjaxz	bokeh-apic                   @  s   e Zd ZU ded< ded< dS )ComponentDefslist[Component]jscssN__name__
__module____qualname____annotations__r+   r+   r+   r,   r<   Y   s   
r<   Hasheszdict[str, Hashes]_ALL_SRI_HASHESztuple[str, ...]r#   c                  C  s    t d d} tdd | D S )zL Report all versions that have SRI hashes.

    Returns:
        tuple

    _sriz*.jsonc                 s  s   | ]}|j V  qd S N)stem.0filer+   r+   r,   	<genexpr>s       z'get_all_sri_versions.<locals>.<genexpr>)r   globset)filesr+   r+   r,   get_all_sri_versionsk   s    rS   )versionr#   c              
   C  s   | t vrzHttd |  d }t|t | < W d   n1 sD0    Y  W n4 ty } ztd|  |W Y d}~n
d}~0 0 t |  S )a   Report SRI script hashes for a specific version of BokehJS.

    Bokeh provides `Subresource Integrity`_ hashes for all JavaScript files that
    are published to CDN for full releases. This function returns a dictionary
    that maps JavaScript filenames to their hashes, for a single version of
    Bokeh.

    Args:
        version (str) :
            The Bokeh version to return SRI hashes for. Hashes are only provided
            for full releases, e.g "1.4.0", and not for "dev" builds or release
            candidates.

    Returns:
        dict

    Raises:
        ValueError: if the specified version does not exist

    Example:

        The returned dict for a single version will map filenames for that
        version to their SRI hashes:

        .. code-block:: python

            {
                'bokeh-1.4.0.js': 'vn/jmieHiN+ST+GOXzRU9AFfxsBp8gaJ/wvrzTQGpIKMsdIcyn6U1TYtvzjYztkN',
                'bokeh-1.4.0.min.js': 'mdMpUZqu5U0cV1pLU9Ap/3jthtPth7yWSJTu1ayRgk95qqjLewIkjntQDQDQA5cZ',
                'bokeh-api-1.4.0.js': 'Y3kNQHt7YjwAfKNIzkiQukIOeEGKzUU3mbSrraUl1KVfrlwQ3ZAMI1Xrw5o3Yg5V',
                'bokeh-api-1.4.0.min.js': '4oAJrx+zOFjxu9XLFp84gefY8oIEr75nyVh2/SLnyzzg9wR+mXXEi+xyy/HzfBLM',
                'bokeh-tables-1.4.0.js': 'I2iTMWMyfU/rzKXWJ2RHNGYfsXnyKQ3YjqQV2RvoJUJCyaGBrp0rZcWiTAwTc9t6',
                'bokeh-tables-1.4.0.min.js': 'pj14Cq5ZSxsyqBh+pnL2wlBS3UX25Yz1gVxqWkFMCExcnkN3fl4mbOF8ZUKyh7yl',
                'bokeh-widgets-1.4.0.js': 'scpWAebHEUz99AtveN4uJmVTHOKDmKWnzyYKdIhpXjrlvOwhIwEWUrvbIHqA0ke5',
                'bokeh-widgets-1.4.0.min.js': 'xR3dSxvH5hoa9txuPVrD63jB1LpXhzFoo0ho62qWRSYZVdyZHGOchrJX57RwZz8l'
            }

    .. _Subresource Integrity: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity

    rH   z.jsonNzMissing SRI hash for version )rF   openr   jsonload	Exception
ValueError)rT   fer+   r+   r,   get_sri_hashes_for_versionv   s    )0&r\   Nonec                  C  s   t  stdtt d d} tt}t|t| k rDt	dt|t| kr\t	dg }| D ]L}t
|jdd\}}| dt d| }t|}|| |krd|| qd|rt	d	|d
S )a6   Verify the SRI hashes in a full release package.

    This function compares the computed SRI hashes for the BokehJS files in a
    full release package to the values in the SRI manifest file. Returns None
    if all hashes match, otherwise an exception will be raised.

    .. note::
        This function can only be called on full release (e.g "1.2.3") packages.

    Returns:
        None

    Raises:
        ValueError
            If called outside a full release package
        RuntimeError
            If there are missing, extra, or mismatched files

    z7verify_sri_hashes() can only be used with full releasesr>   z	bokeh*.jsz5There are unexpected 'bokeh*.js' files in the packagez2There are 'bokeh*.js' files missing in the package.r   -z$SRI Hash mismatches in the package: N)r   rY   listr   bokehjs_pathrP   r\   r   lenRuntimeErrorr   namesplit_compute_single_hashappend)pathshashesbadpathrd   suffixfilenameZsri_hashr+   r+   r,   verify_sri_hashes   s"    rn   )r?   r>   c                   @  s   e Zd ZU ded< ded< dS )RuntimeMessagezLiteral['warn']typer   textNr@   r+   r+   r+   r,   ro      s   
ro   c                   @  s"   e Zd ZeddddddZdS )UrlsFn	list[str]Kind
componentskindr#   c                 C  s   d S rI   r+   rv   rw   r+   r+   r,   __call__   s    zUrlsFn.__call__NrA   rB   rC   staticmethodry   r+   r+   r+   r,   rr      s   rr   c                   @  s"   e Zd ZeddddddZdS )HashesFnrs   rt   rE   ru   c                 C  s   d S rI   r+   rx   r+   r+   r,   ry      s    zHashesFn.__call__Nrz   r+   r+   r+   r,   r|      s   r|   c                   @  s4   e Zd ZU ded< eedZded< dZded< dS )	Urlsrr   urls)default_factorylist[RuntimeMessage]messagesNzHashesFn | Noneri   )rA   rB   rC   rD   r   r`   r   ri   r+   r+   r+   r,   r}      s   
r}   )__css____javascript__c                   @  s  e Zd ZU dZeejZeZ	de
d< de
d< de
d< de
d	< g d
g dZde
d< g dZde
d< d[dddddddddd	ddddddddddd
ddZdddd ddd Zd!d"d#d$ZeZed\d%d d&d'd(Zedd"d)d*Zejdd+d,d-d*Zed!d"d.d/Zd0dd1d2d3Zd0d4d1d5d6Zd7d8d9d:d;Zd<d"d=d>Zd<d"d?d@Zd0dAd1dBdCZedDd!dEdFdGZed8d"dHdIZed8d"dJdKZ edLd"dMdNZ!d!d"dOdPZ"ed8d"dQdRZ#ed8d"dSdTZ$ed8d"dUdVZ%d!d"dWdXZ&d!d"dYdZZ'dS )]	Resourcesa%  
    The Resources class encapsulates information relating to loading or
    embedding Bokeh Javascript and CSS.

    Args:
        mode (str) : how should Bokeh JS and CSS be included in output

            See below for descriptions of available modes

        version (str, optional) : what version of Bokeh JS and CSS to load

            Only valid with the ``'cdn'`` mode

        root_dir (str, optional) : root directory for loading Bokeh JS and CSS assets

            Only valid with ``'relative'`` and ``'relative-dev'`` modes

        minified (bool, optional) : whether JavaScript and CSS should be minified or not (default: True)

        root_url (str, optional) : URL and port of Bokeh Server to load resources from

            Only valid with ``'server'`` and ``'server-dev'`` modes

    The following **mode** values are available for configuring a Resource object:

    * ``'inline'`` configure to provide entire Bokeh JS and CSS inline
    * ``'cdn'`` configure to load Bokeh JS and CSS from ``https://cdn.bokeh.org``
    * ``'server'`` configure to load from a Bokeh Server
    * ``'server-dev'`` same as ``server`` but supports non-minified assets
    * ``'relative'`` configure to load relative to the given directory
    * ``'relative-dev'`` same as ``relative`` but supports non-minified assets
    * ``'absolute'`` configure to load from the installed Bokeh library static directory
    * ``'absolute-dev'`` same as ``absolute`` but supports non-minified assets

    Once configured, a Resource object exposes the following public attributes:

    Attributes:
        js_raw : any raw JS that needs to be placed inside ``<script>`` tags
        css_raw : any raw CSS that needs to be places inside ``<style>`` tags
        js_files : URLs of any JS files that need to be loaded by ``<script>`` tags
        css_files : URLs of any CSS files that need to be loaded by ``<link>`` tags
        messages : any informational messages concerning this configuration

    These attributes are often useful as template parameters when embedding
    Bokeh plots.

    r3   moder   r   r   
_log_levelr=   rv   r6   )r>   r?   zClassVar[ComponentDefs]_component_defs)r7   r8   r9   r:   r;   zClassVar[list[Component]]_default_componentsN)	rT   root_dirdevminified	log_levelroot_urlpath_versionerrv   base_dirzResourcesMode | Noner   zPathLike | Nonezbool | NonezLogLevel | NonePathVersioner | Nonezlist[Component] | None
r   rT   r   r   r   r   r   r   rv   r   c       	         C  s  |	d ur|	nt | j| _t|}|d}|d ur8|ntjp@|| _tt|rX|d d n|| _	| j	t
tvr|td| |r| j	dstd|r| j	dstd|r| j	dstd	t|| _~t|| _~t|d ur|n| j | _~t|| _~|| _~|r.|d
s.|d
 }|| _g | _| j	dkr^|  }| j|j n"| j	dkr|  }| j|j |
d urt|
nt | _d S )Nz-devzvwrong value for 'mode' parameter, expected 'inline', 'cdn', 'server(-dev)', 'relative(-dev)' or 'absolute(-dev)', got r1   zDsetting 'root_dir' makes sense only when 'mode' is set to 'relative'r/   z>setting 'version' makes sense only when 'mode' is set to 'cdn'r0   zBsetting 'root_url' makes sense only when 'mode' is set to 'server'r'   )r`   r   rv   r   	resourcesendswithr   r   r3   r   r   rY   
startswithrootdirr   Zcdn_versionrT   r   r   r   	_root_urlr   	_cdn_urlsextend_server_urlsr   ra   r   )selfr   rT   r   r   r   r   r   r   rv   r   Zmode_devr/   r0   r+   r+   r,   __init__/  sL    

zResources.__init__rv   )rv   r#   c                C  s@   t | j| j| j| j| j| j| j| j|dur.|nt	| j
| jd
S )zK Make a clone of a resources instance allowing to override its components. Nr   )r   r   rT   r   r   r   r   r   r   r`   rv   r   )r   rv   r+   r+   r,   clonen  s    zResources.cloner   rG   c                 C  sN   d| j g}| jr|d | j| jkr<|d| j dd| dS )Nzmode=zdev=Truezcomponents=z
Resources(z, ))r   r   rg   rv   r   join)r   argsr+   r+   r,   __repr__}  s    
zResources.__repr__zResourcesLike | None)r   r#   c                 C  s"   t |tr|S tt|dS d S )Nr   )
isinstancer   r   r   )clsr   r+   r+   r,   build  s    
zResources.buildc                 C  s   | j S rI   )r   r   r+   r+   r,   r     s    zResources.log_levelr]   )levelr#   c                 C  s6   t t}|d u s,||v s,td| d| || _d S )NzUnknown log level 'z', valid levels are: )r   r   rY   r   )r   r   Zvalid_levelsr+   r+   r,   r     s    c                 C  s   | j d ur| j S | jS d S rI   )r   _default_root_urlr   r+   r+   r,   r     s    
zResources.root_urlrt   )rw   r#   c                   s    fddj D S )Nc                   s   g | ]}|j   v r|qS r+   )r   )rL   comprw   r   r+   r,   
<listcomp>  rO   z,Resources.components_for.<locals>.<listcomp>r   )r   rw   r+   r   r,   components_for  s    zResources.components_forz
list[Path]c                   s@   j r
dnd fdd D } fdd|D }|S )N.min c                   s   g | ]}|  d   qS )r^   r+   rL   	component)rw   r   r+   r,   r     rO   z)Resources._file_paths.<locals>.<listcomp>c                   s   g | ]}j   | qS r+   )r   rK   r   r+   r,   r     rO   )r   r   )r   rw   rR   rh   r+   )rw   r   r   r,   _file_paths  s    zResources._file_pathsResourceAttrrs   )resource_attrr#   c                 C  sx   g }t tj dd dD ]X\}}t||d}t|trL||vrr|| qt|tr|D ]}||vrZ|| qZq|S )zI Collect external resources set on resource_attr attribute of all models.c                 S  s   | d S )Nr   r+   )argr+   r+   r,   <lambda>  rO   z7Resources._collect_external_resources.<locals>.<lambda>)keyN)	sortedr   Zmodel_class_reverse_mapitemsgetattrr   r   rg   r`   )r   r   external_resources_r   Zexternalr[   r+   r+   r,   _collect_external_resources  s    

z%Resources._collect_external_resourcesr}   c                 C  s   t | j| jS rI   )_get_cdn_urlsrT   r   r   r+   r+   r,   r     s    zResources._cdn_urlsc                 C  s   t | j| j| jS rI   )_get_server_urlsr   r   r   r   r+   r+   r,   r     s    zResources._server_urlsz#tuple[list[str], list[str], Hashes]c                   s    |}g g  }}i }jdkr6fdd|D }njdkr`jpJj  fdd|D }njdkrzttt|}njjdkr }t|	||}|j
r|
	||}n(jdkr }t|	||}|||fS )	Nr.   c                   s   g | ]}  |qS r+   )_inlinerL   rk   r   r+   r,   r     rO   z&Resources._resolve.<locals>.<listcomp>r1   c                   s   g | ]}t t| qS r+   )r   r   r   )r   r+   r,   r     rO   r2   r/   r0   )r   r   r   _default_root_dirr`   mapr   r   r~   r   ri   r   )r   rw   rh   rR   rawri   r/   r0   r+   )r   r   r,   _resolve  s&    






zResources._resolver   rk   r#   c                 C  sj   | j }d| d}t| d}| d}W d    n1 s@0    Y  d| d}| d| d| S )Nz	/* BEGIN z */rbutf-8z/* END 
)rd   rU   readdecode)rk   rm   beginrZ   Zmiddleendr+   r+   r,   r     s    ,zResources._inlinec                 C  s"   |  d\}}}| d}|| S )Nr>   r   r   r   r   rR   r   r   r+   r+   r,   js_files  s    
zResources.js_filesc                 C  sB   |  d\}}}| jd ur.|d| j d | jr>|d |S )Nr>   zBokeh.set_log_level("z");zBokeh.settings.dev = true)r   r   rg   r   r   r   r   r+   r+   r,   js_raw  s    

zResources.js_rawrE   c                 C  s   |  d\}}}|S )Nr>   r   )r   r   ri   r+   r+   r,   ri     s    zResources.hashesc                 C  s   t j| j| j| jdS )N)r   r   ri   )r   renderr   r   ri   r   r+   r+   r,   	render_js  s    zResources.render_jsc                 C  s"   |  d\}}}| d}|| S )Nr?   r   r   r   r+   r+   r,   	css_files   s    
zResources.css_filesc                 C  s   |  d\}}}|S )Nr?   r   r   r+   r+   r,   css_raw  s    zResources.css_rawc                 C  s   dd | j D S )Nc                 S  s   g | ]}t |qS r+   )rV   dumps)rL   r?   r+   r+   r,   r     rO   z)Resources.css_raw_str.<locals>.<listcomp>)r   r   r+   r+   r,   css_raw_str  s    zResources.css_raw_strc                 C  s   t j| j| jdS )N)r   r   )r   r   r   r   r   r+   r+   r,   
render_css  s    zResources.render_cssc                 C  s    |   |   }}| d| S )Nr   )r   r   )r   r?   r>   r+   r+   r,   r     s    zResources.render)N)N)(rA   rB   rC   __doc__r   oscurdirr   DEFAULT_SERVER_HTTP_URLr   rD   r   r   r   r   r   __str__classmethodr   propertyr   setterr   r   r   r   r   r   r   r{   r   r   r   ri   r   r   r   r   r   r   r+   r+   r+   r,   r      sr   
0
 $?r   c                   @  sr   e Zd ZU dZded< ded< edddddd	d
dZeddddZeddddZ	eddddZ
dS )SessionCoordinateszN Internal class used to parse kwargs for server URL, app_path, and session_id.r   _urlz	ID | None_session_idN)url
session_idr]   )r   r   r#   c                C  sB   || _ | j dkrt| _ | j dr*td| j d| _ || _d S )NdefaultwszIurl should be the http or https URL for the server, not the websocket URLr'   )r   r   r   rY   rstripr   )r   r   r   r+   r+   r,   r     s    
zSessionCoordinates.__init__rG   c                 C  s   | j S rI   )r   r   r+   r+   r,   r   ,  s    zSessionCoordinates.urlr   c                 C  s   | j du rt | _ | j S )z- Session ID derived from the kwargs provided.N)r   r   r   r+   r+   r,   r   0  s    
zSessionCoordinates.session_idc                 C  s   | j S )a   Session ID provided in kwargs, keeping it None if it hasn't been generated yet.

        The purpose of this is to preserve ``None`` as long as possible... in some cases
        we may never generate the session ID because we generate it on the server.
        )r   r   r+   r+   r,   session_id_allowing_none7  s    z+SessionCoordinates.session_id_allowing_none)rA   rB   rC   r   rD   r   r   r   r   r   r   r+   r+   r+   r,   r     s   
r   z^(\d)+\.(\d)+\.(\d)+(\.dev|rc)c                   C  s   dS )Nzhttps://cdn.bokeh.orgr+   r+   r+   r+   r,   _cdn_base_urlG  s    r   T)rT   r   r#   c              	     s   d u r&t  }|r|ntdd t  tr:dndddddfdd	dddd fd
dtfddd}ttddkr|j	
tdd dt dd trd usJ tfdd|_|S )N+r   z	bokeh/devzbokeh/releaser   rt   r   rw   r#   c                   s    |  d  rdnd d| S )Nr_   r   r   r^   r+   r   rw   )r   rT   r+   r,   mk_filenameT  s    z"_get_cdn_urls.<locals>.mk_filenamec                   s     d d| | S )Nr'   r+   r   )base_url	containerr   r+   r,   mk_urlW  s    z_get_cdn_urls.<locals>.mk_urlc                   s    fdd| D S )Nc                   s   g | ]}| qS r+   r+   r   rw   r   r+   r,   r   Z  rO   z3_get_cdn_urls.<locals>.<lambda>.<locals>.<listcomp>r+   rx   r   rw   r,   r   Z  rO   z_get_cdn_urls.<locals>.<lambda>r~   r   warnz Requesting CDN BokehJS version 'z"' from local development version 'z6'. This configuration is unsupported and may not work!)rp   rq   c                   s    fdd| D S )Nc                   s"   i | ]}| |  qS r+   r+   r   )rw   r   r   
sri_hashesr+   r,   
<dictcomp>h  s   z3_get_cdn_urls.<locals>.<lambda>.<locals>.<dictcomp>r+   rx   )r   r   r   r   r,   r   h  s   )r   docs_cdnr   re   r   _DEV_PATmatchr}   rb   r   rg   ro   r   r\   ri   )rT   r   r   resultr+   )r   r   r   r   r   r   rT   r,   r   K  s$    r   r   )r   r   r   r#   c                   s8   |rdnd dddd fddt fdd	d
S )Nr   r   r   rt   r   c                   s6   | d|    d| }d ur(|} d| S )Nr'   r^   zstatic/r+   )r   rw   rk   )	_minifiedr   r   r+   r,   r   v  s    z _get_server_urls.<locals>.mk_urlc                   s    fdd| D S )Nc                   s   g | ]}| qS r+   r+   r   r   r+   r,   r   |  rO   z6_get_server_urls.<locals>.<lambda>.<locals>.<listcomp>r+   rx   r   r   r,   r   |  rO   z"_get_server_urls.<locals>.<lambda>r   )r}   )r   r   r   r+   )r   r   r   r   r,   r   o  s    r   r   r   c           	      C  sj   | j dksJ ddlm}m} d|   }|||d}d }|||j|d}| \}}|d S )	Nz.jsr   )PIPEPopenzopenssl dgst -sha384 -binary )stdoutzopenssl base64 -A)stdinr  r   )	rl   
subprocessr   r  re   r  communicater   strip)	rk   r   r  digestp1Zb64p2outr   r+   r+   r,   rf     s    rf   ResourcesLiker/   r   r.   )CDNINLINEr   rS   r\   rn   )NNF)NT)Ur   
__future__r   logging	getLoggerrA   logrV   r   reos.pathr   pathlibr   typingr   r   r   r   r	   r
   r   r   r   r   r   Zcore.templatesr   r   Z
core.typesr   r   modelr   r   r   Zutil.dataclassesr   r   Z
util.pathsr   Z
util.tokenr   Zutil.versionr   Ztyping_extensionsr   Zdefault_server_hostr(   Zdefault_server_portr)   r-   r   r3   rD   r4   r5   Z	Componentr<   dictr   rE   rF   rS   r\   rn   ZPathVersionerrt   ro   rr   r|   r}   r   r   r   compiler   r   r   r   rf   r  r  r  __all__r+   r+   r+   r,   <module>   sv   
,2+  +.
%

