B
    @\L                 @   sR  d Z ddlmZmZmZmZ ddlZeeZ	ddl
Z
ddlZddlmZmZm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 ddlmZ dZdZdeef Z G dd de!Z"G dd de"Z#G dd de"Z$G dd de#e$Z%G dd de!Z&e
'dZ(dd Z)d&ddZ*d'd d!Z+e%d"d#Z,e%d$d#Z-d%Z.dS )(aZ   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.

Also provides some pre-configured Resources objects.

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

    )absolute_importdivisionprint_functionunicode_literalsN)basenamejoinrelpath)string_types   )__version__)JS_RESOURCESCSS_RESOURCES)Model)settings)
bokehjsdir)generate_session_idZ	localhosti  zhttp://%s:%d/c               @   s|   e Zd ZdZeZdddZedd	 Zej	d
d	 Zedd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )BaseResources.inlineNTinfoc	             C   s  || _ t| dr| j| _t| dr*| j| _t|| _~t	|| _
~t|| _~t|| _~t|| _~|| _~|r|dstd |d }|| _| jdkrtd| j | j
r| jdstd| jr| jd	std
|r| jdstd| jd| _| jr,| jd d | _g | _| jd	krX|  }	| j|	d  n$| jdkr||  }
| j|
d  d S )N_js_components_css_components/z(root_url should end with a /, adding one)r   cdnserverz
server-devrelativezrelative-devabsolutezabsolute-devzxwrong value for 'mode' parameter, expected 'inline', 'cdn', 'server(-dev)', 'relative(-dev)' or 'absolute(-dev)', got %rr   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'r   zBsetting 'root_url' makes sense only when 'mode' is set to 'server'z-devmessages)_componentshasattrr   js_componentsr   css_componentsr   Z	resourcesmodeZrootdirroot_dirversionminified	log_levelpath_versionerendswithlogZwarning	_root_url
ValueError
startswithdevr   	_cdn_urlsextend_server_urls)selfr#   r%   r$   r&   r'   root_urlr(   
componentsr   r    r5   .lib/python3.7/site-packages/bokeh/resources.py__init__F   sP    

      


zBaseResources.__init__c             C   s   | j S )N)
_log_level)r2   r5   r5   r6   r'   x   s    zBaseResources.log_levelc             C   s>   ddddddg}|d ks4||ks4t d|t||| _d S )NZtracedebugr   warnerrorZfatalz,Unknown log level '{}', valid levels are: {})r,   formatstrr8   )r2   levelZvalid_levelsr5   r5   r6   r'   |   s    c             C   s   | j d k	r| j S | jS d S )N)r+   _default_root_url)r2   r5   r5   r6   r3      s    
zBaseResources.root_urlc                s4   |dkr j n j} jd k	r0 fdd|D }|S )Njsc                s   g | ]}| j kr|qS r5   )r   ).0c)r2   r5   r6   
<listcomp>   s    z,BaseResources.components.<locals>.<listcomp>)r!   r"   r   )r2   kindr4   r5   )r2   r6   r4      s    
zBaseResources.componentsc                sP   t | j | js| jrdndfdd| D } fdd|D }|S )Nz.min c                s   g | ]}d | f qS )z%s%s.%sr5   )rA   	component)rD   r&   r5   r6   rC      s    z-BaseResources._file_paths.<locals>.<listcomp>c                s   g | ]}t  |qS r5   )r   )rA   file)bokehjs_dirrD   r5   r6   rC      s    )r   r.   r&   r4   )r2   rD   filespathsr5   )rH   rD   r&   r6   _file_paths   s
    
zBaseResources._file_pathsc             C   s   g }xvt tj dd dD ]\\}}t||d}t|trN||krx|| qt|trx|D ]}||kr^|| q^W qW |S )zI Collect external resources set on resource_attr attribute of all models.c             S   s   | d S )Nr   r5   )argr5   r5   r6   <lambda>   s    z;BaseResources._collect_external_resources.<locals>.<lambda>)keyN)	sortedr   Zmodel_class_reverse_mapitemsgetattr
isinstancer	   appendlist)r2   Zresource_attrexternal_resources_clsZexternaler5   r5   r6   _collect_external_resources   s     


z)BaseResources._collect_external_resourcesc             C   s   t | j| jS )N)_get_cdn_urlsr%   r&   )r2   r5   r5   r6   r/      s    zBaseResources._cdn_urlsc             C   s   t | j| jrdn| j| jS )NF)_get_server_urlsr3   r.   r&   r(   )r2   r5   r5   r6   r1      s    zBaseResources._server_urlsc                s    |}g g  }}jdkr2fdd|D }njdkr\jpFj  fdd|D }njjdkrpt|}nVjdkr }t|d ||}n*jd	krƈ }t|d ||}||fS )
Nr   c                s   g | ]}  |qS r5   )_inline)rA   path)r2   r5   r6   rC      s    z*BaseResources._resolve.<locals>.<listcomp>r   c                s   g | ]}t | qS r5   )r   )rA   r]   )r$   r5   r6   rC      s    r   r   urlsr   )rK   r#   r$   _default_root_dirrT   r/   r4   r1   )r2   rD   rJ   rI   rawr   r   r5   )r$   r2   r6   _resolve   s     







zBaseResources._resolvec          	   C   sJ   dt | }t|d}| d}W d Q R X dt | }d|||f S )Nz/* BEGIN %s */rbzutf-8z/* END %s */z%s
%s
%s)r   openreaddecode)r2   r]   ZbeginfZmiddleendr5   r5   r6   r\      s
    zBaseResources._inline)r   NNTr   NNN)__name__
__module____qualname__r_   DEFAULT_SERVER_HTTP_URLr?   r7   propertyr'   setterr3   r4   rK   rY   r/   r1   ra   r\   r5   r5   r5   r6   r   B   s     
0		r   c               @   s<   e Zd ZdZddddgZedd Zedd	 Zd
d ZdS )JSResourcesae   The Resources class encapsulates information relating to loading or embedding Bokeh Javascript.

    Args:
        mode (str) : How should Bokeh JS be included in output

            See below for descriptions of available modes

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

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

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

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

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

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

            If ``None``, absoute URLs based on the default server configuration will
            be generated.

            ``root_url`` can also be the empty string, in which case relative URLs,
            e.g., "static/css/bokeh.min.js", are generated.

            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 ``http://cdn.pydata.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:
        css_raw : any raw CSS that needs to be places inside ``<style>`` 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.

    bokehzbokeh-widgetszbokeh-tableszbokeh-glc             C   s    |  d\}}| d}|| S )Nr@   Z__javascript__)ra   rY   )r2   rI   rV   rU   r5   r5   r6   js_files  s    
zJSResources.js_filesc             C   s<   |  d\}}| jd k	r(|d| j  | jr8|d |S )Nr@   zBokeh.set_log_level("%s");zBokeh.settings.dev = true)ra   r'   rS   r.   )r2   rV   r`   r5   r5   r6   js_raw  s    

zJSResources.js_rawc             C   s   t j| j| jdS )N)rq   rp   )r   renderrq   rp   )r2   r5   r5   r6   	render_js  s    zJSResources.render_jsN)	rh   ri   rj   __doc__r   rl   rp   rq   rs   r5   r5   r5   r6   rn      s
   1rn   c               @   sF   e Zd ZdZdddgZedd Zedd Zed	d
 Zdd Z	dS )CSSResourcesaI   The CSSResources class encapsulates information relating to loading or embedding Bokeh client-side CSS.

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

            See below for descriptions of available modes

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

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

        root_dir (str, optional) : root directory for loading BokehJS resources

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

        minified (bool, optional) : whether 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 BokehJS code and CSS inline
    * ``'cdn'`` configure to load Bokeh CSS from ``http://cdn.pydata.org``
    * ``'server'`` configure to load from a Bokeh Server
    * ``'server-dev'`` same as ``server`` but supports non-minified CSS
    * ``'relative'`` configure to load relative to the given directory
    * ``'relative-dev'`` same as ``relative`` but supports non-minified CSS
    * ``'absolute'`` configure to load from the installed Bokeh library static directory
    * ``'absolute-dev'`` same as ``absolute`` but supports non-minified CSS

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

    Attributes:
        css_raw : any raw CSS that needs to be places inside ``<style>`` 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.

    ro   zbokeh-widgetszbokeh-tablesc             C   s    |  d\}}| d}|| S )NcssZ__css__)ra   rY   )r2   rI   rV   rU   r5   r5   r6   	css_filesL  s    
zCSSResources.css_filesc             C   s   |  d\}}|S )Nrv   )ra   )r2   rV   r`   r5   r5   r6   css_rawR  s    zCSSResources.css_rawc             C   s   dd | j D S )Nc             S   s   g | ]}t |qS r5   )jsondumps)rA   rv   r5   r5   r6   rC   Y  s    z,CSSResources.css_raw_str.<locals>.<listcomp>)rx   )r2   r5   r5   r6   css_raw_strW  s    zCSSResources.css_raw_strc             C   s   t j| j| jdS )N)rx   rw   )r   rr   rx   rw   )r2   r5   r5   r6   
render_css]  s    zCSSResources.render_cssN)
rh   ri   rj   rt   r   rl   rw   rx   r{   r|   r5   r5   r5   r6   ru     s   *
ru   c               @   s   e Zd ZdZdd 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 ``http://cdn.pydata.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.

    c             C   s   d|   |  f S )Nz%s
%s)r|   rs   )r2   r5   r5   r6   rr     s    zResources.renderN)rh   ri   rj   rt   rr   r5   r5   r5   r6   r}   `  s   .r}   c               @   s<   e Zd ZdZdd Zedd Zedd Zedd	 Zd
S )_SessionCoordinateszN Internal class used to parse kwargs for server URL, app_path, and session_id.c             K   sb   | dt| _| jd kr td| jdkr0t| _| jdrDtd| jd| _| d| _d S )Nurlzurl cannot be NonedefaultZwszIurl should be the http or https URL for the server, not the websocket URLr   
session_id)getrk   _urlr,   r-   rstrip_session_id)r2   kwargsr5   r5   r6   r7     s    

z_SessionCoordinates.__init__c             C   s   | j S )N)r   )r2   r5   r5   r6   r     s    z_SessionCoordinates.urlc             C   s   | j dkrt | _ | j S )z- Session ID derived from the kwargs provided.N)r   r   )r2   r5   r5   r6   r     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   )r2   r5   r5   r6   session_id_allowing_none  s    z,_SessionCoordinates.session_id_allowing_noneN)	rh   ri   rj   rt   r7   rl   r   r   r   r5   r5   r5   r6   r~     s
   r~   z^(\d)+\.(\d)+\.(\d)+(dev|rc)c               C   s   dS )Nzhttps://cdn.pydata.orgr5   r5   r5   r5   r6   _cdn_base_url  s    r   Tc                s   d kr(t  rt  ntdd |r0dnd t d}d}trP|n|drht	d  fd	d
fddg d}t
tddkr|d ddtf d |S )N-r   z.minrE   z	bokeh/devzbokeh/release)r.   ZrczAGetting CDN URL for local dev version will not produce usable URLc                s   d|  |f S )Nz%s/%s/%s-%s%s.%sr5   )comprD   )_minbase_url	containerr%   r5   r6   mk_url  s    z_get_cdn_urls.<locals>.mk_urlc                s    fdd| D S )Nc                s   g | ]}| qS r5   r5   )rA   rF   )rD   r   r5   r6   rC     s    z3_get_cdn_urls.<locals>.<lambda>.<locals>.<listcomp>r5   )r4   rD   )r   )rD   r6   rM     s    z_get_cdn_urls.<locals>.<lambda>)r^   r   r
   r   r:   z|Requesting CDN BokehJS version '%s' from Bokeh development version '%s'. This configuration is unsupported and may not work!)typetext)r   Zdocs_cdnr   splitr   _DEV_PATmatchr)   r*   r9   lenrS   )r%   r&   Zdev_containerZrel_containerresultr5   )r   r   r   r   r%   r6   rZ     s(    



rZ   c                s.   |rdnd  fddfddg dS )Nz.minrE   c                s,   d||  |f }d k	r |}d|f S )Nz
%s/%s%s.%sz%sstatic/%sr5   )r   rD   r]   )r   r(   r3   r5   r6   r     s    z _get_server_urls.<locals>.mk_urlc                s    fdd| D S )Nc                s   g | ]}| qS r5   r5   )rA   rF   )rD   r   r5   r6   rC     s    z6_get_server_urls.<locals>.<lambda>.<locals>.<listcomp>r5   )r4   rD   )r   )rD   r6   rM     s    z"_get_server_urls.<locals>.<lambda>)r^   r   r5   )r3   r&   r(   r5   )r   r   r(   r3   r6   r[     s    
r[   r   )r#   r   )CDNINLINEr}   rn   ru   )NT)TN)/rt   Z
__future__r   r   r   r   ZloggingZ	getLoggerrh   r*   rery   os.pathr   r   r   Zsixr	   rE   r   Zcore.templatesr   r   Zmodelr   r   Z
util.pathsr   Zutil.session_idr   ZDEFAULT_SERVER_HOSTZDEFAULT_SERVER_PORTrk   objectr   rn   ru   r}   r~   compiler   r   rZ   r[   r   r   __all__r5   r5   r5   r6   <module>   s:   
 ND9)

&


