a
    ø£Be†  ã                   @   sX   d Z ddlZddlZddl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.é    N)Úglob)Úurlparse)ÚFileFindHandler)Úurl_path_joinc                   @   s4   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.Nc           
      K   sn   |pg }g }|D ](}|d }	|  dd„ t|	dd�D ƒ¡ qt|tƒsJ|g}|| }tj| |||d� || _dS )zInitialize the handler.z
/**/themesc                 S   s   g | ]}|‘qS © r   )Ú.0Úpathr   r   úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyterlab_server/themes_handler.pyÚ
<listcomp>!   ó    z,ThemesHandler.initialize.<locals>.<listcomp>T)Ú	recursive)Údefault_filenameÚno_cache_pathsN)Úextendr   Ú
isinstanceÚlistr   Ú
initializeÚ
themes_url)
Úselfr	   r   r   r   Zlabextensions_pathÚkwargsZ	ext_pathsZext_dirZtheme_patternr   r   r
   r      s    
ÿzThemesHandler.initializec                 C   s.   t j |¡\}}|dkr&t |||¡S |  ¡ S )zÌRetrieve 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Úextr   r   r
   r   -   s    zThemesHandler.get_contentc                 C   sB   | j dusJ ‚tj | j ¡\}}|dkr2t | ¡S t|  ¡ ƒS d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_sizec                    s�   | j dusJ ‚t| j dƒ�}| ¡  d¡}W d  ƒ n1 s>0    Y  tj | j¡ tjd¡}t	| j
|ƒ‰ d}‡ fdd„}t |||¡ d¡S )z"Get the mangled css file contents.NÚrbzutf-8ú/zurl\('(.*)'\)|url\('(.*)'\)c                    sL   |   ¡ }tdd„ |  ¡ D ƒƒ}t|ƒ}| d¡s6|jr:|S | |tˆ |ƒ¡S )z6Replace the matched relative url with the mangled url.c                 s   s   | ]}|r|V  qd S )Nr   )r   Úgr   r   r
   Ú	<genexpr>U   r   z;ThemesHandler._get_css.<locals>.replacer.<locals>.<genexpr>r%   )ÚgroupÚnextÚgroupsr   Ú
startswithÚschemeÚreplaceÚujoin)Úmr(   ÚpartÚparsed©Zbasepathr   r
   ÚreplacerQ   s    z(ThemesHandler._get_css.<locals>.replacer)r!   ÚopenÚreadÚdecoder   r	   Údirnamer-   Úsepr.   r   ÚreÚsubÚencode)r   ÚfidÚdataZbasedirÚpatternr3   r   r2   r
   r   C   s    ,zThemesHandler._get_css)NNNN)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r   r   r   r   r
   r      s       ú

	r   )rB   r   r9   r   Úurllib.parser   Zjupyter_server.base.handlersr   Zjupyter_server.utilsr   r.   r   r   r   r   r
   Ú<module>   s   