
     )Me                        d Z ddlmZ ddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ  G d	 d
e          ZdS )z+Tornado handlers for dynamic theme loading.    )annotationsN)glob)Any	Generator)urlparse)FileFindHandler)url_path_joinc                  @    e Zd ZdZ	 	 	 	 dddZ	 dddZddZd dZdS )!ThemesHandlerz4A file handler that mangles local urls in CSS files.Npathstr | list[str]default_filename
str | Noneno_cache_pathslist[str] | None
themes_urllabextensions_pathkwargsr   returnNonec                    |pg }g }|D ]5}|dz   }	|                     d t          |	d          D                        6t          |t                    s|g}||z   }t	          j        | |||           || _        dS )zInitialize the handler.z
/**/themesc                    g | ]}|S  r   ).0r   s     @lib/python3.11/site-packages/jupyterlab_server/themes_handler.py
<listcomp>z,ThemesHandler.initialize.<locals>.<listcomp>#   s    SSStdSSS    T)	recursive)r   r   N)extendr   
isinstancelistr   
initializer   )
selfr   r   r   r   r   r   	ext_pathsext_dirtheme_patterns
             r   r"   zThemesHandler.initialize   s     052	) 	U 	UG#l2MSStMT/R/R/RSSSTTTT $%% 	6D4"$)9.	
 	
 	
 	
 %r   abspathstrstart
int | Noneend$bytes | Generator[bytes, None, None]c                    t           j                            |          \  }}|dk    rt          j        |||          S |                                 S )zRetrieve the content of the requested resource which is located
        at the given absolute path.

        This method should either return a byte string or an iterator
        of byte strings.
        .css)osr   splitextr   get_content_get_css)r#   r'   r)   r+   baseexts         r   r1   zThemesHandler.get_content/   sI     G$$W--	c&==".wsCCC}}r   intc                    | j         J t          j                            | j                   \  }}|dk    rt	          j        |           S t          |                                           S )z:Retrieve the total size of the resource at the given path.Nr.   )absolute_pathr/   r   r0   r   get_content_sizelenr2   )r#   r3   r4   s      r   r8   zThemesHandler.get_content_size>   s]    !---G$$T%788	c&=="3D999t}}'''r   bytesc                   | j         J t          | j         d          5 }|                                                    d          }ddd           n# 1 swxY w Y   | j        sdS t
          j                            | j                                      t
          j	        d          }t          | j        |          d}dfd
}t          j        |||                              d          S )z"Get the mangled css file contents.Nrbzutf-8r   /zurl\('(.*)'\)|url\('(.*)'\)mr   r   c                $   |                                  }t          d |                                 D                       }t          |          }|                    d          s|j        r|S |                    |t          |                    S )z6Replace the matched relative url with the mangled url.c              3     K   | ]}||V  	d S )Nr   )r   gs     r   	<genexpr>z;ThemesHandler._get_css.<locals>.replacer.<locals>.<genexpr>\   s'      33a3333333r   r=   )groupnextgroupsr   
startswithschemereplaceujoin)r>   rC   partparsedbasepaths       r   replacerz(ThemesHandler._get_css.<locals>.replacerX   s    GGIIE33188::33333D d^^Fs## v} ==uXt'<'<===r   )r>   r   r   r   )r7   openreaddecoder   r/   r   dirnamerH   seprI   resubencode)r#   fiddatabasedirpatternrM   rL   s         @r   r2   zThemesHandler._get_cssG   s$   !---$$d++ 	.s88::$$W--D	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.  	3'//$),,44RVSAA'22
 1	> 	> 	> 	> 	> 	> vgx..55g>>>s   (AAA)NNNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )NN)r'   r(   r)   r*   r+   r*   r   r,   )r   r5   )r   r:   )__name__
__module____qualname____doc__r"   r1   r8   r2   r   r   r   r   r      s        >>
 (,+/!%/3% % % % %8 IM    ( ( ( (? ? ? ? ? ?r   r   )r]   
__future__r   r/   rS   r   typingr   r   urllib.parser   jupyter_server.base.handlersr   jupyter_server.utilsr	   rI   r   r   r   r   <module>rc      s    1 1 # " " " " " 				 				       ! ! ! ! ! ! ! ! ! ! ! ! ! ! 8 8 8 8 8 8 7 7 7 7 7 7T? T? T? T? T?O T? T? T? T? T?r   