o
    iE                     @   s   d Z ddlZddl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 G d	d
 d
eZdddZdd Zdd Zdd Zdd Zdd ZdS )ae  
This module contains a Jupyter Server extension that attempts to
make classic server and notebook extensions work in the new server.

Unfortunately, you'll notice that requires some major monkey-patching.
The goal is that this extension will only be used as a temporary
patch to transition extension authors from classic notebook server to jupyter_server.
    N)wraps)jupyter_config_path)is_trait)ConfigManager   )NotebookAppTraitsc                   @   s   e Zd ZdS )ClassProxyErrorN)__name__
__module____qualname__ r   r   Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook_shim/nbserver.pyr      s    r   Fc                    s   t | r|du rtdj| jjdt tj}t|s*t tkr4tfdd}nt t	j
rEt  fdd}n }t| j| dS )	zARedirects a method, property, or trait from object 1 to object 2.FzZCannot proxy the attribute '{name}' from {cls2} because {cls1} already has this attribute.)namecls1cls2c                    s
   t  S N)getattrself)r   obj2r   r   <lambda>)   s   
 zproxy.<locals>.<lambda>c                    s    |i |S r   r   )r   argskwargs)attrr   r   thing,   s   zproxy.<locals>.thingN)hasattrr   format	__class__r   r   typeproperty
isinstancetypes
MethodTyper   setattr)obj1r   r   	overwritecls_attrr   r   )r   r   r   r   proxy   s$   
r'   c                 C   s   t | }dd |D S )Nc                 S   s   g | ]\}}| d s|qS )_)
startswith).0mr(   r   r   r   
<listcomp>9   s    z"public_members.<locals>.<listcomp>)inspect
getmembers)objmembersr   r   r   public_members7   s   
r1   c                 C   s   t | }t |}t||S )z5Return all attribute names found in obj2 but not obj1)r1   set
difference)r$   r   m1m2r   r   r   diff_members<   s   r6   c                 C   s,   t | d}|d}|di di }|S )N)read_config_pathjupyter_notebook_configNotebookAppnbserver_extensions)r   get)config_dirscmsection
extensionsr   r   r   get_nbserver_extensionsC   s   

r@   c                 C   s   | j }| j}dd }t||j_z|jd }|d|  |jd }|j}W n t	y1   t
 }Y nw t| |}|D ]}t| || q9t }	|	| jg }
t|
}| D ] \}}||jvrs|j||d}|rs|dj|d || qSd S )Nc                    s4   t  j }|d dg| } fdd|D S )zkDictionary with extension package names as keys
        and an ExtensionPackage objects as values.
        notebook_shimc                    s   i | ]}| j | qS r   )r?   )r*   keyr   r   r   
<dictcomp>\   s    zM_link_jupyter_server_extension.<locals>.sorted_extensions.<locals>.<dictcomp>)sortedr?   keysremove)r   rE   r   r   r   sorted_extensionsT   s   

z9_link_jupyter_server_extension.<locals>.sorted_extensionsrA   )enabledz}{name} | extension was found and enabled by notebook_shim. Consider moving the extension to Jupyter Server's extension paths.)r   )extension_managerlogr   r   rG   r?   
link_pointextension_pointsapp	Exceptionr   r6   r'   r   
config_dirr@   itemsadd_extensioninfor   link_extension)	serverappmanagerloggerrG   pkgpointnbappr0   r+   jupyter_pathsr<   r:   r   rH   
successfulr   r   r   _link_jupyter_server_extensionJ   s<   







r\   c                 C   s*   | j }|j}|dd t D 7 }||_d S )Nc                 S   s   g | ]	}t j|d qS )nbconfig)ospathjoin)r*   pr   r   r   r,      s    z2_load_jupyter_server_extension.<locals>.<listcomp>)config_managerr7   r   )rT   rb   r7   r   r   r   _load_jupyter_server_extension   s   
rc   )F)__doc__r^   r!   r-   	functoolsr   jupyter_core.pathsr   traitlets.traitletsr   &jupyter_server.services.config.managerr   traitsr   rN   r   r'   r1   r6   r@   r\   rc   r   r   r   r   <module>   s     
<