B
    @\lE                 @   s6  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ZddlmZmZmZmZmZ ddlZddlmZm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 dZG dd de Z!G dd de"Z#e$ Z%dZ&dd Z'dd Z(dOddZ)G dd de*Z+G dd de+Z,G dd de,Z-G dd de,Z.G d d! d!e,Z/G d"d# d#e,Z0G d$d% d%e+Z1d&Z2G d'd( d(e*Z3d)d* Z4d+d, Z5d-d. Z6d/d0 Z7i Z8d1d2 Z9d3Z:d4Z;d5Z<d6Z=d7Z>d8Z?d9d: Z@daAdaBd;d< ZCd=d> ZDd?d@ ZEdPdAdBZFdQdCdDZGdRdEdFZHdGdH ZIdIdJ ZJeJaKdKdL ZLdMdN ZMdS )SzM Provide functions and classes to help with various JS and CSS compilation.

    )absolute_importdivisionprint_functionunicode_literalsN)dirnamejoinabspathexistsisabs)PopenPIPE   )Model)settings   )snakify)AttrDictbundle_all_modelsbundle_modelscalc_cache_keyCoffeeScriptCompilationErrorCustomModelFromFileget_cache_hookImplementationInline
JavaScriptLessnodejs_compilenodejs_versionnpmjs_versionset_cache_hook
TypeScriptc               @   s   e Zd ZdZdd ZdS )r   zH Provide a dict subclass that supports access by named attributes.

    c             C   s   | | S )N )selfkeyr$   r$   2lib/python3.7/site-packages/bokeh/util/compiler.py__getattr__J   s    zAttrDict.__getattr__N)__name__
__module____qualname____doc__r(   r$   r$   r$   r'   r   F   s   r   c                   s(   e Zd ZdZ fddZdd Z  ZS )r   zG A ``RuntimeError`` subclass for reporting JS compilation errors.

    c                sN   t t|   |d| _|d| _|d| _|d| _|d| _d S )Nlinecolumnmessagetext	annotated)	superr   __init__getr-   r.   r/   r0   r1   )r%   error)	__class__r$   r'   r3   Q   s    zCompilationError.__init__c             C   s   | j S )N)r0   )r%   r$   r$   r'   __str__Y   s    zCompilationError.__str__)r)   r*   r+   r,   r3   r7   __classcell__r$   r$   )r6   r'   r   M   s   r   )   
   r   c               C   s   t tS )N)_version_run_nodejsr$   r$   r$   r'   r    _   s    r    c               C   s   t tS )N)r;   
_run_npmjsr$   r$   r$   r'   r!   b   s    r!   
javascriptc             C   s0   t tdd}t|gt| ||d}tt|S )Njszcompiler.js)codelangfile)r   bokehjs_dirr<   dictr   jsonloads)r@   rA   rB   Zcompilejs_scriptoutputr$   r$   r'   r   e   s    r   c               @   s   e Zd ZdZdZdS )r   zF Base class for representing Bokeh custom model implementations.

    N)r)   r*   r+   r,   rB   r$   r$   r$   r'   r   j   s   r   c               @   s   e Zd ZdZdddZdS )r   a:   Base class for representing Bokeh custom model implementations that may
    be given as inline code in some language.

    Args:
        code (str) :
            The source code for the implementation

        file (str, optional)
            A file path to a file containing the source text (default: None)

    Nc             C   s   || _ || _d S )N)r@   rB   )r%   r@   rB   r$   r$   r'   r3   |   s    zInline.__init__)N)r)   r*   r+   r,   r3   r$   r$   r$   r'   r   p   s   r   c               @   s   e Zd ZdZedd ZdS )r   a   An implementation for a Bokeh custom model in CoffeeScript.

    Example:

        .. code-block:: python

            class MyExt(Model):
                __implementation__ = CoffeeScript(""" <CoffeeScript code> """)

    Note that ``CoffeeScript`` is the default implementation language for
    custom model implementations. The following is equivalent to example above:

    .. code-block:: python

        class MyExt(Model):
            __implementation__ == """ <some coffeescript code> """

    c             C   s   dS )Ncoffeescriptr$   )r%   r$   r$   r'   rA      s    zCoffeeScript.langN)r)   r*   r+   r,   propertyrA   r$   r$   r$   r'   r      s   r   c               @   s   e Zd ZdZedd ZdS )r#   z An implementation for a Bokeh custom model in TypeScript

    Example:

        .. code-block:: python

            class MyExt(Model):
                __implementation__ = TypeScript(""" <TypeScript code> """)

    c             C   s   dS )N
typescriptr$   )r%   r$   r$   r'   rA      s    zTypeScript.langN)r)   r*   r+   r,   rI   rA   r$   r$   r$   r'   r#      s   
r#   c               @   s   e Zd ZdZedd ZdS )r   z An implementation for a Bokeh custom model in JavaScript

    Example:

        .. code-block:: python

            class MyExt(Model):
                __implementation__ = Javacript(""" <JavaScript code> """)

    c             C   s   dS )Nr>   r$   )r%   r$   r$   r'   rA      s    zJavaScript.langN)r)   r*   r+   r,   rI   rA   r$   r$   r$   r'   r      s   
r   c               @   s   e Zd ZdZedd ZdS )r   z3 An implementation of a Less CSS style sheet.

    c             C   s   dS )Nlessr$   )r%   r$   r$   r'   rA      s    z	Less.langN)r)   r*   r+   r,   rI   rA   r$   r$   r$   r'   r      s   r   c               @   s$   e Zd ZdZdd Zedd ZdS )r   z A custom model implementation read from a separate source file.

    Args:
        path (str) :
            The path to the file containing the extension source code

    c          	   C   s.   t j|dd}| | _W d Q R X || _d S )Nzutf-8)encoding)ioopenreadr@   rB   )r%   pathfr$   r$   r'   r3      s    zFromFile.__init__c             C   sD   | j drdS | j dr dS | j dr0dS | j dr@dS d S )	Nz.coffeerH   z.tsrJ   z.jsr>   )z.cssz.lessrK   )rB   endswith)r%   r$   r$   r'   rA      s    zFromFile.langN)r)   r*   r+   r,   r3   rI   rA   r$   r$   r$   r'   r      s   r   )z.coffeez.tsz.jsz.cssz.lessc               @   sl   e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd ZdS )r   z5 Represent a custom (user-defined) Bokeh model.

    c             C   s
   || _ d S )N)cls)r%   rS   r$   r$   r'   r3      s    zCustomModel.__init__c             C   s   | j jS )N)rS   r)   )r%   r$   r$   r'   name   s    zCustomModel.namec             C   s   | j jd | j }|ddS )N.z	__main__. )rS   r*   rT   replace)r%   rT   r$   r$   r'   	full_name   s    zCustomModel.full_namec             C   s*   t j| jj }t|dr"t|jS d S d S )N__file__)sysmodulesrS   r*   hasattrr   rY   )r%   moduler$   r$   r'   rB      s    

zCustomModel.filec             C   s:   t | jdd }|d k	r|S | jd k	r.t| jS t S d S )NZ__base_path__)getattrrS   rB   r   osgetcwd)r%   rP   r$   r$   r'   rP      s    

zCustomModel.pathc             C   s   | j j}t|tjrLd|krD|trDtt|r4|n
t	| j
|}nt|}t|tr~|jd kr~||j| jppdd | j }|S )N
z<string>:)rS   __implementation__
isinstancesixZstring_typesrR   extsr   r
   r   rP   r   r   rB   r6   r@   rT   )r%   implr$   r$   r'   implementation   s    zCustomModel.implementationc             C   s   t | jdi S )NZ__dependencies__)r^   rS   )r%   r$   r$   r'   dependencies  s    zCustomModel.dependenciesc             C   s   dt | j S )Nz	custom/%s)r   rX   )r%   r$   r$   r'   r]     s    zCustomModel.moduleN)r)   r*   r+   r,   r3   rI   rT   rX   rB   rP   rh   ri   r]   r$   r$   r$   r'   r      s   	r   c               C   s   t S )zpReturns the current cache hook used to look up the compiled
       code given the CustomModel and Implementation)_CACHING_IMPLEMENTATIONr$   r$   r$   r'   r     s    r   c             C   s   | a dS )zrSets a compiled model cache hook used to look up the compiled
       code given the CustomModel and ImplementationN)rj   )hookr$   r$   r'   r"     s    r"   c                s  t |   dkrdS g }g dd dddddg}ttfd	d
|D g t }i fdd fddxN  D ]B}||j }|j|j}||j	|j
f |j
|j|f qW t|dd d}tdd dxftD ]Z\}\}}	}
x>|
 D ]2\}}|	d| d| }	|	d| d| }	qW ||	f|< qW d}|dd |D }|dd D ttt|d }tt|d S )zCreate a bundle of `models`. Nc          	   S   s4   t jttd| d dd}t| S Q R X d S )Nr?   z.jsonzutf-8)rL   )rM   rN   r   rC   rE   rF   rO   )rT   rQ   r$   r$   r'   	read_json)  s    z bundle_models.<locals>.read_jsonZbokehz	bokeh-apizbokeh-widgetszbokeh-tableszbokeh-glc                s   g | ]} |qS r$   r$   ).0rT   )rl   r$   r'   
<listcomp>.  s    z!bundle_models.<locals>.<listcomp>c                sD  i }x8| D ].}| dr0d fdd	}|trT||}t|std| n.x,tD ]}|||}t|rZP qZW td| t|}t|j|j|j	d}d|krt
|j|jdkrttt|jd	 }	g }
n|j}	|j}
t|	d
 }|||< |
t|}|kr<d|< ||	|f qtd| qW |S )N)z./z../rV   c                s   t t f| d | S )N/)r   r   split)r]   ext)rootr$   r'   mkpath7  s    z6bundle_models.<locals>.resolve_modules.<locals>.mkpathzno such module: %s)rA   rB   r5   rK   )Zcsszutf-8T)rV   )
startswithrR   rf   r	   RuntimeErrorr   r   r@   rA   rB   r   r5   _style_templaterD   rE   dumpsdepshashlibsha256encode	hexdigestr   append)Z
to_resolverr   Zresolvedr]   rs   rP   rq   rg   compiledr@   rx   Zsigdeps_map)extra_modulesr[   resolve_deps)rr   r'   resolve_modules3  s<    





z&bundle_models.<locals>.resolve_modulesc                s0   t dd   D }t |  | }||S )Nc             s   s   | ]}|j V  qd S )N)r]   )rm   modelr$   r$   r'   	<genexpr>a  s    z6bundle_models.<locals>.resolve_deps.<locals>.<genexpr>)setvalues)rx   rr   Zcustom_modulesZmissing)custom_modelsknown_modulesr   r$   r'   r   `  s    z#bundle_models.<locals>.resolve_depsc             S   s   | d S )Nr   r$   )specr$   r$   r'   <lambda>m  s    zbundle_models.<locals>.<lambda>)r&   c             S   s   | d S )Nr   r$   )r   r$   r$   r'   r   n  s    zrequire("%s")zrequire('%s')z,
c             s   s"   | ]\}}t t||d  V  qdS ))rT   r]   N)_export_templaterD   )rm   rT   r]   r$   r$   r'   r   x  s    z bundle_models.<locals>.<genexpr>c             s   s"   | ]\}}t t||d  V  qdS ))r]   sourceN)_module_templaterD   )rm   r]   r@   r$   r$   r'   r   y  s    )Zpreludeexportsr[   )content)_get_custom_modelsr   sum_compile_modelsr   rX   rx   rP   r}   rT   r]   r@   sorted	enumerateitemsrW   r   _plugin_templaterD   _plugin_prelude_plugin_umd)modelsr   Zbundlescustom_implsr   r~   r   ir]   r@   rx   rT   refsepr   r$   )r   r   r   r[   rl   r   r   r'   r     s:    -
r   c              C   sZ   t j } d}x2| D ]*}t|dd}|dk	rt|}||j7 }qW t|d	 }|S )aF   Generate a key to cache a custom extension implementation with.

    There is no metadata other than the Model classes, so this is the only
    base to generate a cache key.

    We build the model keys from the list of ``model.full_name``. This is
    not ideal but possibly a better solution can be found found later.

    rV   rc   Nzutf-8)
r   model_class_reverse_mapr   r^   r   rX   ry   rz   r{   r|   )r   Zcustom_model_namesrS   rg   r   r&   r$   r$   r'   r   ~  s    

r   c              C   s8   t  } t| d }|d kr4ttj p*d t| < }|S )NrV   )r   _bundle_cacher4   r   r   r   r   )r&   Zbundler$   r$   r'   r     s
    r   a  (function(root, factory) {
//  if(typeof exports === 'object' && typeof module === 'object')
//    factory(require("Bokeh"));
//  else if(typeof define === 'function' && define.amd)
//    define(["Bokeh"], factory);
//  else if(typeof exports === 'object')
//    factory(require("Bokeh"));
//  else
    factory(root["Bokeh"]);
})(this, function(Bokeh) {
  var define;
  return %(content)s;
});
z(function outer(modules, entry) {
  if (Bokeh != null) {
    return Bokeh.register_plugin(modules, {}, entry);
  } else {
    throw new Error("Cannot find Bokeh. You have to load it prior to loading plugins.");
  }
})
z%(prelude)s({
  "custom/main": function(require, module, exports) {
    var models = {
      %(exports)s
    };
    require("base").register_models(models);
    module.exports = models;
  },
  %(modules)s
}, "custom/main");
aG  (function() {
  var head = document.getElementsByTagName('head')[0];
  var style = document.createElement('style');
  style.type = 'text/css';
  var css = %(css)s;
  if (style.styleSheet) {
    style.styleSheet.cssText = css;
  } else {
    style.appendChild(document.createTextNode(css));
  }
  head.appendChild(style);
}());
z*"%(name)s": require("%(module)s").%(name)sz?"%(module)s": function(require, module, exports) {
%(source)s
}c           	   C   s   t  d k	rt  g} nddg} x| D ]}y"t|dgttd}| \}}W n tk
rb   w&Y nX |jdkrpq&td|	d}|d k	r&t
dd	 | D }|tkr&|S q&W d
ttt}td| d d S )NZnodejsZnodez	--version)stdoutstderrr   z^v(\d+)\.(\d+)\.(\d+).*$zutf-8c             s   s   | ]}t |V  qd S )N)int)rm   vr$   r$   r'   r     s    z!_detect_nodejs.<locals>.<genexpr>rU   zFnode.js v%s or higher is needed to allow compilation of custom models zB("conda install nodejs" or follow https://nodejs.org/en/download/))r   nodejs_pathr   r   communicateOSError
returncoderematchdecodetuplegroupsnodejs_min_versionr   mapstrru   )Znodejs_pathsr   procr   _r   versionr$   r$   r'   _detect_nodejs  s&    

r   c               C   s   t d krt a t S )N)_nodejsr   r$   r$   r$   r'   _nodejs_path	  s    r   c               C   s.   t d kr*ttt da tjdkr*t d7 a t S )NZnpmZwin32z.cmd)_npmjsr   r   r   rZ   platformr$   r$   r$   r'   _npmjs_path  s
    
r   c             C   s   t dd| S )Nz\\r\\n|\\r|\\nz\\n)r   sub)sr$   r$   r'   _crlf_cr_2_lf  s    r   c             C   sb   t | g| tttd}|j|d kr&d nt| d\}}|jdkrPt|nt|	dS d S )N)r   r   stdin)inputr   zutf-8)
r   r   r   rE   rw   r{   r   ru   r   r   )Zappargvr   r   r   Zerroutr$   r$   r'   _run  s
    &

r   c             C   s   t t | |S )N)r   r   )r   r   r$   r$   r'   r<   #  s    r<   c             C   s   t t | |S )N)r   r   )r   r   r$   r$   r'   r=   &  s    r=   c             C   s0   y| dg}W n t k
r"   d S X | S d S )Nz	--version)ru   strip)Zrun_appr   r$   r$   r'   r;   )  s
    r;   c             C   s   dS )z%Return cached compiled implementationNr$   )r   rh   r$   r$   r'   _model_cache_no_op1  s    r   c             C   sD   i }x2| D ]*}t |dd}|dk	r
t|}|||j< q
W |s@dS |S )zBReturns CustomModels for models with a custom `__implementation__`rc   N)r^   r   rX   )r   r   rS   rg   r   r$   r$   r'   r   7  s    
r   c             C   s   t |  dd d}i }g }x |D ]}|t|j  q"W |rlt |dd d}tddgdd |D   xT|D ]L}|j}t||}|d	krt	|j
|j|jd
}d|krt|j|||j< qrW |S )z:Returns the compiled implementation of supplied `models`. c             S   s   | j S )N)rX   )r   r$   r$   r'   r   G  s    z!_compile_models.<locals>.<lambda>)r&   c             S   s   | d S )Nr   r$   )Zname_versionr$   r$   r'   r   O  s    Zinstallz--no-progressc             S   s   g | ]\}}|d  | qS )@r$   )rm   rT   r   r$   r$   r'   rn   P  s    z#_compile_models.<locals>.<listcomp>N)rA   rB   r5   )r   r   extendlistri   r   r=   rh   rj   r   r@   rA   rB   r   r5   rX   )r   Zordered_modelsr   ri   r   rg   r~   r$   r$   r'   r   E  s"    



r   )r>   N)N)N)N)Nr,   Z
__future__r   r   r   r   ZloggingZ	getLoggerr)   logrM   rE   r_   os.pathr   r   r   r	   r
   r   
subprocessr   r   rZ   ry   re   r   r   r   stringr   __all__rD   r   ru   r   Z
bokehjsdirrC   r   r    r!   r   objectr   r   r   r#   r   r   r   rf   r   r   r"   r   r   r   r   r   r   r   rv   r   r   r   r   r   r   r   r   r   r<   r=   r;   r   rj   r   r   r$   r$   r$   r'   <module>	   st   

;_
	

