o
    tf6                     @  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	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZ d	d
lmZ d	dlmZmZmZmZ G dd deZG dd deZG dd deZdS )zThe extension manager.    )annotationsN)starmap)multi)	AnyBoolDict	HasTraitsInstanceListUnicodedefaultobservevalidate)LoggingConfigurable   )ExtensionConfigManager)ExtensionMetadataErrorExtensionModuleNotFound
get_loaderget_metadatac                   @  s   e Zd ZdZedZedddZe Z	e
d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d Zdd Zdd Zdd Zdd ZdS )ExtensionPointzA simple API for connecting to a Jupyter Server extension
    point defined by metadata and importable from a Python package.
    FNT
allow_nonemetadatac                 C  s   |d }z|d | _ W n ty   d}t|dw z	t| j | _W n ty6   d| j  d}t|dw d|v rA|d  | _|S )zValidate metadata.valuemodulez<There is no 'module' key in the extension's metadata packet.NzThe submodule 'z>' could not be found. Are you sure the extension is installed?app)	_module_nameKeyErrorr   	importlibimport_module_moduleImportErrorr   _app)selfZproposedr   msg r'   i/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/jupyter_server/extension/manager.py_valid_metadata   s"   

zExtensionPoint._valid_metadatac                 C  s   | j r| j jS | jS )zHas this extension point been linked to the server.

        Will pull from ExtensionApp's trait, if this point
        is an instance of ExtensionApp.
        )r   _linkedr%   r'   r'   r(   linked3   s   zExtensionPoint.linkedc                 C     | j S )z'If the metadata includes an `app` field)r$   r+   r'   r'   r(   r   >      zExtensionPoint.appc                 C  s   | j r| j  S i S )z:Return any configuration provided by this extension point.)r   Z_jupyter_server_configr+   r'   r'   r(   configC   s   
zExtensionPoint.configc                 C  r-   )zuName of the Python package module where the extension's
        _load_jupyter_server_extension can be found.
        )r   r+   r'   r'   r(   module_nameM   s   zExtensionPoint.module_namec                 C  s   | j r| j jS | jd| jS )zName of the extension.

        If it's not provided in the metadata, `name` is set
        to the extensions' module name.
        name)r   r1   r   getr0   r+   r'   r'   r(   r1   T   s   zExtensionPoint.namec                 C  r-   )z3The imported module (using importlib.import_module))r"   r+   r'   r'   r(   r   _   r.   zExtensionPoint.modulec                 C  s(   | j r	| j j}|S t| jddd }|S )zGet a linker._link_jupyter_server_extensionc                 S  s   d S Nr'   )	serverappr'   r'   r(   <lambda>n   s    z,ExtensionPoint._get_linker.<locals>.<lambda>)r   r3   getattrr   )r%   linkerr'   r'   r(   _get_linkerd   s   	zExtensionPoint._get_linkerc                 C  s   | j }|s| j}t|}|S )zGet a loader.)r   r   r   )r%   locloaderr'   r'   r(   _get_loaderr   s
   zExtensionPoint._get_loaderc                 C  s,   z|    |   W dS  ty   Y dS w )z+Check that both a linker and loader exists.FT)r9   r<   	Exceptionr+   r'   r'   r(   r   z   s   
zExtensionPoint.validatec                 C  s$   | j s|  }|| d| _dS dS )zLink the extension to a Jupyter ServerApp object.

        This looks for a `_link_jupyter_server_extension` function
        in the extension's module or ExtensionApp class.
        TN)r,   r9   r*   )r%   r5   r8   r'   r'   r(   link   s
   
zExtensionPoint.linkc                 C  s   |   }||S )zLoad the extension in a Jupyter ServerApp object.

        This looks for a `_load_jupyter_server_extension` function
        in the extension's module or ExtensionApp class.
        )r<   )r%   r5   r;   r'   r'   r(   load   s   zExtensionPoint.load)__name__
__module____qualname____doc__r   r*   r   r$   r   r   validate_traitr)   propertyr,   r   r/   r0   r1   r   r9   r<   r   r>   r?   r'   r'   r'   r(   r      s0    




	




r   c                      s   e Zd ZdZeddZedddZe Z	e Z
edddZee d	dZed
dZeddd Z fddZdd Zdd Zdd Zdd Zdd Zdd Z  ZS )ExtensionPackagezAn API for interfacing with a Jupyter Server extension package.

    Usage:

    ext_name = "my_extensions"
    extpkg = ExtensionPackage(name=ext_name)
    z)Name of the an importable Python package.helpFz)Whether the extension package is enabled.Tz:The module for this extension package. None if not enabled)r   rH   z5Extension metadata loaded from the extension package.z|
            The version of this extension package, if it can be found.
            Otherwise, an empty string.
            versionc                 C  s   | j sdS t| jddS )N __version__)enabledr7   r   r+   r'   r'   r(   _load_version   s   zExtensionPackage._load_versionc                   s(   t  jdi | | jr|   dS dS )z Initialize an extension package.Nr'   )super__init__rL   _load_metadata)r%   kwargs	__class__r'   r(   rO      s   zExtensionPackage.__init__c              
   C  s|   | j }zt|| jd\| _| _W n ty* } zd| d| d}t|dd}~ww | jD ]}t|d}|| j|j < q.|S )z\Import package and load metadata

        Only used if extension package is enabled
        )loggerzThe module 'z' could not be found (z+). Are you sure the extension is installed?N)r   )	r1   r   logr   r   r#   r   r   extension_points)r%   r1   er&   mpointr'   r'   r(   rP      s   


zExtensionPackage._load_metadatac                 C  s   t dd | j D S )z.Validate all extension points in this package.c                 s  s    | ]}|  V  qd S r4   r   ).0	extensionr'   r'   r(   	<genexpr>   s    z,ExtensionPackage.validate.<locals>.<genexpr>)allrV   valuesr+   r'   r'   r(   r      s   zExtensionPackage.validatec                 C  s.   | j |d}|s| j| }|| dS dS )zLink an extension point.FN)_linked_pointsr2   rV   r>   )r%   
point_namer5   r,   rY   r'   r'   r(   
link_point   s
   
zExtensionPackage.link_pointc                 C  s   | j | }||S )zLoad an extension point.)rV   r?   )r%   r`   r5   rY   r'   r'   r(   
load_point   s   

zExtensionPackage.load_pointc                 C  s   | j D ]}| || qdS )zLink all extension points.N)rV   ra   )r%   r5   r`   r'   r'   r(   link_all_points   s   
z ExtensionPackage.link_all_pointsc                   s    fdd j D S )zLoad all extension points.c                   s   g | ]}  |qS r'   )rb   )rZ   r`   r%   r5   r'   r(   
<listcomp>       z4ExtensionPackage.load_all_points.<locals>.<listcomp>)rV   rd   r'   rd   r(   load_all_points   s   z ExtensionPackage.load_all_points)r@   rA   rB   rC   r   r1   r   rL   r   r_   rV   r   r   r
   r   rI   r   rM   rO   rP   r   ra   rb   rc   rg   __classcell__r'   r'   rR   r(   rF      s(    

rF   c                   @  s   e Zd ZdZeeddZe Ze	ddd Z
eddd Zed	d
Zedd Zedd
Zedd Zedd Zdd Zdd Zdd Zd*ddZdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )+ExtensionManagerzHigh level interface for findind, validating,
    linking, loading, and managing Jupyter Server extensions.

    Usage:
    m = ExtensionManager(config_manager=...)
    Tr   config_managerc                 C  s   t  }| | |S r4   )r   _load_config_managerr%   rj   r'   r'   r(   _load_default_config_manager   s   
z-ExtensionManager._load_default_config_managerc                 C  s   |j r| |j  d S d S r4   )newrk   )r%   Zchanger'   r'   r(   _config_manager_changed   s   z(ExtensionManager._config_manager_changedzq
        Dictionary with extension package names as keys
        and ExtensionPackage objects as values.
        rG   c                 C  s   t t| j S )z8Returns an extensions dictionary, sorted alphabetically.)dictsorted
extensionsitemsr+   r'   r'   r(   sorted_extensions  s   z"ExtensionManager.sorted_extensionsz|
        Dictionary with extension names as keys

        values are True if the extension is linked, False if not.
        c                 C     dd | j  D S )zCReturn mapping of extension names and sets of ExtensionApp objects.c                 S  s&   i | ]\}}|d d |j  D qS )c                 S  s   h | ]}|j r|j qS r'   )r   )rZ   rY   r'   r'   r(   	<setcomp>"  rf   z=ExtensionManager.extension_apps.<locals>.<dictcomp>.<setcomp>)rV   r^   )rZ   r1   r[   r'   r'   r(   
<dictcomp>!  s    z3ExtensionManager.extension_apps.<locals>.<dictcomp>)rr   rs   r+   r'   r'   r(   extension_apps     zExtensionManager.extension_appsc                 C  ru   )zCReturn mapping of extension point names and ExtensionPoint objects.c                 S  s&   i | ]}|j  D ]\}}||q	qS r'   )rV   rs   )rZ   r   r1   rY   r'   r'   r(   rw   )  s    z5ExtensionManager.extension_points.<locals>.<dictcomp>)rr   r^   r+   r'   r'   r(   rV   &  ry   z!ExtensionManager.extension_pointsc                 C  s
   || _ dS )z1Add extensions found by an ExtensionConfigManagerN)rj   rl   r'   r'   r(   from_config_manager/  s   
z$ExtensionManager.from_config_managerc                 C  s   |  }| | dS )z Actually load our config managerN)Zget_jpserver_extensionsfrom_jpserver_extensions)r%   rj   jpserver_extensionsr'   r'   r(   rk   4  s   z%ExtensionManager._load_config_managerc                 C  s$   |  D ]\}}| j||d qdS )z:Add extensions from 'jpserver_extensions'-like dictionary.)rL   N)rs   add_extension)r%   r|   r1   rL   r'   r'   r(   r{   9  s   z)ExtensionManager.from_jpserver_extensionsFc              
   C  sj   zt ||d}|| j|< W dS  ty4 } z| jr| jjr | jjd|||dd W Y d}~dS d}~ww )zeTry to add extension to manager, return True if successful.
        Otherwise, return False.
        )r1   rL   Tz-%s | error adding extension (enabled: %s): %sexc_infoNF)rF   rr   r=   r5   !reraise_server_extension_failuresrU   warning)r%   Zextension_namerL   ZextpkgrW   r'   r'   r(   r}   >  s"   
zExtensionManager.add_extensionc              
   C  s   | j |d}| j| }|sL|jrNz|| j d| j |< | jd| W dS  tyK } z| jr6| jj	r6 | jj
d||dd W Y d}~dS d}~ww dS dS )zLink an extension by name.FTz'%s | extension was successfully linked.z %s | error linking extension: %sr~   N)linked_extensionsr2   rr   rL   rc   r5   rU   infor=   r   r   )r%   r1   r,   r[   rW   r'   r'   r(   link_extensionS  s   


"zExtensionManager.link_extensionc              
   C  s   | j |}|rB|jrDz|| j W n% ty8 } z| jr#| jjr# | jjd||dd W Y d}~dS d}~ww | j	d| dS dS dS )zLoad an extension by name.z.%s | extension failed loading with message: %rTr~   Nz'%s | extension was successfully loaded.)
rr   r2   rL   rg   r5   r=   r   rU   r   r   )r%   r1   r[   rW   r'   r'   r(   load_extensionb  s   
zExtensionManager.load_extensionc                   sB   |D ]}| j d||j | I dH  | j d||j qdS )z.Call the shutdown hooks in the specified apps.z%s | extension app %r stoppingNz%s | extension app %r stopped)rU   debugr1   stop_extension)r%   r1   appsr   r'   r'   r(   r   r  s   zExtensionManager.stop_extensionc                 C     | j D ]}| | qdS )zHLink all enabled extensions
        to an instance of ServerApp
        N)rt   r   r%   r1   r'   r'   r(   link_all_extensionsy     
z$ExtensionManager.link_all_extensionsc                 C  r   )zULoad all enabled extensions and append them to
        the parent ServerApp.
        N)rt   r   r   r'   r'   r(   load_all_extensions  r   z$ExtensionManager.load_all_extensionsc                   s.   t tt| jtt| j I dH  dS )z*Call the shutdown hooks in all extensions.N)r   listr   r   rq   rp   rx   rs   r+   r'   r'   r(   stop_all_extensions  s   ,z$ExtensionManager.stop_all_extensionsc                 C  s:   t t| j D ]\}}|D ]
}| r  dS qq	dS )zMCheck for any activity currently happening across all extension applications.TN)rq   rp   rx   rs   Zcurrent_activity)r%   _r   r   r'   r'   r(   any_activity  s   zExtensionManager.any_activityN)F)r@   rA   rB   rC   r	   r   rj   r   r5   r   rm   r   ro   r   rr   rE   rt   r   rx   rV   rz   rk   r{   r}   r   r   r   r   r   r   r   r'   r'   r'   r(   ri      s>    





		ri   ) rC   
__future__r   r    	itertoolsr   Ztornado.genr   Z	traitletsr   r   r   r   r	   r
   r   r   r   r   rD   Ztraitlets.configr   r/   r   utilsr   r   r   r   r   rF   ri   r'   r'   r'   r(   <module>   s    , 
Q