ó
öÀ„\c           @   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 j e ƒ Z e r8d d l m Z m  Z  m! Z! m" Z" m# Z# d d l$ m% Z% n  e& ƒ  Z' d Z( d „  Z) d e& f d „  ƒ  YZ* d „  Z+ d e& f d „  ƒ  YZ, d S(   sÁ   
    sphinx.theming
    ~~~~~~~~~~~~~~

    Theming support for HTML builders.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iÿÿÿÿN(   t   path(   t   ZipFile(   t   string_typest	   iteritems(   t   configparser(   t   package_dir(   t   RemovedInSphinx20Warning(   t
   ThemeError(   t   __(   t   logging(   t	   ensuredir(   t   Anyt   Dictt   Iteratort   Listt   Tuple(   t   Sphinxs
   theme.confc         C   s«   t  | ƒ t |  ƒ  } x… | j ƒ  D]w } | j d ƒ rA q& n  t j | | ƒ } t  t j | ƒ ƒ t t j | ƒ d ƒ  } | j | j	 | ƒ ƒ Wd QXq& WWd QXd S(   s%   Extract zip file to target directory.t   /t   wbN(
   R
   R   t   namelistt   endswithR    t   joint   dirnamet   opent   writet   read(   t   filenamet	   targetdirt   archivet   namet   entryt   fp(    (    s-   lib/python2.7/site-packages/sphinx/theming.pyt   extract_zip)   s    
t   Themec           B   sA   e  Z d  Z d „  Z d „  Z e d „ Z i  d „ Z d „  Z RS(   s†   A Theme is a set of HTML templates and configurations.

    This class supports both theme directory and theme archive (zipped theme).c         C   so  | |  _  d  |  _ d  |  _ t j | ƒ r? d  |  _ | |  _ n: t j d ƒ |  _ t j	 |  j | ƒ |  _ t
 | |  j ƒ t j ƒ  |  _ |  j j t j	 |  j t ƒ ƒ y |  j j d d ƒ } WnS t j k
 rì t t d ƒ | ƒ ‚ n* t j k
 rt t d ƒ | ƒ ‚ n X| d k rky | j | ƒ |  _ Wqkt k
 rgt t d ƒ | | f ƒ ‚ qkXn  d  S(   Nt   sxtt   themet   inherits%   theme %r doesn't have "theme" settings'   theme %r doesn't have "inherit" settingt   nones(   no theme named %r found, inherited by %r(   R   t   Nonet   baset   rootdirR    t   isdirt   themedirt   tempfilet   mkdtempR   R    R   t   RawConfigParsert   configR   t	   THEMECONFt   gett   NoSectionErrorR   R   t   NoOptionErrort   create(   t   selfR   t
   theme_patht   factoryR$   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyt   __init__=   s.    				c         C   s4   |  j  d k r |  j g S|  j g |  j  j ƒ  Sd S(   s‹   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&   R*   t   get_theme_dirs(   R4   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyR8   ^   s    
c         C   s‰   y |  j  j | | ƒ SWnk t j t j f k
 r„ |  j rR |  j j | | | ƒ S| t k r} t t	 d ƒ | | f ƒ ‚ q… | Sn Xd S(   sd   Return the value for a theme configuration setting, searching the
        base theme chain.
        s:   setting %s.%s occurs in none of the searched theme configsN(
   R.   R0   R   R2   R1   R'   t
   get_configt	   NODEFAULTR   R   (   R4   t   sectionR   t   default(    (    s-   lib/python2.7/site-packages/sphinx/theming.pyR9   h   s    	c         C   s¦   |  j  r |  j  j ƒ  } n i  } y | j |  j j d ƒ ƒ Wn t j k
 rT n XxJ t | ƒ D]< \ } } | | k r” t j	 t
 d ƒ | ƒ qb | | | <qb W| S(   s6   Return a dictionary of theme options and their values.t   optionss!   unsupported theme option %r given(   R'   t   get_optionst   updateR.   t   itemsR   R1   R   t   loggert   warningR   (   R4   t	   overridesR=   t   optiont   value(    (    s-   lib/python2.7/site-packages/sphinx/theming.pyR>   y   s    	c         C   sQ   |  j  r4 y t j |  j  ƒ Wq4 t k
 r0 q4 Xn  |  j rM |  j j ƒ  n  d S(   s   Remove temporary directories.N(   R(   t   shutilt   rmtreet	   ExceptionR'   t   cleanup(   R4   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyRI   Ž   s    		(	   t   __name__t
   __module__t   __doc__R7   R8   R:   R9   R>   RI   (    (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyR!   8   s   	!	
c         C   sB   y) t  |  ƒ  } t | j ƒ  k SWd QXWn t k
 r= t SXd S(   s:   Check the specified file is an archived theme file or not.N(   R   R/   R   RH   t   False(   R   t   f(    (    s-   lib/python2.7/site-packages/sphinx/theming.pyt   is_archived_themeš   s
    t   HTMLThemeFactoryc           B   s_   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   s    A factory class for HTML Themes.c         C   sN   | |  _  | j |  _ |  j ƒ  t | j d d  ƒ rJ |  j | j j ƒ n  d  S(   Nt   html_theme_path(	   t   appt   html_themest   themest   load_builtin_themest   getattrR.   R&   t   load_additional_themesRQ   (   R4   RR   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyR7   §   s
    	
c         C   sI   |  j  t j t d ƒ ƒ } x' t | ƒ D] \ } } | |  j | <q( Wd S(   s   Load built-in themes.RT   N(   t   find_themesR    R   R   R   RT   (   R4   RT   R   R#   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyRU   ¯   s    c         C   so   xh | D]` } t  j t  j |  j j | ƒ ƒ } |  j | ƒ } x' t | ƒ D] \ } } | |  j | <qJ Wq Wd S(   s7   Load additional themes placed at specified directories.N(   R    t   abspathR   RR   t   confdirRX   R   RT   (   R4   t   theme_pathsR5   t   abs_theme_pathRT   R   R#   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyRW   ¶   s
    !c         C   sC   | d k r |  j  ƒ  n& | d k r2 |  j ƒ  n |  j | ƒ d S(   s)   Try to load a theme having specifed name.t	   alabastert   sphinx_rtd_themeN(   t   load_alabaster_themet   load_sphinx_rtd_themet   load_external_theme(   R4   R   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyt   load_extra_theme¿   s
    c         C   s/   d d l  } t j | j ƒ  d ƒ |  j d <d S(   s   Load alabaster theme.iÿÿÿÿNR]   (   R]   R    R   t   get_pathRT   (   R4   R]   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyR_   É   s    c         C   sM   y5 d d l  } | j ƒ  } t j | d ƒ |  j d <Wn t k
 rH n Xd S(   s(   Load sphinx_rtd_theme theme (if exists).iÿÿÿÿNR^   (   R^   t   get_html_theme_pathR    R   RT   t   ImportError(   R4   R^   R5   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyR`   Ï   s    c   	      C   s"  t  j d | ƒ } y0 t | ƒ } |  j j j |  j | j ƒ d SWn t k
 rU n XxÅ t  j d ƒ D]´ } | j ƒ  } t	 | ƒ r¼ | ƒ  } t
 | t ƒ sÂ t j t d ƒ | j ƒ qÂ n | } |  j | ƒ } xF t | ƒ D]8 \ } } | | k rÞ t j d t ƒ | |  j | <qÞ qÞ Wqf Wd S(   sj   Try to load a theme using entry_points.

        Sphinx refers to ``sphinx_themes`` entry_points.
        s   sphinx.html_themesNt   sphinx_themess.   Theme extension %r does not respond correctly.s[   ``sphinx_themes`` entry point is now deprecated. Please use ``sphinx.html_themes`` instead.(   t   pkg_resourcest   iter_entry_pointst   nextRR   t   registryt   load_extensiont   module_namet   StopIterationt   loadt   callablet
   isinstanceR   RA   RB   R   RX   R   t   warningst   warnR   RT   (	   R4   R   t   entry_pointst   entry_pointt   targetR*   RT   R   R#   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyRa   Ù   s*    		c         C   sÑ   i  } t  j | ƒ s | Sx± t j | ƒ D]  } t  j | | ƒ } t  j | ƒ r¡ | j ƒ  j d ƒ r¡ t | ƒ rˆ | d  } | | | <qÉ t	 j
 t d ƒ | ƒ q) t  j t  j | t ƒ ƒ r) | | | <q) q) W| S(   s'   Search themes from specified directory.s   .zipiüÿÿÿsA   file %r on theme path is not a valid zipfile or contains no theme(   R    R)   t   ost   listdirR   t   isfilet   lowerR   RO   RA   RB   R   R/   (   R4   R5   RT   R   t   pathnameR   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyRX   û   s    $

c         C   s‚   | |  j  k r |  j | ƒ n  | |  j  k rh | d k rO t t d ƒ ƒ ‚ qh t t d ƒ | ƒ ‚ n  t | |  j  | d |  ƒS(   s   Create an instance of theme.R^   s~   sphinx_rtd_theme is no longer a hard dependency since version 1.4.0. Please install it manually.(pip install sphinx_rtd_theme)s-   no theme named %r found (missing theme.conf?)R6   (   RT   Rb   R   R   R!   (   R4   R   (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyR3     s    (   RJ   RK   RL   R7   RU   RW   Rb   R_   R`   Ra   RX   R3   (    (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyRP   ¤   s   					
		
	"	(-   RL   Rv   RF   R+   Rq   R    t   zipfileR   Rg   t   sixR   R   t	   six.movesR   t   sphinxR   t   sphinx.deprecationR   t   sphinx.errorsR   t   sphinx.localeR   t   sphinx.utilR	   t   sphinx.util.osutilR
   t	   getLoggerRJ   RA   RM   t   typingR   R   R   R   R   t   sphinx.applicationR   t   objectR:   R/   R    R!   RO   RP   (    (    (    s-   lib/python2.7/site-packages/sphinx/theming.pyt   <module>
   s2   (		b	
