
    MhvZ                    2   d Z ddlm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 Z%d Z& G d de          Z' G d de(          Z) G d de          Z*dS )zAn extension application.    )annotationsN)EnvironmentFileSystemLoader)
JupyterAppNoStart)LogFormatter)RedirectHandler)AnyBoolDict	HasTraitsListUnicodedefault)Config)	ServerApp)_i18n)is_namespace_packageurl_path_join   )ExtensionHandlerMixinc                   t          |          dk    rdS | j        rjt          |          dk    rY|d         |dd         }}t          j        d|          r4|| j        v r- |             }|                    ||           |j        S dS dS dS dS )z.Preparse command line to look for subcommands.r   Nr   z^\w(\-?\w)*$)lensubcommandsrematchinitialize_subcommandsubapp)application_klassargvsubcsubargvapps        d/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_server/extension/application.py_preparse_for_subcommandr%      s     4yyA~~t $ TQQabbg8OT** 	t7H7T/T/T##%%C%%dG444: 	 	/T/T    c                   	 |d|                     d                   n# t          $ r |Y nw xY wt          fddD                       r6 |             }|                    dv            |                    d           dv sdv r3 |             }|                                 |                    d           d	v r5 |             }|                                 |                    d           dS 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      K   | ]}|v V  	d S N ).0xinterpreted_argvs     r$   	<genexpr>z/_preparse_for_stopping_flags.<locals>.<genexpr>@   s)      
I
IQ1  
I
I
I
I
I
Ir&   )z-h
--help-allz--helpr/   r   z	--versionz-Vz--generate-config)index
ValueErrorany
print_helpexitprint_versionwrite_default_config)r   r    r#   r-   s      @r$   _preparse_for_stopping_flagsr7   -   s=     2$**T"2"2 23        
I
I
I
I*H
I
I
III !!|'77888 &&&$2B*B*B!! ...!!  """ /.s   ! 00c                      e Zd ZdZ e ed                                        d          Zej	        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                \   |                                   t          | j                  dk    r)| j                            | j         d| j        i           t          d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
extensions
autoescape_jinja2_envNr*   )
initialize_templatesr   template_pathssettingsupdatenamer   r   jinja2_options
jinja2_envselfs    r$   _prepare_templatesz)ExtensionAppJinjaMixin._prepare_templates\   s     	!!###t"##a''M  TY"?"?"?AT!UVVV & 
#D$788)*
 
 !	
 
 	777IJJJJJr&   N)__name__
__module____qualname____doc__r   r   tagrI   tno_type_checkrM   r*   r&   r$   r9   r9   R   sz        KKTU
 
  
 
cc  _K K _K K Kr&   r9   c                      e Zd ZdZdS )JupyterServerExtensionExceptionz9Exception class for raising for Server extensions errors.N)rN   rO   rP   rQ   r*   r&   r$   rV   rV   u   s        CCCCr&   rV   c                      e Zd ZU dZdZi Zded<    ed                              d          Z	 e
d          d	             Zed
             Zd Zded<   ed             Zed             ZdZ e                                d          Z e
d          d             Z ed          Z ed          ZegZ e            Zded<    e
d          d             ZeZ e
d          d             Z e
d          d             Z  ed                              d          Z! e
d          d             Z" e# e            d                              d          Z$ e# e             e%d                                        d          Z& e' e%d                                         d          Z( e# e%d!                                        d          Z)d"ed#<   d$ Z*d% Z+d& Z,d' Z-d( Z.d) Z/d* Z0d+ Z1d, Z2d?d0Z3d1 Z4 fd2Z5d3 Z6d4 Z7d5 Z8ed6             Z9d7 Z:ed8             Z;eZ<ed@d;            Z=edAd=            Z>edBd>            Z? xZ@S )C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.
    Tzdict[str, t.Any]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.
        r:   r<   open_browserc                `    | j         J | j         j        d                             dd          S )Nr   rZ   T)	serverappr=   getrK   s    r$   _default_open_browserz"ExtensionApp._default_open_browser   s0    ~)))~$[155ndKKKr&   c                ,    | j         J | j         j        S )z4Look on the same path as our parent for config files)r\   config_file_pathsrK   s    r$   r`   zExtensionApp.config_file_paths   s     ~)))~//r&   zstr | Unicode[str, str]rH   c                    | j                             d          }t          |d                   rd                    |dd                   S |d         S )zGet an extension package..r      )rO   splitr   join)clspartss     r$   get_extension_packagez"ExtensionApp.get_extension_package   sP     $$S))a)) 	(88E!A#J'''Qxr&   c                    | j         S )zGet an extension point.)rO   )rf   s    r$   get_extension_pointz ExtensionApp.get_extension_point   s     ~r&   /default_urlc                    | j         S r)   )extension_urlrK   s    r$   _default_urlzExtensionApp._default_url   s    !!r&   	notebooksFzServerApp | Noner\   c                    t          j                    r$	 t          j                    S # t          $ r Y nw xY wt                      S r)   )r   initializedinstance	ExceptionrK   s    r$   _default_serverappzExtensionApp._default_serverapp   sU      "" 	 )+++    {{s   ( 
55	log_levelc                    t           j        S r)   )loggingINFOrK   s    r$   _default_log_levelzExtensionApp._default_log_level   s
    |r&   
log_formatc                    	 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*   rK   s    r$   _default_log_formatz ExtensionApp._default_log_format   s     e	
 	
r&   z9Url where the static assets for the extension are served.static_url_prefixc                ^    d| j          d}| j        J t          | j        j        |          S )Nzstatic/rk   )rH   r\   r   base_url)rL   
static_urls     r$   _default_static_url_prefixz'ExtensionApp._default_static_url_prefix   s7    +ty+++
~)))T^4jAAAr&   zpaths 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.zList[tuple[t.Any, ...]]handlersc                p    | j         sdS d                    | j                             dd                    S )zThe default config file name. zjupyter_{}_config-_)rH   formatreplacerK   s    r$   _config_file_name_defaultz&ExtensionApp._config_file_name_default  s7    y 	2"))$)*;*;C*E*EFFFr&   c                    dS )z1Override this method to add handling of settings.Nr*   rK   s    r$   initialize_settingsz ExtensionApp.initialize_settings        r&   c                    dS )z<Override this method to append handlers to a Jupyter Server.Nr*   rK   s    r$   initialize_handlersz ExtensionApp.initialize_handlers  r   r&   c                    dS )z7Override this method to add handling of template files.Nr*   rK   s    r$   rD   z!ExtensionApp.initialize_templates!  r   r&   c                                                                                       }t           fd|D                        _         j         j         j         d<   dS )zBuilds a Config object from the extension's traits and passes
        the object to the webapp's settings as `<name>_config`.
        c                2    i | ]}|t          |          S r*   )getattr)r+   rS   rL   s     r$   
<dictcomp>z0ExtensionApp._prepare_config.<locals>.<dictcomp>)  s%    'L'L'L74+;+;'L'L'Lr&   _configN)class_own_traitskeysr   extension_configrF   rH   )rL   traitss   ` r$   _prepare_configzExtensionApp._prepare_config$  sg     &&((--// &'L'L'L'LV'L'L'L M M/3/D+++,,,r&   c                   | j         J | j         j        } | j        j        di |j         | j                            | j         d| j        | j         | i           |                                   |j        j        di | j         dS )zPrepare the settings.N_static_pathsr*   )r\   web_apprF   rG   rH   static_pathsr   )rL   webapps     r$   _prepare_settingszExtensionApp._prepare_settings,  s     ~)))'//v/// 	9+++T->9	
 	
 	
 	  """ 	///////r&   c                p   | j         J | j         j        }|                                  g }| j        D ]}t	          |j        d         |d                   }|d         }i }t          |t                    r
| j        |d<   	 |	                    |d                    n# t          $ r Y nw xY w|||f}|                    |           t          | j                  dk    rAt	          | j        d          }||j        d         d	| j        if}|                    |           |                    d
|           dS )zPrepare the handlers.Nr   r   r   rH   rc   z(.*)static_handler_classpathz.*$)r\   r   r   r   r   rF   
issubclassr   rH   rG   
IndexErrorappendr   r   r~   add_handlers)	rL   r   new_handlershandler_itemspatternhandlerkwargsnew_handlerr   s	            r$   _prepare_handlerszExtensionApp._prepare_handlersA  sr   ~)))' 	  """ !] 	- 	-M#FOJ$?qAQRRG#A&G (*F'#899 +!%vmA.////    #GV4K,,,, t !!A%%&t'=vFFJ  67*+G
 (((E<00000s    B
B)(B)c                    t          | j                  dk    r)| j                            | j         d| j        i           |                                  dS )z=Add templates to web app settings if extension has templates.r   r?   N)r   rE   rF   rG   rH   rD   rK   s    r$   rM   zExtensionApp._prepare_templatesl  sY    t"##a''M  TY"?"?"?AT!UVVV!!#####r&   c                t    d| j         | j        | j        di}|d                             | j                   |S )zThe jupyter server config.r   )rl   rZ   file_url_prefix)rl   rZ   r   rG   rY   )rL   base_configs     r$   _jupyter_server_configz#ExtensionApp._jupyter_server_configr  sN     #/ $ 1#'#7 
 	K ''(=>>>r&   r   returnNonec                   || _         |                                  |                     | 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)r\   load_config_fileupdate_configr=   parse_command_line
extra_args_linkedrL   r\   s     r$   _link_jupyter_server_extensionz+ExtensionApp._link_jupyter_server_extension~  sx     # 	4>0111 	 9::: 	$$T[111r&   c                    | 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)r\   rV   r   rM   r   r   )rL   msgs     r$   
initializezExtensionApp.initialize  sv     ~ 	7# 
 2#666!!!        r&   c                    t                                                       | j        J | j                                         dS )zoStart the underlying Jupyter server.

        Server should be started after extension is initialized.
        N)superstartr\   )rL   	__class__s    r$   r   zExtensionApp.start  s>    
 	~)))r&   c                    dS )z6Return a list of activity happening in this extension.Nr*   rK   s    r$   current_activityzExtensionApp.current_activity  s    r&   c                
   K   dS )z0Cleanup any resources managed by this extension.Nr*   rK   s    r$   stop_extensionzExtensionApp.stop_extension  
        r&   c                |    | j         J | j                                          | j                                          dS )z#Stop the underlying Jupyter server.N)r\   stopclear_instancerK   s    r$   r   zExtensionApp.stop  s>    ~)))%%'''''r&   c                    |j         }	 |j        | j                 }|j        }n/# t          $ r"  |             }|                    |           Y nw xY w|                                 |S )zInitialize and configure this extension, then add the extension's
        settings and handlers to the server's web application.
        )extension_managerextension_pointsrH   r#   KeyErrorr   r   )rf   r\   r   point	extensions        r$   _load_jupyter_server_extensionz+ExtensionApp._load_jupyter_server_extension  s    
 &7	@%6sx@E	II 	@ 	@ 	@I44Y?????	@ 	s   # )AAc                
   K   dS )a:  
        An async hook to start e.g. tasks from the extension after
        the server's event loop is running.

        Override this method (no need to call `super()`) to
        start (async) tasks from an extension.

        This is useful for starting e.g. background tasks from
        an extension.
        Nr*   r   s     r$   _start_jupyter_server_extensionz,ExtensionApp._start_jupyter_server_extension  r   r&   c                    |             }||_         |                                 |                    |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)r\   r   r   r=   r   r   r   extendr	   r   r   r   )rf   r\   r   s      r$   load_classic_server_extensionz*ExtensionApp.load_classic_server_extension  s    CEE	'	""$$$	 0111$$Y%9:::!! 4#M)*<>^__` ;#}%.0W    ;#}%.0W    ;#}%.0W    9#}%.0U    =#}%.E   	 =#}%.E   	 -#M)*<>[\\]}CE	
 E	
 E	
L 	r&   r   t.Anyc                &     | j         j        di |S )zqInstantiate the ServerApp

        Override to customize the ServerApp before it loads any configuration
        r*   )serverapp_classrs   )rf   r   s     r$   make_serverappzExtensionApp.make_serverapp=  s      ,s"+55f555r&   Nc                D   |                                  di}| j        }d| j        v r,|                    | j        d                    || j        d<   d} | j        dd|i|}|j                            | j                   |                    |pg | j        |           |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    starter_extensionfind_extensionsr*   )rh   load_other_extensionsrY   rG   r   aliasesr   rH   )rf   r    r   r   r   r   r\   s          r$   initialize_serverzExtensionApp.initialize_serverE  s      #88::DA3 C$888&&s';<Q'RSSS:MC !67#O&C&YY;NYRXYY	  ---!h+ 	 	
 	
 	

 r&   c                r   |t           j        dd         }n|}t          | |          }|r|                                 dS t	          | |           |                     |          }| j        s"|j                            | j	         d           	 |                                 dS # t          $ r Y dS w xY w)zLaunch 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   r7   r   r   loginforH   r   )rf   r    r   argsr   r\   s         r$   launch_instancezExtensionApp.launch_instance]  s     <8ABB<DDD *#t44 	LLNNNF
 	%S$///))t)44	 ( 	[M#(YYYZZZ	OO 	 	 	DD	s   B( (
B65B6)r\   r   r   r   )r   r   r   r   )NTr)   )ArN   rO   rP   rQ   r   rY   __annotations__r   rR   rZ   r   r^   propertyr`   rH   classmethodrh   rj   rn   r   rl   ro   r   r   r   classesr
   r\   ru   r   _log_formatter_clsrz   r}   r~   r   r   r   r   rE   r   rF   r   r   r   r   rD   r   r   r   rM   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r$   rX   rX   ~   s           !
 *,++++
 4   
cc  W^L L L 0 0 X0 %3D2222  [   [
 M'))--t-,,KW]" " " gk**O d5kkG 	G
 #&#%%I''''W[   &W[   W\
 
 
  L  	cc  W !!B B "!B
 4		   
cc  T		UF
 
   
cc  tMNNOOOSS[_S``H(,U9::) ) )	cc     G G G@ @ @K K KF F FE E E0 0 0*)1 )1 )1V$ $ $
 
 
       D! ! !.      ? ? ?( ( (   [
 
 
 N N [N`  O6 6 6 [6    [.    [    r&   rX   )+rQ   
__future__r   rx   r   r   typingrS   jinja2r   r   jupyter_core.applicationr   r   tornado.logr   tornado.webr	   	traitletsr
   r   r   r   r   r   r   traitlets.configr   jupyter_server.serverappr   jupyter_server.transutilsr   jupyter_server.utilsr   r   r   r   r%   r7   r9   rt   rV   rX   r*   r&   r$   <module>r      s     " " " " " "  				 



     0 0 0 0 0 0 0 0 8 8 8 8 8 8 8 8 $ $ $ $ $ $ ' ' ' ' ' ' H H H H H H H H H H H H H H H H H H # # # # # # . . . . . . + + + + + + D D D D D D D D * * * * * *  "" " "JK K K K KY K K KFD D D D Di D D D    :     r&   