B
    ëBcâ<  ã               @   sÜ   d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlmZ d dl	m
Z
 d dlZd dlmZ ddlmZmZmZmZ ddlmZ d	ZG d
d„ deƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZddd„ZdS )é    N)ÚCallable)ÚFileSystemLoader)ÚSandboxedEnvironment)ÚBuiltinTemplateLoaderé   )ÚNumpyDocStringÚFunctionDocÚClassDocÚObjDoc)Ú	make_xrefz1\b(import +matplotlib|from +matplotlib +import)\bc                   sÀ   e Zd Zd0dd„Zdd„ Zd1dd„Zd	d
„ Zd2dd„Zdd„ Zdd„ Z	dd„ Z
d3dd„Zdd„ Zdd„ Zd4dd„Zdd„ Zd d!„ Z‡ fd"d#„Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd5d.d/„Z‡  ZS )6ÚSphinxDocStringNc             C   s*   |d kri }t j| ||d |  |¡ d S )N)Úconfig)r   Ú__init__Úload_config)ÚselfZ	docstringr   © r   ú8lib/python3.7/site-packages/numpydoc/docscrape_sphinx.pyr      s    zSphinxDocString.__init__c             C   sª   |  dd¡| _|  dd¡| _|  dd¡| _|  dd¡| _|  dtƒ ¡| _|  dtƒ ¡| _|  d	d ¡| _	| j	d kr¦t
j t
j t¡d
¡g}t|ƒ}t|d}| d¡| _	d S )NÚ	use_plotsFÚclass_members_toctreeTÚattributes_as_param_listÚxref_param_typeÚxref_aliasesÚxref_ignoreÚtemplateÚ	templates)Úloaderznumpydoc_docstring.rst)Úgetr   r   r   r   Údictr   Úsetr   r   ÚosÚpathÚjoinÚdirnameÚ__file__r   r   Úget_template)r   r   Útemplate_dirsÚtemplate_loaderÚtemplate_envr   r   r   r      s    

zSphinxDocString.load_configú`c             C   s   d| dgS )Nz.. rubric:: Ú r   )r   ÚnameÚsymbolr   r   r   Ú_str_header*   s    zSphinxDocString._str_headerc             C   s   d| d gS )Nú:r   )r   r*   r   r   r   Ú_str_field_list-   s    zSphinxDocString._str_field_listé   c             C   s(   g }x|D ]}|d| | g7 }q
W |S )Nú r   )r   ÚdocÚindentÚoutÚliner   r   r   Ú_str_indent0   s    
zSphinxDocString._str_indentc             C   s   dgS )Nr)   r   )r   r   r   r   Ú_str_signature6   s    zSphinxDocString._str_signaturec             C   s   | d dg S )NZSummaryr)   r   )r   r   r   r   Ú_str_summary9   s    zSphinxDocString._str_summaryc             C   s   | d dg S )NzExtended Summaryr)   r   )r   r   r   r   Ú_str_extended_summary<   s    z%SphinxDocString._str_extended_summaryÚReturnsc             C   sÖ   d}d}g }| | rÒ||   |¡7 }|dg7 }x¤| | D ]˜}|j}|rZ| jrZt|| j| jƒ}|jr€||  ||j ¡ |f g¡7 }n||  || ¡  g¡7 }|j	s²||  dgd¡7 }n||  |j	d¡7 }|dg7 }q6W |S )Nz**%s** : %sz%sr)   z..é   )
r.   Útyper   r   r   r   r*   r5   ÚstripÚdesc)r   r*   Z	named_fmtZunnamed_fmtr3   ÚparamÚ
param_typer   r   r   Ú_str_returns?   s(    

zSphinxDocString._str_returnsc             C   sH   |d d… dkr d|dd …  S |d d… dkr@d|dd …  S |S d S )Né   z**z\*\*r   Ú*z\*r   )r   r*   r   r   r   Ú_escape_args_and_kwargsZ   s
    z'SphinxDocString._escape_args_and_kwargsc       
      C   s  |   | ¡ ¡}d|› d}|s&||fS t| j|dƒ}t|ƒs^t|tƒs^t |¡s^t 	|¡s^d}t
 |¡}|rp|sx||fS t| ddƒ}|r”|› d}nd}d|› d|› |› d}|rt d	| ¡ d
¡d }t dd | ¡ ¡¡}	|	rô|	 d
¡ ¡ }n| d¡d }| d¡}||fS )a  Determine how to display a parameter

        Emulates autosummary behavior if fake_autosummary

        Parameters
        ----------
        param : str
            The name of the parameter
        desc : list of str
            The parameter description as given in the docstring. This is
            ignored when autosummary logic applies.
        fake_autosummary : bool
            If True, autosummary-style behaviour will apply for params
            that are attributes of the class and have a docstring.

        Returns
        -------
        display_param : str
            The marked up parameter name for display. This may include a link
            to the corresponding attribute's own documentation.
        desc : list of str
            A list of description lines. This may be identical to the input
            ``desc``, if ``autosum is None`` or ``param`` is not a class
            attribute, or it will be a summary of the class attribute's
            docstring.

        Notes
        -----
        This does not have the autosummary functionality to display a method's
        signature, and hence is not used to format methods.  It may be
        complicated to incorporate autosummary's signature mangling, as it
        relies on Sphinx's plugin mechanism.
        z**NÚ_namer)   Ú.z:obj:`z <z>`z\n\s*\nr   r   z^([A-Z].*?\.)(?:\s|$)r0   Ú
)rC   r<   ÚgetattrÚ_objÚcallableÚ
isinstanceÚpropertyÚinspectZisgetsetdescriptorZismemberdescriptorÚpydocÚgetdocÚreÚsplitÚsearchr!   ÚgroupÚ	partition)
r   r>   r=   Úfake_autosummaryÚdisplay_paramÚ	param_objZobj_docÚprefixZlink_prefixÚmr   r   r   Ú_process_paramb   s4    "




zSphinxDocString._process_paramFc       	      C   sÌ   g }| | rÈ||   |¡7 }|dg7 }x¢| | D ]–}|  |j|j|¡\}}g }|rZ| |¡ |j}|rŠ|j}| jr€t|| j| j	ƒ}| |¡ ||  
d |¡g¡7 }|sªdg}||  
|d¡7 }|dg7 }q.W |S )aI  Generate RST for a listing of parameters or similar

        Parameter names are displayed as bold text, and descriptions
        are in definition lists.

        Parameters
        ----------
        name : str
            Section name (e.g. Parameters)
        fake_autosummary : bool
            When True, the parameter names may correspond to attributes of the
            object beign documented, usually ``property`` instances on a class.
            In this case, names will be linked to fuller descriptions.

        Returns
        -------
        rst : list of str
        r)   z : z..r:   )r.   rY   r*   r=   Úappendr;   r   r   r   r   r5   r!   )	r   r*   rT   r3   r>   rU   r=   Úpartsr?   r   r   r   Ú_str_param_list°   s.    


zSphinxDocString._str_param_listc             C   sº  g }| | r¶|d|› dg7 }t | ddƒ}|r<d|› d}g }g }x‚| | D ]v}|j|j ¡ d}t | j|jdƒ}t|ƒs”t|tƒs”t 	|¡s”d}|rºt
 |¡rº|d|› |j› g7 }qN| |¡ qNW |rô|d	g7 }| jræ|d
g7 }|dg| 7 }|r¬tdtdd„ |D ƒƒƒ}d| d d }	d|f }
|dd|	g7 }xf|D ]^}d|j ¡  d }d dd„ |jD ƒ¡ ¡ }|jrŠd|j› d|› }||
||f g7 }q@W ||	g7 }|dg7 }|S )zp
        Generate a member listing, autosummary:: table where possible,
        and a table where not.

        z.. rubric:: r)   rD   ú~rE   )r*   Nz   z.. autosummary::z   :toctree:é   c             s   s   | ]}t |jƒd  V  qdS )r/   N)Úlenr*   )Ú.0Úpr   r   r   ú	<genexpr>	  s    z3SphinxDocString._str_member_list.<locals>.<genexpr>ú=z  z
==========z%%%ds  %%s  z**r0   c             s   s   | ]}|  ¡ V  qd S )N)r<   )r`   Úxr   r   r   rb     s    ú(z) )rG   Ú_replacer*   r<   rH   rI   rJ   rK   rL   ZisdatadescriptorrM   rN   rZ   r   Úmaxr!   r=   r;   )r   r*   r3   rW   ZautosumZothersr>   rV   Zmaxlen_0ZhdrZfmtr=   r   r   r   Ú_str_member_listà   sJ    








z SphinxDocString._str_member_listc             C   sJ   g }| | rF||   |¡7 }t d | | ¡¡ d¡}||7 }|dg7 }|S )NrF   r)   )r,   ÚtextwrapÚdedentr!   rP   )r   r*   r3   Zcontentr   r   r   Ú_str_section  s    
zSphinxDocString._str_sectionc                s:   g }| d r6t ƒ  |¡}ddg}||  |dd … ¡7 }|S )NzSee Alsoz.. seealso::r)   rA   )ÚsuperÚ_str_see_alsor5   )r   Ú	func_roler3   Úsee_also)Ú	__class__r   r   rm      s    zSphinxDocString._str_see_alsoc             C   s4   g }| d r0ddg}||   | d ¡7 }|dg7 }|S )NZWarningsz.. warning::r)   )r5   )r   r3   r   r   r   Ú_str_warnings(  s    
zSphinxDocString._str_warningsc             C   s    | d }g }t |ƒdkr|S |d| dd¡› g7 }x\| ¡ D ]P\}}|dkrRq>q>|dkrr|dd |¡› g7 }q>|d	|› d
d |¡› g7 }q>W |dg7 }|S )NÚindexr   z.. index:: Údefaultr)   Zrefguidez   single: z, z   z: ú,)r_   r   Úitemsr!   )r   Úidxr3   ÚsectionÚ
referencesr   r   r   Ú
_str_index0  s     
zSphinxDocString._str_indexc             C   s¸   g }| d r´||   d¡7 }t| d tƒr6| d g| d< | | d ¡ |dg7 }|ddg7 }g }x4| d D ](}t d|tj¡}|rh| | d¡¡ qhW |dd 	dd	„ |D ƒ¡ dg7 }|S )
NZ
Referencesr)   z.. only:: latexz.. \[([a-z0-9._-]+)\]r   z   z, c             S   s   g | ]}d |› d‘qS )ú[z]_r   )r`   Úitemr   r   r   ú
<listcomp>Q  s    z3SphinxDocString._str_references.<locals>.<listcomp>)
r,   rJ   ÚstrÚextendrO   ÚmatchÚIrZ   rR   r!   )r   r3   ru   r4   rX   r   r   r   Ú_str_referencesA  s    
 zSphinxDocString._str_referencesc             C   st   d  | d ¡}| jrft t|¡rfd|krfg }||  d¡7 }|ddg7 }||  | d ¡7 }|dg7 }|S |  d¡S d S )NrF   ZExampleszplot::z	.. plot::r)   )r!   r   rO   rQ   ÚIMPORT_MATPLOTLIB_REr,   r5   rk   )r   Zexamples_strr3   r   r   r   Ú_str_examplesT  s    
zSphinxDocString._str_examplesr   Úobjc             C   sÔ   |   ¡ |  ¡ |  ¡ |  ¡ |  d¡|  d¡|  d¡|  d¡|  d¡|  d¡|  d¡|  ¡ |  |¡|  d¡|  	¡ |  
¡ | jr†| jd	d
dn|  d	¡|  d¡dœ}dd„ | ¡ D ƒ}| jjf |Ž}d |  | d¡|¡¡S )NZ
Parametersr9   ZYieldsZReceiveszOther ParametersZRaisesZWarnsZNotesZ
AttributesT)rT   ZMethods)Z	signaturerr   ZsummaryZextended_summaryZ
parametersZreturnsZyieldsZreceivesZother_parametersZraisesZwarnsÚwarningsro   Znotesrx   ZexamplesZ
attributesÚmethodsc             S   s   i | ]\}}d   |¡|“qS )rF   )r!   )r`   ÚkÚvr   r   r   ú
<dictcomp>|  s    z+SphinxDocString.__str__.<locals>.<dictcomp>rF   )r6   ry   r7   r8   r\   r@   rq   rm   rk   r   rƒ   r   rh   ru   r   Zrenderr!   r5   rP   )r   r2   rn   ÚnsZrenderedr   r   r   Ú__str__e  s,    zSphinxDocString.__str__)N)r(   )r/   )r9   )F)r   r„   )Ú__name__Ú
__module__Ú__qualname__r   r   r,   r.   r5   r6   r7   r8   r@   rC   rY   r\   rh   rk   rm   rq   ry   r   rƒ   r‹   Ú__classcell__r   r   )rp   r   r      s(   



N
07	r   c               @   s   e Zd Zddd„ZdS )ÚSphinxFunctionDocNc             C   s,   |d kri }|   |¡ tj| |||d d S )N)r1   r   )r   r   r   )r   r„   r1   r   r   r   r   r   ƒ  s    
zSphinxFunctionDoc.__init__)NN)rŒ   r   rŽ   r   r   r   r   r   r   ‚  s   r   c               @   s   e Zd Zddd„ZdS )ÚSphinxClassDocNc             C   s.   |d kri }|   |¡ tj| ||d |d d S )N)r1   Úfunc_docr   )r   r	   r   )r   r„   r1   r’   r   r   r   r   r   ‹  s    
zSphinxClassDoc.__init__)NNN)rŒ   r   rŽ   r   r   r   r   r   r‘   Š  s   r‘   c               @   s   e Zd Zddd„ZdS )ÚSphinxObjDocNc             C   s,   |d kri }|   |¡ tj| |||d d S )N)r1   r   )r   r
   r   )r   r„   r1   r   r   r   r   r   “  s    
zSphinxObjDoc.__init__)NN)rŒ   r   rŽ   r   r   r   r   r   r“   ’  s   r“   c             C   sð   |d kr<t  | ¡rd}n$t  | ¡r(d}nt| tƒr8d}nd}|d krHi }tj tj t	¡d¡g}|d k	r~t
ƒ }|j||d nt|ƒ}t|d}| d¡|d	< |dkr¶t| t||d
S |dkrÌt| ||dS |d krÞt | ¡}t| ||dS d S )NÚclassÚmoduleÚfunctionÚobjectr   )Údirs)r   znumpydoc_docstring.rstr   )r’   r1   r   )r–   Úmethod)r1   r   )r   )rL   ZisclassZismodulerJ   r   r   r    r!   r"   r#   r   Zinitr   r   r$   r‘   r   rM   rN   r“   )r„   Zwhatr1   r   Zbuilderr%   r&   r'   r   r   r   Úget_doc_object›  s0    




rš   )NNNN)rO   rL   ri   rM   Zcollections.abcr   r   Zjinja2r   Zjinja2.sandboxr   ZsphinxZsphinx.jinja2gluer   Z	docscraper   r   r	   r
   Zxrefr   r‚   r   r   r‘   r“   rš   r   r   r   r   Ú<module>   s&     p	