U
    \Sh4                     @  sB  d Z ddlmZ ddlZddlmZ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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  erddl!m"Z" e#dZ$e#dZ%dddddZ&e	dZ'G dd deee' Z(G dd deZ)G dd deZ*ddd d!d"Z+dS )#z(Handlers for additional ReST directives.    )annotationsN)TYPE_CHECKINGAnyGenericListTypeVarcast)nodes)Node)
directivesroles)addnodes)desc_signature)docutils)DocFieldTransformerField
TypedField)SphinxDirective)nested_parse_with_titles)
OptionSpec)Sphinxz\\\nz\\(.)strz
int | None)argumentreturnc                 C  s,   | dkrdS t | }|dk r$td|S dS )zS
    Check for an integer argument or None value; raise ``ValueError`` if not.
    Nr   z(negative value; must be positive or zero)int
ValueError)r   value r   M/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/directives/__init__.pyoptional_int   s    r   ObjDescTc                   @  s  e Zd ZU dZdZdZdZdZej	ej	ej	dZ
ded< g Zded	< d
Zded< ded< ded< i Zded< ddddZ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$dd%d&d'Zddd(d)Zdd*d+d,d-Zddd+d.d/Zd0dd1d2Zd
S )3ObjectDescriptionz
    Directive to describe a class, function or similar object.  Not used
    directly, but subclassed (in domain-specific directives) to add custom
    behavior.
    T   r   )noindexnoindexentrynocontentsentryr   option_speczlist[Field]doc_field_typesNz
str | Nonedomainr   objtypezaddnodes.index	indexnodezdict[str, tuple[Field, bool]]_doc_field_type_mapr   c                 C  sf   | j i kr`i | _ | jD ]H}|jD ]}|df| j |< q |jrtt|}|jD ]}|df| j |< qJq| j S )NFT)r+   r'   namesZis_typedr   r   Z	typenames)selffieldnameZtyped_fieldr   r   r   get_field_type_mapG   s    




z$ObjectDescription.get_field_type_mapz	list[str]c                 C  s@   t d| jd d}| jjr.dd |D S dd |D S dS )z
        Retrieve the signatures to document from the directive arguments.  By
        default, signatures are given as arguments, one per line.
         r   
c                 S  s   g | ]}t d | qS )z\1)strip_backslash_resubstrip.0liner   r   r   
<listcomp>]   s     z4ObjectDescription.get_signatures.<locals>.<listcomp>c                 S  s   g | ]}|  qS r   )r6   r7   r   r   r   r:   _   s     N)nl_escape_rer5   	argumentssplitconfigstrip_signature_backslash)r.   linesr   r   r   get_signaturesU   s    z ObjectDescription.get_signaturesr   r    )sigsignoder   c                 C  s   t dS )a  
        Parse the signature *sig* into individual nodes and append them to
        *signode*. If ValueError is raised, parsing is aborted and the whole
        *sig* is put into a single desc_name node.

        The return value should be a value that identifies the object.  It is
        passed to :meth:`add_target_and_index()` unchanged, and otherwise only
        used to skip duplicates.
        N)r   )r.   rB   rC   r   r   r   handle_signaturea   s    
z"ObjectDescription.handle_signatureNone)r0   rB   rC   r   c                 C  s   dS )z
        Add cross-reference IDs and entries to self.indexnode, if applicable.

        *name* is whatever :meth:`handle_signature()` returned.
        Nr   )r.   r0   rB   rC   r   r   r   add_target_and_indexm   s    z&ObjectDescription.add_target_and_indexc                 C  s   dS )z
        Called before parsing content. Used to set information about the current
        directive context on the build environment.
        Nr   r.   r   r   r   before_contentu   s    z ObjectDescription.before_contentzaddnodes.desc_content)contentnoder   c                 C  s   dS )z
        Called after creating the content through nested parsing,
        but before the ``object-description-transform`` event is emitted,
        and before the info-fields are transformed.
        Can be used to manipulate the content.
        Nr   )r.   rI   r   r   r   transform_content|   s    z#ObjectDescription.transform_contentc                 C  s   dS )z
        Called after parsing content. Used to reset information about the
        current directive context on the build environment.
        Nr   rG   r   r   r   after_content   s    zObjectDescription.after_contentztuple[str, ...])sig_noder   c                 C  s   dS )a  
        Returns a tuple of strings, one entry for each part of the object's
        hierarchy (e.g. ``('module', 'submodule', 'Class', 'method')``). The
        returned tuple is used to properly nest children within parents in the
        table of contents, and can also be used within the
        :py:meth:`_toc_entry_name` method.

        This method must not be used outwith table of contents generation.
        r   r   r.   rL   r   r   r   _object_hierarchy_parts   s    
z)ObjectDescription._object_hierarchy_partsc                 C  s   dS )a  
        Returns the text of the table of contents entry for the object.

        This function is called once, in :py:meth:`run`, to set the name for the
        table of contents entry (a special attribute ``_toc_name`` is set on the
        object node, later used in
        ``environment.collectors.toctree.TocTreeCollector.process_doc().build_toc()``
        when the table of contents entries are collected).

        To support table of contents entries for their objects, domains must
        override this method, also respecting the configuration setting
        ``toc_object_entries_show_parents``. Domains must also override
        :py:meth:`_object_hierarchy_parts`, with one (string) entry for each part of the
        object's hierarchy. The result of this method is set on the signature
        node, and can be accessed as ``sig_node['_toc_parts']`` for use within
        this method. The resulting tuple is also used to properly nest children
        within parents in the table of contents.

        An example implementations of this method is within the python domain
        (:meth:`!PyObject._toc_entry_name`). The python domain sets the
        ``_toc_parts`` attribute within the :py:meth:`handle_signature()`
        method.
        r2   r   rM   r   r   r   _toc_entry_name   s    z!ObjectDescription._toc_entry_name
list[Node]c           
   
   C  sz  d| j kr"| j dd\| _| _nd| j  | _| _tjg d| _t }| jj	|_	| 
 \}}|dk	rn|d8 }| jj	|| | j|d< | j |d< |d< d	| jk |d	< }d
| jk|d
< d| jk|d< | jr|d | j |d |d  g | _|  }|D ]}t|d}| | || zNz| ||}W n8 tk
rp   |  |t||7 }Y W qY nX W 5 | jjjjr| ||d< | ||d< nd|d< d|d< X || jkr| j| |s| ||| qt }	||	 | jr| jd | jjd< |   t | j| j!|	| j" | #|	 | jj$d| j| j|	 t%| &|	 d| jjd< | '  | j|gS )a  
        Main directive entry function, called by docutils upon encountering the
        directive.

        This directive is meant to be quite easily subclassable, so it delegates
        to several additional methods.  What it does:

        * find out if called as a domain-specific directive, set self.domain
        * create a `desc` node to fit all description inside
        * parse standard options, currently `noindex`
        * create an index node if needed as self.indexnode
        * parse all given signatures (as returned by self.get_signatures())
          using self.handle_signature(), which should either return a name
          or raise ValueError
        * add index entries using self.add_target_and_index()
        * parse the content and handle doc fields in it
        :r"   r2   )entriesNr(   r)   Zdesctyper#   r$   r%   classesZ
_toc_partsZ	_toc_namer   r   objectobject-description-transform)(r0   r=   r(   r)   r   indexr*   descstateZdocumentZget_source_infoZnote_sourceoptionsappendr-   rA   r   Zset_source_infoenvappr>   Ztoc_object_entriesrN   rO   rD   r   clearZ	desc_namerF   Zdesc_content	temp_datarH   r   contentZcontent_offsetrJ   emitr   Ztransform_allrK   )
r.   nodesourcer9   r#   Z
signaturesrB   rC   r0   rI   r   r   r   run   sn    








  zObjectDescription.run)__name__
__module____qualname____doc__has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer   flagr&   __annotations__r'   r(   r+   r1   rA   rD   rF   rH   rJ   rK   rN   rO   rc   r   r   r   r   r!   -   s0   
	r!   c                   @  s&   e Zd ZdZdZdZddddZdS )	DefaultRolezK
    Set the default interpreted text role.  Overridden from docutils.
    r"   FrP   r,   c                 C  s   | j std g S | j d }t|| jj| j| jj	\}}|rZt
d| || jjd< n8t| j| j}| jj	}|jd| || jd}||g7 }tttj |S )Nr2   r   Zdefault_rolez#Unknown interpreted text role "%s".)r9   )r<   r   Zunregister_roler   roleZstate_machinelanguagelinenorX   reporterZregister_roler[   r^   r	   literal_blockZ
block_texterrorr   r   r
   )r.   Z	role_namero   messagesrs   rr   rt   r   r   r   rc     s&    

 
 
zDefaultRole.runN)rd   re   rf   rg   rj   rk   rc   r   r   r   r   rn     s   rn   c                   @  s<   e Zd ZU dZdZdZdZdZi Zde	d< ddd	d
Z
dS )DefaultDomainzH
    Directive to (re-)set the default domain for this source file.
    Fr"   r   r   r&   rP   r,   c                 C  s(   | j d  }| jj|| jjd< g S )Nr   Zdefault_domain)r<   lowerr[   domainsgetr^   )r.   Zdomain_namer   r   r   rc   ;  s    zDefaultDomain.runN)rd   re   rf   rg   rh   ri   rj   rk   r&   rm   rc   r   r   r   r   rv   0  s   
rv   r   zdict[str, Any])r\   r   c                 C  sT   |  ddd tdt tdt tdt tdt | d d	d
d
dS )Nr?   Fr[   zdefault-rolezdefault-domainZdescriberT   rU   builtinT)versionZparallel_read_safeZparallel_write_safe)Zadd_config_valuer   Zregister_directivern   rv   r!   Z	add_event)r\   r   r   r   setupG  s    
r|   ),rg   
__future__r   retypingr   r   r   r   r   r   r   r	   Zdocutils.nodesr
   Zdocutils.parsers.rstr   r   Zsphinxr   Zsphinx.addnodesr   Zsphinx.utilZsphinx.util.docfieldsr   r   r   Zsphinx.util.docutilsr   Zsphinx.util.nodesr   Zsphinx.util.typingr   Zsphinx.applicationr   compiler;   r4   r   r    r!   rn   rv   r|   r   r   r   r   <module>   s0    

 h