
    Xh                    z    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y)z+Tornado handlers for dynamic theme loading.    )annotationsN)glob)Any	Generator)urlparse)FileFindHandler)url_path_joinc                  f    e Zd ZdZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 d		 	 	 	 	 	 	 d
dZddZddZy)ThemesHandlerz4A file handler that mangles local urls in CSS files.Nc                    |xs g }g }|D ]*  }|dz   }	|j                  d t        |	d      D               , t        |t              s|g}||z   }t	        j
                  | |||       || _        y)zInitialize the handler.z
/**/themesc              3      K   | ]  }|  y wN ).0paths     T/var/www/html/myenv/lib/python3.12/site-packages/jupyterlab_server/themes_handler.py	<genexpr>z+ThemesHandler.initialize.<locals>.<genexpr>#   s     RdTRs   T)	recursive)default_filenameno_cache_pathsN)extendr   
isinstancelistr   
initialize
themes_url)
selfr   r   r   r   labextensions_pathkwargs	ext_pathsext_dirtheme_patterns
             r   r   zThemesHandler.initialize   s     052!	) 	SG#l2MRd=D.QRR	S
 $%6D4""$)9.	
 %    c                    t         j                  j                  |      \  }}|dk7  rt        j                  |||      S | j                         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   abspathstartendbaseexts         r   r'   zThemesHandler.get_content/   sD     GG$$W-	c&="..wsCC}}r"   c                    | j                   J t        j                  j                  | j                         \  }}|dk7  rt	        j
                  |       S t        | j                               S )z:Retrieve the total size of the resource at the given path.r$   )absolute_pathr%   r   r&   r   get_content_sizelenr(   )r   r,   r-   s      r   r0   zThemesHandler.get_content_size>   s[    !!---GG$$T%7%78	c&="33D994==?##r"   c                   | j                   J t        | j                   d      5 }|j                         j                  d      }ddd       | j                  syt
        j                  j                  | j                        j                  t
        j                  d      }t        | j                  |      d}dfd}t        j                  ||      j                  d      S # 1 sw Y   xY w)	z"Get the mangled css file contents.Nrbzutf-8r"   /zurl\('(.*)'\)|url\('(.*)'\)c                    | j                         }t        d | j                         D              }t        |      }|j	                  d      s|j
                  r|S |j                  |t        |            S )z6Replace the matched relative url with the mangled url.c              3  &   K   | ]	  }|s|  y wr   r   )r   gs     r   r   z;ThemesHandler._get_css.<locals>.replacer.<locals>.<genexpr>[   s     3a3s   r4   )groupnextgroupsr   
startswithschemereplaceujoin)mr8   partparsedbasepaths       r   replacerz(ThemesHandler._get_css.<locals>.replacerW   s^    GGIE3188:33D d^Fs#v}}==uXt'<==r"   )r?   r   returnr   )r/   openreaddecoder   r%   r   dirnamer=   sepr>   resubencode)r   fiddatabasedirpatternrC   rB   s         @r   r(   zThemesHandler._get_cssF   s    !!---$$$d+ 	.s88:$$W-D	. ''//$)),44RVVSA'2
 1	> vvgx.55g>>7	. 	.s    C&&C/)NNNN)r   zstr | list[str]r   
str | Noner   list[str] | Noner   rQ   r   rR   r   r   rD   None)NN)r)   strr*   
int | Noner+   rU   rD   z$bytes | Generator[bytes, None, None])rD   int)rD   bytes)__name__
__module____qualname____doc__r   r'   r0   r(   r   r"   r   r   r      s    >
 (,+/!%/3%% %% )	%
 % -% % 
%8 IM#-;E	-$?r"   r   )r[   
__future__r   r%   rJ   r   typingr   r   urllib.parser   jupyter_server.base.handlersr   jupyter_server.utilsr	   r>   r   r   r"   r   <module>ra      s0    1 # 	 	  ! ! 8 7S?O S?r"   