o
    Nib                     @  s  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
 ddl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 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$m%Z% ddl&m'Z' ddl(m)Z) e'*e+Z,e-dZ.erddlm/Z/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8 ddl9m:Z: ddl;m<Z< ddl=m>Z> ddl?m@Z@ eA ZBdeCd< edjd d!ZDdkd%d&ZEdld*d+ZFdkd,d-ZGdmd0d1ZHdnd2d3ZIdod6d7ZJdpd8d9ZKdpd:d;ZLedjd<d=ZMedjd>d?ZNedqdBdCZOedrdqdDdEZPG dFdG dGZQG dHdI dIeRZSG dJdK dKeQZTG dLdM dMZUG dNdO dOeZVG dPdQ dQeZWedsdVdWZXG dXdY dYeZYG dZd[ d[eZZG d\d] d]Z[G d^d_ d_e[Z\G d`da daej]Z^dbeCdc< drdtdhdiZ_dS )uzUtility functions for docutils.    )annotationsN)Sequence)contextmanager)copy)path)IOTYPE_CHECKINGAnycast)nodes)
FileOutput)	Directive
directivesroles)Inliner)StateStateMachine
StringList)Reporterunescape)SphinxError)___)logging)nested_parse_to_nodesz>^(.+?:(?:\d+)?): \((DEBUG|INFO|WARNING|ERROR|SEVERE)/(\d+)?\) )CallableIterator)
ModuleType)Values)ElementNodesystem_message)Builder)Config)BuildEnvironment)RoleFunctionzset[type[Element]]additional_nodesreturnIterator[None]c               
   c  s~    z&t tj} t tj}dV  W | t_|t_ttD ]}t| t| qdS | t_|t_ttD ]}t| t| q2w )z"Create namespace for reST parsers.N)	r   r   _directivesr   _roleslistr&   unregister_nodediscard)r)   r*   node r/   H/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/util/docutils.pydocutils_namespace/   s"   

r1   namestrboolc                 C  
   | t jv S )z1Check the *name* directive is already registered.)r   r)   r2   r/   r/   r0   is_directive_registered@      
r7   	directivetype[Directive]Nonec                 C     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)r2   r9   r/   r/   r0   r=   E      r=   c                 C  r5   )z,Check the *name* role is already registered.)r   r*   r6   r/   r/   r0   is_role_registeredN   r8   r?   roler%   c                 C  r<   )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   register_local_role)r2   r@   r/   r/   r0   register_roleS   r>   rB   c                 C  s   t j| d dS )z Unregister a role from docutils.N)r   r*   popr6   r/   r/   r0   unregister_role\      rD   r.   type[Element]c                 C  s   t tjd| j S )z'Check the *node* is already registered.visit_)hasattrr   GenericNodeVisitor__name__r.   r/   r/   r0   is_node_registereda   rE   rL   c                 C  s2   t tjd| j st| jg t|  dS 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.
    rG   N)rH   r   rI   rJ   _add_node_class_namesr&   addrK   r/   r/   r0   register_nodef   s   rO   c                 C  sb   t tjd| j r/ttjd| j  ttjd| j  ttjd| j  ttjd| j  dS dS )zaUnregister a node from docutils.

    This is inverse of ``nodes._add_nodes_class_names()``.
    rG   depart_N)rH   r   rI   rJ   delattrSparseNodeVisitorrK   r/   r/   r0   r,   q   s   r,   c                  #  sH    ddl m  	dd fd
d} z| tj_dV  W  tj_dS  tj_w )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_coder3   reporterReporter | Noner'   r	   c                       | S Nr/   rU   rV   rS   r/   r0   patched_get_language      z2patched_get_language.<locals>.patched_get_languagerY   rU   r3   rV   rW   r'   r	   )docutils.languagesrT   docutils	languagesr[   r/   rS   r0   r[   }   s   r[   c                  #  sT    ddl m  	dd fd
d} z| tjjj_dV  W  tjjj_dS  tjjj_w )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   rS   NrU   r3   rV   rW   r'   r	   c                   rX   rY   r/   rZ   rS   r/   r0   r[      r\   z6patched_rst_get_language.<locals>.patched_get_languagerY   r]   )docutils.parsers.rst.languagesrT   r_   parsersrstr`   ra   r/   rS   r0   patched_rst_get_language   s   re   confdir
str | Nonec              
   c  s    z-t jdd}| rtt| dt jd< dV  W |du r(t jdd dS |t jd< dS |du r;t jdd w |t jd< w )z?Let docutils know the location of ``docutils.conf`` for Sphinx.DOCUTILSCONFIGNzdocutils.conf)osenvirongetr   joinabspathrC   )rf   docutilsconfigr/   r/   r0   using_user_docutils_conf   s   

ro   c              
   c  s    t  ; t  t|  dV  W d   n1 sw   Y  W d   n1 s*w   Y  W d   dS W d   dS 1 sBw   Y  dS )zPatch to docutils temporarily.N)r[   re   ro   )rf   r/   r/   r0   patch_docutils   s   P rp   c                   @  sV   e Zd ZdZd&ddZd&ddZd'ddZd&ddZd&ddZd(ddZ	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'   r;   c                 C  s   dd | _ dd | _d S )Nc                  W     d g fS rY   r/   argsr/   r/   r0   <lambda>       z/CustomReSTDispatcher.__init__.<locals>.<lambda>c                  W  rr   rY   r/   rs   r/   r/   r0   ru      rv   )directive_func
roles_funcselfr/   r/   r0   __init__   s   
zCustomReSTDispatcher.__init__c                 C     |    d S rY   )enablery   r/   r/   r0   	__enter__   s   zCustomReSTDispatcher.__enter__exc_typetype[Exception]	exc_value	Exception	tracebackr	   c                 C  r|   rY   )disable)rz   r   r   r   r/   r/   r0   __exit__   s   zCustomReSTDispatcher.__exit__c                 C  s$   t j| _tj| _| jt _| jt_d S rY   )r   r9   rw   r   r@   	role_funcry   r/   r/   r0   r}      s   zCustomReSTDispatcher.enablec                 C  s   | j t_| jt_d S rY   )rw   r   r9   r   r   r@   ry   r/   r/   r0   r      s   zCustomReSTDispatcher.disabledirective_namer3   language_moduler   documentnodes.document3tuple[type[Directive] | None, list[system_message]]c                 C  s   |  |||S rY   )rw   rz   r   r   r   r/   r/   r0   r9      s   zCustomReSTDispatcher.directive	role_namelinenointrV   r   )tuple[RoleFunction, list[system_message]]c                 C  s   |  ||||S rY   )r   rz   r   r   r   rV   r/   r/   r0   r@      s   zCustomReSTDispatcher.roleNr'   r;   )r   r   r   r   r   r	   r'   r;   r   r3   r   r   r   r   r'   r   
r   r3   r   r   r   r   rV   r   r'   r   )rJ   
__module____qualname____doc__r{   r~   r   r}   r   r9   r@   r/   r/   r/   r0   rq      s    





rq   c                   @  s   e Zd ZdS )ElementLookupErrorN)rJ   r   r   r/   r/   r/   r0   r      s    r   c                      sH   e Zd ZdZd fddZdddZd  fddZd! fddZ  ZS )"sphinx_domainszcMonkey-patch directive and role dispatch, so that domain-specific
    markup takes precedence.
    envr$   r'   r;   c                   s   || _ t   d S rY   )r   superr{   )rz   r   	__class__r/   r0   r{     s   zsphinx_domains.__init__typer3   r2   r	   c                 C  s   |  }d|v r6|dd\}}|| jjv r,| j|}t|||}|dur+|g fS n$ttd|| n| jj	
d}|durPt|||}|durP|g fS t| jjj||}|durb|g fS t)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:%sdefault_domain)lowersplitr   domains
get_domaingetattrloggerwarningr   	temp_datark   standard_domainr   )rz   r   r2   domain_namedomainelement
def_domainr/   r/   r0   lookup_domain_element  s*   
z$sphinx_domains.lookup_domain_elementr   r   r   r   r   r   c                   s2   z|  d|W S  ty   t ||| Y S w )Nr9   )r   r   r   r9   r   r   r/   r0   r9   ,  s
   zsphinx_domains.directiver   r   r   rV   r   r   c              	     s4   z|  d|W S  ty   t |||| Y S w )Nr@   )r   r   r   r@   r   r   r/   r0   r@   7  s
   zsphinx_domains.role)r   r$   r'   r;   )r   r3   r2   r3   r'   r	   r   r   )	rJ   r   r   r   r{   r   r9   r@   __classcell__r/   r/   r   r0   r     s    
 r   c                   @  s   e Zd ZdddZdS )	WarningStreamtextr3   r'   r;   c                 C  sZ   t |}|stj|ddd d S | \}}}t d| }tj|||dd d S )Nz
r_   )r    )locationr   )	report_researchr   r   rstripgroupssublog)rz   r   matchedr   r   levelmessager/   r/   r0   writeE  s   
zWarningStream.writeN)r   r3   r'   r;   )rJ   r   r   r   r/   r/   r/   r0   r   D  s    r   c                      s:   e Zd ZedddZejejdd	fd fddZ  Z	S )LoggingReporterclstype[LoggingReporter]rV   r   r'   c                 C  s   | |j |j|j|j|jS )zACreate an instance of LoggingReporter from other reporter object.)sourcereport_level
halt_level
debug_flagerror_handler)r   rV   r/   r/   r0   from_reporterP  s   zLoggingReporter.from_reporterFbackslashreplacer   r3   r   r   r   debugr4   r   r;   c                   s(   t tt }t j||||||d d S )N)r   )r
   r   r   r   r{   )rz   r   r   r   r   r   streamr   r/   r0   r{   ]  s   
zLoggingReporter.__init__)r   r   rV   r   r'   r   )r   r3   r   r   r   r   r   r4   r   r3   r'   r;   )
rJ   r   r   classmethodr   r   WARNING_LEVELSEVERE_LEVELr{   r   r/   r/   r   r0   r   O  s    r   c                      s"   e Zd ZdZd fddZ  ZS )NullReporterz A dummy reporter; write nothing.r'   r;   c                   s   t  ddd d S )Nr   i     )r   r{   ry   r   r/   r0   r{   n  s   zNullReporter.__init__r   )rJ   r   r   r   r{   r   r/   r/   r   r0   r   k  s    r   stater   contentr   c                 c  sL    z| j jj}tg d}||_|j| j j_dV  W || j j_dS || j j_w )z1Switch current source input of state temporarily.N)memorV   get_source_and_liner   input_lines)r   r   gsalstate_machiner/   r/   r0   switch_source_inputr  s   

r   c                      s0   e Zd ZdZd fddZd fd
dZ  ZS )SphinxFileOutputz#Better FileOutput class for Sphinx.kwargsr	   r'   r;   c                   s0   | dd| _|dd t jdi | d S )Noverwrite_if_changedFencodingzutf-8r/   )rC   r   
setdefaultr   r{   )rz   r   r   r/   r0   r{     s   zSphinxFileOutput.__init__datar3   c                   s   | j r<| jr<d| jvr<| jr<tj| j r<t| j | jd}|	 |kr-|W  d    S W d    n1 s7w   Y  t
 |S )Nb)r   )destination_path	autoclosemoder   ri   r   existsopenr   readr   r   )rz   r   fr   r/   r0   r     s    
zSphinxFileOutput.write)r   r	   r'   r;   )r   r3   r'   r3   )rJ   r   r   r   r{   r   r   r/   r/   r   r0   r     s    r   c                   @  s~   e Zd ZdZed(ddZed)ddZd*d
dZd+ddZd,ddZ		d-d.ddZ
	d/dddd0d d!Zdd"d1d%d&Zd'S )2SphinxDirectivea  A base class for Sphinx directives.

    This class provides helper methods for Sphinx directives.

    .. versionadded:: 1.8

    .. note:: The subclasses of this class might not work with docutils.
              This class is strongly coupled with Sphinx.
    r'   r$   c                 C     | j jjjS )z[Reference to the :class:`.BuildEnvironment` object.

        .. versionadded:: 1.8
        )r   r   settingsr   ry   r/   r/   r0   r        zSphinxDirective.envr#   c                 C     | j jS )zQReference to the :class:`.Config` object.

        .. versionadded:: 1.8
        r   configry   r/   r/   r0   r        zSphinxDirective.configtuple[str, int]c                 C  s   | j | jS )zCGet source and line number.

        .. versionadded:: 3.0
        )r   r   r   ry   r/   r/   r0   get_source_info  s   zSphinxDirective.get_source_infor.   r    r;   c                 C  s   |   \|_|_dS )zOSet source and line number to the node.

        .. versionadded:: 2.1
        Nr   r   linerz   r.   r/   r/   r0   set_source_info  s   zSphinxDirective.set_source_infor3   c                 C  B   |   \}}|r|r| d| S |r| dS |rd| S dS zNGet current location info for logging.

        .. versionadded:: 4.2
        r   z
<unknown>:r   r   rz   r   r   r/   r/   r0   get_location     

zSphinxDirective.get_locationFallow_section_headingsr4   
list[Node]c                 C  s   t | j| j| j|dS )a  Parse the directive's content into nodes.

        :param allow_section_headings:
            Are titles (sections) allowed in the directive's content?
            Note that this option bypasses Docutils' usual checks on
            doctree structure, and misuse of this option can lead to
            an incoherent doctree. In Docutils, section nodes should
            only be children of ``Structural`` nodes, which includes
            ``document``, ``section``, and ``sidebar`` nodes.

        .. versionadded:: 7.4
        offsetr   )r   r   r   content_offset)rz   r   r/   r/   r0   parse_content_to_nodes  s   z&SphinxDirective.parse_content_to_nodesr   r   r   r   r   c               C  s    |dkr| j }t| j|||dS )a  Parse *text* into nodes.

        :param text:
            Text, in string form. ``StringList`` is also accepted.
        :param allow_section_headings:
            Are titles (sections) allowed in *text*?
            Note that this option bypasses Docutils' usual checks on
            doctree structure, and misuse of this option can lead to
            an incoherent doctree. In Docutils, section nodes should
            only be children of ``Structural`` nodes, which includes
            ``document``, ``section``, and ``sidebar`` nodes.
        :param offset:
            The offset of the content.

        .. versionadded:: 7.4
        r   r   )r   r   r   )rz   r   r   r   r/   r/   r0   parse_text_to_nodes  s   z#SphinxDirective.parse_text_to_nodes)r   r   'tuple[list[Node], list[system_message]]c                C  s   |dkr| j }| j||S )a  Parse *text* as inline elements.

        :param text:
            The text to parse, which should be a single line or paragraph.
            This cannot contain any structural elements (headings,
            transitions, directives, etc).
        :param lineno:
            The line number where the interpreted text begins.
        :returns:
            A list of nodes (text and inline elements) and a list of system_messages.

        .. versionadded:: 7.4
        r   )r   r   inline_text)rz   r   r   r/   r/   r0   parse_inline
  s   zSphinxDirective.parse_inlineNr'   r$   r'   r#   )r'   r   r.   r    r'   r;   r'   r3   )F)r   r4   r'   r   )r   )r   r3   r   r   r   r4   r'   r   )r   r3   r   r   r'   r  )rJ   r   r   r   propertyr   r   r   r   r   r   r  r  r/   r/   r/   r0   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< 		d(d)ddZd*ddZed+ddZed,ddZ	d-d.dd Z
d-d/d$d%Zd0d&d'ZdS )1
SphinxRolezA base class for Sphinx roles.

    This class provides helper methods for Sphinx roles.

    .. versionadded:: 2.0

    .. note:: The subclasses of this class might not work with docutils.
              This class is strongly coupled with Sphinx.
    r3   r2   rawtextr   r   r   r   inlinerzdict[str, Any]optionsSequence[str]r   Nr/   dict | Noner'   r  c           	      C  s   || _ t|| _|| _|| _|d ur|ni | _|| _|r%| | _|  S | j	j
dd| _| js7| j	jj| _| js@d}t||  S )Ndefault_roler   zcannot determine default role!)r  r   r   r   r  r  r   r   r2   r   r   rk   r   r  r   run)	rz   r2   r  r   r   r  r  r   msgr/   r/   r0   __call__8  s    


	zSphinxRole.__call__c                 C  s   t rY   )NotImplementedErrorry   r/   r/   r0   r  V  s   zSphinxRole.runr$   c                 C  r   )z[Reference to the :class:`.BuildEnvironment` object.

        .. versionadded:: 2.0
        )r  r   r   r   ry   r/   r/   r0   r   Y  r   zSphinxRole.envr#   c                 C  r   )zQReference to the :class:`.Config` object.

        .. versionadded:: 2.0
        r   ry   r/   r/   r0   r   a  r   zSphinxRole.config
int | Noner   c                 C  s   |d u r| j }| jj|S rY   )r   r  rV   r   )rz   r   r/   r/   r0   r   i  s   zSphinxRole.get_source_infor.   r    r;   c                 C  s   |  |\|_|_d S rY   r   )rz   r.   r   r/   r/   r0   r   o  s   zSphinxRole.set_source_infoc                 C  r   r   r   r   r/   r/   r0   r   s  r   zSphinxRole.get_locationNr/   r2   r3   r  r3   r   r3   r   r   r  r   r  r  r   r  r'   r  )r'   r  r  r  rY   )r   r  r'   r   )r.   r    r   r  r'   r;   r  )rJ   r   r   r   __annotations__r  r  r	  r   r   r   r   r   r/   r/   r/   r0   r
    s(   
 

r
  c                      sX   e Zd ZU dZded< ded< ded< ded< edejZ			
dd fddZ	  Z
S )ReferenceRolea  A 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``.

    .. versionadded:: 2.0
    r4   has_explicit_titledisabledr3   titletargetz^(.+?)\s*(?<!\x00)<(.*?)>$Nr/   r2   r  r   r   r   r  r   r  r  r   r  r'   r  c           	   	     s   |d u ri }| d| _| j|}|r(d| _t|d| _t|d| _nd| _t|| _t|| _t	 
|||||||S )N!Tr      F)
startswithr  explicit_title_rematchr  r   groupr  r  r   r  )	rz   r2   r  r   r   r  r  r   r   r   r/   r0   r    s   


zReferenceRole.__call__r  r  )rJ   r   r   r   r  recompileDOTALLr!  r  r   r/   r/   r   r0   r    s   
 
	r  c                      sH   e Zd ZdZd fdd	Zd fddZd fddZdddZ  ZS )SphinxTranslatora  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.

    .. versionadded:: 2.0

    .. note:: The subclasses of this class might not work with docutils.
              This class is strongly coupled with Sphinx.
    r   r   builderr"   r'   r;   c                   s&   t  | || _|j| _|j| _d S rY   )r   r{   r(  r   r   )rz   r   r(  r   r/   r0   r{     s   zSphinxTranslator.__init__r.   r    c                   B   |j jD ]}t| d|j d}|r||  dS 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   rJ   r   dispatch_visitrz   r.   
node_classmethodr   r/   r0   r+       	zSphinxTranslator.dispatch_visitc                   r)  )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   rJ   r   dispatch_departurer,  r   r/   r0   r0    r/  z#SphinxTranslator.dispatch_departurec                 C  s   t jtd||d d S )Nzunknown node type: %r)r   )r   r   r   r   r/   r/   r0   unknown_visit  s   zSphinxTranslator.unknown_visit)r   r   r(  r"   r'   r;   r  )	rJ   r   r   r   r{   r+  r0  r1  r   r/   r/   r   r0   r'    s    r'  ztuple[Values, Reporter]__document_cache__source_pathr   r	   r   c                 C  s|   zt \}}W n ty   tj| }|j|jf a \}}Y nw |du r(t|}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   )r2  	NameErrorr_   utilsnew_documentr   rV   r   sphinxr4  r   note_source)r3  r   cached_settingsrV   docr4  r   r/   r/   r0   r7    s   r7  )r'   r(   )r2   r3   r'   r4   )r2   r3   r9   r:   r'   r;   )r2   r3   r@   r%   r'   r;   )r2   r3   r'   r;   )r.   rF   r'   r4   )r.   rF   r'   r;   )rf   rg   r'   r(   rY   )r   r   r   r   r'   r(   )r3  r3   r   r	   r'   r   )`r   
__future__r   ri   r$  collections.abcr   
contextlibr   r   r   typingr   r   r	   r
   r_   r   docutils.ior   docutils.parsers.rstr   r   r   docutils.parsers.rst.statesr   docutils.statemachiner   r   r   docutils.utilsr   r   sphinx.errorsr   sphinx.localer   r   sphinx.utilr   sphinx.util.parsingr   	getLoggerrJ   r   r%  r   r   r   typesr   docutils.frontendr   docutils.nodesr   r    r!   sphinx.buildersr"   sphinx.configr#   sphinx.environmentr$   sphinx.util.typingr%   setr&   r  r1   r7   r=   r?   rB   rD   rL   rO   r,   r[   re   ro   rp   rq   r   r   r   r   r   r   r   r   r   r
  r  NodeVisitorr'  r7  r/   r/   r/   r0   <module>   s    



	

	



5A c1<