o
    NiN                     @  s   d Z ddlmZ dZddl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 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dkrjddl Z nddl!Z erddl"m#Z# ddlm$Z$ ddl%m&Z& ddl'm(Z( G dd de$ddZ)G dd de$ddZ*G dd de$ddZ+e,e-Z.e/ Z0dZ1dZ2G dd dZ3G d d! d!Z4dPd&d'Z5dQd.d/Z6dRd2d3Z7dSd6d7Z8dTd9d:Z9dUd<d=Z:dVd?d@Z;dWdBdCZ<dXdDdEZ=dYdFdGZ>G dHd> d>Z?dZdLdMZ@e-dNkreAe@ejBdOd dS )[z"Theming support for HTML builders.    )annotations)ThemeHTMLThemeFactoryN)entry_points)path)TYPE_CHECKINGAny)ZipFile)package_dir)check_confval_types)
ThemeError)__)logging)	ensuredir)      )Callable)	TypedDict)Required)Sphinxc                   @  s   e Zd ZU ded< ded< dS )
_ThemeTomlzRequired[_ThemeTomlTheme]themedict[str, str]optionsN__name__
__module____qualname____annotations__ r   r   B/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/theming.pyr   '      
 r   F)totalc                   @  s.   e Zd ZU ded< ded< ded< ded< dS )	_ThemeTomlThemezRequired[str]inherit	list[str]stylesheetssidebars_ThemeTomlThemePygmentspygments_styleNr   r   r   r   r    r#   +   s
   
 r#   c                   @  s   e Zd ZU ded< ded< dS )r(   strdefaultdarkNr   r   r   r   r    r(   1   r!   r(   z
theme.tomlz
theme.confc                   @  sH   e Zd ZdZdddZdddZefdddZdd ddZd!ddZ	dS )"r   zA Theme is a set of HTML templates and configurations.

    This class supports both theme directory and theme archive (zipped theme).
    namer*   configsdict[str, _ConfigFile]pathsr%   tmp_dirsreturnNonec                C  s   || _ t|| _|| _i }d| _d| _d | _d | _t|	 D ]+}||j
O }|jd ur/|j| _|jd ur8|j| _|jd urA|j| _|jd urJ|j| _q|| _d S )Nr   )r-   tuple_dirs	_tmp_dirsr&   sidebar_templatespygments_style_defaultpygments_style_darkreversedvaluesr   _options)selfr-   r.   r0   r1   r   configr   r   r    __init__C   s(   






zTheme.__init__c                 C  s
   t | j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.
        )listr5   r=   r   r   r    get_theme_dirsa   s   
zTheme.get_theme_dirssectionr+   r   c                 C  s   |dkr5|dkrd | jp|}n8|dkrd | jp|}n+|dkr(| jp&|}n!|dkr2| jp0|}n|}n|dkrA| j||}ntd}t||t	u rYtd	||f }t||S )
zdReturn the value for a theme configuration setting, searching the
        base theme chain.
        r   
stylesheet, r'   r)   pygments_dark_styler   zoTheme configuration sections other than [theme] and [options] are not supported (tried to get a value from %r).z:setting %s.%s occurs in none of the searched theme configs)
joinr&   r7   r8   r9   r<   getr   r   _NO_DEFAULT)r=   rC   r-   r+   valuemsgr   r   r    
get_configg   s.   zTheme.get_configN	overridesdict[str, Any] | Nonedict[str, Any]c                 C  sN   |du ri }| j  }| D ]\}}||vr ttd| q|||< q|S )z6Return a dictionary of theme options and their values.Nz!unsupported theme option %r given)r<   copyitemsloggerwarningr   )r=   rM   r   optionrJ   r   r   r    get_options   s   

zTheme.get_optionsc              	   C  sD   | j D ]}tt t| W d   n1 sw   Y  qdS )zRemove temporary directories.N)r6   
contextlibsuppress	Exceptionshutilrmtree)r=   tmp_dirr   r   r    _cleanup   s   
zTheme._cleanup)
r-   r*   r.   r/   r0   r%   r1   r%   r2   r3   )r2   r%   )rC   r*   r-   r*   r+   r   r2   r   N)rM   rN   r2   rO   r2   r3   )
r   r   r   __doc__r?   rB   rI   rL   rU   r\   r   r   r   r    r   =   s    

r   c                   @  sP   e Zd ZdZdddZddd	ZdddZdddZedddZ	dddZ
dS )r   z A factory class for HTML Themes.appr   r2   r3   c                 C  sF   || _ |jj| _i | _|   t|jdd r| |jj	 | 
  d S )Nhtml_theme_path)_appregistryhtml_themes_themes_entry_point_themes_load_builtin_themesgetattrr>   _load_additional_themesra   _load_entry_point_themes)r=   r`   r   r   r    r?      s   
zHTMLThemeFactory.__init__c                 C  s2   |  ttd}| D ]	\}}|| j|< qdS )zLoad built-in themes.themesN)_find_themesr   rG   r
   rQ   re   )r=   rk   r-   r   r   r   r    rg      s   z%HTMLThemeFactory._load_builtin_themestheme_pathsr%   c                 C  sJ   |D ] }t t | jj|}| |}| D ]	\}}|| j|< qqdS )z7Load additional themes placed at specified directories.N)r   abspathrG   rb   confdirrl   rQ   re   )r=   rm   
theme_pathabs_theme_pathrk   r-   r   r   r   r    ri      s   
z(HTMLThemeFactory._load_additional_themesc                 C  sB   t ddD ]}|j| jv rq| j|jfdd	d
}|| j|j< qdS )zTry to load a theme with the specified name.

        This uses the ``sphinx.html_themes`` entry point from package metadata.
        zsphinx.html_themes)groupr`   r   theme_moduler*   r2   r3   c                 S  s   |  | t| | j d S r]   )setup_extension_config_post_initr>   )r`   rs   r   r   r    _load_theme_closure   s   
zFHTMLThemeFactory._load_entry_point_themes.<locals>._load_theme_closureN)r`   r   rs   r*   r2   r3   )r   r-   re   rb   modulerf   )r=   entry_pointrv   r   r   r    rj      s   z)HTMLThemeFactory._load_entry_point_themesrp   r*   r   c                 C  s   i }t | s	|S t| D ]F}t | |}t |r:| dr:t|r1|dd }|||< qt	
td| qt |t}t |t}t |sPt |rT|||< q|S )z'Search themes from specified directory.z.zipNzAfile %r on theme path is not a valid zipfile or contains no theme)r   isdiroslistdirrG   isfilelowerendswith_is_archived_themerR   rS   r   _THEME_TOML_THEME_CONF)rp   rk   entrypathnamer-   	toml_path	conf_pathr   r   r    rl      s*   

zHTMLThemeFactory._find_themesr-   r   c                 C  sZ   || j v r| j | }|  || jvrttd| t|| j| j \}}}t||||dS )zCreate an instance of theme.z-no theme named %r found (missing theme.toml?))r.   r0   r1   )rf   re   r   r   _load_theme_with_ancestorsr   )r=   r-   entry_point_loaderrk   
theme_dirsr1   r   r   r    create   s   



zHTMLThemeFactory.createN)r`   r   r2   r3   r^   )rm   r%   r2   r3   )rp   r*   r2   r   )r-   r*   r2   r   )r   r   r   r_   r?   rg   ri   rj   staticmethodrl   r   r   r   r   r    r      s    

	

r   filenamer*   r2   boolc                C  sb   z&t | }t| }t|v pt|v W  d   W S 1 sw   Y  W dS  ty0   Y dS w )zBCheck whether the specified file is an archived theme file or not.NF)r	   	frozensetnamelistr   r   rX   )r   fr   r   r   r    r      s   
(r   r-   rm   r   entry_point_themesdict[str, Callable[[], None]]3tuple[dict[str, _ConfigFile], list[str], list[str]]c                C  s   i }g }g }t dD ]Z}t| ||  \}}}	}
|| |	d ur%||	 |
|| < |dkr/ n@||v r=td|  }t|||v rL||vrL|| }|  ||vrbtd| |dt|f }t||} q
td|  }t||||fS )N
   nonez%The %r theme has circular inheritancezQThe %r theme inherits from %r, which is not a loaded theme. Loaded themes are: %srE   z#The %r theme has too many ancestors)range_load_themeappendr   r   rG   sorted)r-   rm   r   rk   r   r1   _r$   	theme_dirr[   r>   rK   r   r   r   r    r      s8   


r   rp   (tuple[str, str, str | None, _ConfigFile]c          
      C  s   t |r
d }|}ntd}t || }t|| t t |t }r3t|}t	|| }t
|}n!t t |t }rLt|}	t|	| }t|	}nttd| ||||fS )Nsxtz'no theme configuration file found in %r)r   rz   tempfilemkdtemprG   _extract_zipr}   r   _load_theme_toml_validate_theme_toml_convert_theme_tomlr   _load_theme_conf_validate_theme_conf_convert_theme_confr   r   )
r-   rp   r[   r   r   
_cfg_tabler$   r>   r   _cfg_parserr   r   r    r   (  s    






r   
target_dirr3   c             
   C  s   t | t| C}| D ]5}|drqt||}t t| tt|d}||	| W d   n1 s=w   Y  qW d   dS 1 sNw   Y  dS )z%Extract zip file to target directory./wbN)
r   r	   r   r   r   rG   dirnameopenwriteread)r   r   archiver-   r   fpr   r   r    r   C  s   

"r   config_file_pathc                  sP   t | dd}| }W d    n1 sw   Y  t|  fdddD S )Nutf-8encodingc                   s   i | ]}| v r| | qS r   r   .0scr   r    
<dictcomp>U  s    z$_load_theme_toml.<locals>.<dictcomp>)r   r   )r   r   tomllibloads)r   r   config_textr   r   r    r   Q  s
   

r   cfgc                 C  s   d| vrt d| }t|| d }t|ts!t d| }t||dd}|s3t d| }t|d| v rHt| d tsHt d| }t||S )	Nr   'theme %r doesn't have the "theme" tablez+The %r theme "[theme]" table is not a tabler$    4The %r theme must define the "theme.inherit" settingr   z-The %r theme "[options]" table is not a table)r   r   
isinstancedictrH   )r   r-   rK   r   r$   r   r   r    r   X  s    
r   _ConfigFilec          	   	   C  s   | d }d|v rt |d }nd }d|v rt |d }nd }|di }t|tr9d| d}td| }t||d}|d	}t||||| d
i dS )Nr   r&   r'   r)   zpygments_style = { default = "z" }z>The "theme.pygments_style" setting must be a table. Hint: "%s"r+   r,   r   r&   r7   r8   r9   r   )r4   rH   r   r*   r   r   r   )	r   r   r&   r7   pygments_tablehintrK   r8   r9   r   r   r    r   k  s,   




r   configparser.RawConfigParserc                C  s   t  }|j| dd |S )Nr   r   )configparserRawConfigParserr   )r   r   r   r   r    r     s   r   c                 C  sF   |  dsttd| | jddd d }r|S td| }t|)Nr   r   r$   fallbackr   )has_sectionr   r   rH   )r   r-   r$   rK   r   r   r    r     s   
r   c                C  s   | j dddd }rtttj|d}nd }| j dddd }r.tttj|d}nd }| j ddd d}| j ddd d}| d	rLt| d	ni }t	|||||d
S )Nr   rD   r   r   ,r'   r)   rF   r   r   )
rH   r4   mapr*   stripsplitr   r   rQ   r   )r   rD   r&   sidebarr7   r8   r9   r   r   r   r    r     s0   r   c                   @  s8   e Zd ZdZdddZdddZdddZdddZdS )r   r   r&   tuple[str, ...] | Noner7   r8   
str | Noner9   r   r   r2   r3   c                 C  s&   || _ || _|| _|| _| | _d S r]   )r&   r7   r8   r9   rP   r   )r=   r&   r7   r8   r9   r   r   r   r    r?     s
   z_ConfigFile.__init__r*   c                 C  s6   | j j d| jd| jd| jd| jd| jdS )Nz(stylesheets=z, sidebar_templates=z, pygments_style_default=z, pygments_style_dark=z
, options=))	__class__r   r&   r7   r8   r9   r   rA   r   r   r    __repr__  s   
z_ConfigFile.__repr__otherobjectr   c                 C  sJ   t |tr#| j|jko"| j|jko"| j|jko"| j|jko"| j|jkS tS r]   )r   r   r&   r7   r8   r9   r   NotImplemented)r=   r   r   r   r    __eq__  s   




z_ConfigFile.__eq__intc                 C  s"   t | jj| j| j| j| j| jfS r]   )hashr   r   r&   r7   r8   r9   r   rA   r   r   r    __hash__  s   z_ConfigFile.__hash__N)r&   r   r7   r   r8   r   r9   r   r   r   r2   r3   )r2   r*   )r   r   r2   r   )r2   r   )r   r   r   	__slots__r?   r   r   r   r   r   r   r    r     s    



argvr%   r   c                   s  | d d dgkrt d| dd  } t| dkr!td t dt| d }t|t}t|r8t|sAt|d dS t	|}|
dsPtd dS |jddd d	}|s`td
 dS dd| dg}|jdddd	}|dkrz|d n|dur|d |dd ttj|dD  |d |jdddd	}|dkr|d n|dur|d |dd ttj|dD 7 }|d g }|jdddd	}	|	dur|d|	 d |jdddd	}
|
dur|d|
 d |r|d d!| d"  |
d#r"|d |d$ | fd%d|d#D 7 }t|t}t|d&d'd(}|d)|d)  W d    n	1 sEw   Y  td*| dS )+N   conf_to_tomlr   z9Usage: python -m sphinx.theming conf_to_toml <theme path>zC must be a path to a theme directory containing a "theme.conf" filer   zThe "theme" table is missing.r$   r   z'The "theme.inherit" setting is missing.z[theme]zinherit = ""rD   .r   zstylesheets = []zstylesheets = [c                 s  s    | ]	}d | dV  qdS )    "",Nr   r   r   r   r    	<genexpr>  s    z(_migrate_conf_to_toml.<locals>.<genexpr>r   ]r'   zsidebars = []zsidebars = [c                 S  s   g | ]}d | dqS )r   r   r   r   r   r   r    
<listcomp>  s    z)_migrate_conf_to_toml.<locals>.<listcomp>r)   zdefault = "rF   zdark = "zpygments_style = { rE   z }r   z	[options]c                   s4   g | ]\}}| d d  s		 | d  d qS )r   z\"Tz = ")replace)r   keyr+   dr   r    r   #  s    wr   r   
zWritten converted settings to )
SystemExitlenprintr   realpathrG   r   rz   r}   r   r   rH   r   extendr   r*   r   r   rQ   r   r   r   )r   r   r   r   r$   
toml_linesrD   r   stylesr+   r,   r   r   r   r   r    _migrate_conf_to_toml  sv   


"

 


r   __main__r   )r   r*   r2   r   )r-   r*   rm   r   r   r   r2   r   )r-   r*   rp   r*   r2   r   )r   r*   r   r*   r2   r3   )r   r*   r2   r   )r   r   r-   r*   r2   r*   )r   r   r2   r   )r   r*   r2   r   )r   r   r-   r*   r2   r*   )r   r   r2   r   )r   r%   r2   r   )Cr_   
__future__r   __all__r   rV   r{   rY   sysr   importlib.metadatar   r   typingr   r   zipfiler	   sphinxr
   sphinx.configr   ru   sphinx.errorsr   sphinx.localer   sphinx.utilr   sphinx.util.osutilr   version_infor   tomlicollections.abcr   r   typing_extensionsr   sphinx.applicationr   r   r#   r(   	getLoggerr   rR   r   rI   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    <module>   sf    


^
[


(






	
7
F