a
    ø£Be2  ã                   @   sô   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z
 ddlZddlmZmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZmZ e 	e e¡d
¡Zdd„ Zdd„ Z ddd„Z!dd„ Z"ddd„Z#ddd„Z$G dd„ deƒZ%dd„ Z&dS )zJupyterLab Server configé    N)Úiglob)Úchain)Újoin)ÚSYSTEM_CONFIG_PATHÚjupyter_config_dirÚjupyter_path)ÚConfigManagerÚrecursive_update)Úurl_path_join)ÚBoolÚ	HasTraitsÚListÚUnicodeÚdefaultZ	templatesc                 C   sB   d| v r| d }n,d| v r:t | d tƒr:| d  dd¡}nd}|S )z#Get the url from the extension dataZhomepageÚ
repositoryÚurlÚ )Ú
isinstanceÚdictÚget)Údatar   © r   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyterlab_server/config.pyÚget_package_url   s    
r   c                 C   s.  i }| D �]}t tt|ddƒƒtt|dddƒƒƒD ]ô}t|dd��}t |¡}W d  ƒ n1 sb0    Y  |d |vr2t|d |d	 | d
d¡t|ƒ|t	 
|¡d| dtƒ ¡| dtƒ ¡d�	}t	 t	 
|¡d¡}t	 |¡�rt|dd��}t |¡|d< W d  ƒ n1 �s0    Y  |||d < q2q|S )z+Get the metadata about federated extensionsz[!@]*úpackage.jsonz@*Ú*úutf-8©ÚencodingNÚnameÚversionÚdescriptionr   FÚdependenciesÚ
jupyterlab)	r   r    r!   r   Úext_dirÚext_pathÚis_localr"   r#   zinstall.jsonÚinstall)r   r   ÚpjoinÚopenÚjsonÚloadr   r   r   ÚospÚdirnamer   Úexists)Úlabextensions_pathÚfederated_extensionsr$   r%   ÚfidZpkgdatar   Zinstall_pathr   r   r   Úget_federated_extensions%   s4    
þ(
÷.r2   Úallc                 C   s   t |ƒ}| d¡S )a  Get the static page config for JupyterLab

    Parameters
    ----------
    logger: logger, optional
        An optional logging object
    level: string, optional ['all']
        The level at which to get config: can be 'all', 'user', 'sys_prefix', or 'system'
    Úpage_config)Ú_get_config_managerr   )Úapp_settings_dirÚloggerÚlevelÚcmr   r   r   Úget_static_page_configD   s    
r:   c                 C   sj   t | dd��J}|  d¡r0t |¡W  d  ƒ S t |¡W  d  ƒ S W d  ƒ n1 s\0    Y  dS )zØLoad either a json5 or a json config file.

    Parameters
    ----------
    path : str
        Path to the file to be loaded

    Returns
    -------
    Dict[Any, Any]
        Dictionary of json or json5 data
    r   r   z.json5N)r)   ÚendswithÚjson5r+   r*   )Úpathr1   r   r   r   Úload_configR   s    
r>   c                    sÀ  i }d}|rvt |dƒt |dƒg}|D ]P}t |¡r$t|ƒ}|dfD ]"}||v rBdd„ || D ƒ||< qBt||ƒ  qvq$t|dd�}	t||	ƒ i }
g  }|d	< t| ƒ}| |i ¡ | ¡ D ]¼\}}d
|d vrà| 	d|d  ¡ q¸|d d
 }|d |d dœ}d|v �r|d |d< d|v �r*|d |d< d|v �r@|d |d< | 
|¡ |d }|d  |¡r¸|d | |
|d < q¸|�r t |¡}t |ddƒ}t |¡�r t|dd��}t |¡}W d  ƒ n1 �sÌ0    Y  |d  di ¡}| ¡ D ]0\}}||
v �r�qî| |¡�rî|| |
|< �qîi }t|
ƒD ]:}||  |¡du �rH�q,|
| }|D ]}d||< �qT�q,|}| | |g ¡¡ |||< | ¡ D ],\}‰ tˆ tƒ�rŽ‡ fdd„ˆ D ƒ||< �qŽ|S )z/Get the page config for the application handlerZdisabledExtensionszpage_config.json5zpage_config.jsonZdeferredExtensionsc                 S   s   i | ]
}|d “qS )Tr   )Ú.0Úkeyr   r   r   Ú
<dictcomp>y   ó    z#get_page_config.<locals>.<dictcomp>r3   )r7   r8   r0   Z_buildr#   z%s is not a valid extensionr   r+   )r   r+   Ú	extensionZmimeExtensionÚstyleZstaticr   r   r   NZextensionMetadataTc                    s   g | ]}ˆ | r|‘qS r   r   )r?   Úsubkey©Úvaluer   r   Ú
<listcomp>À   rB   z#get_page_config.<locals>.<listcomp>)r(   r,   r.   r>   r	   r:   r2   Ú
setdefaultÚitemsÚwarningÚappendr   r-   r)   r*   r+   ÚsortedÚupdater   r   )r/   r6   r7   r4   Zdisabled_keyZconfig_pathsr=   r   r@   Zstatic_page_configZdisabled_by_extensions_allÚ
extensionsZfederated_extsÚ_Zext_dataZextbuildrC   r   Zapp_dirZpackage_data_filer1   Zapp_dataZall_ext_dataÚextZdisabled_by_extensionsZdisabled_listÚitemZrollup_disabledr   rF   r   Úget_page_configf   s|    þ







*
rS   c                 C   s   t |ƒ}| d| ¡ dS )zWrite page config to diskr4   N)r5   Úset)r4   r8   r9   r   r   r   Úwrite_page_configÅ   s    rU   c                   @   sŽ  e Zd ZdZeddd�jdd�Zeddd�jdd�Zeddd�jdd�Zed	d
d�jdd�Z	eddd�jdd�Z
eeƒ dd�jdd�Zeeƒ dd�jdd�Zeddd�jdd�Zeddd�jdd�Zeddd�jdd�Zedd�jdd�Zeddd�jdd�Zeddd�jdd�Zedd�jdd�Zeddd�jdd�Zedd�jdd�Zedd�jdd�Zedd�Zeddd�jdd�Zedd�jdd�Zedd�jdd�Zeddd�jdd�Zeddd�jdd�Zeddd�jdd�Ze d ƒd!d"„ ƒZ!e d#ƒd$d%„ ƒZ"e d&ƒd'd(„ ƒZ#e d)ƒd*d+„ ƒZ$e d,ƒd-d.„ ƒZ%e d/ƒd0d1„ ƒZ&e d2ƒd3d4„ ƒZ'e d5ƒd6d7„ ƒZ(e d8ƒd9d:„ ƒZ)e d;ƒd<d=„ ƒZ*e d>ƒd?d@„ ƒZ+dAS )BÚ	LabConfigz)The lab application configuration object.r   zThe name of the application.)ÚhelpT)ÚconfigzThe version of the application.z!The namespace of the application.z/labz!The url path for the application.z#The application settings directory.z7Extra paths to look for federated JupyterLab extensionszAThe standard paths to look in for federated JupyterLab extensionsz$The application templates directory.z[The optional location of local static files. If given, a static file handler will be added.z+The url for federated JupyterLab extensionsz%The url path of the settings handler.z5The optional location of the user settings directory.zhThe optional location of the settings schemas directory. If given, a handler will be added for settings.z#The url path of the workspaces API.zjThe optional location of the saved workspaces directory. If given, a handler will be added for workspaces.zThe listings url.zThe theme url.zThe third-party licenses url.z\The optional location of the themes directory. If given, a handler will be added for themes.z)The url path of the translations handler.z!The url path of the tree handler.zOWhether to cache files on the server. This should be `True` except in dev mode.z7Whether a notebook should start a kernel automatically.FzOWhether getting a relative (False) or absolute (True) path when copying a path.Ztemplate_dirc                 C   s   t S )N)ÚDEFAULT_TEMPLATE_PATH©Úselfr   r   r   Ú_default_template_dir,  s    zLabConfig._default_template_dirÚlabextensions_urlc                 C   s   t | jdƒS )Nzextensions/©ÚujoinÚapp_urlrZ   r   r   r   Ú_default_labextensions_url0  s    z$LabConfig._default_labextensions_urlr/   c                 C   s   t dƒS )NZlabextensions)r   rZ   r   r   r   Ú_default_labextensions_path4  s    z%LabConfig._default_labextensions_pathZworkspaces_urlc                 C   s   t | jdƒS )Núworkspaces/r^   rZ   r   r   r   Ú_default_workspaces_url8  s    z!LabConfig._default_workspaces_urlÚworkspaces_api_urlc                 C   s   t | jddƒS )NÚapirc   r^   rZ   r   r   r   Ú_default_workspaces_api_url<  s    z%LabConfig._default_workspaces_api_urlÚsettings_urlc                 C   s   t | jddƒS )Nrf   z	settings/r^   rZ   r   r   r   Ú_default_settings_url@  s    zLabConfig._default_settings_urlÚlistings_urlc                 C   s   t | jddƒS )Nrf   z	listings/r^   rZ   r   r   r   Ú_default_listings_urlD  s    zLabConfig._default_listings_urlÚ
themes_urlc                 C   s   t | jddƒS )Nrf   zthemes/r^   rZ   r   r   r   Ú_default_themes_urlH  s    zLabConfig._default_themes_urlÚlicenses_urlc                 C   s   t | jddƒS )Nrf   z	licenses/r^   rZ   r   r   r   Ú_default_licenses_urlL  s    zLabConfig._default_licenses_urlÚtree_urlc                 C   s   t | jdƒS )Nztree/r^   rZ   r   r   r   Ú_default_tree_urlP  s    zLabConfig._default_tree_urlÚtranslations_api_urlc                 C   s   t | jddƒS )Nrf   ztranslations/r^   rZ   r   r   r   Ú_default_translations_api_urlT  s    z'LabConfig._default_translations_api_urlN),Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚtagZapp_nameZapp_versionZapp_namespacer`   r6   r   Zextra_labextensions_pathr/   Ztemplates_dirZ
static_dirr]   rh   Zuser_settings_dirZschemas_dirre   Zworkspaces_dirrj   rl   rn   Z
themes_dirrr   rp   r   Zcache_filesZnotebook_starts_kernelZcopy_absolute_pathr   r\   ra   rb   rd   rg   ri   rk   rm   ro   rq   rs   r   r   r   r   rV   Ë   sÂ   ÿþÿþýù	ÿÿþýù	ýù	
ýù	
ÿþýÿþþý









rV   c                 C   sˆ   g d¢}| |vr"d|› �}t |ƒ‚d}| dkr8t|d�S | dkrHtƒ }n&| dkrfdd	lm} |d }ntd }t ||¡}t|g|d
�S )zgGet the location of config files for the current context
    Returns the string to the environment
    )r3   ÚuserÚ
sys_prefixÚsystemZapprC   z"Page config level must be one of: Z	labconfigr3   )Úconfig_dir_namery   rz   r   )ÚENV_CONFIG_PATH)Zread_config_pathZwrite_config_dir)Ú
ValueErrorr   r   Újupyter_core.pathsr}   r   r,   r   )r8   ÚallowedÚmsgZconfig_nameZ
config_dirr}   Zfull_config_pathr   r   r   r5   Y  s    


r5   )NNr3   )NN)r3   )'rw   r*   Úos.pathr=   r,   Úglobr   Ú	itertoolsr   r   r(   r<   r   r   r   r   Z&jupyter_server.services.config.managerr   r	   Zjupyter_server.utilsr
   r_   Z	traitletsr   r   r   r   r   r-   Ú__file__rY   r   r2   r:   r>   rS   rU   rV   r5   r   r   r   r   Ú<module>   s(   

_
 