B
    öÀ„\…'  ã               @   sð   d 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 ddlZddl	m
Z
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 ddlmZ ddlmZ e e¡Zeƒ ZdZdd„ Z G dd„ deƒZ!dd„ Z"G dd„ deƒZ#dS )zÁ
    sphinx.theming
    ~~~~~~~~~~~~~~

    Theming support for HTML builders.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
é    N)Úpath)ÚZipFile)Ústring_typesÚ	iteritems)Úconfigparser)Úpackage_dir)ÚRemovedInSphinx20Warning)Ú
ThemeError)Ú__)Úlogging)Ú	ensuredirz
theme.confc             C   s„   t |ƒ t| ƒj}xb| ¡ D ]V}| d¡r,qt ||¡}t t |¡ƒ tt |¡dƒ}| | 	|¡¡ W dQ R X qW W dQ R X dS )z%Extract zip file to target directory.ú/ÚwbN)
r   r   ÚnamelistÚendswithr   ÚjoinÚdirnameÚopenÚwriteÚread)ÚfilenameZ	targetdirÚarchiveÚnameÚentryÚfp© r   ú-lib/python3.7/site-packages/sphinx/theming.pyÚextract_zip)   s    

r   c               @   s@   e Zd ZdZdd„ Zdd„ Zefdd„Zi fdd	„Zd
d„ Z	dS )ÚThemez†A Theme is a set of HTML templates and configurations.

    This class supports both theme directory and theme archive (zipped theme).c             C   s  || _ d | _d | _t |¡r*d | _|| _n(t d¡| _t | j|¡| _t	|| jƒ t
 ¡ | _| j t | jt¡¡ y| j dd¡}W nJ t
jk
rª   ttdƒ| ƒ‚Y n& t
jk
rÎ   ttdƒ| ƒ‚Y nX |dkry| |¡| _W n* tk
r   ttdƒ||f ƒ‚Y nX d S )NZsxtÚthemeÚinheritz%theme %r doesn't have "theme" settingz'theme %r doesn't have "inherit" settingZnonez(no theme named %r found, inherited by %r)r   ÚbaseÚrootdirr   ÚisdirÚthemedirÚtempfileZmkdtempr   r   r   ZRawConfigParserÚconfigr   Ú	THEMECONFÚgetÚNoSectionErrorr	   r
   ÚNoOptionErrorÚcreate)Úselfr   Ú
theme_pathÚfactoryr    r   r   r   Ú__init__=   s.    


zTheme.__init__c             C   s(   | j dkr| jgS | jg| j  ¡  S dS )z‹Return 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!   r$   Úget_theme_dirs)r,   r   r   r   r0   ^   s    
zTheme.get_theme_dirsc          	   C   sh   y| j  ||¡S  tjtjfk
rb   | jr<| j |||¡S |tkrZtt	dƒ||f ƒ‚n|S 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)
r&   r(   r   r*   r)   r!   Ú
get_configÚ	NODEFAULTr	   r
   )r,   Zsectionr   Údefaultr   r   r   r1   h   s    zTheme.get_configc             C   s‚   | j r| j  ¡ }ni }y| | j d¡¡ W n tjk
rB   Y nX x8t|ƒD ],\}}||krrt 	t
dƒ| ¡ qN|||< qNW |S )z6Return a dictionary of theme options and their values.Úoptionsz!unsupported theme option %r given)r!   Úget_optionsÚupdater&   Úitemsr   r)   r   ÚloggerÚwarningr
   )r,   Z	overridesr4   ZoptionÚvaluer   r   r   r5   y   s    zTheme.get_optionsc             C   s@   | j r,yt | j ¡ W n tk
r*   Y nX | jr<| j ¡  dS )zRemove temporary directories.N)r"   ÚshutilZrmtreeÚ	Exceptionr!   Úcleanup)r,   r   r   r   r=   Ž   s    zTheme.cleanupN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r0   r2   r1   r5   r=   r   r   r   r   r   8   s   !
r   c          	   C   s:   y t | ƒ}t| ¡ kS Q R X W n tk
r4   dS X dS )z:Check the specified file is an archived theme file or not.NF)r   r'   r   r<   )r   Úfr   r   r   Úis_archived_themeš   s
    
rC   c               @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚHTMLThemeFactoryz A factory class for HTML Themes.c             C   s6   || _ |j| _|  ¡  t|jdd ƒr2|  |jj¡ d S )NÚhtml_theme_path)ÚappZhtml_themesÚthemesÚload_builtin_themesÚgetattrr&   Úload_additional_themesrE   )r,   rF   r   r   r   r/   §   s
    zHTMLThemeFactory.__init__c             C   s6   |   t td¡¡}xt|ƒD ]\}}|| j|< qW dS )zLoad built-in themes.rG   N)Úfind_themesr   r   r   r   rG   )r,   rG   r   r   r   r   r   rH   ¯   s    z$HTMLThemeFactory.load_builtin_themesc             C   sR   xL|D ]D}t  t  | jj|¡¡}|  |¡}xt|ƒD ]\}}|| j|< q4W qW dS )z7Load additional themes placed at specified directories.N)r   Úabspathr   rF   ZconfdirrK   r   rG   )r,   Ztheme_pathsr-   Zabs_theme_pathrG   r   r   r   r   r   rJ   ¶   s
    

z'HTMLThemeFactory.load_additional_themesc             C   s2   |dkr|   ¡  n|dkr$|  ¡  n
|  |¡ dS )z)Try to load a theme having specifed name.Ú	alabasterÚsphinx_rtd_themeN)Úload_alabaster_themeÚload_sphinx_rtd_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   NrM   )rM   r   r   Zget_pathrG   )r,   rM   r   r   r   rO   É   s    z%HTMLThemeFactory.load_alabaster_themec             C   s@   y&ddl }| ¡ }t |d¡| jd< W n tk
r:   Y nX dS )z(Load sphinx_rtd_theme theme (if exists).r   NrN   )rN   Zget_html_theme_pathr   r   rG   ÚImportError)r,   rN   r-   r   r   r   rP   Ï   s    z&HTMLThemeFactory.load_sphinx_rtd_themec       	      C   sÒ   t  d|¡}y t|ƒ}| jj | j|j¡ dS  tk
r@   Y nX xŠt  d¡D ]|}| ¡ }t	|ƒrˆ|ƒ }t
|tƒsŒt tdƒ|j ¡ n|}|  |¡}x2t|ƒD ]&\}}||kr t dt¡ || j|< q W qNW dS )zjTry to load a theme using entry_points.

        Sphinx refers to ``sphinx_themes`` entry_points.
        zsphinx.html_themesNZsphinx_themesz.Theme extension %r does not respond correctly.z[``sphinx_themes`` entry point is now deprecated. Please use ``sphinx.html_themes`` instead.)Úpkg_resourcesZiter_entry_pointsÚnextrF   ÚregistryZload_extensionZmodule_nameÚStopIterationÚloadÚcallableÚ
isinstancer   r8   r9   r
   rK   r   ÚwarningsÚwarnr   rG   )	r,   r   Zentry_pointsZentry_pointÚtargetr$   rG   r   r   r   r   r   rQ   Ù   s*    


z$HTMLThemeFactory.load_external_themec             C   s˜   i }t  |¡s|S x€t |¡D ]r}t  ||¡}t  |¡rv| ¡  d¡rvt|ƒrd|dd… }|||< qt	 
tdƒ|¡ qt  t  |t¡¡r|||< qW |S )z'Search themes from specified directory.z.zipNéüÿÿÿzAfile %r on theme path is not a valid zipfile or contains no theme)r   r#   ÚosÚlistdirr   ÚisfileÚlowerr   rC   r8   r9   r
   r'   )r,   r-   rG   r   Úpathnamer   r   r   r   rK   û   s    


zHTMLThemeFactory.find_themesc             C   sX   || j kr|  |¡ || j krD|dkr4ttdƒƒ‚nttdƒ| ƒ‚t|| j | | dS )zCreate an instance of theme.rN   z~sphinx_rtd_theme is no longer a hard dependency since version 1.4.0. Please install it manually.(pip install sphinx_rtd_theme)z-no theme named %r found (missing theme.conf?))r.   )rG   rR   r	   r
   r   )r,   r   r   r   r   r+     s    


zHTMLThemeFactory.createN)r>   r?   r@   rA   r/   rH   rJ   rR   rO   rP   rQ   rK   r+   r   r   r   r   rD   ¤   s   	

"rD   )$rA   r_   r;   r%   r[   r   Zzipfiler   rT   Zsixr   r   Z	six.movesr   Zsphinxr   Zsphinx.deprecationr   Zsphinx.errorsr	   Zsphinx.localer
   Zsphinx.utilr   Zsphinx.util.osutilr   Z	getLoggerr>   r8   Úobjectr2   r'   r   r   rC   rD   r   r   r   r   Ú<module>
   s,   
b
