B
    dKZ                 @   s  U d 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 ddlm	Z	 ddl
mZmZmZmZmZmZ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/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9 e7:e;Z<e=dZ>erddl?m@Z@ ddlAmBZB ddlCmDZD ddlEmFZF e0ddejGie/ddi eH ZIeee  eJd< eed dd d!ZKeLeMd"d#d$ZNeLee  dd%d&d'ZOeLeMd"d(d)ZPeLe9dd*d+d,ZQeLdd"d-d.ZRee eMd/d0d1ZSee dd/d2d3ZTee dd/d4d5ZUeed dd6d7ZVeed dd8d9ZWeeeL ed d:d;d<ZXeed dd=d>ZYeddeeL ed d:d?d@ZZG dAdB dBZ[G dCdD dDe\Z]G dEdF dFe[Z^G dGdH dHZ_G dIdJ dJe*Z`G dKdL dLe*ZaeMddMdNZbee&e(ed dOdPdQZcG dRdS dSeZdG dTdU dUe ZeG dVdW dWZfG dXdY dYefZgG dZd[ d[ejhZiejGd\krd]d^ Zjeje_jed_e*f eJd`< deeLeejkdadbdcZldS )fzUtility functions for docutils.    N)contextmanager)copy)path)
ModuleType)IOTYPE_CHECKINGAnyCallableDict	GeneratorListOptionalSetTupleTypecast)nodes)
FileOutput)ElementNodesystem_message)	Directive
directivesroles)Inliner)StateStateMachine
StringList)Reporterunescape)HTMLTranslator)RemovedInSphinx70Warningdeprecated_alias)SphinxError)___)logging)RoleFunctionz>^(.+?:(?:\d+)?): \((DEBUG|INFO|WARNING|ERROR|SEVERE)/(\d+)?\) )Values)Builder)Config)BuildEnvironmentzsphinx.util.docutils__version_info__zdocutils.__version_info__additional_nodes)NNN)returnc           
   c   sV   zt tj} t tj}dV  W d| t_|t_x"ttD ]}t| t| q6W X dS )z"Create namespace for reST parsers.N)	r   r   _directivesr   _roleslistr-   unregister_nodediscard)r/   r0   node r5   a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sphinx/util/docutils.pydocutils_namespace2   s    


r7   )namer.   c             C   s
   | t jkS )z1Check the *name* directive is already registered.)r   r/   )r8   r5   r5   r6   is_directive_registeredC   s    r9   )r8   	directiver.   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)r8   r:   r5   r5   r6   r;   H   s    r;   c             C   s
   | t jkS )z,Check the *name* role is already registered.)r   r0   )r8   r5   r5   r6   is_role_registeredQ   s    r<   )r8   roler.   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)r8   r=   r5   r5   r6   register_roleV   s    r>   c             C   s   t j| d dS )z Unregister a role from docutils.N)r   r0   pop)r8   r5   r5   r6   unregister_role_   s    r@   )r4   r.   c             C   s   t tjd| j S )z'Check the *node* is already registered.visit_)hasattrr   GenericNodeVisitor__name__)r4   r5   r5   r6   is_node_registeredd   s    rE   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.
    rA   N)rB   r   rC   rD   Z_add_node_class_namesr-   add)r4   r5   r5   r6   register_nodei   s    rG   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()``.
    rA   Zdepart_N)rB   r   rC   rD   delattrZSparseNodeVisitor)r4   r5   r5   r6   r2   t   s
    r2   c              #   sF   ddl m  dtttd fdd} z| tj_dV  W d 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_languageN)language_codereporterr.   c                s    | S )Nr5   )rJ   rK   )rI   r5   r6   patched_get_language   s    z2patched_get_language.<locals>.patched_get_language)N)Zdocutils.languagesrI   strr   r   docutils	languages)rL   r5   )rI   r6   rL      s    
rL   c              #   sN   ddl m  dtttd fdd} z| tjjj_dV  W d 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   )rI   N)rJ   rK   r.   c                s    | S )Nr5   )rJ   rK   )rI   r5   r6   rL      s    z6patched_rst_get_language.<locals>.patched_get_language)N)	Zdocutils.parsers.rst.languagesrI   rM   r   r   rN   parsersrstrO   )rL   r5   )rI   r6   patched_rst_get_language   s    
rR   )confdirr.   c          
   c   s^   z4t jdd}| r,tt| dt jd< dV  W d|dkrNt jdd n
|t jd< X dS )z?Let docutils know the location of ``docutils.conf`` for Sphinx.ZDOCUTILSCONFIGNzdocutils.conf)osenvirongetr   joinabspathr?   )rS   Zdocutilsconfigr5   r5   r6   using_user_docutils_conf   s    
rY   c           	   c   sj   dd } dd }t j}t j}tjd d dkr:| t _|t _z
d V  W d 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)classesr=   )	settingsZfootnote_references
isinstanceZprevious_siblingtypebodyappendZstarttagtagname)selfr4   Zlabel_styler5   r5   r6   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)r^   r_   r\   Z	next_noder]   )ra   r4   r5   r5   r6   depart_footnote   s    
z'du19_footnotes.<locals>.depart_footnote   )r      )r    rb   rc   rN   r,   )rb   rc   Zold_visit_footnoteZold_depart_footnoter5   r5   r6   du19_footnotes   s    
rf   c             c   sT   t  D t 2 t|  t  dV  W dQ R X W dQ R X W dQ R X W dQ R X dS )zPatch to docutils temporarily.N)rL   rR   rY   rf   )rS   r5   r5   r6   patch_docutils   s
    
rg   c               @   s   e Zd ZdZddddZddddZee eeddd	d
Z	ddddZ
ddddZeeejeeee  ee f dddZeeeeeeee f 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.
    N)r.   c             C   s   dd | _ dd | _d S )Nc              W   s   d g fS )Nr5   )argsr5   r5   r6   <lambda>       z/CustomReSTDispatcher.__init__.<locals>.<lambda>c              W   s   d g fS )Nr5   )ri   r5   r5   r6   rj      rk   )directive_funcZ
roles_func)ra   r5   r5   r6   __init__   s    
zCustomReSTDispatcher.__init__c             C   s   |    d S )N)enable)ra   r5   r5   r6   	__enter__   s    zCustomReSTDispatcher.__enter__)exc_type	exc_value	tracebackr.   c             C   s   |    d S )N)disable)ra   rp   rq   rr   r5   r5   r6   __exit__   s    zCustomReSTDispatcher.__exit__c             C   s$   t j| _tj| _| jt _| jt_d S )N)r   r:   rl   r   r=   	role_func)ra   r5   r5   r6   rn      s    zCustomReSTDispatcher.enablec             C   s   | j t_| jt_d S )N)rl   r   r:   ru   r   r=   )ra   r5   r5   r6   rs      s    zCustomReSTDispatcher.disable)directive_namelanguage_moduledocumentr.   c             C   s   |  |||S )N)rl   )ra   rv   rw   rx   r5   r5   r6   r:     s    zCustomReSTDispatcher.directive)	role_namerw   linenorK   r.   c             C   s   |  ||||S )N)ru   )ra   ry   rw   rz   rK   r5   r5   r6   r=     s    zCustomReSTDispatcher.role)rD   
__module____qualname____doc__rm   ro   r   	Exceptionr   rt   rn   rs   rM   r   r   rx   r   r   r   r   r   r:   intr   r'   r=   r5   r5   r5   r6   rh      s   "rh   c               @   s   e Zd ZdS )ElementLookupErrorN)rD   r{   r|   r5   r5   r5   r6   r     s   r   c                   s   e Zd ZdZddd fddZeeeddd	Zeee	j
eeee  ee f d
 fddZeeeeeeee f d fddZ  ZS )sphinx_domainszcMonkey-patch directive and role dispatch, so that domain-specific
    markup takes precedence.
    r+   N)envr.   c                s   || _ t   d S )N)r   superrm   )ra   r   )	__class__r5   r6   rm     s    zsphinx_domains.__init__)r]   r8   r.   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_datarV   r   )ra   r]   r8   Zdomain_namedomainelementZ
def_domainr5   r5   r6   lookup_domain_element  s$    
z$sphinx_domains.lookup_domain_element)rv   rw   rx   r.   c                s2   y|  d|S  tk
r,   t |||S X d S )Nr:   )r   r   r   r:   )ra   rv   rw   rx   )r   r5   r6   r:   7  s    zsphinx_domains.directive)ry   rw   rz   rK   r.   c          	      s4   y|  d|S  tk
r.   t ||||S X d S )Nr=   )r   r   r   r=   )ra   ry   rw   rz   rK   )r   r5   r6   r=   ?  s    zsphinx_domains.role)rD   r{   r|   r}   rm   rM   r   r   r   r   rx   r   r   r   r   r   r   r:   r   r   r'   r=   __classcell__r5   r5   )r   r6   r     s   &r   c               @   s   e Zd ZeddddZdS )WarningStreamN)textr.   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)ra   r   matchedr   r]   levelmessager5   r5   r6   writeH  s    
zWarningStream.write)rD   r{   r|   rM   r   r5   r5   r5   r6   r   G  s   r   c                   sN   e Zd Zeed dddZejejddfee	e	e
edd fdd	Z  ZS )
LoggingReporter)rK   r.   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)clsrK   r5   r5   r6   from_reporterS  s    zLoggingReporter.from_reporterFbackslashreplaceN)r   r   r   debugr   r.   c                s(   t tt }t j||||||d d S )N)r   )r   r   r   r   rm   )ra   r   r   r   r   r   stream)r   r5   r6   rm   Y  s    zLoggingReporter.__init__)rD   r{   r|   classmethodr   r   ZWARNING_LEVELZSEVERE_LEVELrM   r   boolrm   r   r5   r5   )r   r6   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.N)r.   c                s   t  ddd d S )Nr   i     )r   rm   )ra   )r   r5   r6   rm   d  s    zNullReporter.__init__)rD   r{   r|   r}   rm   r   r5   r5   )r   r6   r   a  s   r   c               C   s   t dt dS )Nz*is_html5_writer_available() is deprecated.T)warningswarnr!   r5   r5   r5   r6   is_html5_writer_availableh  s    r   )statecontentr.   c             c   sB   z0| j jj}tg d}||_|j| j j_dV  W d|| j j_X dS )z1Switch current source input of state temporarily.N)memorK   get_source_and_liner   Zinput_lines)r   r   r   state_machiner5   r5   r6   switch_source_inputn  s    


r   c                   s<   e Zd ZdZedd fddZeed fddZ  ZS )	SphinxFileOutputz#Better FileOutput class for Sphinx.N)kwargsr.   c                s,   | dd| _|dd t jf | d S )Noverwrite_if_changedFencodingzutf-8)r?   r   
setdefaultr   rm   )ra   r   )r   r5   r6   rm     s    zSphinxFileOutput.__init__)datar.   c          	      sb   | j rV| jrVd| jkrV| jrVtj| j rVt| j | jd}|	 |krL|S W d Q R X t
 |S )Nb)r   )Zdestination_pathZ	autoclosemoder   rT   r   existsopenr   readr   r   )ra   r   f)r   r5   r6   r     s    zSphinxFileOutput.write)	rD   r{   r|   r}   r   rm   rM   r   r   r5   r5   )r   r6   r     s   r   c               @   sh   e Zd ZdZeddddZeddddZeee	f dd	d
Z
eddddZe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+   )r.   c             C   s   | j jjjS )z3Reference to the :class:`.BuildEnvironment` object.)r   rx   r[   r   )ra   r5   r5   r6   r     s    zSphinxDirective.envr*   c             C   s   | j jS )z)Reference to the :class:`.Config` object.)r   config)ra   r5   r5   r6   r     s    zSphinxDirective.configc             C   s   | j | jS )zGet source and line number.)r   r   rz   )ra   r5   r5   r6   get_source_info  s    zSphinxDirective.get_source_infoN)r4   r.   c             C   s   |   \|_|_dS )z'Set source and line number to the node.N)r   r   line)ra   r4   r5   r5   r6   set_source_info  s    zSphinxDirective.set_source_infoc             C   s   d dd |  D S )z&Get current location info for logging.r   c             s   s   | ]}t |V  qd S )N)rM   ).0sr5   r5   r6   	<genexpr>  s    z/SphinxDirective.get_location.<locals>.<genexpr>)rW   r   )ra   r5   r5   r6   get_location  s    zSphinxDirective.get_location)rD   r{   r|   r}   propertyr   r   r   rM   r   r   r   r   r   r5   r5   r5   r6   r     s   r   c               @   s  e Zd ZU dZeed< eed< eed< eed< eed< eed< e	e ed< i g feeeeeee	e e
e	e e	e f d	d
dZe
e	e e	e f dddZeddddZeddddZdee
eef dddZdeeddddZe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.
    r8   rawtextr   rz   inlineroptionsr   )r8   r   r   rz   r   r   r   r.   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   rz   r   r   r   r   r8   r   r   rV   r   r   r#   run)ra   r8   r   r   rz   r   r   r   r5   r5   r6   __call__  s    
zSphinxRole.__call__)r.   c             C   s   t d S )N)NotImplementedError)ra   r5   r5   r6   r     s    zSphinxRole.runr+   c             C   s   | j jjjS )z3Reference to the :class:`.BuildEnvironment` object.)r   rx   r[   r   )ra   r5   r5   r6   r     s    zSphinxRole.envr*   c             C   s   | j jS )z)Reference to the :class:`.Config` object.)r   r   )ra   r5   r5   r6   r     s    zSphinxRole.configN)rz   r.   c             C   s   |d kr| j }| jj|S )N)rz   r   rK   r   )ra   rz   r5   r5   r6   r     s    zSphinxRole.get_source_info)r4   rz   r.   c             C   s   |  |\|_|_d S )N)r   r   r   )ra   r4   rz   r5   r5   r6   r     s    zSphinxRole.set_source_infoc             C   s   d dd |  D S )z&Get current location info for logging.r   c             s   s   | ]}t |V  qd S )N)rM   )r   r   r5   r5   r6   r     s    z*SphinxRole.get_location.<locals>.<genexpr>)rW   r   )ra   r5   r5   r6   r     s    zSphinxRole.get_location)N)N)rD   r{   r|   r}   rM   __annotations__r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r5   r5   r5   r6   r     s$   
r   c                   s~   e Zd ZU dZeed< eed< eed< eed< edej	Z
i g feeeeeeee eee ee f 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``.
    has_explicit_titledisabledtitletargetz^(.+?)\s*(?<!\x00)<(.*?)>$)r8   r   r   rz   r   r   r   r.   c       	   	      sv   | d| _| j|}|rDd| _t|d| _t|d| _nd| _t|| _t|| _t	 
|||||||S )N!Tr   rd   F)
startswithr   explicit_title_rematchr   r   groupr   r   r   r   )	ra   r8   r   r   rz   r   r   r   r   )r   r5   r6   r     s    

zReferenceRole.__call__)rD   r{   r|   r}   r   r   rM   recompileDOTALLr   r   r   r
   r   r   r   r   r   r   r5   r5   )r   r6   r     s   
r   c                   sd   e Zd ZdZejddd fddZedd fdd	Zedd fd
dZ	e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)   N)rx   builderr.   c                s&   t  | || _|j| _|j| _d S )N)r   rm   r   r   r[   )ra   rx   r   )r   r5   r6   rm   #  s    zSphinxTranslator.__init__)r4   r.   c                sB   x<|j jD ]$}t| d|j d}|r
|| P q
W 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   rD   r   dispatch_visit)ra   r4   
node_classmethod)r   r5   r6   r   )  s    	zSphinxTranslator.dispatch_visitc                sB   x<|j jD ]$}t| d|j d}|r
|| P q
W 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   rD   r   dispatch_departure)ra   r4   r   r   )r   r5   r6   r   :  s    	z#SphinxTranslator.dispatch_departurec             C   s   t jtd||d d S )Nzunknown node type: %r)r   )r   r   r%   )ra   r4   r5   r5   r6   unknown_visitK  s    zSphinxTranslator.unknown_visit)rD   r{   r|   r}   r   rx   rm   r   r   r   r   r   r5   r5   )r   r6   r     s
   
r   )r   re   c             O   s   t | j||S )N)itertraverse)ra   ri   r   r5   r5   r6   findallS  s    r   r(   __document_cache__)source_pathr[   r.   c             C   s~   y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   	NameErrorrN   utilsnew_documentr[   rK   r   Zsphinxr   rx   Znote_source)r   r[   Zcached_settingsrK   docr   rx   r5   r5   r6   r   ^  s    r   )N)N)mr}   rT   r   r   
contextlibr   r   r   typesr   typingr   r   r   r	   r
   r   r   r   r   r   r   r   rN   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.deprecationr!   r"   Zsphinx.errorsr#   Zsphinx.localer$   r%   Zsphinx.utilr&   Zsphinx.util.typingr'   	getLoggerrD   r   r   r   Zdocutils.frontendr(   Zsphinx.buildersr)   Zsphinx.configr*   Zsphinx.environmentr+   r,   setr-   r   r7   rM   r   r9   r;   r<   r>   r@   rE   rG   r2   rL   rR   rY   rf   rg   rh   r~   r   r   r   r   r   r   r   r   r   r   r   NodeVisitorr   r   rx   r   r5   r5   r5   r6   <module>   s   8


		 	&6 B";