o
    ^ir                     @  s   d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ er>ddlmZ dd	lmZ dd
lmZ G dd dZG dd dZdS )zAn extension handler.    )annotations)Logger)TYPE_CHECKINGAnycast)Template)TemplateNotFound)FileFindHandler)Config)ExtensionApp)	ServerAppc                      s"   e Zd ZdZd fddZ  ZS )	ExtensionHandlerJinjaMixinz`Mixin class for ExtensionApp handlers that use jinja templating for
    template rendering.
    namestrreturnr   c                   sN   z| j  d}tt| j| |}|W S  ty&   ttt | Y S w )z1Return the jinja template object for a given name_jinja2_env)r   r   r   settingsget_templater   super)selfr   envtemplate	__class__ [/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/jupyter_server/extension/handler.pyr      s   z'ExtensionHandlerJinjaMixin.get_template)r   r   r   r   )__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r      s    r   c                      s   e Zd ZU dZded< d) fddZed*ddZed+ddZed, fddZ	ed-ddZ
ed-ddZed.ddZd/ fddZed.dd Zed.d!d"Zd0d1d'd(Z  ZS )2ExtensionHandlerMixina.  Base class for Jupyter server extension handlers.

    Subclasses can serve static files behind a namespaced
    endpoint: "<base_url>/static/<name>/"

    This allows multiple extensions to serve static files under
    their own namespace and avoid intercepting requests for
    other extensions.
    zdict[str, Any]r   r   r   argsr   kwargsr   Nonec                   s4   || _ zt j|i | W d S  ty   Y d S w N)r   r   
initialize	TypeError)r   r   r"   r#   r   r   r   r&   0   s   z ExtensionHandlerMixin.initializer   c                 C  s   t d| j| j S )Nr   r   r   r   r   r   r   r   extensionapp7   s   z"ExtensionHandlerMixin.extensionappr   c                 C  s   d}t d| j| S )N	serverappr   r   r   )r   keyr   r   r   r+   ;   s   zExtensionHandlerMixin.serverappr   c                   sJ   t | dsttt jS ztt| jjW S  ty$   tt| jj Y S w )Nr   )hasattrr   r   r   logr*   AttributeErrorr+   r)   r   r   r   r/   @   s   
zExtensionHandlerMixin.logr
   c                 C  s   t d| j| j d S )Nr
   _configr(   r)   r   r   r   configJ      zExtensionHandlerMixin.configc                 C  s   t d| jd S )Nr
   r2   r,   r)   r   r   r   server_configN   s   z#ExtensionHandlerMixin.server_configc                 C  s   t t| jddS )Nbase_url/)r   r   r   getr)   r   r   r   r5   R   s   zExtensionHandlerMixin.base_urlc                   sN   t t| |}|| j |j| jd u rt j|d< t t	|j
di |S )zOverride render template to handle static_paths

        If render_template is called with a template from the base environment
        (e.g. default error pages)
        make sure our extension-specific static_url is _not_ used.
        
jinja2_env
static_urlNr   )r   r   r   updatetemplate_namespaceenvironmentr   r   r9   r   render)r   r   nsr   r   r   r   render_templateV   s
   z%ExtensionHandlerMixin.render_templatec                 C  s   | j jS r%   )r*   static_url_prefixr)   r   r   r   r@   d   s   z'ExtensionHandlerMixin.static_url_prefixc                 C  s   t t| j| j d S )N_static_paths)r   r   r   r   r)   r   r   r   static_pathh   r3   z!ExtensionHandlerMixin.static_pathNpathinclude_hostbool | Nonec           
   
   K  s   | j  d}z| |d W n ty( } z|| jv r"d}t|d|d}~ww | jdtj}|du r;t| dd}d}|rI| jj	d	 | jj
 }| j| jd
}	|tt||	|fi | S )a  Returns a static URL for the given relative static file path.
        This method requires you set the ``{name}_static_path``
        setting in your extension (which specifies the root directory
        of your static files).
        This method returns a versioned url (by default appending
        ``?v=<signature>``), which allows the static files to be
        cached indefinitely.  This can be disabled by passing
        ``include_version=False`` (in the default implementation;
        other static file implementations are not required to support
        this, but they may support other options).
        By default this method returns URLs relative to the current
        host, but if ``include_host`` is true the URL returned will be
        absolute.  If this handler has an ``include_host`` attribute,
        that value will be used as the default for all `static_url`
        calls that do not pass ``include_host`` as a keyword argument.
        rA   r9   zlThis extension doesn't have any static paths listed. Check that the extension's `static_paths` trait is set.Nstatic_handler_classrD   F z://)rB   r@   )r   require_setting	Exceptionr   r7   r	   make_static_urlgetattrrequestprotocolhostrB   r@   r   r   )
r   rC   rD   r#   r-   emsgget_urlbaser   r   r   r   r9   l   s*   


z ExtensionHandlerMixin.static_url)r   r   r"   r   r#   r   r   r$   )r   r   )r   r   )r   r   )r   r
   )r   r   )r   r   r   r   r%   )rC   r   rD   rE   r#   r   r   r   )r   r   r   r   __annotations__r&   propertyr*   r+   r/   r2   r4   r5   r?   r@   rB   r9   r    r   r   r   r   r!   #   s,   
 
	r!   N)r   
__future__r   loggingr   typingr   r   r   jinja2r   jinja2.exceptionsr   jupyter_server.base.handlersr	   traitlets.configr
   $jupyter_server.extension.applicationr   jupyter_server.serverappr   r   r!   r   r   r   r   <module>   s    