
    Fe:                    ^   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\  rddlmZ nddlm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 ddlmZ e
rddlmZ  ej>                  e       Z! e"       Z#dZ$ddZ% G d d      Z&ddZ' G d d      Z(y)z"Theming support for HTML builders.    )annotationsN)path)TYPE_CHECKINGAny)ZipFile)   
   )entry_points)package_dir)
ThemeError)__)logging)	ensuredir)Sphinxz
theme.confc                   t        |       t        |       5 }|j                         D ]  }|j                  d      rt	        j
                  ||      }t        t	        j                  |             t        t	        j
                  |      d      5 }|j                  |j                  |             ddd        	 ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)z%Extract zip file to target directory./wbN)
r   r   namelistendswithr   joindirnameopenwriteread)filename	targetdirarchivenameentryfps         0/usr/lib/python3/dist-packages/sphinx/theming.pyextract_zipr"   %   s    i		 -g$$& 	-D}}S!IIi.Edll5)*dii&- -d+,- -	-- -- -- -s$   A9C!C1
CC
CCc                  >    e Zd ZdZddZd	dZefd
dZdddZddZ	y)ThemezA Theme is a set of HTML templates and configurations.

    This class supports both theme directory and theme archive (zipped theme).c                \   || _         d | _        d | _        t        j                  |      rd | _        || _        nUt        j                  d      | _        t        j                  | j                  |      | _        t        || j
                         t        j                         | _        | j                  j                  t        j                  | j
                  t              d       	 | j                  j                  dd      }|dk7  r	 |j)                  |      | _        y y # t        j                   $ r}t#        t%        d      |z        |d }~wt        j&                  $ r}t#        t%        d      |z        |d }~ww xY w# t"        $ r}t#        t%        d	      ||fz        |d }~ww xY w)
N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   
theme_pathfactoryr)   excs         r!   __init__zTheme.__init__8   sn   		::j!DL&DM $++E2DL IIdllD9DM
DMM2"2244==)<wO	]kkoogy9G f;#NN73	  ** 	[R HIDPQWZZ)) 	]R JKdRSY\\	]  ; $N!O")4"1 27:;;s<   (D" 
F "F 5EF #E;;F 	F+F&&F+c                    | j                   | j                  gS | j                  g| j                   j                         z   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.
        )r+   r.   get_theme_dirsr9   s    r!   r?   zTheme.get_theme_dirsX   s8     99MM?"MM?TYY%=%=%???    c                @   	 | j                   j                  ||      S # t        j                  t        j                  f$ r\}| j
                  r"| j
                  j                  |||      cY d}~S |t        u rt        t        d      ||fz        ||cY d}~S d}~ww xY w)zdReturn the value for a theme configuration setting, searching the
        base theme chain.
        Nz:setting %s.%s occurs in none of the searched theme configs)
r3   r5   r1   r7   r6   r+   
get_config	NODEFAULTr   r   )r9   sectionr   defaultr<   s        r!   rC   zTheme.get_configa   s    		;;??7D11**L,G,GH 	yyyy++GT7CC)#  %= ">AH$"P QVYZN	s'    #B(B)B/#BBBNc                   |i }| j                   r| j                   j                         }ni }t        j                  t        j
                        5  |j                  | j                  j                  d             ddd       |j                         D ]0  \  }}||vr"t        j                  t        d      |z         ,|||<   2 |S # 1 sw Y   NxY w)z6Return a dictionary of theme options and their values.Noptionsz!unsupported theme option %r given)r+   get_options
contextlibsuppressr1   r6   updater3   itemsloggerwarningr   )r9   	overridesrH   optionvalues        r!   rI   zTheme.get_optionsp   s    I99ii++-GG  !<!<= 	9NN4;;,,Y78	9 '__. 	(MFEW$r"EFOP"'		( 	9 	9s   +C		Cc                   | j                   rAt        j                  t              5  t	        j
                  | j                          ddd       | j                  r| j                  j                          yy# 1 sw Y   1xY w)zRemove temporary directories.N)r,   rJ   rK   	Exceptionshutilrmtreer+   cleanupr@   s    r!   rW   zTheme.cleanup   sZ    <<$$Y/ ,dll+, 99II , ,s    A66A?)r   strr:   rX   r;   HTMLThemeFactoryreturnNone)rZ   z	list[str])rE   rX   r   rX   rF   r   rZ   r   )N)rP   zdict[str, Any] | NonerZ   zdict[str, Any]rZ   r[   )
__name__
__module____qualname____doc__r=   r?   rD   rC   rI   rW    rA   r!   r$   r$   3   s*    R;@@ BK * rA   r$   c                    	 t        |       5 }t        |j                         v cddd       S # 1 sw Y   yxY w# t        $ r Y yw xY w)zBCheck whether the specified file is an archived theme file or not.NF)r   r4   r   rT   )r   fs     r!   is_archived_themerd      sE    X 	-!

,	- 	- 	- s#   9 -	9 69 9 	AAc                  P    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y
)rY   z A factory class for HTML Themes.c                    || _         |j                  j                  | _        | j	                          t        |j                  dd       r&| j                  |j                  j                         y y )Nhtml_theme_path)	appregistryhtml_themesthemesload_builtin_themesgetattrr3   load_additional_themesrg   )r9   rh   s     r!   r=   zHTMLThemeFactory.__init__   sT    ll..  "3::0$7''

(B(BC 8rA   c                    | j                  t        j                  t        d            }|j	                         D ]  \  }}|| j
                  |<    y)zLoad built-in themes.rk   N)find_themesr   r   r   rM   rk   )r9   rk   r   r(   s       r!   rl   z$HTMLThemeFactory.load_builtin_themes   sD    !!$))K"BC!<<> 	&KD% %DKK	&rA   c                    |D ]w  }t        j                  t        j                  | j                  j                  |            }| j                  |      }|j                         D ]  \  }}|| j                  |<    y y)z7Load additional themes placed at specified directories.N)r   abspathr   rh   confdirrp   rM   rk   )r9   theme_pathsr:   abs_theme_pathrk   r   r(   s          r!   rn   z'HTMLThemeFactory.load_additional_themes   sk    % 	*J!\\$))DHH4D4Dj*QRN%%n5F%||~ *e$)D!*	*rA   c                R    |dk(  r| j                          y| j                  |       y)z,Try to load a theme with the specified name.	alabasterN)load_alabaster_themeload_external_themer9   r   s     r!   load_extra_themez!HTMLThemeFactory.load_extra_theme   s$    ;%%'$$T*rA   c                n    ddl }t        j                  |j                         d      | j                  d<   y)zLoad alabaster theme.r   Nrw   )rw   r   r   get_pathrk   )r9   rw   s     r!   rx   z%HTMLThemeFactory.load_alabaster_theme   s'    #'99Y-?-?-A;#OK rA   c                   t        d      }	 ||   }| j                  j                  j                  | j                  |j                         | j                  j
                  j                          y# t        $ r Y yw xY w)zjTry to load a theme using entry_points.

        Sphinx refers to ``sphinx_themes`` entry_points.
        zsphinx.html_themes)groupN)r
   rh   ri   load_extensionmoduler3   post_init_valuesKeyError)r9   r   theme_entry_pointsentry_points       r!   ry   z$HTMLThemeFactory.load_external_theme   sl     *0DE	,T2KHH,,TXX{7I7IJHHOO,,. 		s   A#A2 2	A>=A>c                   i }t        j                  |      s|S t        j                  |      D ]  }t        j                  ||      }t        j
                  |      rU|j                         j                  d      r6t        |      r|dd }|||<   ct        j                  t        d      |       t        j
                  t        j                  |t                    s|||<    |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   rd   rN   rO   r   r4   )r9   r:   rk   r   pathnamer   s         r!   rp   zHTMLThemeFactory.find_themes   s    !#zz*%MZZ
+ 	-EyyU3H{{8$)?)?)G$X. ":D#+F4LNN2 'E $FGLN ;;tyy9=>$,F5M	- rA   c                    || j                   vr| j                  |       || j                   vrt        t        d      |z        t	        || j                   |   |       S )zCreate an instance of theme.z-no theme named %r found (missing theme.conf?))r;   )rk   r{   r   r   r$   rz   s     r!   r8   zHTMLThemeFactory.create   sW    t{{"!!$'t{{"R OPSWWXXT4;;t,d;;rA   N)rh   r   rZ   r[   r\   )rt   rX   rZ   r[   )r   rX   rZ   r[   )r:   rX   rZ   zdict[str, str])r   rX   rZ   r$   )r]   r^   r_   r`   r=   rl   rn   r{   rx   ry   rp   r8   ra   rA   r!   rY   rY      s/    *D&*+P
*<rA   rY   )r   rX   r   rX   rZ   r[   )r   rX   rZ   bool))r`   
__future__r   r1   r   rU   sysr/   r   typingr   r   zipfiler   version_infoimportlib.metadatar
   importlib_metadatarJ   sphinxr   sphinx.errorsr   sphinx.localer   sphinx.utilr   sphinx.util.osutilr   sphinx.applicationr   	getLoggerr]   rN   objectrD   r4   r"   r$   rd   rY   ra   rA   r!   <module>r      s    ( "  	  
   % w//   $   () 
		8	$H		-Y  Y xP< P<rA   