
    -eJ                       U d Z ddlmZ ddlZ ej        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e!e"ef                   Z# G d de$          Z% ej&                    Z'dZ(dYdZ)dYdZ*dZd[dZ+ G d d          Z, G d d e,          Z- G d! d"e-          Z. G d# d$e-          Z/ G d% d&e-          Z0 G d' d(e,          Z1d)Z2 G d* d+          Z3d\d-Z4d]d0Z5d^d3Z6i Z7d4e8d5<   d_d8Z9dYd9Z:d:Z;d;Z<d<Z=d=Z>d>Z?d?Z@d`d@ZAdaBdaCd`dAZDd`dBZEdadDZFdbdcdJZGdbdddKZHdbdddLZIdedOZJdfdSZKeKaLdgdUZMdhdWZNd^dXZOdS )izM 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                      e Zd ZdZddZdS )	r   zH Provide a dict subclass that supports access by named attributes.

    keystrreturnr   c                    | |         S N )selfr&   s     3lib/python3.11/site-packages/bokeh/util/compiler.py__getattr__zAttrDict.__getattr__M   s    Cy    N)r&   r'   r(   r   )__name__
__module____qualname____doc__r.   r+   r/   r-   r   r   I   s2              r/   r   c                  ,     e Zd ZdZd	 fdZd
dZ xZS )r   zG A ``RuntimeError`` subclass for reporting JS compilation errors.

    errordict[str, str] | strr(   Nonec                   t                                                       t          |t                    r|                    d          | _        |                    d          | _        |                    d          | _        |                    dd          | _        |                    d          | _	        d S || _        d S )Nlinecolumnmessagetext 	annotated)
super__init__
isinstancedictgetr9   r:   r;   r<   r>   )r,   r5   	__class__s     r-   r@   zCompilationError.__init__T   s    eT"" 			&))DI))H--DK 99Y//DL		&"--DI"YY{33DNNNDIIIr/   r'   c                :    d| j                                         z   S )N
)r<   stripr,   s    r-   __str__zCompilationError.__str___   s    dioo''''r/   )r5   r6   r(   r7   r(   r'   )r0   r1   r2   r3   r@   rI   __classcell__)rD   s   @r-   r   r   P   s[         	 	 	 	 	 	( ( ( ( ( ( ( (r/   r   )   r   r   r(   
str | Nonec                 *    t          t                    S r*   )_version_run_nodejsr+   r/   r-   r!   r!   e   s    K   r/   c                 *    t          t                    S r*   )rO   
_run_npmjsr+   r/   r-   r"   r"   h   s    Jr/   
javascriptcoder'   langfilec           	        t          t          dd          }t          |gt          | ||t                              }|                    d          }t          |          D ]+\  }}|                    d          s nt          |           ,t          j	        d                     ||d                              }t          |t                    rt          |          S t          |          )Njszcompiler.js)rT   rU   rV   bokehjs_dirrF   LOG)r   rY   rP   rB   split	enumerate
startswithprintjsonloadsrA   r   r   )	rT   rU   rV   compilejs_scriptoutputlinesir9   objs	            r-   r    r    k   s    K}==*+Tt$T_j-k-k-kllFLLEU##  4u%% 	E$KKKK
*TYYuQRRy))
*
*C#t }}
3

r/   c                  D    e Zd ZU dZdZded<   ded<   ed	d            ZdS )
r   zF Base class for representing Bokeh custom model implementations.

    NrM   rV   r'   rT   r(   c                    t                      r*   )NotImplementedErrorrH   s    r-   rU   zImplementation.lang   s    !###r/   rJ   )r0   r1   r2   r3   rV   __annotations__propertyrU   r+   r/   r-   r   r   y   sZ           DIII$ $ $ X$ $ $r/   r   c                      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)

    NrT   r'   rV   rM   r(   r7   c                "    || _         || _        d S r*   )rT   rV   )r,   rT   rV   s      r-   r@   zInline.__init__   s    				r/   r*   )rT   r'   rV   rM   r(   r7   )r0   r1   r2   r3   r@   r+   r/   r-   r   r      s7        
 
      r/   r   c                  *    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> """)

    r(   r'   c                    dS )N
typescriptr+   rH   s    r-   rU   zTypeScript.lang       |r/   NrJ   r0   r1   r2   r3   rj   rU   r+   r/   r-   r$   r$      >        	 	    X  r/   r$   c                  *    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> """)

    r(   r'   c                    dS )NrS   r+   rH   s    r-   rU   zJavaScript.lang   rp   r/   NrJ   rq   r+   r/   r-   r   r      rr   r/   r   c                  *    e Zd ZdZedd            ZdS )r   z3 An implementation of a Less CSS style sheet.

    r(   r'   c                    dS )Nlessr+   rH   s    r-   rU   z	Less.lang   s    vr/   NrJ   rq   r+   r/   r-   r   r      s>             X  r/   r   c                  2    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

    pathr'   r(   r7   c                    t          |d          5 }|                                | _        d d d            n# 1 swxY w Y   || _        d S )Nutf-8)encoding)openreadrT   rV   )r,   ry   fs      r-   r@   zFromFile.__init__   s    $))) 	!QDI	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!			s   8<<c                    | j         T| j                             d          rdS | j                             d          rdS | j                             d          rdS t          d| j                    )N.tsro   .jsrS   ).css.lessrw   zunknown file type )rV   endswith
ValueErrorrH   s    r-   rU   zFromFile.lang   s{    9 y!!%(( $#|y!!%(( $#|y!!"344 v9di99:::r/   N)ry   r'   r(   r7   rJ   )r0   r1   r2   r3   r@   rj   rU   r+   r/   r-   r   r      sR            
 ; ; ; X; ; ;r/   r   )r   r   r   r   c                      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.

    clstype[HasProps]r(   r7   c                    || _         d S r*   )r   )r,   r   s     r-   r@   zCustomModel.__init__   s    r/   r'   c                    | j         j        S r*   )r   r0   rH   s    r-   namezCustomModel.name   s    x  r/   c                \    | j         j        dz   | j        z   }|                    dd          S )N.z	__main__.r=   )r   r1   r   replace)r,   r   s     r-   	full_namezCustomModel.full_name   s,    x"S(494||K,,,r/   rM   c                    t           j        | j        j                 }t	          |d          r|j        x}t          |          S d S )N__file__)sysmodulesr   r1   hasattrr   r   )r,   modulerV   s      r-   rV   zCustomModel.file   sB    TX016:&& 	FO,CD+P4== 4r/   c                    t          | j        dd           }||S | j        t          | j                  S t	          j                    S )N__base_path__)getattrr   rV   r   osgetcwd)r,   ry   s     r-   ry   zCustomModel.path   sD    tx$77KY"49%%%9;;r/   r   c                   t          | j        d          }t          |t                    rad|vrN|                    t
                    r4t          t          |          r|nt          | j	        |                    }nt          |          }t          |t                    r?|j        8| j        r
| j        dz   nd | j         d}|                    |j        |          }|S )N__implementation__rF   :r=   r   )r   r   rA   r'   r   extsr   r   r   ry   r$   r   rV   r   rD   rT   )r,   implrV   s      r-   implementationzCustomModel.implementation   s    tx!566dC   	(4DMM$$7$7d Ndi9N9NOO!$''dF## 	3	(9)-:di#ooJDIJJJD>>$)T22Dr/   dict[str, str]c                .    t          | j        di           S )N__dependencies__)r   r   rH   s    r-   dependencieszCustomModel.dependencies  s    tx!3R888r/   c                0    dt          | j                   S )Nzcustom/)r   r   rH   s    r-   r   zCustomModel.module  s    200222r/   N)r   r   r(   r7   rJ   r(   rM   )r(   r   )r(   r   )r0   r1   r2   r3   r@   rj   r   r   rV   ry   r   r   r   r+   r/   r-   r   r      s            ! ! ! X! - - - X-    X    X    X 9 9 9 X9 3 3 3 X3 3 3r/   r   8Callable[[CustomModel, Implementation], AttrDict | None]c                     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/   hookr7   c                
    | a dS )zrSets a compiled model cache hook used to look up the compiled
       code given the CustomModel and ImplementationNr   )r   s    r-   r#   r#     s     #r/   custom_modelsdict[str, CustomModel]c                    d |                                  D             }d                    t          |                                        d          }t	          j        |                                          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                    h | ]	}|j         
S r+   r   .0models     r-   	<setcomp>z!calc_cache_key.<locals>.<setcomp>%  s    GGGu5?GGGr/   ,r{   )valuesr   sortedencodehashlibsha256	hexdigest)r   model_namesencoded_namess      r-   r   r     sg     HG0D0D0F0FGGGKHHVK001188AAM>-((22444r/   r   _bundle_cachemodelsSequence[type[HasProps]] | Nonec                   t          |           }|dS t          |          }t                              |d          }|	 t	          |          xt          |<   }nn# t
          $ ra}t          dt          j                   t          t          |          t          j                   t          j
        d           Y d}~nd}~ww xY w|S )z&Create a bundle of selected `models`. NzCompilation failed:)rV   r   )_get_custom_modelsr   r   rC   _bundle_modelsr   r^   r   stderrr'   exit)r   r   r&   bundler5   s        r-   r   r   +  s    &v..Mt

'
'CsD))F~	*8*G*GGM# 	 	 	'cj9999#e**3:....HQKKKKKKKK	 Ms   A 
C%ACCc                      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                 &   t          j                    } | | gnddg}|D ]} 	 t          | dgt          t                    }|                                \  }}n# t
          $ r Y Ew xY w|j        dk    rUt          j        d|	                    d                    }|:t          d |                                D                       }|t          k    r| c S d	                    d
 t          D                       }t          d| ddz             )Nnodejsnode	--version)stdoutr   r   z^v(\d+)\.(\d+)\.(\d+).*$r{   c              3  4   K   | ]}t          |          V  d S r*   )int)r   vs     r-   	<genexpr>z!_detect_nodejs.<locals>.<genexpr>  s(      ;;qCFF;;;;;;r/   r   c              3  4   K   | ]}t          |          V  d S r*   )r'   )r   xs     r-   r   z!_detect_nodejs.<locals>.<genexpr>  s(      ??qCFF??????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   RuntimeError)r   nodejs_pathsprocr   _r   versionversion_reprs           r-   _detect_nodejsr     sK   &((K$/$;K==(FASL# # #	+{3DNNND**,,KVQQ 	 	 	H	 ?a4fmmG6L6LMM;;ELLNN;;;;;G,,,"""" 88??,>?????L
l<lll[\ ] ] ]s   5A
A%$A%c                 :    t           t                      a t           S r*   )_nodejsr   r+   r/   r-   _nodejs_pathr     s     ""Nr/   c                     t           Ct          t          t                                d          a t          j        dk    r
t           dz  a t           S )Nnpmwin32z.cmd)_npmjsr   r   r   r   platformr+   r/   r-   _npmjs_pathr     s>    ~glnn--u55<7""fFMr/   sc                .    t          j        dd|           S )Nz\\r\\n|\\r|\\nz\\n)r   sub)r   s    r-   _crlf_cr_2_lfr     s    6#VQ///r/   appargv	list[str]inputdict[str, Any] | Nonec                l   t          | g|t          t          t                    }|                    |d n%t          j        |                                                    \  }}|j        dk    r"t          |                    d                    t          |                    d                    S )N)r   r   stdin)r   r   r{   )
r   r
   r   r_   dumpsr   r   r   r   r   )r   r   r   r   r   errouts         r-   _runr     s    #d4tDDDD''emddTYIZIZIaIaIcIc'ddVV!6==11222V]]733444r/   c                <    t          t                      | |          S r*   )r   r   r   r   s     r-   rP   rP     s    e,,,r/   c                <    t          t                      | |          S r*   )r   r   r   s     r-   rR   rR     s    tU+++r/   run_app1Callable[[list[str], dict[str, Any] | None], str]c                h    	  | dgd           }|                                 S # t          $ r Y d S w xY w)Nr   )rG   r   )r   r   s     r-   rO   rO     sN    ';-.. }}    tts   # 
11r   r   AttrDict | Nonec                    dS )z%Return cached compiled implementationNr+   )r   r   s     r-   _model_cache_no_opr    s    4r/   dict[str, CustomModel] | Nonec                    t                      }| pt          j                                        D ].}t	          |dd          }|t          |          }|||j        <   /|r|ndS )zBReturns CustomModels for models with a custom `__implementation__`r   N)rB   r   model_class_reverse_mapr   r   r   r   )r   r   r   r   r   s        r-   r   r     so    ,0FFMB9@@BB 3 3s0$77$$E-2M%/*)3==t3r/   dict[str, AttrDict]c                   t          |                                 d           }i }g }|D ];}|                    t          |j                                                             <|r0t          |d           }t          ddgd |D             z              |D ]^}|j        }t          ||          }|9t          |j
        |j        |j                  }d	|v rt          |j                  |||j        <   _|S )
z:Returns the compiled implementation of supplied `models`. c                    | j         S r*   r   )r   s    r-   <lambda>z!_compile_models.<locals>.<lambda>  s    eo r/   r&   c                    | d         S Nr   r+   )name_versions    r-   r  z!_compile_models.<locals>.<lambda>  s    \RS_ r/   installz--no-progressc                $    g | ]\  }}|d z   |z   S )@r+   )r   r   r   s      r-   
<listcomp>z#_compile_models.<locals>.<listcomp>  s&    2n2n2n_dT[D3J4H2n2n2nr/   NrU   rV   r5   )r   r   extendlistr   itemsrR   r   r   r    rT   rU   rV   r   r5   r   )r   ordered_modelscustom_implsr   r   r   compileds          r-   _compile_modelsr    s(   M00228U8UVVVNL*,L > >D!3!9!9!;!;<<==== pl0T0TUUUI/2n2n`l2n2n2nnooo 1 1#*5$77%dididiPPPH(""&x~666(0U_%%r/   c                    g }g t          t                    dz  dz  }t                      |                    d          D ]}}t	          |                    |                    }|                    d          r
|dd         }|                    t          j	        j
        d          }                    |           ~t                     }i dfdd fd                                 D ]h}||j                 } |j        |j	                  }|                    |j        |j        f                               |j        |j        |f           it)          |d           }t)          d           g }	t+                    D ]r\  }
\  }}}|                                D ]=\  }}|                    d|z  d|z            }|                    d|z  d|z            }>|	                    ||f           sd}|                    d |D                       }|                    d |	D                       }t0          t3          t4          ||          z  }t6          t3          |          z  S )z4 Create a JavaScript bundle with selected `models`. rX   libz*.d.tsz.d.tsN/
to_resolveset[str]rootr'   r(   r   c                   i }| D ]}|                     d          r|ddfd}|                    t                    r- ||          }t          |          st	          d|z            n9t          D ]} |||          }t          |          r n t	          d|z            t          |          }t          |j        |j        |j	        	          }|j        d
k    r2t          t          t          j        |j                            z  }	g }
n|j        }	|j        }
t          j        |	                    d                                                    }|||<    |
t'          |                    }|vrd|<                       ||	|f           ||                    d          rdndz   dz   }|vrt	          d|z            |S )N)z./z../r=   r   r'   extr(   c                d    t          t          g|                     d          R  |z             S )Nr   )r   r   r[   )r   r%  r#  s     r-   mkpathz7_bundle_models.<locals>.resolve_modules.<locals>.mkpath  s2    "4#Av||C/@/@#A#A#AC#GHHHr/   zno such module: %sr  rw   )cssr{   Tr   index)r=   )r   r'   r%  r'   r(   r'   )r]   r   r   r   r   r   r    rT   rU   rV   _style_templaterB   r_   r   depsr   r   r   r   r   append)r!  r#  resolvedr   r'  ry   r%  r   r  rT   r+  sigdeps_mapr)  extra_modulesknown_modulesr   resolve_depss    `            r-   resolve_modulesz'_bundle_models.<locals>.resolve_modules  s(     &	F &	FF  // %FI I I I I I I ??4(( 
J!6&>>D!$<< J*+?&+HIIIJ  $ J J%vfc22!$<< "!E" ++?&+HIII~~)$)$)$)TTT9&&*Tdj6O6O-P-P-PPDDD#=D#=DnT[[%9%9::DDFF#& '<gdmm<<m++)-M#&NNCx#8999(<(<"E""#FP--&';f'DEEE . r/   r+  r   c                    d                                  D             }t          |           z
  |z
  } ||          S )Nc                    h | ]	}|j         
S r+   )r   r   s     r-   r   z7_bundle_models.<locals>.resolve_deps.<locals>.<setcomp>5  s    KKK5%,KKKr/   )r   set)r+  r#  custom_modulesmissingr   r1  r3  s       r-   r2  z$_bundle_models.<locals>.resolve_deps4  sI    KKM4H4H4J4JKKKd))m+n<w---r/   c                    | d         S )Nr   r+   specs    r-   r  z _bundle_models.<locals>.<lambda>A  
    tAw r/   r  c                    | d         S r  r+   r:  s    r-   r  z _bundle_models.<locals>.<lambda>B  r<  r/   zrequire("%s")zrequire('%s')z,
c              3  N   K   | ] \  }}t           t          ||           z  V  !dS ))r   r   N)_export_templaterB   )r   r   r   s      r-   r   z!_bundle_models.<locals>.<genexpr>M  s<      ppVdW[]c 04T&3Q3Q3Q Qppppppr/   c              3  N   K   | ] \  }}t           t          ||           z  V  !dS ))r   sourceN)_module_templaterB   )r   r   rT   s      r-   r   z!_bundle_models.<locals>.<genexpr>N  s<      wwXfY_ae 04vd3S3S3S Swwwwwwr/   )preludeexportsr   )content)r!  r"  r#  r'   r(   r   )r+  r   r#  r'   r(   r   )r	   rY   r6  rglobr'   relative_tor   r   r   ry   sepaddr  r   r   r+  r,  r   r   rT   r   r\   r  r   _plugin_templaterB   _plugin_prelude_plugin_umd)r   rD  lib_dirry   r   r  r   r  r/  bare_modulesrd   r   rT   r+  r   refrH  rendered_exportsrendered_modulesrE  r0  r1  r   r2  r3  s   `                   @@@@@r-   r   r     s   GG;$&.G!eeMh''    ))**::g 	#2#AIIbgk3''!"=11LM* * * * * * * * *X. . . . . . . .
 %%'' @ @0<uz::
EL1222hmX>???? W"6"6777GW"6"6777GL#,W#5#5 , ,FD$ 	W 	WID#<< 3d :<ORU<UVVD<< 3d :<ORU<UVVDDVTN++++
CxxpphopppppxxwwjvwwwwwoGWaq!r!r!rrGg.....r/   r   )rS   N)rT   r'   rU   r'   rV   rM   r(   r   )r(   r   )r   r   r(   r7   )r   r   r(   r'   )r   r   r(   rM   rJ   )r   r'   r(   r'   r*   )r   r'   r   r   r   r   r(   r'   )r   r   r   r   r(   r'   )r   r  r(   rM   )r   r   r   r   r(   r  )r   r   r(   r  )r   r   r(   r	  )Pr3   
__future__r   logging	getLoggerr0   logr   r_   r   r   r   os.pathr   r   r   r   r   pathlibr	   
subprocessr
   r   typingr   r   r   core.has_propsr   r   stringsr   __all__rB   r'   r   r   r   
bokehjsdirrY   r   r!   r"   r    r   r   r$   r   r   r   r   r   r   r#   r   r   ri   r   r   rL  rK  rJ  r*  r?  rB  r   r   r   r   r   r   r   rP   rR   rO   r  r   r   r  r   r+   r/   r-   <module>r^     s     # " " " " " g!!   				 				 



                    " " " " " " " " * * * * * * * * * * & % % % % %            0    tCH~   ( ( ( ( (| ( ( ($ "h!## ! ! ! !                $ $ $ $ $ $ $ $    ^                      6   ; ; ; ; ;~ ; ; ;2 ':3 :3 :3 :3 :3 :3 :3 :3x# # # #
# # # #5 5 5 5 !# " " " "   "        " 1  H ] ] ] ]6 	      0 0 0 05 5 5 5 5- - - - -, , , , ,       - 4 4 4 4   2\/ \/ \/ \/ \/ \/r/   