U
    \ShX                     @  sp  U d Z ddlmZ ddl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
mZmZmZmZmZ ddl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mZmZ ddlmZ ddlmZm Z m!Z! ddl"m#Z#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/ e-0e1Z2e3dZ4ertddl5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z< ddl=m>Z> dej?dfiZ@dd ZAeB ZCdeDd< ed d!d"d#ZEd$d%d&d'd(ZFd$d)d*d+d,d-ZGd$d%d&d.d/ZHd$d0d*d1d2d3ZId$d*d&d4d5ZJd6d%d7d8d9ZKd6d*d7d:d;ZLd6d*d7d<d=ZMed d!d>d?ZNed d!d@dAZOedBd dCdDdEZPed d!dFdGZQedldBd dCdHdIZRG dJdK dKZSG dLdM dMeTZUG dNdO dOeSZVG dPdQ dQZWG dRdS dSe#ZXG dTdU dUe#ZYedVdWd dXdYdZZZG d[d\ d\eZ[G d]d^ d^eZ\G d_d` d`Z]G dadb dbe]Z^G dcdd ddej_Z`deeDdf< dmd$dgdhdidjdkZadS )nzUtility functions for docutils.    )annotationsN)contextmanager)copy)path)IOTYPE_CHECKINGAnyCallable	Generatorcast)nodes)
FileOutput)ElementNodesystem_message)	Directive
directivesroles)Inliner)StateStateMachine
StringList)Reporterunescape)HTMLTranslator)SphinxError)___)logging)RoleFunctionz>^(.+?:(?:\d+)?): \((DEBUG|INFO|WARNING|ERROR|SEVERE)/(\d+)?\) )
ModuleType)Values)Builder)Config)BuildEnvironment__version_info__zdocutils.__version_info__c                 C  sH   | t krtdtd| ddlm} t |  \}}|t| |dd |S )Nzmodule z has no attribute r   )_deprecation_warning)   r   )remove)_DEPRECATED_OBJECTSAttributeError__name__Zsphinx.deprecationr&   )namer&   Zdeprecated_objectcanonical_name r.   G/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/util/docutils.py__getattr__-   s    r0   zset[type[Element]]additional_nodeszGenerator[(None, None, None)]returnc               
   c  sR   ztt j} ttj}dV  W 5 | t _|t_ttD ]}t| t| q4X dS )z"Create namespace for reST parsers.N)	r   _directivesr   _roleslistr1   unregister_nodediscardr   )r4   r5   noder.   r.   r/   docutils_namespace;   s    


r:   strbool)r,   r3   c                 C  s
   | t jkS )z1Check the *name* directive is already registered.)r   r4   r,   r.   r.   r/   is_directive_registeredL   s    r>   ztype[Directive]None)r,   	directiver3   c                 C  s   t | | dS )zRegister a directive to docutils.

    This modifies global state of docutils.  So it is better to use this
    inside ``docutils_namespace()`` to prevent side-effects.
    N)r   register_directive)r,   r@   r.   r.   r/   rA   Q   s    rA   c                 C  s
   | t jkS )z,Check the *name* role is already registered.)r   r5   r=   r.   r.   r/   is_role_registeredZ   s    rB   r   )r,   roler3   c                 C  s   t | | dS )zRegister a role to docutils.

    This modifies global state of docutils.  So it is better to use this
    inside ``docutils_namespace()`` to prevent side-effects.
    N)r   Zregister_local_role)r,   rC   r.   r.   r/   register_role_   s    rD   c                 C  s   t j| d dS )z Unregister a role from docutils.N)r   r5   popr=   r.   r.   r/   unregister_roleh   s    rF   ztype[Element]r9   r3   c                 C  s   t tjd| j S )z'Check the *node* is already registered.visit_)hasattrr   GenericNodeVisitorr+   r9   r.   r.   r/   is_node_registeredm   s    rL   c                 C  s.   t tjd| j s*t| jg t|  dS )zRegister a node to docutils.

    This modifies global state of some visitors.  So it is better to use this
    inside ``docutils_namespace()`` to prevent side-effects.
    rH   N)rI   r   rJ   r+   Z_add_node_class_namesr1   addrK   r.   r.   r/   register_noder   s    rN   c                 C  s^   t tjd| j rZttjd| j  ttjd| j  ttjd| j  ttjd| j  dS )zaUnregister a node from docutils.

    This is inverse of ``nodes._add_nodes_class_names()``.
    rH   Zdepart_N)rI   r   rJ   r+   delattrZSparseNodeVisitorrK   r.   r.   r/   r7   }   s
    r7   c                  #  sF   ddl m  d
dddd fdd	} z| tj_dV  W 5  tj_X dS )zPatch docutils.languages.get_language() temporarily.

    This ignores the second argument ``reporter`` to suppress warnings.
    refs: https://github.com/sphinx-doc/sphinx/issues/3788
    r   get_languageNr;   Reporter | Noner   language_codereporterr3   c                   s    | S Nr.   rT   rU   rP   r.   r/   patched_get_language   s    z2patched_get_language.<locals>.patched_get_language)N)Zdocutils.languagesrQ   docutils	languagesrX   r.   rP   r/   rX      s    
rX   c                  #  sN   ddl m  d
dddd fdd	} z| tjjj_dV  W 5  tjjj_X dS )a^  Patch docutils.parsers.rst.languages.get_language().
    Starting from docutils 0.17, get_language() in ``rst.languages``
    also has a reporter, which needs to be disabled temporarily.

    This should also work for old versions of docutils,
    because reporter is none by default.

    refs: https://github.com/sphinx-doc/sphinx/issues/10179
    r   rP   Nr;   rR   r   rS   c                   s    | S rV   r.   rW   rP   r.   r/   rX      s    z6patched_rst_get_language.<locals>.patched_get_language)N)Zdocutils.parsers.rst.languagesrQ   rY   parsersrstrZ   r[   r.   rP   r/   patched_rst_get_language   s    
r^   z
str | None)confdirr3   c              
   c  s^   z4t jdd}| r,tt| dt jd< dV  W 5 |dkrNt jdd n
|t jd< X dS )z?Let docutils know the location of ``docutils.conf`` for Sphinx.NZDOCUTILSCONFIGzdocutils.conf)osenvironrE   getr   joinabspath)r_   Zdocutilsconfigr.   r.   r/   using_user_docutils_conf   s    
re   c               	   c  sj   dd } dd }t j}t j}tjd d dkr:| t _|t _z
d V  W 5 tjd d dkrd|t _|t _X d S )Nc                 S  sR   | j j}t| t|s.| jd| d | j| j|d|j|gdd d S )Nz<aside class="footnote-list z">
ZasideZnote)classesrC   )	settingsZfootnote_references
isinstanceZprevious_siblingtypebodyappendZstarttagtagname)selfr9   Zlabel_styler.   r.   r/   visit_footnote   s    z&du19_footnotes.<locals>.visit_footnotec                 S  s4   | j d t|jdddt|s0| j d d S )Nz	</aside>
FT)ZdescendZsiblings)rj   rk   rh   Z	next_noderi   rm   r9   r.   r.   r/   depart_footnote   s
    z'du19_footnotes.<locals>.depart_footnote   )r      )r   rn   rp   rY   r%   )rn   rp   Zold_visit_footnoteZold_depart_footnoter.   r.   r/   du19_footnotes   s    
rs   c                 c  sT   t  D t 2 t|  t  dV  W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X dS )zPatch to docutils temporarily.N)rX   r^   re   rs   )r_   r.   r.   r/   patch_docutils   s    rt   c                   @  s   e Zd ZdZddddZddddZdd	d
ddddZddddZddddZdddddddZ	ddddddddZ
dS ) CustomReSTDispatcherzCustom reST's mark-up dispatcher.

    This replaces docutils's directives and roles dispatch mechanism for reST parser
    by original one temporarily.
    r?   r2   c                 C  s   dd | _ dd | _d S )Nc                  W  s   d g fS rV   r.   argsr.   r.   r/   <lambda>       z/CustomReSTDispatcher.__init__.<locals>.<lambda>c                  W  s   d g fS rV   r.   rv   r.   r.   r/   rx      ry   )directive_funcZ
roles_funcrm   r.   r.   r/   __init__   s    
zCustomReSTDispatcher.__init__c                 C  s   |    d S rV   )enabler{   r.   r.   r/   	__enter__   s    zCustomReSTDispatcher.__enter__ztype[Exception]	Exceptionr   )exc_type	exc_value	tracebackr3   c                 C  s   |    d S rV   )disable)rm   r   r   r   r.   r.   r/   __exit__   s    zCustomReSTDispatcher.__exit__c                 C  s$   t j| _tj| _| jt _| jt_d S rV   )r   r@   rz   r   rC   	role_funcr{   r.   r.   r/   r}     s    zCustomReSTDispatcher.enablec                 C  s   | j t_| jt_d S rV   )rz   r   r@   r   r   rC   r{   r.   r.   r/   r   
  s    zCustomReSTDispatcher.disabler;   r    nodes.document3tuple[type[Directive] | None, list[system_message]]directive_namelanguage_moduledocumentr3   c                 C  s   |  |||S rV   )rz   rm   r   r   r   r.   r.   r/   r@     s    zCustomReSTDispatcher.directiveintr   )tuple[RoleFunction, list[system_message]]	role_namer   linenorU   r3   c                 C  s   |  ||||S rV   )r   rm   r   r   r   rU   r.   r.   r/   rC     s    zCustomReSTDispatcher.roleN)r+   
__module____qualname____doc__r|   r~   r   r}   r   r@   rC   r.   r.   r.   r/   ru      s   ru   c                   @  s   e Zd ZdS )ElementLookupErrorN)r+   r   r   r.   r.   r.   r/   r     s   r   c                      sl   e Zd ZdZddd fddZdddd	d
dZddddd fddZdddddd fddZ  ZS )sphinx_domainszcMonkey-patch directive and role dispatch, so that domain-specific
    markup takes precedence.
    r$   r?   )envr3   c                   s   || _ t   d S rV   )r   superr|   )rm   r   	__class__r.   r/   r|   !  s    zsphinx_domains.__init__r;   r   )ri   r,   r3   c                 C  s   |  }d|krl|dd\}}|| jjkrX| j|}t|||}|dk	rj|g fS qttd|| n4| jj	
d}|dk	rt|||}|dk	r|g fS t| jd||}|dk	r|g fS tdS )ztLookup a markup element (directive or role), given its name which can
        be a full name (with domain).
        :   Nz%unknown directive or role name: %s:%sZdefault_domainZstd)lowersplitr   domainsZ
get_domaingetattrloggerwarningr   	temp_datarb   r   )rm   ri   r,   Zdomain_namedomainelementZ
def_domainr.   r.   r/   lookup_domain_element%  s$    
z$sphinx_domains.lookup_domain_elementr    r   r   r   c                   s8   z|  d|W S  tk
r2   t ||| Y S X d S )Nr@   )r   r   r   r@   r   r   r.   r/   r@   C  s    zsphinx_domains.directiver   r   r   r   c              	     s:   z|  d|W S  tk
r4   t |||| Y S X d S )NrC   )r   r   r   rC   r   r   r.   r/   rC   K  s    zsphinx_domains.role)	r+   r   r   r   r|   r   r@   rC   __classcell__r.   r.   r   r/   r     s
   r   c                   @  s   e Zd ZdddddZdS )WarningStreamr;   r?   )textr3   c                 C  sR   t |}|s t|d n.| \}}}t d| }tj|||d d S )Nz
 location)	report_researchr   r   rstripgroupssublog)rm   r   matchedr   ri   levelmessager.   r.   r/   writeU  s    
zWarningStream.writeN)r+   r   r   r   r.   r.   r.   r/   r   T  s   r   c                      sN   e Zd Zedd dddZejejddfdddd	dd
d fddZ  Z	S )LoggingReporterr   )rU   r3   c                 C  s   | |j |j|j|j|jS )zACreate an instance of LoggingReporter from other reporter object.)sourcereport_level
halt_levelZ
debug_flagerror_handler)clsrU   r.   r.   r/   from_reporter`  s     zLoggingReporter.from_reporterFbackslashreplacer;   r   r<   r?   )r   r   r   debugr   r3   c                   s(   t tt }t j||||||d d S )N)r   )r   r   r   r   r|   )rm   r   r   r   r   r   streamr   r.   r/   r|   f  s      zLoggingReporter.__init__)
r+   r   r   classmethodr   r   ZWARNING_LEVELZSEVERE_LEVELr|   r   r.   r.   r   r/   r   _  s    r   c                      s&   e Zd ZdZdd fddZ  ZS )NullReporterz A dummy reporter; write nothing.r?   r2   c                   s   t  ddd d S )Nr   i     )r   r|   r{   r   r.   r/   r|   q  s    zNullReporter.__init__)r+   r   r   r   r|   r   r.   r.   r   r/   r   n  s   r   r   r   )statecontentr3   c                 c  sB   z0| j jj}tg d}||_|j| j j_dV  W 5 || j j_X dS )z1Switch current source input of state temporarily.N)memorU   get_source_and_liner   Zinput_lines)r   r   r   state_machiner.   r.   r/   switch_source_inputu  s    


r   c                      s<   e Zd ZdZddd fddZddd fd	d
Z  ZS )SphinxFileOutputz#Better FileOutput class for Sphinx.r   r?   )kwargsr3   c                   s,   | dd| _|dd t jf | d S )Noverwrite_if_changedFencodingzutf-8)rE   r   
setdefaultr   r|   )rm   r   r   r.   r/   r|     s    zSphinxFileOutput.__init__r;   )datar3   c              
     sn   | j rb| jrbd| jkrb| jrbtj| j rbt| j | jd"}|	 |krX|W  5 Q R  S W 5 Q R X t
 |S )Nb)r   )Zdestination_pathZ	autoclosemoder   r`   r   existsopenr   readr   r   )rm   r   fr   r.   r/   r     s    zSphinxFileOutput.write)r+   r   r   r   r|   r   r   r.   r.   r   r/   r     s   r   c                   @  s`   e Zd ZdZeddddZeddddZd	dd
dZdddddZddddZ	dS )SphinxDirectivezA base class for Sphinx directives.

    This class provides helper methods for Sphinx directives.

    .. note:: The subclasses of this class might not work with docutils.
              This class is strongly coupled with Sphinx.
    r$   r2   c                 C  s   | j jjjS z3Reference to the :class:`.BuildEnvironment` object.)r   r   rg   r   r{   r.   r.   r/   r     s    zSphinxDirective.envr#   c                 C  s   | j jS z)Reference to the :class:`.Config` object.r   configr{   r.   r.   r/   r     s    zSphinxDirective.configtuple[str, int]c                 C  s   | j | jS )zGet source and line number.)r   r   r   r{   r.   r.   r/   get_source_info  s    zSphinxDirective.get_source_infor   r?   rG   c                 C  s   |   \|_|_dS )z'Set source and line number to the node.Nr   r   linero   r.   r.   r/   set_source_info  s    zSphinxDirective.set_source_infor;   c                 C  s   d dd |  D S )&Get current location info for logging.r   c                 s  s   | ]}t |V  qd S rV   r;   .0sr.   r.   r/   	<genexpr>  s     z/SphinxDirective.get_location.<locals>.<genexpr>rc   r   r{   r.   r.   r/   get_location  s    zSphinxDirective.get_locationN)
r+   r   r   r   propertyr   r   r   r   r   r.   r.   r.   r/   r     s   r   c                
   @  s   e Zd ZU dZded< ded< ded< ded< ded	< d
ed< ded< i g fdddddd
dddddZddddZeddddZeddddZ	d(ddddd Z
d)d!dd"d#d$d%Zddd&d'ZdS )*
SphinxRolezA base class for Sphinx roles.

    This class provides helper methods for Sphinx roles.

    .. note:: The subclasses of this class might not work with docutils.
              This class is strongly coupled with Sphinx.
    r;   r,   rawtextr   r   r   r   inlinerdictoptions	list[str]r   'tuple[list[Node], list[system_message]]r,   r   r   r   r   r   r   r3   c                 C  sr   || _ t|| _|| _|| _|| _|| _|r8| | _n2| j	j
dd| _| js\| j	jj| _| jsjtd|  S )Ndefault_roler   zcannot determine default role!)r   r   r   r   r   r   r   r   r,   r   r   rb   r   r   r   run)rm   r,   r   r   r   r   r   r   r.   r.   r/   __call__  s    
zSphinxRole.__call__r2   c                 C  s   t d S rV   )NotImplementedErrorr{   r.   r.   r/   r     s    zSphinxRole.runr$   c                 C  s   | j jjjS r   )r   r   rg   r   r{   r.   r.   r/   r     s    zSphinxRole.envr#   c                 C  s   | j jS r   r   r{   r.   r.   r/   r     s    zSphinxRole.configNz
int | Noner   )r   r3   c                 C  s   |d kr| j }| jj|S rV   )r   r   rU   r   )rm   r   r.   r.   r/   r     s    zSphinxRole.get_source_infor   r?   )r9   r   r3   c                 C  s   |  |\|_|_d S rV   r   )rm   r9   r   r.   r.   r/   r     s    zSphinxRole.set_source_infoc                 C  s   d dd |  D S )r   r   c                 s  s   | ]}t |V  qd S rV   r   r   r.   r.   r/   r     s     z*SphinxRole.get_location.<locals>.<genexpr>r   r{   r.   r.   r/   r     s    zSphinxRole.get_location)N)N)r+   r   r   r   __annotations__r   r   r   r   r   r   r   r   r.   r.   r.   r/   r     s&   
 r   c                
      sj   e Zd ZU dZded< ded< ded< ded< edejZi g fdddd	d
dddd fddZ	  Z
S )ReferenceRolezA base class for reference roles.

    The reference roles can accept ``link title <target>`` style as a text for
    the role.  The parsed result; link title and target will be stored to
    ``self.title`` and ``self.target``.
    r<   has_explicit_titledisabledr;   titletargetz^(.+?)\s*(?<!\x00)<(.*?)>$r   r   r   r   r   r   c           	   	     sv   | d| _| j|}|rDd| _t|d| _t|d| _nd| _t|| _t|| _t	 
|||||||S )N!Tr   rq   F)
startswithr   explicit_title_rematchr   r   groupr   r   r   r   )	rm   r,   r   r   r   r   r   r   r   r   r.   r/   r     s    

zReferenceRole.__call__)r+   r   r   r   r   recompileDOTALLr   r   r   r.   r.   r   r/   r     s   
 r   c                      sb   e Zd ZdZdddd fddZddd	 fd
dZddd	 fddZddd	ddZ  ZS )SphinxTranslatorax  A base class for Sphinx translators.

    This class adds a support for visitor/departure method for super node class
    if visitor/departure method for node class is not found.

    It also provides helper methods for Sphinx translators.

    .. note:: The subclasses of this class might not work with docutils.
              This class is strongly coupled with Sphinx.
    r   r"   r?   )r   builderr3   c                   s&   t  | || _|j| _|j| _d S rV   )r   r|   r  r   rg   )rm   r   r  r   r.   r/   r|   *  s    zSphinxTranslator.__init__r   rG   c                   s@   |j jD ]&}t| d|j d}|r||  q<qt | dS )z
        Dispatch node to appropriate visitor method.
        The priority of visitor method is:

        1. ``self.visit_{node_class}()``
        2. ``self.visit_{super_node_class}()``
        3. ``self.unknown_visit()``
        zvisit_%sN)r   __mro__r   r+   r   dispatch_visitrm   r9   Z
node_classmethodr   r.   r/   r  0  s    	zSphinxTranslator.dispatch_visitc                   s@   |j jD ]&}t| d|j d}|r||  q<qt | dS )z
        Dispatch node to appropriate departure method.
        The priority of departure method is:

        1. ``self.depart_{node_class}()``
        2. ``self.depart_{super_node_class}()``
        3. ``self.unknown_departure()``
        z	depart_%sN)r   r  r   r+   r   dispatch_departurer  r   r.   r/   r
  A  s    	z#SphinxTranslator.dispatch_departurec                 C  s   t jtd||d d S )Nzunknown node type: %rr   )r   r   r   ro   r.   r.   r/   unknown_visitR  s    zSphinxTranslator.unknown_visit)	r+   r   r   r   r|   r  r
  r  r   r.   r.   r   r/   r    s
   r  ztuple[Values, Reporter]__document_cache__r   r   )source_pathrg   r3   c                 C  s~   zt \}}W n4 tk
r@   tj| }|j|jf a \}}Y nX |dkrRt|}ddlm	} |j
||| d}|| d |S )a&  Return a new empty document object.  This is an alternative of docutils'.

    This is a simple wrapper for ``docutils.utils.new_document()``.  It
    caches the result of docutils' and use it on second call for instantiation.
    This makes an instantiation of document nodes much faster.
    Nr   )addnodes)r   )r  	NameErrorrY   utilsnew_documentrg   rU   r   Zsphinxr  r   Znote_source)r  rg   Zcached_settingsrU   docr  r   r.   r.   r/   r  [  s    r  )N)N)br   
__future__r   r`   r  
contextlibr   r   r   typingr   r   r   r	   r
   r   rY   r   Zdocutils.ior   Zdocutils.nodesr   r   r   Zdocutils.parsers.rstr   r   r   Zdocutils.parsers.rst.statesr   Zdocutils.statemachiner   r   r   Zdocutils.utilsr   r   Zdocutils.writers._html_baser   Zsphinx.errorsr   Zsphinx.localer   r   Zsphinx.utilr   Zsphinx.util.typingr   	getLoggerr+   r   r  r   typesr    Zdocutils.frontendr!   Zsphinx.buildersr"   Zsphinx.configr#   Zsphinx.environmentr$   r%   r)   r0   setr1   r   r:   r>   rA   rB   rD   rF   rL   rN   r7   rX   r^   re   rs   rt   ru   r   r   r   r   r   r   r   r   r   r   r   NodeVisitorr  r  r.   r.   r.   r/   <module>   s    

 		 	)7 B":