B
    \<              	   @   sx  d Z ddl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mZ ddlmZ ddl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 ddlmZ ddlm Z! G dd de"Z#dd Z$dd Z%dd Z&d*ddZ'dde&e%dddddf	ddZ(dd Z)d+d d!Z*d,d"d#Z+d$d% Z,e	j-d&d fd'd(Z.e/d)krte.  dS )-a"  
    sphinx.ext.autosummary.generate
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Usable as a library or script to generate automatic RST source files for
    items referred to in autosummary:: directives.

    Each generated RST file contains a single auto*:: directive which
    extracts the docstring of the referred item.

    Example Makefile rule::

       generate:
               sphinx-autogen -o source/generated source/*.rst

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    )print_functionN)FileSystemLoaderTemplateNotFound)SandboxedEnvironment)__display_version__)package_dir)import_by_nameget_documenter)BuiltinTemplateLoader)__)SphinxComponentRegistry)safe_getattr)	ensuredir)escapec               @   s   e Zd ZdZdd ZdS )DummyApplicationz3Dummy Application class for sphinx-autogen command.c             C   s   t  | _d S )N)r   registry)self r   >lib/python3.7/site-packages/sphinx/ext/autosummary/generate.py__init__9   s    zDummyApplication.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r   6   s   r   c             C   s^   ddl m}m}m}m}m}m}m}m} ||||||||g}	x|	D ]}
| j	
|
j|
 qBW d S )Nr   )ModuleDocumenterClassDocumenterExceptionDocumenterDataDocumenterFunctionDocumenterMethodDocumenterAttributeDocumenterInstanceAttributeDocumenter)Zsphinx.ext.autodocr   r   r   r   r   r   r    r!   r   Zadd_documenterobjtype)appr   r   r   r   r   r   r    r!   Zdocumenters
documenterr   r   r   setup_documenters>   s    (
r%   c             C   s   t |  d S )N)print)msgr   r   r   _simple_infoN   s    r(   c             C   s   t d|  tjd d S )Nz	WARNING: )file)r&   sysstderr)r'   r   r   r   _simple_warnS   s    r,   =c             C   s$   d| krt d| d |t|   S )N
zCan only underline single lines)
ValueErrorlen)titleliner   r   r   
_underlineX   s    r3   z.rstFc
       !         s  t t| }
t|
dkr6|
d d dg |
dd   }
|tdd|
  |r`|td|  d k	rzfdd	| D } d }tjtd
ddg}d }|d k	rt }|j	||d n|r|
d| t|}t|d}t|jd< t|jd< t|jd< t| }g }xtt|tdD ]\}}}|d kr.q|p>tj|}t| yt|\}}}}W n8 tk
r } z|d||f  wW d d }~X Y nX tj||| }tj|rq|| t|d}t ||}|d k	r||}n6y|d|j }W n  tk
r(   |d}Y nX g df fdd	}i }|jdkrt||d< ||d|d\|d< |d < ||d!|d\|d"< |d#< ||d$|d\|d%< |d&< nf|jd!krt||d< tt|t|j   |d'< ||d(d)g\|d*< |d+< ||d,\|d-< |d.< |!d/}|jd0krZd/|d d1 }|d1 }d/|d1d  }||d!< nd/|d d2 |d2  }}||d3< ||d< ||d4< |d2 |d5< |j|d6< t|d7 |d< |j"f |} |#|  W d Q R X qW |rt$||||||| d8	 d S )9N   
   z...iz,[autosummary] generating autosummary for: %sz, z[autosummary] writing to %sc                s   g | ]}t j |qS r   )ospathjoin).0filename)	base_pathr   r   
<listcomp>q   s    z-generate_autosummary_docs.<locals>.<listcomp>ZextZautosummary	templates)dirsr   )loaderZ	underliner   e)keyz%[autosummary] failed to import %r: %swzautosummary/%s.rstzautosummary/base.rstTc       	   	      s   g }xlt | D ]`}yt| |}W n tk
r6   wY nX t|| }|j|kr|sdt|dd | jkr|| qW  fdd|D }||fS )Nr   c                s"   g | ]}| ks| d s|qS )_)
startswith)r9   x)include_publicr   r   r<      s    zBgenerate_autosummary_docs.<locals>.get_members.<locals>.<listcomp>)dirr   AttributeErrorr	   r"   getattrr   append)	objtyprF   importeditemsnamevaluer$   Zpublic)r#   )rF   r   get_members   s    
z.generate_autosummary_docs.<locals>.get_membersmodulemembersZfunction)rM   Z	functionsZall_functionsclassclassesZall_classesZ	exception
exceptionsZall_exceptionsZinherited_membersmethodr   methodsZall_methods	attributeZ
attributesZall_attributes.)rW   rY   fullnameZobjnamerO   r"   r-   )
output_dirsuffixwarninfor;   buildertemplate_dirr#   )%listsortedr0   r   r8   r6   r7   r   r
   Zinitinsertr   r   r3   filters
rst_escapefind_autosummary_in_filessetstrabspathr   r   ImportErrorisfilerJ   openr	   Zget_templater"   r   rG   __dict__keyssplitZrenderwritegenerate_autosummary_docs)!Zsourcesr^   r_   r`   ra   r;   rb   rc   imported_membersr#   Zshowed_sourcesZtemplate_dirsZtemplate_loaderZtemplate_envrN   Z	new_filesrO   r7   Ztemplate_namerK   parentmod_namer@   fnfdoctemplaterQ   nspartsZcls_nameZobj_nameZrenderedr   )r#   r;   r   rt   a   s    





 





rt   c          
   C   sR   g }xH| D ]@}t j|dddd$}|  }|t||d W dQ R X q
W |S )z^Find out what items are documented in source/*.rst.

    See `find_autosummary_in_lines`.
    rzutf-8ignore)encodingerrors)r:   N)codecsro   read
splitlinesextendfind_autosummary_in_lines)	filenames
documentedr:   ry   linesr   r   r   ri      s    


 ri   c       	   
   C   s   y,t | \}}}}t| }t|| |dS  tk
r@   Y nP tk
rp } ztd| |f  W dd}~X Y n  tk
r   td|   Y nX g S )znFind out what items are documented in the given object's docstring.

    See `find_autosummary_in_lines`.
    )rR   r:   zFailed to import '%s': %sNz_Failed to import '%s'; the module executes module level statement and it might call sys.exit().)	r   pydocZgetdocr   r   rH   rm   r&   
SystemExit)	rO   rR   r:   Z	real_namerK   rv   modnamer   r@   r   r   r   find_autosummary_in_docstring  s     r   c             C   s  t d}t d}t d}t d}t d}t d}g }	d}
d}|}d}d	}xp| D ]f}|rP||}|r|d
}
|r\tjtj||
}
q\||}|r|d
 }q\| 	drq\||}|r2|d
 }|	dr|d
d }|r |	|d s d||f }|	
||
|f q\| r\|	|d rLq\d}||}|rxd}|d
}d}
d}q\||}|r|d
 }|	t||d q\||}|r\|d}q\q\W |	S )a  Find out what items appear in autosummary:: directives in the
    given lines.

    Returns a list of (name, toctree, template) where *name* is a name
    of an object and *toctree* the :toctree: path of the corresponding
    autosummary directive (relative to the root of the file name), and
    *template* the value of the :template: option. *toctree* and
    *template* ``None`` if the directive does not have the
    corresponding options set.
    z^(\s*)\.\.\s+autosummary::\s*z.^\s*\.\.\s+automodule::\s*([A-Za-z0-9_.]+)\s*$z4^\s*\.\.\s+(current)?module::\s*([a-zA-Z0-9_.]+)\s*$z%^\s+(~?[_a-zA-Z][a-zA-Z0-9_.]*)\s*.*?z^\s+:toctree:\s*(.*?)\s*$z^\s+:template:\s*(.*?)\s*$NF    :~rZ   z%s.%s T)r:      )recompilematchgroupr6   r7   r8   dirnamestriprD   rJ   searchr   r   )r   rR   r:   Zautosummary_reZautomodule_reZ	module_reZautosummary_item_reZtoctree_arg_reZtemplate_arg_rer   Ztoctreer{   Zcurrent_moduleZin_autosummaryZbase_indentr2   mrO   r   r   r   r     sx    













r   c              C   s   t jdtdtdd} | jddddt d	 | jd
dtdd | jddddtdd | jdddddtdd | jddddd tdd | jddd d!d"td#d | S )$Nz#%(prog)s [OPTIONS] <SOURCE_FILE>...z5For more information, visit <http://sphinx-doc.org/>.a  
Generate ReStructuredText using autosummary directives.

sphinx-autogen is a frontend to sphinx.ext.autosummary.generate. It generates
the reStructuredText files from the autosummary directives contained in the
given input files.

The format of the autosummary directive is documented in the
``sphinx.ext.autosummary`` Python module and can be read using::

  pydoc sphinx.ext.autosummary
)ZusageZepilogZdescriptionz	--versionversionZshow_versionz%%(prog)s %s)actiondestr   source_file+z&source files to generate rST files for)nargshelpz-oz--output-dirZstorer^   z directory to place all output in)r   r   r   z-sz--suffixr_   Zrstz/default suffix for files (default: %(default)s))r   r   defaultr   z-tz--templatesr=   z0custom template directory (default: %(default)s)z-iz--imported-members
store_trueru   Fz0document imported members (default: %(default)s))argparseArgumentParserr   add_argumentr   )parserr   r   r   
get_parsern  s,    



r   r   c             C   sh   t jtjd t jtjtdd t	 }t
| t | }t|j|jd|j |j|j|d d S )Nr   localesphinxrZ   )rc   ru   r#   )r   r   	setlocaleLC_ALLZinit_consoler6   r7   r8   r   r   r%   r   
parse_argsrt   r   r^   r_   r=   ru   )argvr#   argsr   r   r   main  s    
r   __main__)r-   )NN)NN)0r   Z
__future__r   r   r   r   r6   r   r   r*   Zjinja2r   r   Zjinja2.sandboxr   Zsphinx.localer   r   r   Zsphinx.ext.autosummaryr   r	   Zsphinx.jinja2gluer
   r   Zsphinx.registryr   Zsphinx.util.inspectr   Zsphinx.util.osutilr   Zsphinx.util.rstr   rh   objectr   r%   r(   r,   r3   rt   ri   r   r   r   r   r   r   r   r   r   r   <module>   sJ   
	 

W+
