a
    \Df5J                     @  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
Z
ddlZddl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mZ dd	lmZ dd
lmZ ddlmZ dZ G dd de!e"ef Z#G dd de$Z%e& Z'dZ(ddddZ)ddddZ*dmdddd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-Z0G d(d) d)e,Z1d*Z2G d+d, d,Z3d-dd.d/Z4d-d0d1d2d3Z5d4dd5d6d7Z6i Z7d8e8d9< d:dd;d<d=Z9ddd>d?Z:d@Z;dAZ<dBZ=dCZ>dDZ?dEZ@dFddGdHZAdaBdIe8dJ< daCdIe8dK< dFddLdMZDdFddNdOZEdddPdQdRZFdndFdSdTddUdVdWZGdodSdTddXdYdZZHdpdSdTddXd[d\ZId]dd^d_d`ZJd,ddadbdcddZKeKaLd:ded;dfdgZMd4dhd5didjZNd4dd5dkdlZOdS )qzM Provide functions and classes to help with various JS and CSS compilation.

    )annotationsN)abspathdirnameexistsisabsjoin)Path)PIPEPopen)AnyCallableSequence   )HasProps)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dddZdS )r   zH Provide a dict subclass that supports access by named attributes.

    strr   )keyreturnc                 C  s   | | S N )selfr%   r(   r(   `/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/bokeh/util/compiler.py__getattr__M   s    zAttrDict.__getattr__N)__name__
__module____qualname____doc__r+   r(   r(   r(   r*   r   I   s   r   c                      s6   e Zd ZdZddd fddZddd	d
Z  ZS )r   zG A ``RuntimeError`` subclass for reporting JS compilation errors.

    zdict[str, str] | strNone)errorr&   c                   s^   t    t|trT|d| _|d| _|d| _|dd| _|d| _	n|| _d S )Nlinecolumnmessagetext 	annotated)
super__init__
isinstancedictgetr2   r3   r4   r5   r7   )r)   r1   	__class__r(   r*   r9   T   s    

zCompilationError.__init__r$   r&   c                 C  s   d| j   S )N
)r5   stripr)   r(   r(   r*   __str___   s    zCompilationError.__str__)r,   r-   r.   r/   r9   rC   __classcell__r(   r(   r=   r*   r   P   s   r   )   r   r   
str | Noner?   c                   C  s   t tS r'   )_version_run_nodejsr(   r(   r(   r*   r    e   s    r    c                   C  s   t tS r'   )rG   
_run_npmjsr(   r(   r(   r*   r!   h   s    r!   
javascriptr$   )codelangfiler&   c           	   	   C  s   t tdd}t|gt| ||ttd}|d}t|D ] \}}|dsT q^q<t	| q<t
d ||d  }t|trt|S t|d S )Njszcompiler.js)rK   rL   rM   bokehjs_dirr@   ZLOG)r   rO   rH   r;   osfspathsplit	enumerate
startswithprintjsonloadsr:   r   r   )	rK   rL   rM   Zcompilejs_scriptoutputlinesir2   objr(   r(   r*   r   k   s    



r   c                   @  s8   e Zd ZU dZdZded< ded< edddd	ZdS )
r   zF Base class for representing Bokeh custom model implementations.

    NrF   rM   r$   rK   r?   c                 C  s
   t  d S r'   )NotImplementedErrorrB   r(   r(   r*   rL      s    zImplementation.lang)r,   r-   r.   r/   rM   __annotations__propertyrL   r(   r(   r(   r*   r   y   s
   
r   c                   @  s$   e Zd ZdZd	d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$   rF   r0   )rK   rM   r&   c                 C  s   || _ || _d S r'   )rK   rM   )r)   rK   rM   r(   r(   r*   r9      s    zInline.__init__)N)r,   r-   r.   r/   r9   r(   r(   r(   r*   r      s   r   c                   @  s"   e Zd ZdZedd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> """)

    r$   r?   c                 C  s   dS )N
typescriptr(   rB   r(   r(   r*   rL      s    zTypeScript.langNr,   r-   r.   r/   r^   rL   r(   r(   r(   r*   r#      s   
r#   c                   @  s"   e Zd ZdZedd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> """)

    r$   r?   c                 C  s   dS )NrJ   r(   rB   r(   r(   r*   rL      s    zJavaScript.langNr`   r(   r(   r(   r*   r      s   
r   c                   @  s"   e Zd ZdZeddddZdS )r   z3 An implementation of a Less CSS style sheet.

    r$   r?   c                 C  s   dS )Nlessr(   rB   r(   r(   r*   rL      s    z	Less.langNr`   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$   r0   )pathr&   c                 C  s@   t |dd}| | _W d    n1 s,0    Y  || _d S )Nutf-8)encoding)openreadrK   rM   )r)   rb   fr(   r(   r*   r9      s    (zFromFile.__init__r?   c                 C  sN   | j d ur:| j drdS | j dr*dS | j dr:dS td| j  d S )N.tsr_   .jsrJ   ).css.lessra   zunknown file type )rM   endswith
ValueErrorrB   r(   r(   r*   rL      s    
zFromFile.langN)r,   r-   r.   r/   r9   r^   rL   r(   r(   r(   r*   r      s   r   )rh   ri   rj   rk   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ddZeddddZdS )r   z5 Represent a custom (user-defined) Bokeh model.

    ztype[HasProps]r0   )clsr&   c                 C  s
   || _ d S r'   )rn   )r)   rn   r(   r(   r*   r9      s    zCustomModel.__init__r$   r?   c                 C  s   | j jS r'   )rn   r,   rB   r(   r(   r*   name   s    zCustomModel.namec                 C  s   | j jd | j }|ddS )N.z	__main__.r6   )rn   r-   ro   replace)r)   ro   r(   r(   r*   	full_name   s    zCustomModel.full_namerF   c                 C  s6   t j| jj }t|dr.|j }d ur.t|S d S d S )N__file__)sysmodulesrn   r-   hasattrrs   r   )r)   modulerM   r(   r(   r*   rM      s    zCustomModel.filec                 C  s:   t | jdd }|d ur|S | jd ur.t| jS t S d S )NZ__base_path__)getattrrn   rM   r   rP   getcwd)r)   rb   r(   r(   r*   rb      s    

zCustomModel.pathr   c                 C  s   t | jd}t|trNd|vrF|trFtt|r6|n
t| j	|}nt
|}t|tr|jd u r| jrr| jd nd | j d}||j|}|S )N__implementation__r@   :r6   rh   )rx   rn   r:   r$   rl   extsr   r   r   rb   r#   r   rM   ro   r>   rK   )r)   implrM   r(   r(   r*   implementation   s    
 zCustomModel.implementationdict[str, str]c                 C  s   t | jdi S )NZ__dependencies__)rx   rn   rB   r(   r(   r*   dependencies  s    zCustomModel.dependenciesc                 C  s   dt | j S )Nzcustom/)r   rr   rB   r(   r(   r*   rw     s    zCustomModel.moduleN)r,   r-   r.   r/   r9   r^   ro   rr   rM   rb   r~   r   rw   r(   r(   r(   r*   r      s    
r   z8Callable[[CustomModel, Implementation], AttrDict | None]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   r0   )hookr&   c                 C  s   | a dS )zrSets a compiled model cache hook used to look up the compiled
       code given the CustomModel and ImplementationNr   )r   r(   r(   r*   r"     s    r"   zdict[str, CustomModel])custom_modelsr&   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(   rr   .0modelr(   r(   r*   	<setcomp>%      z!calc_cache_key.<locals>.<setcomp>,rc   )valuesr   sortedencodehashlibsha256	hexdigest)r   Zmodel_namesZencoded_namesr(   r(   r*   r     s    
r   r   _bundle_cachezSequence[type[HasProps]] | None)modelsr&   c              
   C  s   t | }|du rdS t|}t|d}|du rzt| t|< }W nN ty } z6tdtjd tt	|tjd t
d W Y d}~n
d}~0 0 |S )z&Create a bundle of selected `models`. NzCompilation failed:)rM   r   )_get_custom_modelsr   r   r<   _bundle_modelsr   rU   rt   stderrr$   exit)r   r   r%   Zbundler1   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
}r   c               	   C  s   t  } | d ur| gnddg}|D ]} z"t| dgttd}| \}}W n ty^   Y q"Y n0 |jdkrlq"td|	d}|d ur"t
dd	 | D }|tkr"t|   S q"d
dd	 tD }td| dd d S )NZnodejsnode	--version)stdoutr   r   z^v(\d+)\.(\d+)\.(\d+).*$rc   c                 s  s   | ]}t |V  qd S r'   )int)r   vr(   r(   r*   	<genexpr>  r   z!_detect_nodejs.<locals>.<genexpr>rp   c                 s  s   | ]}t |V  qd S r'   )r$   )r   xr(   r(   r*   r     r   z	node.js vz; 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   r   RuntimeError)r   Znodejs_pathsprocr   _r   versionZversion_reprr(   r(   r*   _detect_nodejs  s&    

r   zPath | None_nodejs_npmjsc                   C  s   t d u rt a t S r'   )r   r   r(   r(   r(   r*   _nodejs_path  s    r   c                  C  s*   t d u r&tjdkrdnd} t j|  a t S )Nwin32znpm.cmdZnpm)r   rt   platformr   parent)
executabler(   r(   r*   _npmjs_path  s    r   )sr&   c                 C  s   t dd| S )Nz\\r\\n|\\r|\\nz\\n)r   sub)r   r(   r(   r*   _crlf_cr_2_lf  s    r   	list[str]zdict[str, Any] | None)appargvinputr&   c                 C  sh   t | g|tttd}|j|d u r&d nt| d\}}|jdkrVt|dnt	|dS d S )N)r   r   stdin)r   r   rc   )
r
   r	   r   rV   dumpsr   r   r   r   r   )r   r   r   r   r   Zerroutr(   r(   r*   _run  s
    &
r   )r   r   r&   c                 C  s   t t | |S r'   )r   r   r   r   r(   r(   r*   rH     s    rH   c                 C  s   t t | |S r'   )r   r   r   r(   r(   r*   rI     s    rI   z1Callable[[list[str], dict[str, Any] | None], str])run_appr&   c                 C  s2   z| dgd }W n t y$   Y d S 0 | S d S )Nr   )r   rA   )r   r   r(   r(   r*   rG     s
    rG   zAttrDict | None)r   r~   r&   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urt|}|||j< q|rH|S dS )zBReturns CustomModels for models with a custom `__implementation__`rz   N)r;   r   Zmodel_class_reverse_mapr   rx   r   rr   )r   r   rn   r}   r   r(   r(   r*   r     s    r   zdict[str, AttrDict]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	u rt	|j
|j|jd
}d|v 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>  r   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   ro   r   r(   r(   r*   
<listcomp>  r   z#_compile_models.<locals>.<listcomp>NrL   rM   r1   )r   r   extendlistr   itemsrI   r~   r   r   rK   rL   rM   r   r1   rr   )r   Zordered_modelscustom_implsr   r   r}   compiledr(   r(   r*   _compile_models  s"    

r   c                   s  g }g t td d }t |dD ]B}t||}|drP|dd }|tj	j
d}| q(t }i dd	d
dfdddd	d
d fdd  D ]B}||j }|j|j	}||j|jf |j|j|f qt|dd d}tdd dg }	tD ]Z\}
\}}}| 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`. rN   libz*.d.tsz.d.tsN/zset[str]r$   r   )
to_resolverootr&   c                   sX  i }| D ]H}| dr"ddddd fdd}|trZ||}t|std| n,tD ]}|||}t|r^ qq^td| t|}t|j|j|j	d}|jd	krt
tt|jd
 }	g }
n|j}	|j}
t|	d }|||< |
t|}|vrRd|< ||	|f q||dr4dnd d }|vrtd| q|S )N)z./z../r6   r$   )rw   extr&   c                   s   t t g| dR  | S )Nr   )r   r   rR   )rw   r   r   r(   r*   mkpath  s    z7_bundle_models.<locals>.resolve_modules.<locals>.mkpathzno such module: %sr   ra   )cssrc   Tr   index)r6   )rT   rl   r|   r   r   r   r   rK   rL   rM   _style_templater;   rV   r   depsr   r   r   r   r   append)r   r   Zresolvedrw   r   rb   r   r}   r   rK   r   sigdeps_mapr   )extra_modulesknown_modulesru   resolve_depsr   r*   resolve_modules  s<    




z'_bundle_models.<locals>.resolve_modulesr   )r   r   r&   c                   s,   dd    D }t|  | }||S )Nc                 S  s   h | ]
}|j qS r(   )rw   r   r(   r(   r*   r   4  r   z7_bundle_models.<locals>.resolve_deps.<locals>.<setcomp>)r   set)r   r   Zcustom_modulesmissing)r   r   r   r(   r*   r   3  s    z$_bundle_models.<locals>.resolve_depsc                 S  s   | d S )Nr   r(   specr(   r(   r*   r   @  r   z _bundle_models.<locals>.<lambda>r   c                 S  s   | d S r   r(   r   r(   r(   r*   r   A  r   zrequire("%s")zrequire('%s')z,
c                 s  s"   | ]\}}t t||d  V  qdS ))ro   rw   N)_export_templater;   )r   ro   rw   r(   r(   r*   r   L  r   z!_bundle_models.<locals>.<genexpr>c                 s  s"   | ]\}}t t||d  V  qdS ))rw   sourceN)_module_templater;   )r   rw   rK   r(   r(   r*   r   M  r   )Zpreludeexportsru   )content)r   rO   r   rglobr$   relative_torl   rq   rP   rb   sepaddr   r   rr   r   r   ro   rw   rK   r   rS   r   r   _plugin_templater;   _plugin_prelude_plugin_umd)r   r   lib_dirrb   r   r   r   r   r   Zbare_modulesrZ   rw   rK   r   ro   refr   Zrendered_exportsZrendered_modulesr   r(   )r   r   r   ru   r   r   r*   r     s@    
,
r   )rJ   N)N)N)N)Pr/   
__future__r   logging	getLoggerr,   logr   rV   rP   r   rt   os.pathr   r   r   r   r   pathlibr   
subprocessr	   r
   typingr   r   r   Zcore.has_propsr   r   stringsr   __all__r;   r$   r   r   r   Zbokehjs_pathrO   r   r    r!   r   r   r   r#   r   r   r   r|   r   r   r"   r   r   r]   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   rI   rG   r   r   r   r   r   r(   r(   r(   r*   <module>   s   

<	