o
    tf                     @  st   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
 d
eZdS )z+Tornado handlers for dynamic theme loading.    )annotationsN)glob)Any	Generator)urlparse)FileFindHandler)url_path_joinc                   @  sF   e Zd ZdZ				dd ddZ	d!d"ddZd#ddZd$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           
      K  sn   |pg }g }|D ]}|d }	| dd t|	ddD  qt|ts%|g}|| }tj| |||d || _dS )zInitialize the handler.z
/**/themesc                 s  s    | ]}|V  qd S N ).0r
   r   r   i/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/jupyterlab_server/themes_handler.py	<genexpr>#   s    z+ThemesHandler.initialize.<locals>.<genexpr>T)	recursive)r   r   N)extendr   
isinstancelistr   
initializer   )
selfr
   r   r   r   r   r   Z	ext_pathsZext_dirZtheme_patternr   r   r   r      s   

zThemesHandler.initializeabspathstrstart
int | Noneend$bytes | Generator[bytes, None, None]c                 C  s.   t j|\}}|dkrt|||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extr   r   r   r)   /   s   	zThemesHandler.get_contentintc                 C  s>   | j dusJ tj| j \}}|dkrt| S t|  S )z:Retrieve the total size of the resource at the given path.Nr&   )absolute_pathr'   r
   r(   r   get_content_sizelenr*   )r   r+   r,   r   r   r   r/   >   s
   
zThemesHandler.get_content_sizebytesc                   s   | j dusJ t| j d}| d}W d   n1 sw   Y  | js)dS tj| jtj	d}t
| j| d}d fd
d}t|||dS )z"Get the mangled css file contents.Nrbzutf-8    /zurl\('(.*)'\)|url\('(.*)'\)mr   r   c                   sL   |   }tdd |  D }t|}|ds|jr|S ||t |S )z6Replace the matched relative url with the mangled url.c                 s  s    | ]}|r|V  qd S r   r   )r   gr   r   r   r   [   s    z;ThemesHandler._get_css.<locals>.replacer.<locals>.<genexpr>r4   )groupnextgroupsr   
startswithschemereplaceujoin)r5   r7   partparsedZbasepathr   r   replacerW   s   z(ThemesHandler._get_css.<locals>.replacer)r5   r   r   r   )r.   openreaddecoder   r'   r
   dirnamer<   sepr=   resubencode)r   fiddataZbasedirpatternrA   r   r@   r   r*   F   s   zThemesHandler._get_css)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   r-   )r   r1   )__name__
__module____qualname____doc__r   r)   r/   r*   r   r   r   r   r	      s    
r	   )rP   
__future__r   r'   rG   r   typingr   r   urllib.parser   Zjupyter_server.base.handlersr   Zjupyter_server.utilsr   r=   r	   r   r   r   r   <module>   s    