U
    /eAF                     @  sr  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
Z
ddlZddlmZmZmZmZmZ ddlmZmZ ddlmZmZmZm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ddZ'ddddZ(d[ddZ)G dd 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/d'Z0G d(d) d)Z1d*d+ Z2d,d- Z3d.d/d0d1d2Z4i Z5d3e6d4< d5dd6d7d8Z7ddd9d:Z8d;Z9d<Z:d=Z;d>Z<d?Z=d@Z>dAdB Z?da@daAd/ddCdDZBdEdF ZCdGdH ZDd\dIdJZEd]dKdLZFd^dMdNZGdddOdPZHdQdR ZIeIaJd5dSd6dTdUZKd.dVdWdXZLd/ddYdZZMdS )_zM Provide functions and classes to help with various JS and CSS compilation.

    )annotationsN)abspathdirnameexistsisabsjoin)PIPEPopen)DictListSequenceType   )Model)settings   )snakify)AttrDictbundle_all_modelsbundle_modelscalc_cache_key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%   7/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/util/compiler.py__getattr__Q   s    zAttrDict.__getattr__N)__name__
__module____qualname____doc__r)   r%   r%   r%   r(   r   M   s   r   c                      s4   e Zd ZdZdd fddZddddZ  ZS )	r   zG A ``RuntimeError`` subclass for reporting JS compilation errors.

    Nonereturnc                   s\   t    t|trR|d| _|d| _|d| _|d| _|d| _	n|| _d S )Nlinecolumnmessagetext	annotated)
super__init__
isinstancedictgetr1   r2   r3   r4   r5   )r&   error	__class__r%   r(   r7   X   s    

zCompilationError.__init__strc                 C  s   d| j   S )N
)r4   stripr&   r%   r%   r(   __str__c   s    zCompilationError.__str__)r*   r+   r,   r-   r7   rB   __classcell__r%   r%   r<   r(   r   T   s   r   )   r   r   
str | Noner/   c                   C  s   t tS r$   )_version_run_nodejsr%   r%   r%   r(   r    i   s    r    c                   C  s   t tS r$   )rF   
_run_npmjsr%   r%   r%   r(   r!   l   s    r!   
javascriptc           	      C  s   t tdd}t|gt| ||td}|d}t|D ] \}}|dsN qXq6t| q6t	d ||d  }t
|trt|S t|dS d S )Njszcompiler.js)codelangfilebokehjs_dirr?   ZLOG)r;   )r   rN   rG   r9   split	enumerate
startswithprintjsonloadsr8   r   )	rK   rL   rM   Zcompilejs_scriptoutputlinesir1   objr%   r%   r(   r   o   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-   rM   r%   r%   r%   r(   r   ~   s   r   c                   @  s    e Zd ZdZdd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)

    Nr.   r/   c                 C  s   || _ || _d S r$   )rK   rM   )r&   rK   rM   r%   r%   r(   r7      s    zInline.__init__)N)r*   r+   r,   r-   r7   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%   rA   r%   r%   r(   rL      s    zTypeScript.langNr*   r+   r,   r-   propertyrL   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__ = JavaScript(""" <JavaScript code> """)

    c                 C  s   dS )NrI   r%   rA   r%   r%   r(   rL      s    zJavaScript.langNrZ   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%   rA   r%   r%   r(   rL      s    z	Less.langNrZ   r%   r%   r%   r(   r      s   r   c                   @  s2   e Zd ZdZdddddZedd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

    r>   r.   )pathr0   c              	   C  s,   t |dd}| | _W 5 Q R X || _d S )Nutf-8encoding)openreadrK   rM   )r&   r]   fr%   r%   r(   r7      s    zFromFile.__init__rE   r/   c                 C  s4   | j drdS | j dr dS | j dr0dS d S )N.tsrY   .jsrI   ).css.lessr\   )rM   endswithrA   r%   r%   r(   rL      s    zFromFile.langN)r*   r+   r,   r-   r7   r[   rL   r%   r%   r%   r(   r      s   r   )rd   re   rf   rg   c                   @  s   e Zd ZdZdddddZeddd	d
ZeddddZeddddZeddddZ	eddddZ
edd Zedd ZdS )r   z5 Represent a custom (user-defined) Bokeh model.

    zType[Model]r.   )clsr0   c                 C  s
   || _ d S r$   )ri   )r&   ri   r%   r%   r(   r7      s    zCustomModel.__init__r>   r/   c                 C  s   | j jS r$   )ri   r*   rA   r%   r%   r(   name   s    zCustomModel.namec                 C  s   | j jd | j }|ddS )N.z	__main__. )ri   r+   rj   replace)r&   rj   r%   r%   r(   	full_name   s    zCustomModel.full_namerE   c                 C  s*   t j| jj }t|dr"t|jS d S d S )N__file__)sysmodulesri   r+   hasattrr   ro   )r&   moduler%   r%   r(   rM      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__)getattrri   rM   r   osgetcwd)r&   r]   r%   r%   r(   r]      s    

zCustomModel.pathr   c                 C  s   | j j}t|trJd|krB|trBtt|r2|n
t| j	|}nt
|}t|tr|jd krd| jrp| jd nd| jf }||j|}|S )Nr?   z%s%s.ts:rl   )ri   __implementation__r8   r>   rh   extsr   r   r   r]   r#   r   rM   rj   r=   rK   )r&   implrM   r%   r%   r(   implementation   s    
zCustomModel.implementationc                 C  s   t | jdi S )NZ__dependencies__)rt   ri   rA   r%   r%   r(   dependencies  s    zCustomModel.dependenciesc                 C  s   dt | j S )Nz	custom/%s)r   rn   rA   r%   r%   r(   rs     s    zCustomModel.moduleN)r*   r+   r,   r-   r7   r[   rj   rn   rM   r]   r{   r|   rs   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 ImplementationNr}   )hookr%   r%   r(   r"     s    r"   zDict[str, CustomModel]r>   )custom_modelsr0   c                 C  s4   dd |   D }dt|d}t| 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.

    c                 S  s   h | ]
}|j qS r%   rn   .0modelr%   r%   r(   	<setcomp>$  s     z!calc_cache_key.<locals>.<setcomp>,r^   )valuesr   sortedencodehashlibsha256	hexdigest)r   Zmodel_namesZencoded_namesr%   r%   r(   r     s    
r   zDict[str, str]_bundle_cachezSequence[Type[Model]] | None)modelsr0   c              
   C  s   t | }|dkrdS t|}t|d}|dkrzt| t|< }W nL tk
r } z.tdtjd tt	|tjd t
d W 5 d}~X Y nX |S )z&Create a bundle of selected `models`. NzCompilation failed:)rM   r   )_get_custom_modelsr   r   r:   _bundle_modelsr   rR   rp   stderrr>   exit)r   r   r'   Zbundler;   r%   r%   r(   r   *  s    r   c                   C  s   t dS )zCreate a bundle of all models. N)r   r%   r%   r%   r(   r   ;  s    r   zz(function(root, factory) {
    factory(root["Bokeh"]);
})(this, function(Bokeh) {
  let 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) {
    const models = {
      %(exports)s
    };
    require("base").register_models(models);
    module.exports = models;
  },
  %(modules)s
}, "custom/main");
aM  (function() {
  const head = document.getElementsByTagName('head')[0];
  const style = document.createElement('style');
  style.type = 'text/css';
  const 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} | D ]}z"t|dgttd}| \}}W n tk
rb   Y q$Y nX |jdkrpq$td|	d}|d k	r$t
dd	 | D }|tkr$|  S q$d
ttt}td| d d S )NZnodejsnode	--version)stdoutr   r   z^v(\d+)\.(\d+)\.(\d+).*$r^   c                 s  s   | ]}t |V  qd S r$   )int)r   vr%   r%   r(   	<genexpr>  s     z!_detect_nodejs.<locals>.<genexpr>rk   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   mapr>   RuntimeError)Znodejs_pathsr   procr   _r   versionr%   r%   r(   _detect_nodejs  s(    


r   c                   C  s   t d krt a t S r$   )_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npmwin32z.cmd)_npmjsr   r   r   rp   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  sh   t | g| tttd}|j|d kr&d nt| d\}}|jdkrVt|dnt	|dS d S )N)r   r   stdin)inputr   r^   )
r	   r   r   rS   dumpsr   r   r   r   r   )Zappargvr   r   r   Zerroutr%   r%   r(   _run  s
    &
r   c                 C  s   t t | |S r$   )r   r   r   r   r%   r%   r(   rG     s    rG   c                 C  s   t t | |S r$   )r   r   r   r%   r%   r(   rH     s    rH   c                 C  s2   z| dg}W n t k
r$   Y d S X | S d S )Nr   )r   r@   )Zrun_appr   r%   r%   r(   rF     s
    rF   c                 C  s   dS )z%Return cached compiled implementationNr%   )r   r{   r%   r%   r(   _model_cache_no_op  s    r   zDict[str, CustomModel] | Nonec                 C  sL   t  }| ptj D ]*}t|dd}|dk	rt|}|||j< q|rH|S dS )zBReturns CustomModels for models with a custom `__implementation__`rx   N)r9   r   Zmodel_class_reverse_mapr   rt   r   rn   )r   r   ri   rz   r   r%   r%   r(   r     s    r   )r   c                 C  s   t |  dd d}i }g }|D ]}|t|j  q |rht |dd d}tddgdd |D   |D ]L}|j}t||}|d	krt	|j
|j|jd
}d|krt|j|||j< ql|S )z:Returns the compiled implementation of supplied `models`. c                 S  s   | j S r$   r   )r   r%   r%   r(   <lambda>      z!_compile_models.<locals>.<lambda>r'   c                 S  s   | d S Nr   r%   )Zname_versionr%   r%   r(   r     r   installz--no-progressc                 S  s   g | ]\}}|d  | qS )@r%   )r   rj   r   r%   r%   r(   
<listcomp>  s     z#_compile_models.<locals>.<listcomp>NrL   rM   r;   )r   r   extendlistr|   itemsrH   r{   r~   r   rK   rL   rM   r   r;   rn   )r   Zordered_modelscustom_implsr|   r   rz   compiledr%   r%   r(   _compile_models  s"    

r   c              	     s  g }g t ttdddd}t| }W 5 Q R X t |d D ]$}|d d}|dk	rD| qDt	 }i fd	d
 fdd 
 D ]B}||j }|j|j}	||j|jf |j|j|	f qt|dd d}tdd dtD ]X\}
\}}}| D ]2\}}|d| d| }|d| d| }q"||f|
< qd}|dd |D }|dd D ttt|d }tt|d S )z4 Create a JavaScript bundle with selected `models`. rJ   z
bokeh.jsonr^   r_   Z	artifactsrs   	canonicalNc                   s`  i }| D ]P}| dr*d fdd	}|trP||}t|s|td| n,tD ]}|||}t|rT q|qTt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rZd|< ||	|f q||dr<dnd d }|krtd| q|S )N)z./z../rl   c                   s   t t f| d | S )N/)r   r   rO   )rs   extrootr%   r(   mkpath  s    z7_bundle_models.<locals>.resolve_modules.<locals>.mkpathzno such module: %sr   r;   r\   )cssr^   Tr   index)rl   )rQ   rh   ry   r   r   r   r   rK   rL   rM   r   r;   _style_templater9   rS   r   depsr   r   r   r   r   append)Z
to_resolver   Zresolvedrs   r   r]   r   rz   r   rK   r   sigdeps_mapr   )extra_modulesknown_modulesrq   resolve_depsr   r(   resolve_modules	  s@    





z'_bundle_models.<locals>.resolve_modulesc                   s,   dd    D }t|  | }||S )Nc                 S  s   h | ]
}|j qS r%   )rs   r   r%   r%   r(   r   9  s     z7_bundle_models.<locals>.resolve_deps.<locals>.<setcomp>)r   set)r   r   Zcustom_modulesmissing)r   r   r   r%   r(   r   8  s    z$_bundle_models.<locals>.resolve_depsc                 S  s   | d S )Nr   r%   specr%   r%   r(   r   E  r   z _bundle_models.<locals>.<lambda>r   c                 S  s   | d S r   r%   r   r%   r%   r(   r   F  r   zrequire("%s")zrequire('%s')z,
c                 s  s"   | ]\}}t t||d  V  qdS ))rj   rs   N)_export_templater9   )r   rj   rs   r%   r%   r(   r   P  s     z!_bundle_models.<locals>.<genexpr>c                 s  s"   | ]\}}t t||d  V  qdS ))rs   sourceN)_module_templater9   )r   rs   rK   r%   r%   r(   r   Q  s     )Zpreludeexportsrq   )content)ra   r   rN   rS   rT   rb   r   r:   addr   r   rn   r   r]   r   rj   rs   rK   r   rP   r   rm   _plugin_templater9   _plugin_prelude_plugin_umd)r   r   rc   ZbokehZartifactr   r   r   r   r   rW   rs   rK   r   rj   refsepr   r%   )r   r   r   rq   r   r   r(   r     s<    /
r   )rI   N)N)N)N)Nr-   
__future__r   logging	getLoggerr*   logr   rS   ru   r   rp   os.pathr   r   r   r   r   
subprocessr   r	   typingr
   r   r   r   r   r   r   stringr   __all__r9   r   r   r   Z
bokehjsdirrN   r   r    r!   r   r   r   r#   r   r   r   ry   r   r   r"   r   r   __annotations__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   rH   rF   r   r~   r   r   r   r%   r%   r%   r(   <module>   s~   

<
	

