o
    Ni                     @  s  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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mZ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"m#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/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z; ddl<m=Z= ddl<m>Z> erddl?m@Z@mAZAmBZB ddlCmDZD ddlEmFZF ddlGmHZH ddlImJZJ ddlKmLZL e MeNZOG dd dZPG d d! d!ZQdS )"z$Builder superclass for all builders.    )annotationsN)nullcontext)path)TYPE_CHECKINGAnyLiteralfinal)nodes)DependencyList)CONFIG_CHANGED_REASON	CONFIG_OKBuildEnvironment)ImageAdapter)SphinxError)__)get_filetypeloggingrst)import_object)
BuildPhase)bold)progress_messagestatus_iterator)sphinx_domains)CatalogInfoCatalogRepositorydocname_to_domain)SEP
canon_path	ensuredirrelative_urirelpath)ParallelTasksSerialTasksmake_chunksparallel_available)
directives)roles)IterableSequenceSet)Node)Sphinx)Config)EventManager)Tagsc                   @  s  e Zd ZU dZdZded< dZded< dZded< ded< d	Zd
Z	d
Z
ded< dZg Zded< d
Zded< d
Zded< d~ddZdddZddd Zdd!d"Zdd#d$Zddd)d*Zddd-d.Zdd0d1Zdd2d3Zdd6d7Zdd;d<Zdd=d>Zdd@dAZddBdCZeddDdEZeddGdHZeddIdJZ e	%	KdddQdRZ!eddSdTZ"ddUdVZ#ddYdZZ$edd[dd]d^Z%edd[dd`daZ&e	KdddedfZ'ddhdiZ(ddjdkZ)ddldmZ*ddndoZ+ddpdqZ,ddrdsZ-ddtduZ.ddvdwZ/ddxdyZ0dd|d}Z1d%S )Builderz6
    Builds target formats from the reST sources.
     strnameformatepilogtype[nodes.NodeVisitor]default_translator_classnoneFboolallow_parallelT	list[str]supported_image_typessupported_remote_imagessupported_data_uri_imagesappr,   envr   returnNonec                 C  s   |j | _ |j| _|j| _|j| _t| j || _|| _| j| j| j	 |j
| _
|j| _|j| _| j| j | j| j | jd| j  | jd| j  i | _d| _d| _d| _d | _d S )Nformat_builder_r1   F)srcdirconfdiroutdir
doctreedirr   r?   r@   set_versioning_methodversioning_methodversioning_compareeventsconfigtagsaddr4   r3   imagesimagedirimgpathparallel_okfinish_tasks)selfr?   r@    rV   L/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/builders/__init__.py__init__`   s(   

zBuilder.__init__argsr   c                 G  s   | j j| S )zReturn a class of translator.)r?   registryget_translator_classrU   rY   rV   rV   rW   r[   }   s   zBuilder.get_translator_classnodes.NodeVisitorc                 G  s   | j jj| g|R  S )zReturn an instance of translator.

        This method returns an instance of ``default_translator_class`` by default.
        Users can replace the translator class with ``app.set_translator()`` API.
        )r?   rZ   create_translatorr\   rV   rV   rW   r^      s   zBuilder.create_translatorc                 C     dS )zoLoad necessary templates and perform initialization.  The default
        implementation does nothing.
        NrV   rU   rV   rV   rW   init      zBuilder.initc                 C  s<   | j jrt| j jdd}| | _dS ddlm} | | _dS )z&Return the template bridge configured.ztemplate_bridge setting)sourcer   )BuiltinTemplateLoaderN)rM   template_bridger   	templatessphinx.jinja2gluerd   )rU   template_bridge_clsrd   rV   rV   rW   create_template_bridge   s   zBuilder.create_template_bridgeNdocnametyp
str | Nonec                 C     t )zReturn the target URI for a document name.

        *typ* can be used to qualify the link characteristic for individual
        builders.
        NotImplementedError)rU   rj   rk   rV   rV   rW   get_target_uri   s   zBuilder.get_target_urifrom_toc                 C  s   t | || ||S )zReturn a relative URI between two source filenames.

        :raises: :exc:`!NoUri` if there's no way to return a sensible URI.
        )r    rp   )rU   rq   rr   rk   rV   rV   rW   get_relative_uri   s   
zBuilder.get_relative_uristr | Iterable[str]c                 C  rm   )aD  Return an iterable of output files that are outdated, or a string
        describing what an update build will build.

        If the builder does not output individual files corresponding to
        source files, return a string here.  If it does, return an iterable
        of those files that need to be written.
        rn   r`   rV   rV   rW   get_outdated_docs   s   zBuilder.get_outdated_docsc                 C  s   g S )z;Return list of paths for assets (ex. templates, CSS, etc.).rV   r`   rV   rV   rW   get_asset_paths      zBuilder.get_asset_pathsdoctreer+   c                 C  s   t | j}|tjD ]f}d|d v rqd|d vr[| jD ]}|d |d}|r+ n+qt|d }||d }|rIt	j
td| j|||d nt	j
td| j||d q||d< n|d }|| jjvrfq| jj| d	 | j|< qdS )
z+Pick the best candidate for all image URIs.?
candidates*Nuriz2a suitable image for %s builder not found: %s (%s)locationz-a suitable image for %s builder not found: %s   )r   r@   findallr	   imager<   getsortedget_original_image_uriloggerwarningr   r3   rP   )rU   rx   rP   nodeimgtype	candidate	mimetypes	image_urirV   rV   rW   post_process_images   sB   


zBuilder.post_process_imagescatalogsset[CatalogInfo]messagec                   sn    j jsd S d fdd}tttd|  t|tdd	t| jj	|d
D ]}|
 j j j j q(d S )Ncatr   rA   r2   c                   s   t | j jjtjtS N)r!   mo_pathr@   rE   replacer   sepr   )r   r`   rV   rW   cat2relpath   s   z-Builder.compile_catalogs.<locals>.cat2relpathzbuilding [mo]: writing output... 	darkgreen)stringify_func)r   r   rA   r2   )rM   gettext_auto_buildr   infor   r   r   lenr?   	verbositywrite_molanguage gettext_allow_fuzzy_translations)rU   r   r   r   catalogrV   r`   rW   compile_catalogs   s    
zBuilder.compile_catalogsc                 C  sH   t | j| jj| jj| jj}tdtt|j	 }| 
t|j	| d S )Nzall of %d po files)r   rE   rM   locale_dirsr   source_encodingr   r   listr   r   set)rU   repor   rV   rV   rW   compile_all_catalogs   s   zBuilder.compile_all_catalogsspecified_filesc                   s   d fdd}t  }t t||}t j jj jj jj}|jD ]}|j	|v r2|
 r2|| q"tdt| } || d S )	Nfpathr2   rA   rl   c                   s(    j t| }|rt| jjS d S r   )r@   path2docr   abspathr   rM   gettext_compact)r   rj   r`   rV   rW   	to_domain  s   z4Builder.compile_specific_catalogs.<locals>.to_domainz*targets for %d po files that are specified)r   r2   rA   rl   )r   mapr   rE   rM   r   r   r   r   domainis_outdatedrO   r   r   r   )rU   r   r   r   domainsr   r   r   rV   r`   rW   compile_specific_catalogs  s   

z!Builder.compile_specific_catalogsc                 C  sL   t | j| jj| jj| jj}dd |jD }tdt| }| 	|| d S )Nc                 S  s   h | ]}|  r|qS rV   )r   ).0crV   rV   rW   	<setcomp>"  s    z2Builder.compile_update_catalogs.<locals>.<setcomp>z,targets for %d po files that are out of date)
r   rE   rM   r   r   r   r   r   r   r   )rU   r   r   r   rV   rV   rW   compile_update_catalogs  s   zBuilder.compile_update_catalogsc                 C  s    |    | jdtddd dS )zBuild all source files.Nzall source filesall)summarymethod)r   buildr   r`   rV   rV   rW   	build_all(  s   zBuilder.build_all	filenamesc                 C  s   g }|D ]?}t t |}t |sttd| q|t| j	s-ttd| q| j
|}|s>ttd| q|| q| | | j|dtdt| d dS )z>Only rebuild as much as needed for changes in the *filenames*.z.file %r given on command line does not exist, zIfile %r given on command line is not under the source directory, ignoringz?file %r given on command line is not a valid document, ignoringspecificz%%d source files given on command line)r   r   N)r   normpathr   isfiler   r   r   
startswithr2   rE   r@   r   appendr   r   r   )rU   r   docnamesfilenamerj   rV   rV   rW   build_specific/  s@   


zBuilder.build_specificc                 C  sR   |    |  }t|tr| dg| dS t|}| j|tdt| d dS )z8Only rebuild what was changed or added since last build.__all__z0targets for %d source files that are out of date)r   N)r   ru   
isinstancer2   r   r   r   r   )rU   to_buildrV   rV   rW   build_update\  s   

zBuilder.build_updateupdater   Iterable[str] | Noner   r   $Literal['all', 'specific', 'update']c           	   	   C  s.  |rt ttd| | j | jjrt nt	  t
|  }W d   n1 s+w   Y  t|}t jttddd || j| j| t|| }|r[t td| nt td |rdd	lm} ttd
+ tt| j|d}t| j|tj W d   n1 sw   Y  W d   n1 sw   Y  tj| j_ttd | j  W d   n1 sw   Y  n|dkr|st ttd dS tj| j_|r|dgkrt
|| jj@ }t r| jj!dkr| j"r| j#d| _$nd| _$t% | _&| '|t(|| | )  | j&  dS )zMain build method, usually called by a specific ``build_*`` method.

        First updates the environment, and then calls
        :meth:`!write`.
        zbuilding [%s]: Nz"looking for now-outdated files... Tnonlz%d foundz
none foundr   )ENV_PICKLE_FILENAMEzpickling environmentwbzchecking consistencyr   zno targets are out of date.r   r   writeF)*r   r   r   r   r3   r?   _exception_on_warningr   r   pending_warningsr   readr   r   r@   check_dependentssphinx.applicationr   r   openr   joinrH   pickledumpHIGHEST_PROTOCOLr   CONSISTENCY_CHECKphasecheck_consistency	RESOLVING
found_docsr%   parallelr:   is_parallel_allowedrS   r#   rT   r   r   finish)	rU   r   r   r   updated_docnamesdoccountoutdatedr   frV   rV   rW   r   l  sX   
 

zBuilder.buildc                   s  t jttddd | j| j|  | jjtk}| j	|\}}}| j
d| j|||D ]}|t|| jj@  q-|s?|rJ|| jj| jj@  |rbt| jjd| jjpXd }t jd|dd t tdt|t|t| |D ]}| j
d| j| | j| qtt||B }| j
d	| j| tr| jjd
kr| jd}	nd}	|	r| j|| jjd n| | | jj| jjvr>ddlm}
 ddl m!  | j"| jj}t#||
D ]}t$% |sqtd}t&|||f | jj'D ]}t$% |sqtd}t&|||f t| jj(dhkr4t) fdd| jj(D s4td}t&||| jj(f td}t&|| | j
d| jD ]}|durR|*| qFt| j_t|S )z(Re-)read all files new or changed since last update.

        Store all environment docnames in the canonical format (ie using SEP as
        a separator in place of os.path.sep).
        zupdating environment: Tr   zenv-get-outdatedr1   z[%s] z %s added, %s changed, %s removedenv-purge-doczenv-before-read-docsr   r   Fnprocr   )EXCLUDE_PATHS)_translate_patternzSphinx is unable to load the master document (%s) because it matches a built-in exclude pattern %r. Please move your master document to a different location.zSphinx is unable to load the master document (%s) because it matches an exclude pattern specified in conf.py, %r. Please remove this pattern from conf.py.z**c                 3  s     | ]}t  |V  qd S r   )rematch)r   patr   master_doc_canonrV   rW   	<genexpr>  s
    
zBuilder.read.<locals>.<genexpr>zSphinx is unable to load the master document (%s) because it is not included in the custom include_patterns = %r. Ensure that a pattern in include_patterns matches the master document.zSphinx is unable to load the master document (%s). The master document must be within the source directory or a subdirectory of it.zenv-updatedN)+r   r   r   r   r@   
find_filesrM   config_statusr   get_outdated_filesrL   emitr   r   r   glob_toctreesr   r   config_status_extrar   	clear_docr   r%   r?   r   r   _read_parallel_read_serial
master_docall_docssphinx.projectr   sphinx.util.matchingr   doc2pathr   r   r   r   exclude_patternsinclude_patternsanyextend)rU   updatedaddedchangedremoveddocsreasonrj   r   par_okr   master_doc_pathr   msgretvalrV   r   rW   r     s   

"


zBuilder.readc                 C  sN   t |tddt|| jjD ]}| jd| j| | j| | 	| qd S )Nreading sources... purpler   )
r   r   r   r?   r   rL   r   r@   r   read_doc)rU   r   rj   rV   rV   rW   r   ,  s   zBuilder._read_serialr   intc           	        s   t ||}t|tddt|jj |D ]}jdj| j	| qdfdd	}d fdd}t
|}|D ]	}|||| q<|  td d S )Nr  r  r   r  r;   rA   bytesc                   s2    j  j_ | D ]	} j|dd qt jtjS )NF_cache)r?   r@   r  r   dumpsr   )r  rj   r`   rV   rW   read_processG  s   
z,Builder._read_parallel.<locals>.read_processotherenvrB   c                   s(   t |}j| |j t  d S r   )r   loadsr@   merge_info_fromr?   next)r  r  r@   progressrU   rV   rW   mergeN  s   
z%Builder._read_parallel.<locals>.merger1   )r  r;   rA   r  )r  r;   r  r  rA   rB   )r$   r   r   r   r?   r   rL   r   r@   r   r"   add_taskr   r   r   )	rU   r   r   chunksrj   r  r  taskschunkrV   r  rW   r   9  s   
zBuilder._read_parallelr  r  c          	   	   C  sB  | j | t| jd}t|r| j | t| j |}t	| j
jj|}| j
j| j
|}|j| j jd< t |j_t| j 7 t|| jj t|}td| |j|d |  |j}W d   n1 smw   Y  W d   n1 s|w   Y  t d | j j|< | j j   | j j!   | j"|||d dS )z>Parse a file and add/update inventory entries for the doctree.zdocutils.conf_parsersphinx)source_pathNi  r  )#r@   prepare_settingsr   r   rF   r   note_dependencyr2   r   r   r?   rM   source_suffixrZ   get_publisherparser	temp_datar
   settingsrecord_dependenciesr   r   default_role_UnicodeDecodeErrorHandlercodecsregister_error
set_sourcepublishdocumenttimetime_nsr   clearref_contextwrite_doctree)	rU   rj   r  docutilsconfr   filetype	publishererror_handlerrx   rV   rV   rW   r  \  s2   

 zBuilder.read_docnodes.documentc                C  s   d|_ d|_|j |_d|j_d|j_d|j_t| j	|d }t
t| t|d}t||tj W d   n1 s@w   Y  |rO|| jj|< dS dS )z@Write the doctree to a file, to be used as a cache by re-builds.Nz.doctreer   )reportertransformerr+  copywarning_streamr@   r,  r   r   rH   r   dirnamer   r   r   r   _write_doc_doctree_cache)rU   rj   rx   r  doctree_filenamer   rV   rV   rW   r8    s   
zBuilder.write_doctreebuild_docnamesr   Sequence[str]c                 C  sB  | j d|  |du s|dgkr| jj}|dkr!t|t|B }nt|}ttddt	| | j
jh}|D ]}| jj|t D ]}|| jjv rQ|| qDq9||O }tt	| jj | j_ttd | | W d   n1 syw   Y  ttdd	d
 |   W d   n1 sw   Y  | | dS )z$Write builder specific output files.zwrite-startedNr   r   zdocnames to write: %sz, zpreparing documentszcopying assetsFr   )rL   r   r@   r   r   r   debugr   r   r   rM   root_docfiles_to_rebuildr   rO   dicttoctree_includesitemsr   prepare_writingcopy_assetswrite_documents)rU   rE  r   r   r   extrarj   
tocdocnamerV   rV   rW   r     s.   	


zBuilder.writeSet[str]c                 C  s6   t |}| jr| j|| jjd d dS | | dS )zWrite all documents in *docnames*.

        This method can be overridden if a builder does not create
        output files for each document.
        r   r   N)r   rS   _write_parallelr?   r   _write_serial)rU   r   sorted_docnamesrV   rV   rW   rO    s   zBuilder.write_documentsc                 C  s   | j jrt nt 7 t|tddt|| j jD ]}t	j
| j _| j|| }t	j| j _| || | || qW d    d S 1 sEw   Y  d S )Nr   r   )r?   r   r   r   r   r   r   r   r   r   r   r   r@   get_and_resolve_doctreeWRITINGwrite_doc_serialized	write_doc)rU   r   rj   rx   rV   rV   rW   rT    s$   

"zBuilder._write_serialc                   s  dfdd}|d |dd  }}t jj_j|}t jj_|| || t	|}t
||}t|td	d
t|jj d fdd}t jj_|D ]$}	g }
|	D ]}j|}|| |
||f qZ|||
| qT|  td d S )Nr   list[tuple[str, nodes.document]]rA   rB   c                   s(   t j j_| D ]
\}} || qd S r   )r   rW  r?   r   rY  )r  rj   rx   r`   rV   rW   write_process  s   
z.Builder._write_parallel.<locals>.write_processr   r   r   r   rY   resultc                   s   t   d S r   )r  )rY   r\  )r  rV   rW   on_chunk_done  s   z.Builder._write_parallel.<locals>.on_chunk_doner1   )r  rZ  rA   rB   )rY   rZ  r\  rB   rA   rB   )r   r   r?   r   r@   rV  rW  rX  rY  r"   r$   r   r   r   r   r   r  r   r   r   )rU   r   r   r[  	firstnamerx   r   r  r]  r!  argrj   rV   r  rW   rS    s6   



zBuilder._write_parallelc                 C  r_   )z?A place where you can add logic before :meth:`write_doc` is runNrV   )rU   r   rV   rV   rW   rM    rw   zBuilder.prepare_writingc                 C  r_   )zBWhere assets (images, static files, etc) are copied before writingNrV   r`   rV   rV   rW   rN    rw   zBuilder.copy_assetsc                 C  rm   )ab  
        Write the output file for the document

        :param docname: the :term:`docname <document name>`.
        :param doctree: defines the content to be written.

        The output filename must be determined within this method,
        typically by calling :meth:`~.Builder.get_target_uri`
        or :meth:`~.Builder.get_relative_uri`.
        rn   rU   rj   rx   rV   rV   rW   rY    s   zBuilder.write_docc                 C  r_   )zoHandle parts of write_doc that must be called in the main process
        if parallel build is active.
        NrV   r`  rV   rV   rW   rX  *  rb   zBuilder.write_doc_serializedc                 C  r_   )zWFinish the building process.

        The default implementation does nothing.
        NrV   r`   rV   rV   rW   r   0     zBuilder.finishc                 C  r_   )zQCleanup any resources.

        The default implementation does nothing.
        NrV   r`   rV   rV   rW   cleanup7  ra  zBuilder.cleanupoptiondefaultc                 C  sL   z| j  d| }t| j|W S  ty%   | d| }t| j| Y S w )zReturn a builder specific option.

        This method allows customization of common builder settings by
        inserting the name of the current builder in the option key.
        If the key does not exist, use default as builder name.
        _)r3   getattrrM   AttributeError)rU   rc  rd  optnamerV   rV   rW   get_builder_config>  s   	zBuilder.get_builder_config)r?   r,   r@   r   rA   rB   )rY   r   rA   r6   )rY   r   rA   r]   )rA   rB   r   )rj   r2   rk   rl   rA   r2   )rq   r2   rr   r2   rk   rl   rA   r2   )rA   rt   )rA   r;   )rx   r+   rA   rB   )r   r   r   r2   rA   rB   )r   r;   rA   rB   )r   r;   rA   rB   )Nr   )r   r   r   rl   r   r   rA   rB   )r   r;   rA   rB   )r   r;   r   r  rA   rB   )rj   r2   r  r9   rA   rB   )rj   r2   rx   r=  r  r9   rA   rB   )r   )rE  r   r   rF  r   r   rA   rB   )r   rR  rA   rB   )r   rF  rA   rB   )r   rF  r   r  rA   rB   )rj   r2   rx   r=  rA   rB   )rc  r2   rd  r2   rA   r   )2__name__
__module____qualname____doc__r3   __annotations__r4   r5   rJ   rK   r:   use_message_catalogr<   r=   r>   rX   r[   r^   ra   ri   rp   rs   ru   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r8  r   rO  rT  rS  rM  rN  rY  rX  r   rb  ri  rV   rV   rV   rW   r0   9   sv   
 



	






)



,P
n
#%
'


+




r0   c                   @  s$   e Zd ZdZdddZdddZdS )r.  z8Custom error handler for open() that warns and replaces.rj   r2   rA   rB   c                C  s
   || _ d S r   )rj   )rU   rj   rV   rV   rW   rX   R  s   
z#_UnicodeDecodeErrorHandler.__init__errorUnicodeDecodeErrortuple[str, int]c                 C  s   |j dd|j}|j d|j}|dkrt|j }|j dd|jd }tjtd|j |d |j d |j |j|j	  d |j |j	|  | j
|fd d	|j	fS )
N   
r   r   z5undecodable source characters, replacing with "?": %rs   >>>s   <<<r}   ry   )objectrfindstartfindr   countr   r   r   endrj   )rU   rp  
line_startline_endline_numrV   rV   rW   __call__U  s&   

z#_UnicodeDecodeErrorHandler.__call__N)rj   r2   rA   rB   )rp  rq  rA   rr  )rj  rk  rl  rm  rX   r~  rV   rV   rV   rW   r.  O  s    
r.  )Rrm  
__future__r   r/  r   r   r4  
contextlibr   osr   typingr   r   r   r   docutilsr	   docutils.utilsr
   sphinx.environmentr   r   r   !sphinx.environment.adapters.assetr   sphinx.errorsr   sphinx.localer   sphinx.utilr   r   r   sphinx.util._importerr   sphinx.util.build_phaser   sphinx.util.consoler   sphinx.util.displayr   r   sphinx.util.docutilsr   sphinx.util.i18nr   r   r   sphinx.util.osutilr   r   r   r    r!   sphinx.util.parallelr"   r#   r$   r%   r#  r&   r'   collections.abcr(   r)   r*   docutils.nodesr+   r   r,   sphinx.configr-   sphinx.eventsr.   sphinx.util.tagsr/   	getLoggerrj  r   r0   r.  rV   rV   rV   rW   <module>   sT    
      