B
    \NJ                 @   sr  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mZ ddlm	Z	 ddl
mZ ddl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ejkrejd dZn
dddgZdZeddgZdd Zdd Z d0ddZ!d1ddZ"dd Z#g fdd Z$d2d"d#Z%g fd$d%Z&d&d' Z'd(d) Z(d*d+ Z)ej*d,d fd-d.Z+e,d/krne+  dS )3u  
    sphinx.ext.apidoc
    ~~~~~~~~~~~~~~~~~

    Parses a directory tree looking for Python modules and packages and creates
    ReST files appropriately to create code documentation with Sphinx.  It also
    creates a modules index (named modules.<suffix>).

    This is derived from the "sphinx-autopackage" script, which is:
    Copyright 2008 Société des arts technologiques (SAT),
    https://sat.qc.ca/

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    )print_functionN)fnmatch)path)binary_type)__display_version__package_dir)
EXTENSIONS)__)rst)FileAvoidWrite	ensuredirwalkZSPHINX_APIDOC_OPTIONS,memberszundoc-memberszshow-inheritancez__init__.pyz.pyz.pyxc             C   s"   | r| }|r|d| 7 }n|}|S )z#Join package and module with a dot.. )packagemodulenamer   r   0lib/python3.7/site-packages/sphinx/ext/apidoc.pymakename8   s    r   c          	   C   s   t |jd| |jf }|jr2ttd|  dS |jsTt |rTttd|  n.ttd|  t	|}|
| W dQ R X dS )z0Write the output file for module/package <name>.z%s.%szWould create file %s.Nz!File %s already exists, skipping.zCreating file %s.)r   joindestdirsuffixdryrunprintr	   forceisfiler   write)r   textoptsfnamefr   r   r   
write_fileE   s    
r#   Tc             C   s4   |rt |}dddg| d  t| }d||f S )z2Create a heading of <level> [1, 2 or 3 supported].=-~   z%s
%s

)r
   escapelen)levelr   r(   Zunderliningr   r   r   format_headingT   s    
r+   c             C   s,   dt ||  }xtD ]}|d| 7 }qW |S )z4Create the automodule directive and add the options.z.. automodule:: %s
z	    :%s:
)r   OPTIONS)r   r   Z	directiveZoptionr   r   r   format_directive]   s    
r-   c             C   s>   |j stdd| }nd}|t|| 7 }tt| ||| dS )z.Build the text of the file and write the file.r'   z	%s module N)
noheadingsr+   r-   r#   r   )r   r   r    r   r   r   r   create_module_filef   s
    r0   c                s  t d|sdndt|| }jr<|s<|t||7 }|d7 } fdd|D }|r|t dd7 }|d	7 }x"|D ]}	|d
t|||	f 7 }qrW |d7 } fdd|D }
|
r|t dd7 }jr:|d	7 }x|
D ]\}t|t||}|d| 7 }jst dd| }nd}|tt|||7 }t|| qW nVxT|
D ]L}t|t||}jsn|t dd| 7 }|tt|||7 }|d7 }q@W |d7 }js|s|t dd7 }|t||7 }tt||| dS )z.Build the text of the file and write the file.r'   z
%s packagez%s namespace
c                s&   g | ]}t t|t s|qS r   )
shall_skipr   r   INITPY).0sub)excludesr    rootr   r   
<listcomp>   s    z'create_package_file.<locals>.<listcomp>   ZSubpackagesz.. toctree::

z
    %s.%s
c                s6   g | ].}t t| s|tkrt|d  qS )r   )r2   r   r   r3   splitext)r4   r5   )r6   r    r7   r   r   r8      s    Z
Submodulesz   %s
z	%s moduler.   zModule contentsN)r+   r   modulefirstr-   separatemodulesr/   r#   )r7   Zmaster_packageZsubrootpy_filesr    subsis_namespacer6   r   r5   ZsubmodsZsubmodZmodfileZfiletextr   )r6   r    r7   r   create_package_filer   sL    



r@   modulesc             C   st   t dd|j dd}|d7 }|d|j 7 }|   d}x,| D ]$}||d rPq<|}|d	| 7 }q<W t||| d
S )zCreate the module's index.r'   z%sF)r(   z.. toctree::
z   :maxdepth: %s

r.   r   z   %s
N)r+   headermaxdepthsort
startswithr#   )rA   r    r   r   prev_moduler   r   r   r   create_modules_toc_file   s    
rG   c             C   s   |j st| sdS tj| tkrld}t| }x0tt|dD ]}t	t|||sFd}qFW |rldS t| }|dkr|
dr|jsdS dS )z%Check if we want to skip this module.Tz*.pyFz__init__.py_)implicit_namespacesr   existsosbasenamer3   dirnameglobr   is_excludedrE   includeprivate)r   r    r6   Zall_skippedZ
basemoduleZ	submodulefilenamer   r   r   r2      s    

r2   c          
      s  t |dd}t |dd}t |dd}tt| ks6|rH| tjd }nd}g }xt| |dD ]\}}	tfdd	|	D }
t|
k}t|
ko|}|r|
	t |

d
t n| kr|s|dd= q`|rd nd t fdd	|D |dd< |s|r|s0t|
dks0ttt|st| d tjtjd}|rft|
d
krt|||
||| |t|| q`| kr|dkstxF|
D ]>}tt| ||st|d
 }t||| || qW q`W |S )z`
    Look for every file in the directory tree and create the corresponding
    ReST files.
    followlinksFrP   rI   N)rR   c             3   s6   | ].}t |d  tkrtt | s|V  qdS )r'   N)r   r:   PY_SUFFIXESrO   r   )r4   r"   )r6   r7   r   r   	<genexpr>   s    zrecurse_tree.<locals>.<genexpr>r   )r   )r   rH   c             3   s.   | ]&}|  stt|s|V  qd S )N)rE   rO   r   r   )r4   r5   )exclude_prefixesr6   r7   r   r   rU     s    r'   r   )getattrr3   rK   listdirsplitr   sepr   sortedremoveinsertr)   r2   r   lstripreplacer@   appendr   AssertionErrorr:   r0   )rootpathr6   r    rR   rP   rI   Zroot_packageZ	toplevelsr>   filesr=   Zis_pkgr?   Z
subpackageZpy_filer   r   )rV   r6   r7   r   recurse_tree   sJ    

"(

rd   c             C   s    x|D ]}t | |rdS qW dS )zCheck if the directory is in the exclude list.

    Note: by having trailing slashes, we avoid common prefix issues, like
          e.g. an exclude "foo" also accidentally excluding "foobar".
    TF)r   )r7   r6   excluder   r   r   rO     s    

rO   c           	   C   sj  t jdtdtdd} | jddddt d	 | jd
tdd | jddtdd | jdddddtdd | jddddt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,td-d# | jd.d/d d0td1d# | jd2d3d d4td5d# | jd6dd7d8td9d) | jd:d;d<d7td=d# | jd>d?d d@tdAd# | jdBdCd dDtdEd# | jdFd dGtdHd# | jdIdJddKdLtdMd) | jdNdOd dPtdQd# | jdRdSd dTtdUd# | jdVdWddXtdYd# | jdZd[dd\td]d# | jd^d_ddtd`d# | jdadbddctddd# | tde}x2tD ]*}|jdf| dgdh| ditdj| dk q8W | S )lNzH%(prog)s [OPTIONS] -o <OUTPUT_PATH> <MODULE_PATH> [EXCLUDE_PATTERN, ...]z5For more information, visit <http://sphinx-doc.org/>.aE  
Look recursively in <MODULE_PATH> for Python modules and packages and create
one reST file with automodule directives per package in the <OUTPUT_PATH>.

The <EXCLUDE_PATTERN>s can be file and/or directory patterns that will be
excluded from generation.

Note: By default this script will not overwrite already created files.)ZusageZepilogZdescriptionz	--versionversionZshow_versionz%%(prog)s %s)actiondestrf   module_pathzpath to module to document)helpexclude_pattern*zGfnmatch-style file and/or directory patterns to exclude from generation)nargsrj   z-oz--output-dirZstorer   Tzdirectory to place all output)rg   rh   Zrequiredrj   z-dz
--maxdepthrC      z;maximum depth of submodules to show in the TOC (default: 4))rg   rh   typedefaultrj   z-fz--force
store_truer   zoverwrite existing files)rg   rh   rj   z-lz--follow-linksrR   FzNfollow symbolic links. Powerful when combined with collective.recipe.omelette.)rg   rh   rp   rj   z-nz	--dry-runr   z%run the script without creating filesz-ez
--separater<   z1put documentation for each module on its own pagez-Pz	--privaterP   zinclude "_private" modulesz	--tocfiletocfilerA   z0filename of table of contents (default: modules)z-Tz--no-tocZstore_falsez%don't create a table of contents filez-Ez--no-headingsr/   zedon't create headings for the module/package packages (e.g. when the docstrings already contain them)z-Mz--module-firstr;   z7put module documentation before submodule documentationz--implicit-namespacesrI   zNinterpret module paths according to PEP-0420 implicit namespaces specificationz-sz--suffixr   r
   zfile suffix (default: rst)z-Fz--fullfullz.generate a full project with sphinx-quickstartz-az--append-syspathappend_syspathz9append module_path to sys.path, used when --full is givenz-Hz--doc-projectrB   z(project name (default: root module name)z-Az--doc-authorauthorz,project author(s), used when --full is givenz-Vz--doc-versionz*project version, used when --full is givenz-Rz--doc-releasereleasezEproject release, used when --full is given, defaults to --doc-versionzextension optionsz--ext-%sZappend_constzsphinx.ext.%s
extensionszenable %s extension)rg   Zconstrh   rj   )argparseArgumentParserr	   add_argumentr   intZadd_argument_groupr   )parsergroupZextr   r   r   
get_parser*  s    








r~   r'   c             C   sV  t jtjd t jtjtdd t	 }|
| }t|j}|jdkr`|tjd |_|jdr||jdd |_t|sttd| tjd	 td |jst|j d
d |jD }t|||}|jr<ddlm} |   d}d}x.|D ]&}	|	|d rq|	}|d|	 7 }qW t!|jdd|j|j"p>d|j#pHd|j$pZ|j#pZdd|j dddddgddd|j%|d||j&d}
|j'r|
d (|j' t)|jt*r|
d +d|
d< t)|j"t*r|
d +d|
d< t)|j#t*r |
d +d|
d< t)|j$t*r |
d +d|
d< |jsR|j,|
d|j-d n|j.rRt/|||j. dS ) z+Parse and check the command line arguments.r.   localesphinxNrS   r   r'   z%s is not a directory.)filec             S   s   g | ]}t |qS r   )r   abspath)r4   re   r   r   r   r8     s    zmain.<locals>.<listcomp>r   )
quickstartz   %s
FrH   ZAuthorindexTzsphinx.ext.autodoczsphinx.ext.viewcodezsphinx.ext.todoZen)r   rZ   dotprojectru   rf   rv   r   ZmasterZepubrw   ZmakefileZ	batchfileZ	make_modeZmastertocmaxdepthZmastertoctreeZlanguageri   rt   rw   r   zutf-8ru   rf   rv   )ZsilentZ	overwrite)0r   r   	setlocaleLC_ALLZinit_consolerK   r   r   r   r~   
parse_argsr   ri   rB   rY   rZ   r   rE   isdirr   r	   sysstderrexitr   r   r   rk   rd   rs   Z
sphinx.cmdr   rD   dictru   rf   rv   rC   rt   rw   extend
isinstancer   decodeZgenerater   rr   rG   )argvr|   argsrb   r6   rA   ZqsrF   r   r   dr   r   r   main  s|    








r   __main__)T)N)rA   )-__doc__Z
__future__r   rx   rN   r   rK   r   r   r   Zsixr   Zsphinx.localer   r   r   Zsphinx.cmd.quickstartr   r	   Zsphinx.utilr
   Zsphinx.util.osutilr   r   r   environrY   r,   r3   setrT   r   r#   r+   r-   r0   r@   rG   r2   rd   rO   r~   r   r   __name__r   r   r   r   <module>   sH   

	
	<
AYN
