o
    tf7                     @  s<  d Z ddlm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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$d4ddZ%d5ddZ&				d6d7d!d"Z'd8d%d&Z(	d9d:d'd(Z)d;d<d+d,Z*G d-d. d.eZ+d=d/d0Z,d>d?d2d3Z-dS )@zJupyterLab Server config    )annotationsN)iglob)chain)Logger)join)Any)SYSTEM_CONFIG_PATHjupyter_config_dirjupyter_path)ConfigManagerrecursive_update)url_path_join)Bool	HasTraitsListUnicodedefaultZ	templatesdatadict[str, Any]returnstrc                 C  sF   d| v r
| d }|S d| v rt | d tr| d dd}|S d}|S )z#Get the url from the extension dataZhomepage
repositoryurl )
isinstancedictget)r   r    r   a/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/jupyterlab_server/config.pyget_package_url   s   r   labextensions_path	list[str]c                 C  sZ  i }| D ]}t tt|ddtt|dddD ]}t|dd}t|}W d   n1 s0w   Y  |d |vrt|d |d	 |d
dt||t	
|d|dt |dt d	}d|v rxd|di v rxt|ddd|d< t	t	
|d}t	|rt|dd}t||d< W d   n1 sw   Y  |||d < qq|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+   r   r   )r   zinstall.jsoninstall)r   r   pjoinopenjsonloadr   r   r   ospdirnamer   exists)r    federated_extensionsr,   r-   fidZpkgdatar   Zinstall_pathr   r   r   get_federated_extensions(   s@   

r9   allFapp_settings_dir
str | NoneloggerLogger | Nonelevelinclude_higher_levelsboolc                 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   )r;   r=   r?   r@   cmr   r   r   get_static_page_configL   s   

rE   pathr   c                 C  s\   t | dd}| drt|W  d   S t|W  d   S 1 s'w   Y  dS )zLoad 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)r1   endswithjson5r3   r2   )rF   r8   r   r   r   load_config_   s   
$rI   c                   s  i }d}|r@t |dt |dg}|D ]-}t|r?t|r?t|}|dfD ]}||v r7dd || D ||< q&t||  nqt|dd}	t||	 i }
g  }|d	< t| }||i  |	 D ]c\}}d
|d vrv|ru|
d|d  qa|d d
 }|d |d d}d|v r|d |d< d|v r|d |d< d|v r|d |d< |d|d< || |d }|d |r|d | |
|d < qa|rt|}t |dd}t|rt|dd}t|}W d   n1 sw   Y  |d di }|	 D ]\}}||
v rq ||r|| |
|< q i }t|
D ]}|| |du r-q|
| }|D ]}d||< q3q|}|||g  |||< |	 D ]\} t tre fdd D ||< qP|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>   s    z#get_page_config.<locals>.<dictcomp>r:   )r=   r?   r7   Z_buildr+   z%s is not a valid extensionr'   r3   )r'   r3   	extensionZmimeExtensionstyleentrypointsZstaticr"   r$   r%   NZextensionMetadataTc                   s   g | ]} | r|qS r   r   )rJ   subkeyvaluer   r   
<listcomp>   s    z#get_page_config.<locals>.<listcomp>)r0   r4   r6   getsizerI   r   rE   r9   
setdefaultitemswarningr   appendr5   r1   r2   r3   sortedupdater   r   )r    r;   r=   rB   Zdisabled_keyZconfig_pathsrF   r   rK   Zstatic_page_configZdisabled_by_extensions_all
extensionsZfederated_exts_Zext_dataZextbuildrM   r'   Zapp_dirZpackage_data_filer8   Zapp_dataZall_ext_dataextZdisabled_by_extensionsZdisabled_listitemZrollup_disabledr   rQ   r   get_page_configr   s   




r_   rB   Nonec                 C  s   t |}|d|  dS )zWrite page config to diskrB   N)rC   set)rB   r?   rD   r   r   r   write_page_config   s   rb   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 dEd#d$Z!e d%dEd&d'Z"e d(dFd*d+Z#e d,dEd-d.Z$e d/dEd0d1Z%e d2dEd3d4Z&e d5dEd6d7Z'e d8dEd9d:Z(e d;dEd<d=Z)e d>dEd?d@Z*e dAdEdBdCZ+dDS )G	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_dirr   r   c                 C  s   t S )N)DEFAULT_TEMPLATE_PATHselfr   r   r   _default_template_dir=  s   zLabConfig._default_template_dirlabextensions_urlc                 C     t | jdS )Nzextensions/ujoinapp_urlrg   r   r   r   _default_labextensions_urlA     z$LabConfig._default_labextensions_urlr    r!   c                 C  s   t dS )NZlabextensions)r
   rg   r   r   r   _default_labextensions_pathE  s   z%LabConfig._default_labextensions_pathZworkspaces_urlc                 C  rk   )Nworkspaces/rl   rg   r   r   r   _default_workspaces_urlI  rp   z!LabConfig._default_workspaces_urlworkspaces_api_urlc                 C     t | jddS )Napirr   rl   rg   r   r   r   _default_workspaces_api_urlM     z%LabConfig._default_workspaces_api_urlsettings_urlc                 C  ru   )Nrv   z	settings/rl   rg   r   r   r   _default_settings_urlQ  rx   zLabConfig._default_settings_urllistings_urlc                 C  ru   )Nrv   z	listings/rl   rg   r   r   r   _default_listings_urlU  rx   zLabConfig._default_listings_url
themes_urlc                 C  ru   )Nrv   zthemes/rl   rg   r   r   r   _default_themes_urlY  rx   zLabConfig._default_themes_urllicenses_urlc                 C  ru   )Nrv   z	licenses/rl   rg   r   r   r   _default_licenses_url]  rx   zLabConfig._default_licenses_urltree_urlc                 C  rk   )Nztree/rl   rg   r   r   r   _default_tree_urla  rp   zLabConfig._default_tree_urltranslations_api_urlc                 C  ru   )Nrv   ztranslations/rl   rg   r   r   r   _default_translations_api_urle  rx   z'LabConfig._default_translations_api_urlN)r   r   r   r!   ),__name__
__module____qualname____doc__r   tagZapp_nameZapp_versionZapp_namespacern   r;   r   Zextra_labextensions_pathr    Ztemplates_dirZ
static_dirrj   ry   Zuser_settings_dirZschemas_dirrt   Zworkspaces_dirr{   r}   r   Z
themes_dirr   r   r   Zcache_filesZnotebook_starts_kernelZcopy_absolute_pathr   ri   ro   rq   rs   rw   rz   r|   r~   r   r   r   r   r   r   r   rc      s    			

	rc   c                   C  s   g dS )z9
    Returns the levels where configs can be stored.
    )r:   user
sys_prefixsystemapprM   r   r   r   r   r   get_allowed_levelsj  s   r   r   c                 C  s   ddl m} t }| |vrd| }t|d}| dkr!t|dS g t|d gt gg d}|r9||| d n| g}g d}}	|D ]$}
||
 D ]}|t	
|| qI|	du rg||
 rgt	
||
 d |}	qCt||	d	S )
zgGet the location of config files for the current context
    Returns the string to the environment
    r   )ENV_CONFIG_PATHz"Page config level must be one of: Z	labconfigr:   )config_dir_name)r   r   r   r   rM   N)Zread_config_pathwrite_config_dir)jupyter_core.pathsr   r   
ValueErrorr   r   r	   indexrX   r4   r   )r?   r@   r   allowedmsgZconfig_namepathsZlevelsZread_config_pathsr   _levelpr   r   r   rC   q  s.   


rC   )r   r   r   r   )r    r!   r   r   )NNr:   F)
r;   r<   r=   r>   r?   r   r@   rA   r   r   )rF   r   r   r   )NN)r    r!   r;   r<   r=   r>   r   r   )r:   )rB   r   r?   r   r   r`   r   )F)r?   r   r@   rA   r   r   ).r   
__future__r   r2   Zos.pathrF   r4   globr   	itertoolsr   loggingr   r   r0   typingr   rH   r   r   r	   r
   Z&jupyter_server.services.config.managerr   r   Zjupyter_server.utilsr   rm   Z	traitletsr   r   r   r   r   r5   __file__rf   r   r9   rE   rI   r_   rb   rc   r   rC   r   r   r   r   <module>   s<    

%
d 
