U
    \Sh                     @  s&  d Z ddlmZ ddlZddlZddl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 ejdkrxddlmZ nddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ e
rddlmZ eeZ e! Z"dZ#ddddddZ$G dd dZ%dddddZ&G dd dZ'dS )z"Theming support for HTML builders.    )annotationsN)path)TYPE_CHECKINGAny)ZipFile)   
   )entry_points)package_dir)
ThemeError)__)logging)	ensuredir)Sphinxz
theme.confstrNone)filename	targetdirreturnc                 C  s   t | t| f}| D ]V}|dr*qt||}t t| tt|d}||	| W 5 Q R X qW 5 Q R X dS )z%Extract zip file to target directory./wbN)
r   r   namelistendswithr   joindirnameopenwriteread)r   r   archivenameentryfp r"   A/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/theming.pyextract_zip#   s    

r$   c                   @  sl   e Zd ZdZdddddddZdd	d
dZefdddddddZi fdddddZdd	ddZ	dS )ThemezA Theme is a set of HTML templates and configurations.

    This class supports both theme directory and theme archive (zipped theme).r   HTMLThemeFactoryr   )r   
theme_pathfactoryr   c              
   C  sN  || _ d | _d | _t|r*d | _|| _n(td| _t| j|| _t	|| j t
 | _| jjt| jtdd z| jdd}W nj t
jk
r } zttd| |W 5 d }~X Y n6 t
jk
r } zttd| |W 5 d }~X Y nX |dkrJz||| _W n: tk
rH } zttd	||f |W 5 d }~X Y nX d S )
NZsxtzutf-8)encodingthemeinheritz%theme %r doesn't have "theme" settingz'theme %r doesn't have "inherit" settingnonez(no theme named %r found, inherited by %r)r   baserootdirr   isdirthemedirtempfilemkdtempr   r$   configparserRawConfigParserconfigr   	THEMECONFgetNoSectionErrorr   r   NoOptionErrorcreate)selfr   r'   r(   r+   excr"   r"   r#   __init__6   s4    

"$
zTheme.__init__z	list[str]r   c                 C  s(   | j dkr| jgS | jg| j   S dS )zReturn a list of theme directories, beginning with this theme's,
        then the base theme's, then that one's base theme's, etc.
        N)r-   r0   get_theme_dirsr;   r"   r"   r#   r?   V   s    
zTheme.get_theme_dirsr   )sectionr   defaultr   c              
   C  s   z| j ||W S  tjtjfk
r~ } zH| jrH| j||| W Y ,S |tkrftt	d||f || W Y S d}~X Y nX dS )zdReturn the value for a theme configuration setting, searching the
        base theme chain.
        z:setting %s.%s occurs in none of the searched theme configsN)
r5   r7   r3   r9   r8   r-   
get_config	NODEFAULTr   r   )r;   rA   r   rB   r<   r"   r"   r#   rC   _   s    zTheme.get_configzdict[str, Any])	overridesr   c                 C  s~   | j r| j  }ni }z|| jd W n tjk
rB   Y nX | D ],\}}||krptt	d|  qL|||< qL|S )z6Return a dictionary of theme options and their values.optionsz!unsupported theme option %r given)
r-   get_optionsupdater5   itemsr3   r8   loggerwarningr   )r;   rE   rF   optionvaluer"   r"   r#   rG   n   s    
zTheme.get_optionsc                 C  s@   | j r,zt| j  W n tk
r*   Y nX | jr<| j  dS )zRemove temporary directories.N)r.   shutilrmtree	Exceptionr-   cleanupr@   r"   r"   r#   rQ      s    zTheme.cleanupN)
__name__
__module____qualname____doc__r=   r?   rD   rC   rG   rQ   r"   r"   r"   r#   r%   1   s    	r%   bool)r   r   c              
   C  sJ   z.t | }t| kW  5 Q R  W S Q R X W n tk
rD   Y dS X dS )zBCheck whether the specified file is an archived theme file or not.FN)r   r6   r   rP   )r   fr"   r"   r#   is_archived_theme   s
    
$rX   c                   @  s   e Zd ZdZdddddZdddd	Zd
ddddZd
ddddZddddZd
ddddZ	d
ddddZ
d
ddddZdS )r&   z A factory class for HTML Themes.r   r   )appr   c                 C  s8   || _ |jj| _|   t|jdd r4| |jj d S )Nhtml_theme_path)	rY   registryZhtml_themesthemesload_builtin_themesgetattrr5   load_additional_themesrZ   )r;   rY   r"   r"   r#   r=      s
    
zHTMLThemeFactory.__init__r>   c                 C  s2   |  ttd}| D ]\}}|| j|< qdS )zLoad built-in themes.r\   N)find_themesr   r   r
   rI   r\   )r;   r\   r   r*   r"   r"   r#   r]      s    z$HTMLThemeFactory.load_builtin_themesr   )theme_pathsr   c                 C  sJ   |D ]@}t t | jj|}| |}| D ]\}}|| j|< q0qdS )z7Load additional themes placed at specified directories.N)r   abspathr   rY   Zconfdirr`   rI   r\   )r;   ra   r'   Zabs_theme_pathr\   r   r*   r"   r"   r#   r_      s
    
z'HTMLThemeFactory.load_additional_themes)r   r   c                 C  s    |dkr|    n
| | dS )z,Try to load a theme with the specified name.	alabasterN)load_alabaster_themeload_external_themer;   r   r"   r"   r#   load_extra_theme   s    
z!HTMLThemeFactory.load_extra_themec                 C  s"   ddl }t| d| jd< dS )zLoad alabaster theme.r   Nrc   )rc   r   r   get_pathr\   )r;   rc   r"   r"   r#   rd      s    z%HTMLThemeFactory.load_alabaster_themec                 C  sR   t dd}z.|| }| jj| j|j | jj  W dS  tk
rL   Y nX dS )zjTry to load a theme using entry_points.

        Sphinx refers to ``sphinx_themes`` entry_points.
        zsphinx.html_themes)groupN)r	   rY   r[   Zload_extensionmoduler5   Zpost_init_valuesKeyError)r;   r   Ztheme_entry_pointsentry_pointr"   r"   r#   re      s    
z$HTMLThemeFactory.load_external_themezdict[str, str])r'   r   c                 C  s   i }t |s|S t|D ]r}t ||}t |rt| drtt|rb|dd }|||< qt	
td| qt t |tr|||< q|S )z'Search themes from specified directory.z.zipNzAfile %r on theme path is not a valid zipfile or contains no theme)r   r/   oslistdirr   isfilelowerr   rX   rJ   rK   r   r6   )r;   r'   r\   r    pathnamer   r"   r"   r#   r`      s    



zHTMLThemeFactory.find_themesr%   c                 C  sB   || j kr| | || j kr.ttd| t|| j | | dS )zCreate an instance of theme.z-no theme named %r found (missing theme.conf?))r(   )r\   rg   r   r   r%   rf   r"   r"   r#   r:      s
    


zHTMLThemeFactory.createN)rR   rS   rT   rU   r=   r]   r_   rg   rd   re   r`   r:   r"   r"   r"   r#   r&      s   r&   )(rU   
__future__r   r3   rn   rN   sysr1   r   typingr   r   zipfiler   version_infoimportlib.metadatar	   Zimportlib_metadataZsphinxr
   Zsphinx.errorsr   Zsphinx.localer   Zsphinx.utilr   Zsphinx.util.osutilr   Zsphinx.applicationr   	getLoggerrR   rJ   objectrD   r6   r$   r%   rX   r&   r"   r"   r"   r#   <module>   s4   

\	