U
    \at                     @   s   d Z ddlmZmZmZmZmZ ddlmZ ddl	Z
dZdd Zdd	 Zd
d ZG dd deZdd Zdd Zdd Zdd ZdS )aQ  Collect information on document sections and Pygame API objects

The following persistent Pygame specific environment structures are built:

pyg_sections: [{'docname': <docname>,
                'fullname': <fullname>,
                'refid': <ref>},
               ...]
    all Pygame api sections in the documents in order processed.
pyg_descinfo_tbl: {<id>: {'fullname': <fullname>,
                          'desctype': <type>,
                          'summary': <summary>,
                          'signatures': <sigs>,
                          'children': <toc>,
                          'refid': <ref>,
                          'docname': <docname>},
                   ...}
    object specific information, including a list of direct children, if any.

<docname>: (str) the simple document name without path or extension.
<fullname>: (str) a fully qualified object name. It is a unique identifier.
<ref>: (str) an id usable as local uri reference.
<id>: (str) unique desc id, the first entry in the ids attribute list.
<type>: (str) an object's type: the desctype attribute.
<summary>: (str) a summary line as identified by a :summaryline: role.
           This corresponds to the first line of a docstring.
<sigs>: (list of str) an object's signatures, in document order.
<toc>: (list of str) refids of an object's children, in document order.

    )Visitorget_fullname	get_refidas_refidGetError)dequeNzmodule-c                 C   s   |  dt |  dt d S )Nzenv-purge-doczdoctree-read)connectprep_document_infocollect_document_info)app r   8/home/runner/work/pygame/pygame/docs/reST/ext/indexer.pysetup)   s    r   c                    s   z fdd|j D |_ W n& tk
r.   Y n tk
r@   Y nX z
|j}W n tk
r`   Y n(X  fdd| D }|D ]
}||= q|d S )Nc                    s   g | ]}|d   kr|qS docnamer   ).0er   r   r   
<listcomp>0   s      z&prep_document_info.<locals>.<listcomp>c                    s    g | ]\}}|d   kr|qS r   r   )r   kvr   r   r   r   :   s      )pyg_sectionsAttributeErrorKeyErrorpyg_descinfo_tblitems)r   envr   Zdescinfo_tblZ	to_remover   r   r   r   r	   .   s    
r	   c                 C   s   | t| | d S N)	walkaboutCollectInfo)r   doctreer   r   r   r
   ?   s    r
   c                       s   e Zd ZdZdddddddd	hZ fd
dZdd Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Z  ZS )(r   z&Records the information for a documentdatafunction	exceptionclass	attributemethodstaticmethodclassmethodc                    s   t t| || | jj| _t | _t | _t | _z| jj	 W n t
k
r\   g | j_	Y nX z| jj W n t
k
r   i | j_Y nX d S r   )superr   __init__r   r   r   summary_stack	sig_stack
desc_stackr   r   r   )selfr   document_node	__class__r   r   r)   R   s    
zCollectInfo.__init__c                 C   s   |d }t j|\}}|s"| jt j|\}}|dkr@| jt j|\}}|dkr^| jt j|\}}|dkr|| jd S )NsourcerefZreSTdocs)ospathsplit	skip_node)r-   noder1   head	file_namedir_namer   r   r   visit_documenta   s    zCollectInfo.visit_documentc                 C   s   |d s| j |   d S )Nnames)r7   _pushr-   r8   r   r   r   visit_sectionr   s    zCollectInfo.visit_sectionc                 C   s   |   \}}}|jsdS |d d trF| | | |||| n>|r|d }t|| jdd}| | | 	|t|| j dS )zRecord section infoNidsr   summary )
_popchildren
startswithMODULE_ID_PREFIX_add_section_add_descinfoget_descinfor   get_add_descinfo_entry)r-   r8   rB   sigschild_descs	desc_noder   r   r   depart_sectionw   s    

zCollectInfo.depart_sectionc                 C   s$   | dd| jkr| j|   dS )z9Prepare to collect a summary and toc for this descriptiondesctyperC   N)rK   	desctypesr7   r>   r?   r   r   r   
visit_desc   s    zCollectInfo.visit_descc                 C   s"   | j |f|    | | dS )z<Record descinfo information and add descinfo to parent's tocN)rI   rD   	_add_descr?   r   r   r   depart_desc   s    zCollectInfo.depart_descc                 C   s8   d|d kr|  | nd|d kr.| | | jdS )zCollect a summary or signatureZsummarylineclasses	signatureN)_add_summary_add_sigskip_departurer?   r   r   r   visit_inline   s
    
zCollectInfo.visit_inlinec                 C   s(   | j t|t|d}| jj| d S )N)r   fullnamerefid)r   r   r   r   r   append)r-   r8   entryr   r   r   rH      s
    zCollectInfo._add_sectionc                 C   s@   t ||dd||dd |D t|| jd}| || d S )NrQ   modulec                 S   s   g | ]}t |qS r   )r   )r   nr   r   r   r      s     z-CollectInfo._add_descinfo.<locals>.<listcomp>)r\   rQ   rB   
signaturesrE   r]   r   )r   rK   r   r   rL   )r-   r8   rB   rM   rN   r_   r   r   r   rI      s    
	zCollectInfo._add_descinfoc                 C   s2   t |}|tr"|ttd  }|| jj|< d S r   )r   rF   rG   lenr   r   )r-   r8   r_   keyr   r   r   rL      s    
zCollectInfo._add_descinfo_entryc                 C   s(   | j d | jg  | jg  d S )NrC   )r*   r^   r+   r,   r-   r   r   r   r>      s    zCollectInfo._pushc                 C   s   | j  | j | j fS r   )r*   popr+   r,   re   r   r   r   rD      s    zCollectInfo._popc                 C   s   | j d | d S )N)r,   r^   )r-   rO   r   r   r   rT      s    zCollectInfo._add_descc                 C   s   |d   | jd< d S )Nr   rg   )astextr*   r-   Ztext_element_noder   r   r   rX      s    zCollectInfo._add_summaryc                 C   s   | j d |d   d S )Nrg   r   )r+   r^   rh   ri   r   r   r   rY      s    zCollectInfo._add_sig)__name__
__module____qualname____doc__rR   r)   r<   r@   rP   rS   rU   r[   rH   rI   rL   r>   rD   rT   rX   rY   __classcell__r   r   r/   r   r   C   s2   	r   c                 C   sL   zt ||}W n tk
r$   Y d S X | | |d D ]}t| || q6d S NrE   )rJ   r   tour_descinfo_refid)fnr8   r   descinfor]   r   r   r   tour_descinfo   s    rs   c                 C   s0   |j | }| | |d D ]}t| || qd S ro   )r   rp   )rq   r]   r   rr   r   r   r   rp      s    
rp   c                 C   s   t t| |S r   )get_descinfo_refidr   )r8   r   r   r   r   rJ      s    rJ   c                 C   sH   |  tr| ttd  } z|j|  W S  tk
rB   tdY nX d S )Nz	Not found)rF   rG   rc   r   r   r   )r]   r   r   r   r   rt      s    
rt   )rm   	ext.utilsr   r   r   r   r   collectionsr   os.pathr4   rG   r   r	   r
   r   rs   rp   rJ   rt   r   r   r   r   <module>   s    
