a
    ø£BeBW  ã                   @   s  d Z ddlZddlZddlZddlZddlmZmZ ddl	m
Z
m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 ddlmZ dd	lmZ dd
lmZ ddlmZm Z  ddl!m"Z" dd„ Z#dd„ Z$G dd„ deƒZ%G dd„ de&ƒZ'G dd„ de
ƒZ(dS )zAn extension application.é    N)ÚEnvironmentÚFileSystemLoader)Ú
JupyterAppÚNoStart)ÚLogFormatter)ÚRedirectHandler)ÚAnyÚBoolÚDictÚ	HasTraitsÚListÚUnicodeÚdefault)ÚConfig)Ú	ServerApp)Ú_i18n)Úis_namespace_packageÚurl_path_joiné   )ÚExtensionHandlerMixinc                 C   sj   t |ƒdkrdS | jrft |ƒdkrf|d |dd…  }}t d|¡rf|| jv rf| ƒ }| ||¡ |jS dS )z.Preparse command line to look for subcommands.r   Nr   z^\w(\-?\w)*$)ÚlenÚsubcommandsÚreÚmatchZinitialize_subcommandÚsubapp)Úapplication_klassÚargvZsubcZsubargvÚapp© r   úg/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/extension/application.pyÚ_preparse_for_subcommand   s    r    c                    s®   z|d|  d¡… ‰ W n ty,   |‰ Y n0 t‡ fdd„dD ƒƒrb| ƒ }| dˆ v ¡ | d¡ dˆ v srd	ˆ v rŠ| ƒ }| ¡  | d¡ d
ˆ v rª| ƒ }| ¡  | d¡ dS )a@  Looks for 'help', 'version', and 'generate-config; commands
    in command line. If found, raises the help and version of
    current Application.

    This is useful for traitlets applications that have to parse
    the command line multiple times, but want to control when
    when 'help' and 'version' is raised.
    Nz--c                 3   s   | ]}|ˆ v V  qd S ©Nr   )Ú.0Úx©Zinterpreted_argvr   r   Ú	<genexpr>=   ó    z/_preparse_for_stopping_flags.<locals>.<genexpr>)z-hú
--help-allz--helpr'   r   z	--versionz-Vz--generate-config)ÚindexÚ
ValueErrorÚanyÚ
print_helpÚexitÚprint_versionZwrite_default_config)r   r   r   r   r$   r   Ú_preparse_for_stopping_flags*   s     


r.   c                   @   s4   e Zd ZdZeedƒd�jdd�Zej	dd„ ƒZ
dS )	ÚExtensionAppJinjaMixinzAUse Jinja templates for HTML templates on top of an ExtensionApp.z;Options to pass to the jinja2 environment for this
        ©ÚhelpT©Úconfigc                 C   sp   |   ¡  t| jƒdkr0| j | j› d�| ji¡ tf t| jƒdgddœ| j¤Ž| _	| j | j› d�| j	i¡ dS )z$Get templates defined in a subclass.r   Ú_template_pathszjinja2.ext.i18nT)ÚloaderÚ
extensionsZ
autoescapeZ_jinja2_envN)
Úinitialize_templatesr   Útemplate_pathsÚsettingsÚupdateÚnamer   r   Újinja2_optionsZ
jinja2_env©Úselfr   r   r   Ú_prepare_templatesY   s    ýüz)ExtensionAppJinjaMixin._prepare_templatesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   Útagr<   ÚtÚno_type_checkr?   r   r   r   r   r/   O   s   ÿÿûr/   c                   @   s   e Zd ZdZdS )ÚJupyterServerExtensionExceptionz9Exception class for raising for Server extensions errors.N)r@   rA   rB   rC   r   r   r   r   rG   r   s   rG   c                       s<  e Zd ZU dZdZi Zeed< edd�j	dd�Z
edƒdd	„ ƒZed
d„ ƒZd Zejeef ed< edd„ ƒZedd„ ƒZdZeƒ j	dd�Zedƒdd„ ƒZedƒZedƒZegZeƒ Zedƒdd„ ƒZ e!Z"edƒdd„ ƒZ#edƒdd„ ƒZ$ed d�j	dd�Z%ed!ƒd"d#„ ƒZ&e'eƒ d$d�j	dd�Z(e'eƒ e)d%ƒd�j	dd�Z*e+e)d&ƒd�j	dd�Z,e'e)d'ƒd�j	dd�Z-d(d)„ Z.d*d+„ Z/d,d-„ Z0d.d/„ Z1d0d1„ Z2d2d3„ Z3d4d5„ Z4d6d7„ Z5d8d9„ Z6d:d;„ Z7d<d=„ Z8‡ fd>d?„Z9d@dA„ Z:dBdC„ Z;dDdE„ Z<edFdG„ ƒZ=edHdI„ ƒZ>eZ?eejedJœdKdL„ƒZ@edRdNdO„ƒZAedSdPdQ„ƒZB‡  ZCS )TÚExtensionAppaà  Base class for configurable Jupyter Server Extension Applications.

    ExtensionApp subclasses can be initialized two ways:

    - Extension is listed as a jpserver_extension, and ServerApp calls
      its load_jupyter_server_extension classmethod. This is the
      classic way of loading a server extension.

    - Extension is launched directly by calling its `launch_instance`
      class method. This method can be set as a entry_point in
      the extensions setup.py.
    TÚserverapp_configa  Whether to open in a browser after starting.
        The specific browser used is platform dependent and
        determined by the python standard library `webbrowser`
        module, unless it is overridden using the --browser
        (ServerApp.browser) configuration option.
        r0   r2   Úopen_browserc                 C   s   | j jd  dd¡S )Nr   rJ   T)Ú	serverappr3   Úgetr=   r   r   r   Ú_default_open_browserŸ   s    z"ExtensionApp._default_open_browserc                 C   s   | j jS )z4Look on the same path as our parent for config files)rK   Úconfig_file_pathsr=   r   r   r   rN   £   s    zExtensionApp.config_file_pathsr;   c                 C   s2   | j  d¡}t|d ƒr*d |dd… ¡S |d S )zGet an extension package.Ú.r   é   )rA   Úsplitr   Újoin)ÚclsÚpartsr   r   r   Úget_extension_package¯   s    z"ExtensionApp.get_extension_packagec                 C   s   | j S )zGet an extension point.)rA   )rS   r   r   r   Úget_extension_point¸   s    z ExtensionApp.get_extension_pointú/Údefault_urlc                 C   s   | j S r!   )Úextension_urlr=   r   r   r   Ú_default_urlÂ   s    zExtensionApp._default_urlZ	notebooksFrK   c                 C   s,   t  ¡ r&z
t  ¡ W S  ty$   Y n0 t ƒ S r!   )r   ZinitializedÚinstanceÚ	Exceptionr=   r   r   r   Ú_default_serverappÓ   s    
zExtensionApp._default_serverappÚ	log_levelc                 C   s   t jS r!   )ÚloggingÚINFOr=   r   r   r   Ú_default_log_levelã   s    zExtensionApp._default_log_levelZ
log_formatc                 C   s   dS )z2override default log format to include date & timezV%(color)s[%(levelname)1.1s %(asctime)s.%(msecs).03d %(name)s]%(end_color)s %(message)sr   r=   r   r   r   Ú_default_log_formatç   s    ÿz ExtensionApp._default_log_formatz9Url where the static assets for the extension are served.Ústatic_url_prefixc                 C   s   d| j › d�}t| jj|ƒS )Nzstatic/rW   )r;   r   rK   Úbase_url)r>   Ú
static_urlr   r   r   Ú_default_static_url_prefixò   s    z'ExtensionApp._default_static_url_prefixzÅpaths to search for serving static files.

        This allows adding javascript/css to be available from the notebook server machine,
        or overriding individual files in the IPython
        zpPaths to search for serving jinja templates.

        Can be used to override templates from notebook.templates.z(Settings that will passed to the server.z Handlers appended to the server.c                 C   s   | j s
dS d | j  dd¡¡S )zThe default config file name.Ú zjupyter_{}_configú-Ú_)r;   ÚformatÚreplacer=   r   r   r   Ú_config_file_name_default  s    z&ExtensionApp._config_file_name_defaultc                 C   s   dS )z1Override this method to add handling of settings.Nr   r=   r   r   r   Úinitialize_settings  s    z ExtensionApp.initialize_settingsc                 C   s   dS )z<Override this method to append handlers to a Jupyter Server.Nr   r=   r   r   r   Úinitialize_handlers  s    z ExtensionApp.initialize_handlersc                 C   s   dS )z7Override this method to add handling of template files.Nr   r=   r   r   r   r7     s    z!ExtensionApp.initialize_templatesc                    s<   ˆ   ¡  ¡ }t‡ fdd„|D ƒƒˆ _ˆ jˆ jˆ j› d�< dS )z†Builds a Config object from the extension's traits and passes
        the object to the webapp's settings as `<name>_config`.
        c                    s   i | ]}|t ˆ |ƒ“qS r   )Úgetattr)r"   rE   r=   r   r   Ú
<dictcomp>$  r&   z0ExtensionApp._prepare_config.<locals>.<dictcomp>Ú_configN)Zclass_own_traitsÚkeysr   Zextension_configr9   r;   )r>   Ztraitsr   r=   r   Ú_prepare_config  s    zExtensionApp._prepare_configc                 C   s^   | j j}| jjf i |j¤Ž | j | j› d�| j| j› | i¡ |  ¡  |jjf i | j¤Ž dS )zPrepare the settings.Z_static_pathsN)rK   Úweb_appr9   r:   r;   Ústatic_pathsrm   )r>   Úwebappr   r   r   Ú_prepare_settings'  s    þÿzExtensionApp._prepare_settingsc           	   	   C   sØ   | j j}|  ¡  g }| jD ]r}t|jd |d ƒ}|d }i }t|tƒrR| j|d< z| 	|d ¡ W n t
yv   Y n0 |||f}| |¡ qt| jƒdkrÈt| jdƒ}||jd d| jif}| |¡ | d	|¡ d
S )zPrepare the handlers.rd   r   r   r;   rP   z(.*)Zstatic_handler_classÚpathz.*$N)rK   rt   rn   Úhandlersr   r9   Ú
issubclassr   r;   r:   Ú
IndexErrorÚappendr   ru   rc   Úadd_handlers)	r>   rv   Znew_handlersZhandler_itemsÚpatternÚhandlerÚkwargsZnew_handlerre   r   r   r   Ú_prepare_handlers;  s.    



ý
zExtensionApp._prepare_handlersc                 C   s4   t | jƒdkr(| j | j› d�| ji¡ |  ¡  dS )z=Add templates to web app settings if extension has templates.r   r4   N)r   r8   r9   r:   r;   r7   r=   r   r   r   r?   e  s    zExtensionApp._prepare_templatesc                 C   s*   d| j | j| jdœi}|d  | j¡ |S )zThe jupyter server config.r   )rX   rJ   Úfile_url_prefix)rX   rJ   r‚   r:   rI   )r>   Zbase_configr   r   r   Ú_jupyter_server_configk  s    ýÿz#ExtensionApp._jupyter_server_configc                 C   sB   || _ |  ¡  |  | j j¡ |  | j j¡ | j  | j¡ d| _dS )aË  Link the ExtensionApp to an initialized ServerApp.

        The ServerApp is stored as an attribute and config
        is exchanged between ServerApp and `self` in case
        the command line contains traits for the ExtensionApp
        or the ExtensionApp's config files have server
        settings.

        Note, the ServerApp has not initialized the Tornado
        Web Application yet, so do not try to affect the
        `web_app` attribute.
        TN)rK   Úload_config_fileÚupdate_configr3   Úparse_command_lineÚ
extra_argsÚ_linked)r>   rK   r   r   r   Ú_link_jupyter_server_extensionw  s    z+ExtensionApp._link_jupyter_server_extensionc                 C   s6   | j sd}t|ƒ‚|  ¡  |  ¡  |  ¡  |  ¡  dS )aX  Initialize the extension app. The
        corresponding server app and webapp should already
        be initialized by this step.

        - Appends Handlers to the ServerApp,
        - Passes config and settings from ExtensionApp
          to the Tornado web application
        - Points Tornado Webapp to templates and static assets.
        zoThis extension has no attribute `serverapp`. Try calling `.link_to_serverapp()` before calling `.initialize()`.N)rK   rG   rs   r?   rw   r�   )r>   Úmsgr   r   r   Ú
initialize™  s    
ÿzExtensionApp.initializec                    s   t ƒ  ¡  | j ¡  dS )zoStart the underlying Jupyter server.

        Server should be started after extension is initialized.
        N)ÚsuperÚstartrK   r=   ©Ú	__class__r   r   r�   °  s    
zExtensionApp.startc                 C   s   dS )z6Return a list of activity happening in this extension.Nr   r=   r   r   r   Úcurrent_activity¹  s    zExtensionApp.current_activityc                 Ã   s   dS )z0Cleanup any resources managed by this extension.Nr   r=   r   r   r   Ústop_extension½  s    zExtensionApp.stop_extensionc                 C   s   | j  ¡  | j  ¡  dS )z#Stop the underlying Jupyter server.N)rK   ÚstopZclear_instancer=   r   r   r   r’   À  s    
zExtensionApp.stopc                 C   sL   |j }z|j| j }|j}W n" ty>   | ƒ }| |¡ Y n0 | ¡  |S )z‰Initialize and configure this extension, then add the extension's
        settings and handlers to the server's web application.
        )Úextension_managerZextension_pointsr;   r   ÚKeyErrorr‰   r‹   )rS   rK   r“   ÚpointÚ	extensionr   r   r   Ú_load_jupyter_server_extensionÅ  s    
z+ExtensionApp._load_jupyter_server_extensionc                 C   sä   | ƒ }||_ | ¡  | |j¡ | |j¡ |j dtdt	|j
dƒifdtdt	|j
dƒifdtdt	|j
dƒifdtdt	|j
d	ƒifd
tdt	|j
dƒifdtdt	|j
dƒifdtdt	|j
dƒifdtdt	|j
dƒifg¡ | ¡  dS )zPEnables extension to be loaded as classic Notebook (jupyter/notebook) extension.z/static/favicons/favicon.icoÚurlzstatic/base/images/favicon.icoz#/static/favicons/favicon-busy-1.icoz%static/base/images/favicon-busy-1.icoz#/static/favicons/favicon-busy-2.icoz%static/base/images/favicon-busy-2.icoz#/static/favicons/favicon-busy-3.icoz%static/base/images/favicon-busy-3.icoz!/static/favicons/favicon-file.icoz#static/base/images/favicon-file.icoz%/static/favicons/favicon-notebook.icoz'static/base/images/favicon-notebook.icoz%/static/favicons/favicon-terminal.icoz'static/base/images/favicon-terminal.icoz/static/logo/logo.pngzstatic/base/images/logo.pngN)rK   r„   r…   r3   r†   r‡   ry   Úextendr   r   rd   r‹   )rS   rK   r–   r   r   r   Úload_classic_server_extensionÕ  sz    ýÿÿý
ÿÿý
ÿÿý
ÿÿý
þÿýþÿýýÂÿFz*ExtensionApp.load_classic_server_extension)r€   Úreturnc                 K   s   | j jf i |¤ŽS )zqInstantiate the ServerApp

        Override to customize the ServerApp before it loads any configuration
        )Úserverapp_classr[   )rS   r€   r   r   r   Úmake_serverapp(  s    zExtensionApp.make_serverappNc                 K   sv   |   ¡ di}| j}d| jv r:| | jd ¡ || jd< d}| jf d|i|¤Ž}|j | j¡ |j|pfg | j|d� |S )a  Creates an instance of ServerApp and explicitly sets
        this extension to enabled=True (i.e. superseding disabling
        found in other config from files).

        The `launch_instance` method uses this method to initialize
        and start a server.
        TÚjpserver_extensionsF)r   Zstarter_extensionÚfind_extensions)rU   Úload_other_extensionsrI   r:   r�   Úaliasesr‹   r;   )rS   r   r    r€   rž   rŸ   rK   r   r   r   Úinitialize_server0  s    	

ýzExtensionApp.initialize_serverc                 K   sŠ   |du rt jdd… }n|}t| |ƒ}|r6| ¡  dS t| |ƒ | j|d�}| jsf|j | j	› d�¡ z| ¡  W n t
y„   Y n0 dS )zÍLaunch the extension like an application. Initializes+configs a stock server
        and appends the extension to the server. Then starts the server and routes to
        extension's landing page.
        Nr   )r   z- is running without loading other extensions.)Úsysr   r    r�   r.   r¢   r    ÚlogÚinfor;   r   )rS   r   r€   Úargsr   rK   r   r   r   Úlaunch_instanceH  s    

zExtensionApp.launch_instance)NT)N)Dr@   rA   rB   rC   r    rI   ÚdictÚ__annotations__r	   rD   rJ   r   rM   ÚpropertyrN   r;   rE   ÚUnionÚstrr   ÚclassmethodrU   rV   rY   rX   rZ   r‚   rˆ   r   Úclassesr   rK   r]   r   Z_log_formatter_clsra   rb   rc   rf   r   ru   r   r8   r
   r9   ry   rl   rm   rn   r7   rs   rw   r�   r?   rƒ   r‰   r‹   r�   r�   r‘   r’   r—   rš   rœ   r�   r¢   r§   Ú__classcell__r   r   rŽ   r   rH   {   s¤   
ÿù	

	


ÿ


ÿþ
þù	ÿþù	*"	

PrH   ))rC   r_   r   r£   ÚtypingrE   Zjinja2r   r   Zjupyter_core.applicationr   r   Ztornado.logr   Ztornado.webr   Z	traitletsr   r	   r
   r   r   r   r   Ztraitlets.configr   Zjupyter_server.serverappr   Zjupyter_server.transutilsr   Zjupyter_server.utilsr   r   r   r   r    r.   r/   r\   rG   rH   r   r   r   r   Ú<module>   s&   $%#	