a
    ø£BeE  ã                   @   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   úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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|ƒsTtˆ ƒtkrht‡‡fdd„ƒ}n&tˆ t	j
ƒrŠtˆ ƒ‡ 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.)ÚnameZcls1Zcls2c                    s
   t ˆˆ ƒS ©N)Úgetattr©Úself)r   Úobj2r   r   Ú<lambda>)   ó    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   Ú	overwriteZ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   r   z"public_members.<locals>.<listcomp>)ÚinspectÚ
getmembers)ÚobjÚmembersr   r   r   Úpublic_members7   s    
r/   c                 C   s   t | ƒ}t |ƒ}t|ƒ |¡S )z5Return all attribute names found in obj2 but not obj1)r/   ÚsetÚ
difference)r#   r   Úm1Úm2r   r   r   Údiff_members<   s    r4   c                 C   s,   t | d�}| d¡}| di ¡ di ¡}|S )N)Úread_config_pathZjupyter_notebook_configZ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	yb   t
ƒ }Y n0 t| |ƒ}|D ]}t| ||ƒ qrtƒ }	|	| jg }
t|
ƒ}| ¡ D ]@\}}||jvr¦|j||d�}|r¦| dj|d�¡ | |¡ q¦d 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>\   r   zM_link_jupyter_server_extension.<locals>.sorted_extensions.<locals>.<dictcomp>)Úsortedr;   ÚkeysÚremove)r   rA   r   r   r   Úsorted_extensionsT   s    

z9_link_jupyter_server_extension.<locals>.sorted_extensionsr=   )Úenabledz}{name} | extension was found and enabled by notebook_shim. Consider moving the extension to Jupyter Server's extension paths.)r   )Zextension_managerÚlogr   r   rC   r;   Z
link_pointZextension_pointsZappÚ	Exceptionr   r4   r%   r   Z
config_dirr<   ÚitemsÚadd_extensionÚinfor   Zlink_extension)Ú	serverappÚmanagerÚloggerrC   ÚpkgÚpointZnbappr.   r)   Zjupyter_pathsr8   r6   r   rD   Z
successfulr   r   r   Ú_link_jupyter_server_extensionJ   s6    





þÿrO   c                 C   s*   | j }|j}|dd„ tƒ D ƒ7 }||_d S )Nc                 S   s   g | ]}t j |d ¡‘qS )Znbconfig)ÚosÚpathÚjoin)r(   Úpr   r   r   r*   ‹   s   ÿz2_load_jupyter_server_extension.<locals>.<listcomp>)Úconfig_managerr5   r   )rJ   rT   r5   r   r   r   Ú_load_jupyter_server_extension†   s    ÿrU   )F)Ú__doc__rP   r    r+   Ú	functoolsr   Zjupyter_core.pathsr   Ztraitlets.traitletsr   Z&jupyter_server.services.config.managerr   Ztraitsr   rF   r   r%   r/   r4   r<   rO   rU   r   r   r   r   Ú<module>   s   
<