B
    §Ê¾Z8;  ã               @   s
  d dl mZmZmZ d dlZd dlZd dlZd dl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Zd dlmZ ddlmZmZmZ ejd  dkr¤d	d
„ Zndd
„ 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ƒZddi dfdd„ZdS )é    )ÚdivisionÚabsolute_importÚprint_functionN)ÚFileSystemLoader)ÚSandboxedEnvironment)ÚBuiltinTemplateLoaderé   )ÚNumpyDocStringÚFunctionDocÚClassDocé   c             C   s   | S )N© )Úsr   r   ú8lib/python3.7/site-packages/numpydoc/docscrape_sphinx.pyÚ<lambda>   s    r   c             C   s
   t | dƒS )NZunicode_escape)Zunicode)r   r   r   r   r      s    z1\b(import +matplotlib|from +matplotlib +import)\bc                   sÆ   e Zd Zi fdd„Zdd„ Zd/dd„Zdd	„ Zd0dd„Zdd„ Zdd„ Z	dd„ Z
d1dd„Zdd„ Zd2dd„Zedd„ ƒZdd„ Zdd „ Z‡ fd!d"„Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd3d-d.„Z‡  ZS )4ÚSphinxDocStringc             C   s   t j| ||d |  |¡ d S )N)Úconfig)r	   Ú__init__Úload_config)ÚselfZ	docstringr   r   r   r   r      s    zSphinxDocString.__init__c             C   s|   |  dd¡| _|  dd¡| _|  dd¡| _|  dd ¡| _| jd krxtj tj t	¡d¡g}t
|ƒ}t|d}| d	¡| _d S )
NÚ	use_plotsFÚuse_blockquotesÚclass_members_toctreeTÚtemplateÚ	templates)Úloaderznumpydoc_docstring.rst)Úgetr   r   r   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   ÚnameZ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_indent2   s    
zSphinxDocString._str_indentc             C   s   dgS dgS d S )Nr'   Z	Signaturez``%s``r   )r   r   r   r   Ú_str_signature8   s      zSphinxDocString._str_signaturec             C   s   | d dg S )NZSummaryr'   r   )r   r   r   r   Ú_str_summary?   s    zSphinxDocString._str_summaryc             C   s   | d dg S )NzExtended Summaryr'   r   )r   r   r   r   Ú_str_extended_summaryB   s    z%SphinxDocString._str_extended_summaryÚReturnsc             C   s¼   d}d}g }| | r¸||   |¡7 }|dg7 }xŠ| | D ]~\}}}|rb||  || ¡ |f g¡7 }n||  || ¡  g¡7 }|r| jr|dg7 }n
|sšdg}||  |d¡7 }|dg7 }q6W |S )Nz**%s** : %sz**%s**r'   z..é   )r+   r2   Ústripr   )r   r(   Z	typed_fmtZuntyped_fmtr0   ÚparamÚ
param_typeÚdescr   r   r   Ú_str_returnsE   s$    

zSphinxDocString._str_returnsc             C   s   |  ¡ }d| }|s||fS t| j|dƒ}t|ƒsJt|tƒsJt |¡sJd}t 	|¡}|r\|sd||fS t| ddƒ}|r†d| }d| }	nd}d}	d||	|f }|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**%s**NÚ_namer'   z~%s.z%s.z:obj:`%s <%s%s>`z
\s*
r   r   z^([A-Z].*?\.)(?:\s|$)r-   Ú
)r8   ÚgetattrÚ_objÚcallableÚ
isinstanceÚpropertyÚinspectZisgetsetdescriptorÚpydocÚgetdocÚreÚsplitÚsearchr   ÚgroupÚ	partition)r   r9   r;   Úfake_autosummaryÚdisplay_paramÚ	param_objZobj_docÚprefixZautosum_prefixZlink_prefixÚmr   r   r   Ú_process_param[   s<    "




zSphinxDocString._process_paramFc             C   sº   g }| | r¶||   |¡7 }|dg7 }x| | D ]„\}}}|  |||¡\}}|rh||  d||f g¡7 }n||  |g¡7 }|rŽ| jrŽ|dg7 }n
|s˜dg}||  |d¡7 }|dg7 }q.W |S )a†  Generate RST for a listing of parameters or similar

        Parameter names are displayed as bold text, and descriptions
        are in blockquotes.  Descriptions may therefore contain block
        markup as well.

        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%s : %sz..r7   )r+   rQ   r2   r   )r   r(   rL   r0   r9   r:   r;   rM   r   r   r   Ú_str_param_listª   s$    



zSphinxDocString._str_param_listc             C   s$   t | dƒr| jS t | dƒr | jS d S )NÚ_clsÚ_f)ÚhasattrrS   rT   )r   r   r   r   r@   Õ   s
    

zSphinxDocString._objc             C   s¾  g }| | rº|d| dg7 }t | ddƒ}|r6d| }g }g }x~| | D ]r\}}}| ¡ }t | j|dƒ}	t|	ƒsˆt|	tƒsˆt |	¡sˆd}	|	rªt 	|	¡rª|d||f g7 }qH| 
|||f¡ qHW |rê|dg7 }| jrÜ|dg7 }|dg| 7 }|r°td	td
d„ |D ƒƒƒ}
tdƒ|
 tdƒ tdƒd  }tdƒ|
f }|dd|g7 }x`|D ]X\}}}tdƒ dd„ |D ƒ¡ ¡ }|r‚d||f }||d| ¡  d |f g7 }qJW ||g7 }|dg7 }|S )zp
        Generate a member listing, autosummary:: table where possible,
        and a table where not.

        z.. rubric:: %sr'   r=   z~%s.Nz   %s%sz.. autosummary::z   :toctree:r   c             S   s   g | ]}t |d  ƒd ‘qS )r   r,   )Úlen)Ú.0Úxr   r   r   ú
<listcomp>  s    z4SphinxDocString._str_member_list.<locals>.<listcomp>ú=z  é
   z%%%ds  %%s  r-   c             s   s   | ]}|  ¡ V  qd S )N)r8   )rW   rX   r   r   r   ú	<genexpr>	  s    z3SphinxDocString._str_member_list.<locals>.<genexpr>z(%s) %sz**)r?   r8   r@   rA   rB   rC   rD   ZisdatadescriptorrE   rF   Úappendr   ÚmaxÚsixur   )r   r(   r0   rO   ZautosumZothersr9   r:   r;   rN   Zmaxlen_0ZhdrZfmtr   r   r   Ú_str_member_listÝ   sH    




 $

z SphinxDocString._str_member_listc             C   sJ   g }| | rF||   |¡7 }t d | | ¡¡ d¡}||7 }|dg7 }|S )Nr>   r'   )r)   ÚtextwrapÚdedentr   rH   )r   r(   r0   Zcontentr   r   r   Ú_str_section  s    
zSphinxDocString._str_sectionc                s>   g }| d r:t t| ƒ |¡}ddg}||  |dd … ¡7 }|S )NzSee Alsoz.. seealso::r'   é   )Úsuperr   Ú_str_see_alsor2   )r   Ú	func_roler0   Úsee_also)Ú	__class__r   r   rf     s    zSphinxDocString._str_see_alsoc             C   s4   g }| d r0ddg}||   | d ¡7 }|dg7 }|S )NZWarningsz.. warning::r'   )r2   )r   r0   r   r   r   Ú_str_warnings"  s    
zSphinxDocString._str_warningsc             C   s˜   | d }g }t |ƒdkr|S |d| dd¡ g7 }xV| ¡ D ]J\}}|dkrPq<q<|dkrn|dd |¡ g7 }q<|d	|d
 |¡f g7 }q<W |dg7 }|S )NÚindexr   z.. index:: %sÚdefaultr'   Zrefguidez   single: %sz, z	   %s: %sú,)rV   r   Úitemsr   )r   Úidxr0   ZsectionÚ
referencesr   r   r   Ú
_str_index*  s    
zSphinxDocString._str_indexc             C   sÐ   g }| d rÌ||   d¡7 }t| d tƒr6| d g| d< | | d ¡ |dg7 }tjdkrf|ddg7 }n|ddg7 }g }x4| d D ](}t d|tj¡}|r€| 	| 
d¡¡ q€W |dd	 d
d„ |D ƒ¡ dg7 }|S )NZ
Referencesr'   z0.6z.. only:: latexz.. latexonly::z.. \[([a-z0-9._-]+)\]r   z   z, c             S   s   g | ]}d | ‘qS )z[%s]_r   )rW   Úitemr   r   r   rY   N  s    z3SphinxDocString._str_references.<locals>.<listcomp>)r)   rB   ÚstrÚextendÚsphinxÚ__version__rG   ÚmatchÚIr]   rJ   r   )r   r0   rn   r1   rP   r   r   r   Ú_str_references;  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 )Nr>   ZExampleszplot::z	.. plot::r'   )r   r   rG   rI   ÚIMPORT_MATPLOTLIB_REr)   r2   rc   )r   Zexamples_strr0   r   r   r   Ú_str_examplesQ  s    
zSphinxDocString._str_examplesr   Úobjc             C   sÀ   |   ¡ |  ¡ |  ¡ |  ¡ |  d¡|  d¡|  d¡|  d¡|  d¡|  d¡|  ¡ |  |¡|  d¡|  	¡ |  
¡ | jdd	d
|  d¡dœ}tdd„ | ¡ D ƒƒ}| jjf |Ž}d |  | d¡|¡¡S )NZ
Parametersr6   ZYieldszOther ParametersZRaisesZWarnsZNotesZ
AttributesT)rL   ZMethods)Z	signaturerk   ZsummaryZextended_summaryZ
parametersZreturnsZyieldsZother_parametersZraisesZwarnsÚwarningsrh   Znotesrp   ZexamplesZ
attributesÚmethodsc             s   s    | ]\}}|d   |¡fV  qdS )r>   N)r   )rW   ÚkÚvr   r   r   r\   t  s    z*SphinxDocString.__str__.<locals>.<genexpr>r>   )r3   rq   r4   r5   rR   r<   rj   rf   rc   ry   r{   r`   Údictrn   r   Zrenderr   r2   rH   )r   r/   rg   ÚnsZrenderedr   r   r   Ú__str___  s*    zSphinxDocString.__str__)r&   )r,   )r6   )F)r   r|   )Ú__name__Ú
__module__Ú__qualname__r   r   r)   r+   r2   r3   r4   r5   r<   rQ   rR   rC   r@   r`   rc   rf   rj   rq   ry   r{   rƒ   Ú__classcell__r   r   )ri   r   r      s(   


O
+4	r   c               @   s   e Zd Zdi fdd„ZdS )ÚSphinxFunctionDocNc             C   s    |   |¡ tj| |||d d S )N)r.   r   )r   r
   r   )r   r|   r.   r   r   r   r   r   {  s    
zSphinxFunctionDoc.__init__)r„   r…   r†   r   r   r   r   r   rˆ   z  s   rˆ   c               @   s   e Zd Zddi fdd„ZdS )ÚSphinxClassDocNc             C   s"   |   |¡ tj| ||d |d d S )N)r.   Úfunc_docr   )r   r   r   )r   r|   r.   rŠ   r   r   r   r   r     s    
zSphinxClassDoc.__init__)r„   r…   r†   r   r   r   r   r   r‰   €  s   r‰   c               @   s   e Zd Zdi fdd„ZdS )ÚSphinxObjDocNc             C   s$   || _ |  |¡ tj| ||d d S )N)r   )rT   r   r   r   )r   r|   r.   r   r   r   r   r   ‡  s    
zSphinxObjDoc.__init__)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jƒr:d}nd}tj tj 	t
¡d¡g}|d k	rttƒ }|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Š   r.   r   )rŽ   Úmethod)r.   r   )r   )rD   ZisclassZismodulerB   ÚcollectionsÚCallabler   r   r   r    r!   r   Zinitr   r   r"   r‰   rˆ   rE   rF   r‹   )r|   Zwhatr.   r   Zbuilderr#   r$   r%   r   r   r   Úget_doc_object  s.    



r”   )Z
__future__r   r   r   ÚsysrG   rD   ra   rE   r’   r   Zjinja2r   Zjinja2.sandboxr   ru   Zsphinx.jinja2gluer   Z	docscraper	   r
   r   Úversion_infor_   rz   r   rˆ   r‰   r‹   r”   r   r   r   r   Ú<module>   s.   
  a