U
    \Shv                     @  s  U d Z ddlm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mZmZ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m Z 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,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8 ertddl9m:Z: ddl;m<Z< e.=e>Z?ddddddd dd!dddd"dg d#Z@d$eAd%< d&ZBd'ZCd(ZDd)ZEd*ZFeDe&d+eEe&d,eFe&d-iZGde5d.ZHd/eAd0< erdd1lmIZImJZJ dd2lKmLZL dd3lMmNZN dd4lOmPZP dd5lQmRZR dd6lSmTZT dd7lUmVZV dd8lWmXZX dd9lYmZZZ dd:l[m\Z\ dd;l]m^Z^ dd<l_m`Z` dd=lambZb dd>lcmdZd G d?d@ d@eJeeef ZfnegZfG dAdB dBZhdCdDdEdFdGZidS )HzGlobal creation environment.    )annotationsN)defaultdict)copy)datetimetimezone)path)TYPE_CHECKINGAnyCallable	GeneratorIterator)nodes)Node)addnodes)Config)Domain)TocTree)BuildEnvironmentErrorDocumentErrorExtensionErrorSphinxError)EventManager)__)Project)SphinxTransformer)DownloadFilesFilenameUniqDictlogging)LoggingReporter)CatalogRepositorydocname_to_domain)is_translatable)
canon_pathos_path)Sphinx)BuilderidlinkFTzhttps://peps.python.org/z&https://datatracker.ietf.org/doc/html/z	utf-8-sig   )Zauto_id_prefixZimage_loadingZembed_stylesheetZcloak_email_addressesZpep_base_urlZpep_referencesZrfc_base_urlZrfc_referencesinput_encodingZdoctitle_xformZsectsubtitle_xformZsection_self_linkZ
halt_levelZfile_insertion_enabledZsmartquotes_localeszdict[str, Any]default_settings:               z
new configzconfig changedzextensions changed)nonetextzdict[str, bool | Callable]versioning_conditions)LiteralMutableMapping)overload)CDomain)ChangeSetDomain)CitationDomain)	CPPDomain)IndexDomain)JavaScriptDomain)
MathDomain)PythonDomain)
ReSTDomain)StandardDomain)DurationDomain)
TodoDomainc                   @  s8  e Zd ZedddddZedddddZed	d
dddZedddddZedddddZedddddZedddddZedddddZedddddZedddd dZed!d"dd#dZed$d%dd&dZed'd(dd)dZd*d Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3S )4_DomainsTypezLiteral['c']r6   )keyreturnc                 C  s   d S N selfrC   rF   rF   N/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/environment/__init__.py__getitem__a   s    z_DomainsType.__getitem__zLiteral['cpp']r9   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   c   s    zLiteral['changeset']r7   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   e   s    zLiteral['citation']r8   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   g   s    zLiteral['index']r:   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   i   s    zLiteral['js']r;   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   k   s    zLiteral['math']r<   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   m   s    zLiteral['py']r=   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   o   s    zLiteral['rst']r>   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   q   s    zLiteral['std']r?   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   s   s    zLiteral['duration']r@   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   u   s    zLiteral['todo']rA   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   w   s    strr   c                 C  s   d S rE   rF   rG   rF   rF   rI   rJ   y   s    c                 C  s   t d S rE   NotImplementedErrorrG   rF   rF   rI   rJ   {       c                 C  s   t d S rE   rL   )rH   rC   valuerF   rF   rI   __setitem__|   rN   z_DomainsType.__setitem__c                 C  s   t d S rE   rL   rG   rF   rF   rI   __delitem__}   rN   z_DomainsType.__delitem__c                 C  s   t d S rE   rL   rH   rF   rF   rI   __iter__~   rN   z_DomainsType.__iter__c                 C  s   t d S rE   rL   rR   rF   rF   rI   __len__   rN   z_DomainsType.__len__N)	__name__
__module____qualname__r5   rJ   rP   rQ   rS   rT   rF   rF   rF   rI   rB   `   s>   rB   c                   @  s<  e Zd ZU dZde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ddddZ
ddddddZdddd d!Zd"d ddd#d$d%Zdd&d'd(d)Zdpdddd+d,d-Zdqdd&d/d0d1d2Zed3d	d4d5Zdd6dd7d8d9Zdd:d;d<d=Zdd3d>d?d@dAZddddBdCZedd	dDdEZdrddGdHdIdJZddd'dKdLZddd'dMdNZdd	dOdPZddQdRdSdTZddUddVdWZejdUd	dXdYZ dsdd6d[dddUd\d]d^Z!dtdd6d`ddGddddadb	dcddZ"dUdd6ddedfdgZ#dUdddhdidjZ$dkd	dldmZ%dd	dndoZ&d.S )uBuildEnvironmentz
    The environment in which the ReST files are translated.
    Stores an inventory of cross-file targets and provides doctree
    transformations to resolve links to them.
    rB   domainsr$   )appc                 C  s<  d | _ d | _d | _d | _d | _d | _d | _d | _d | _d | _	d | _
t | _t | _| | jd< i | _tt| _tt| _t | _i | _i | _tt| _i | _i | _i | _i | _i | _i | _i | _ i | _!t | _"t | _#i | _$t% | _&t' | _(i | _)i | _*i | _+i | _,i | _-i | _.i | _/i | _0i | _1i | _2i | _3| 4| d S )Nenv)5rZ   
doctreedirsrcdirconfigconfig_statusconfig_status_extraeventsprojectversionversioning_conditionversioning_comparerB   rY   r*   r   settingsall_docsr   setdependenciesincludedreread_always_pickled_doctree_cache_write_doc_doctree_cachedictmetadatatitlesZ
longtitlesZtocsZtoc_num_entriesZtoc_secnumbersZtoc_fignumberstoctree_includesfiles_to_rebuildZglob_toctreesZnumbered_toctrees
domaindatar   Zimagesr   ZdlfilesZoriginal_image_uri	temp_dataZref_contextZ_search_index_titlesZ_search_index_filenamesZ_search_index_mappingZ_search_index_title_mappingZ_search_index_all_titlesZ_search_index_index_entriesZ_search_index_objtypesZ_search_index_objnamessetup)rH   rZ   rF   rF   rI   __init__   s\    




zBuildEnvironment.__init__rn   )rD   c                 C  s   | j  }|jdi dd |S )z'Obtains serializable data for pickling.N)rZ   rY   ra   )__dict__r   update)rH   rw   rF   rF   rI   __getstate__  s    
zBuildEnvironment.__getstate__None)staterD   c                 C  s   | j | d S rE   )rw   rx   )rH   r{   rF   rF   rI   __setstate__	  s    zBuildEnvironment.__setstate__)rZ   rD   c                 C  s   | j r$| j |j|kr$ttd| jrB| j|jkrBttd| jrV|j| j || _|j	| _	|j
| _
|j| _|j| _|j|| _ t | _|j| D ]}|| j|j< q| j D ]}|  q| |j | |j dS )zSet up BuildEnvironment object.z%build environment version not currentzsource directory has changedN)rc   registryZget_envversionr   r   r]   rb   ZrestorerZ   r\   ra   rB   rY   Zcreate_domainsnamevaluesru   _update_configr^   _update_settings)rH   rZ   domainrF   rF   rI   ru     s&    
zBuildEnvironment.setupr   )r^   rD   c                 C  s   t | _d| _| jdkrt| _n| jj|jkrt| _tt| jjt|jA }t	|dkrb|d }ndt	|f }d|d| _n<|
dD ]0}| j|j |jkrt| _d|jd| _ qq|| _dS )	z!Update configurations by new one. Nr,   r   z%dz ()r[   )	CONFIG_OKr_   r`   r^   
CONFIG_NEW
extensionsCONFIG_EXTENSIONS_CHANGEDsortedrh   lenfilterr~   rO   CONFIG_CHANGED)rH   r^   r   	extensionitemrF   rF   rI   r   ,  s&    

zBuildEnvironment._update_configc                 C  s6   |j | jd< |j| jd< |j| jd< | jdd dS )zUpdate settings by new config.r)   trim_footnote_reference_spaceZlanguage_codeZsmart_quotesTN)source_encodingrf   r   language
setdefault)rH   r^   rF   rF   rI   r   F  s    z!BuildEnvironment._update_settingszstr | Callablebool)methodcomparerD   c                 C  sT   t |r|}n|tkr"td| t| }| jd|fkrDttd|| _|| _dS )aX  This sets the doctree versioning method for this environment.

        Versioning methods are a builder property; only builders with the same
        versioning method can share the same doctree directory.  Therefore, we
        raise an exception if the user tries to use an environment with an
        incompatible versioning method.
        zinvalid versioning method: %rNzdThis environment is incompatible with the selected builder, please choose another doctree directory.)callabler2   
ValueErrorrd   r   r   re   )rH   r   r   	conditionrF   rF   rI   set_versioning_methodO  s    	z&BuildEnvironment.set_versioning_methodrK   )docnamerD   c                 C  sP   || j kr2| j |d | j|d | j| | j D ]}|| q<dS )z4Remove all traces of a source file in the inventory.N)rg   poprj   rk   discardrY   r   	clear_doc)rH   r   r   rF   rF   rI   r   f  s    
zBuildEnvironment.clear_docz	list[str])docnamesotherrZ   rD   c                 C  s   t |}|D ]:}|j| | j|< |j| | j|< ||jkr| j| q| j D ]\}}|||j|  qR| j	
d| || dS )zMerge global information gathered about *docnames* while reading them
        from the *other* environment.

        This possibly comes from a parallel build process.
        zenv-merge-infoN)rh   rg   rj   rk   addrY   itemsZmerge_domaindatars   ra   emit)rH   r   r   rZ   r   
domainnamer   rF   rF   rI   merge_info_fromp  s    
z BuildEnvironment.merge_info_from
str | NonefilenamerD   c                 C  s   | j |S )zReturn the docname for the filename if the file is document.

        *filename* should be absolute or relative to the source directory.
        )rb   path2docrH   r   rF   rF   rI   r     s    zBuildEnvironment.path2docT)r   baserD   c                 C  s   | j ||S )zReturn the filename for the document name.

        If *base* is True, return absolute path under self.srcdir.
        If *base* is False, return relative path to self.srcdir.
        )rb   doc2path)rH   r   r   rF   rF   rI   r     s    zBuildEnvironment.doc2pathNztuple[str, str])r   r   rD   c                 C  sn   t |}|dtjfr&|dd }n&t| j|p6| jdd}t||}t	t
|t
t| j|fS )a+  Return paths to a file referenced from a document, relative to
        documentation root and absolute.

        In the input "filename", absolute filenames are taken as relative to the
        source dir, while relative filenames are relative to the dir of the
        containing document.
        /r,   NF)r   )r#   
startswithossepr   dirnamer   r   joinr"   normpathr]   )rH   r   r   Zrel_fnZdocdirrF   rF   rI   
relfn2path  s    zBuildEnvironment.relfn2pathzset[str]c                 C  s   | j jS )zcontains all existing docnames.)rb   r   rR   rF   rF   rI   
found_docs  s    zBuildEnvironment.found_docsr%   )r^   builderrD   c           	   
   C  s   z| j j| j j |  }| j|| j j |jrt| j	| j j
| j j| j j}dd |jD }| jD ].}t|| j j}||krd| j| ||  qdW n: tk
r } zttd| j	|f |W 5 d}~X Y nX dS )zYFind all source files in the source dir and put them in
        self.found_docs.
        c                 S  s   i | ]}|j |jqS rF   )r   Zmo_path).0crF   rF   rI   
<dictcomp>  s      z/BuildEnvironment.find_files.<locals>.<dictcomp>z"Failed to scan documents in %s: %rN)r^   Zexclude_patternsZtemplates_pathZget_asset_pathsrb   discoverZinclude_patternsZuse_message_catalogr   r]   Zlocale_dirsr   r   Zcatalogsr   r    Zgettext_compactri   r   OSErrorr   r   )	rH   r^   r   Zexclude_pathsrepoZmo_pathsr   r   excrF   rF   rI   
find_files  s.     
zBuildEnvironment.find_filesz#tuple[set[str], set[str], set[str]])config_changedrD   c              
   C  s  t | j| j }t  }t  }|r*| j}nv| jD ]l}|| jkrXtd| || q0t| j|d }t	|std| || q0|| j
krtd| || q0| j| }t| |}||krtj|d tjd}	tj|d tjd}
td||	|
 || q0| j| D ]~}zPt| j|}t	|sN|| W  q0t|}||krp|| W  q0W n& tk
r   || Y  q0Y nX qq0|||fS )z&Return (added, changed, removed) sets.z[build target] added %r.doctreez[build target] changed %ri@B )tzz$[build target] outdated %r: %s -> %s)rh   rg   r   loggerdebugr   r   r   r\   isfilerk   _last_modified_timer   r   fromtimestampr   utcri   r]   r   )rH   r   Zremovedaddedchangedr   r   mtimeZnewmtimeZmtime_dtZnewmtime_dtdepZdeppathZdepmtimerF   rF   rI   get_outdated_files  sZ    








  





z#BuildEnvironment.get_outdated_fileszGenerator[str, None, None])rZ   alreadyrD   c                 c  sB   g }| j d| D ]}|| qt|D ]}||kr*|V  q*d S )Nzenv-get-updated)ra   r   extendrh   )rH   rZ   r   Z
to_rewriter   r   rF   rF   rI   check_dependents  s    z!BuildEnvironment.check_dependentsc                 C  s2   || j d< | jj| j d< | j| jj| j d< dS )z*Prepare to set up environment for reading.r   default_roleZdefault_domainN)rt   r^   r   rY   getZprimary_domain)rH   r   rF   rF   rI   prepare_settings  s    
z!BuildEnvironment.prepare_settingsc                 C  s
   | j d S )z;Returns the docname of the document currently being parsed.r   )rt   rR   rF   rF   rI   r     s    zBuildEnvironment.docnamer   int)categoryrD   c                 C  s(   |d }| j |d}|d | j |< |S )zReturn a serial number, e.g. for index entry targets.

        The number is guaranteed to be unique in the current document.
        Zserialnor   r,   )rt   r   )rH   r   rC   currF   rF   rI   new_serialno  s    zBuildEnvironment.new_serialnoc                 C  s   | j | j | dS )zAdd *filename* as a dependency of the current document.

        This means that the document will be rebuilt if this file changes.

        *filename* should be absolute or relative to the source directory.
        N)ri   r   r   r   rF   rF   rI   note_dependency$  s    z BuildEnvironment.note_dependencyc                 C  s$   |  |}|r | j| j | dS )zAdd *filename* as a included from other document.

        This means the document is not orphaned.

        *filename* should be absolute or relative to the source directory.
        N)r   rj   r   r   )rH   r   docrF   rF   rI   note_included-  s    
zBuildEnvironment.note_includedc                 C  s   | j | j dS )zxAdd the current document to the list of documents that will
        automatically be re-read at the next build.
        N)rk   r   r   rR   rF   rF   rI   note_reread8  s    zBuildEnvironment.note_rereadr   )r   rD   c              
   C  sF   z| j | W S  tk
r@ } zttd| |W 5 d}~X Y nX dS )zReturn the domain instance with the specified name.

        Raises an ExtensionError if the domain is not registered.
        zDomain %r is not registeredN)rY   KeyErrorr   r   )rH   r   r   rF   rF   rI   
get_domain>  s    zBuildEnvironment.get_domainznodes.documentc                 C  s   z| j | }W nN tk
r\   t| j|d }t|d}|  }| j |< W 5 Q R X Y nX t|}| |j	_
t| ||_|S )z:Read the doctree for a file from the pickle and return it.r   rb)rl   r   r   r   r\   openreadpickleloadsrf   r[   r   r   reporter)rH   r   Z
serialisedr   fdoctreerF   rF   rI   get_doctreeJ  s    "
zBuildEnvironment.get_doctreec                 C  s   |  | jjS rE   )r   r^   root_docrR   rF   rF   rI   master_doctreeX  s    zBuildEnvironment.master_doctreeFznodes.document | None)r   r   r   prune_toctreesincludehiddenrD   c                 C  s   |dkrPz(| j |}| |j_t| ||_W n tk
rN   | |}Y nX | 	|| |
tjD ]>}t| j|||||d}|dkr|j|g  qh|| qh|S )zgRead the doctree from the pickle, resolve cross-references and
        toctrees and return it.
        N)pruner   )rm   r   rf   r[   r   r   r   r   r   apply_post_transformsfindallr   toctreer   resolveparentreplaceZreplace_self)rH   r   r   r   r   r   ZtoctreenoderesultrF   rF   rI   get_and_resolve_doctree\  s"    z(BuildEnvironment.get_and_resolve_doctreer   zaddnodes.toctreezNode | None)	r   r   r   r   maxdepthtitles_onlycollapser   rD   c	           	   
   C  s   t | ||||||||S )a   Resolve a *toctree* node into individual bullet lists with titles
        as items, returning None (if no containing titles are found) or
        a new node.

        If *prune* is True, the tree is pruned to *maxdepth*, or if that is 0,
        to the value of the *maxdepth* option on the *toctree* node.
        If *titles_only* is True, only toplevel document titles will be in the
        resulting tree.
        If *collapse* is True, all branches not containing docname will
        be collapsed.
        )r   r   )	rH   r   r   r   r   r   r   r   r   rF   rF   rI   resolve_toctree~  s      z BuildEnvironment.resolve_toctree)r   fromdocnamer   rD   c                 C  s   |  || d S rE   )r   )rH   r   r   r   rF   rF   rI   resolve_references  s    z#BuildEnvironment.resolve_references)r   r   rD   c                 C  sb   zDt| j }|| j d< t|}||  || jj  |  W 5 || _ X | j	
d|| dS )zApply all post-transforms.r   zdoctree-resolvedN)rt   r   r   Zset_environmentZadd_transformsrZ   r}   Zget_post_transformsZapply_transformsra   r   )rH   r   r   backupZtransformerrF   rF   rI   r     s    


z&BuildEnvironment.apply_post_transformszdict[str, list[str | None]]c                   s|   t  dddd fddi }d  jj}d }t|\}}|D ]"\}}|||g||< |}|}|}qF||d g||< |S )Nr   rK   z Iterator[tuple[str | None, str]])r   r   rD   c                 3  s   | |kr"t jtd|ddd d S | |fV  |  j|pDg D ]4}||D ]$\}}|krT||fV  | qTqFd S )Nz'self referenced toctree found. Ignored.ZtocZcircular)locationtypesubtype)r   warningr   r   rq   r   )r   r   childZ	subparentZ
subdocnamerH   traverse_toctreeZ	traversedrF   rI   r     s    
 


z<BuildEnvironment.collect_relations.<locals>.traverse_toctree)rh   r^   r   next)rH   Z	relationsr   Zprevdocr   r   Z
nextparentZnextdocrF   r   rI   collect_relations  s    z"BuildEnvironment.collect_relationsc                 C  s   t  j| j  }t| jD ]H}|| jkr|| jjkr8q||krBqd| j	| krRqt
jtd|d q| j D ]}|  qp| jd|  dS )zDo consistency checks.Zorphanz&document isn't included in any toctree)r   zenv-check-consistencyN)rh   unionrj   r   r   rg   rr   r^   r   ro   r   r   r   rY   check_consistencyra   r   )rH   rj   r   r   rF   rF   rI   r    s    


z"BuildEnvironment.check_consistency)T)N)r   )NTF)Tr   FFF)'rU   rV   rW   __doc____annotations__rv   ry   r|   ru   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   	functoolscached_propertyr   r   r   r   r   r  r  rF   rF   rF   rI   rX      sX   
s 	
;


	   #         $rX   zstr | os.PathLike[str]r   r   c                 C  s   t | jd  S )a5  Return the last modified time of ``filename``.

    The time is returned as integer microseconds.
    The lowest common denominator of modern file-systems seems to be
    microsecond-level precision.

    We prefer to err on the side of re-rendering a file,
    so we round up to the nearest microsecond.
    i)r   statst_mtime_ns)r   rF   rF   rI   r     s    r   )jr  
__future__r   r  r   r   collectionsr   r   r   r   r   typingr   r	   r
   r   r   Zdocutilsr   Zdocutils.nodesr   Zsphinxr   Zsphinx.configr   Zsphinx.domainsr   Z#sphinx.environment.adapters.toctreer   Zsphinx.errorsr   r   r   r   Zsphinx.eventsr   Zsphinx.localer   Zsphinx.projectr   Zsphinx.transformsr   Zsphinx.utilr   r   r   Zsphinx.util.docutilsr   Zsphinx.util.i18nr   r    Zsphinx.util.nodesr!   Zsphinx.util.osutilr"   r#   Zsphinx.applicationr$   Zsphinx.buildersr%   	getLoggerrU   r   r*   r  ZENV_VERSIONr   r   r   r   ZCONFIG_CHANGED_REASONr2   r3   r4   typing_extensionsr5   Zsphinx.domains.cr6   Zsphinx.domains.changesetr7   Zsphinx.domains.citationr8   Zsphinx.domains.cppr9   Zsphinx.domains.indexr:   Zsphinx.domains.javascriptr;   Zsphinx.domains.mathr<   Zsphinx.domains.pythonr=   Zsphinx.domains.rstr>   Zsphinx.domains.stdr?   Zsphinx.ext.durationr@   Zsphinx.ext.todorA   rK   rB   rn   rX   r   rF   rF   rF   rI   <module>   s   
   "    ^