B
    dK                 @   s  d 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m
Z
 ddlmZmZmZmZ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mZ ddlmZ dejkrejd dZn
dddgZde e	 Z!e
"eddZ#e$e%dddZ&e$e$dddZ'dDee$ eee$  e%dddZ(e$e$edddd Z)dEe$e$eee$ dd!d"d#Z*g dfe$e$e$ee$ eee$ e%ee$ ee$ dd$
d%d&Z+dFee$ ee$ee$ dd(d)d*Z,g fe$eee$ e%d+d,d-Z-e$eee$ e%d.d/d0Z.e$ee$ eeee$ee$ ee$ f ddf d1d2d3Z/e$ee$ ee%d1d4d5Z0dGe$ee$ eee$ ee$ d6d7d8Z1e$ee$ e%d9d:d;Z2ej3d<d=d>Z4ej5d?d fee$ e6d@dAdBZ7e8dCkre7  dS )Hu  Creates reST files corresponding to Python modules for code documentation.

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/
    N)copy)fnmatch)EXTENSION_SUFFIXES)path)Any	GeneratorListOptionalTuple)__display_version__package_dir)
EXTENSIONS)__)FileAvoidWrite	ensuredir)ReSTRendererZSPHINX_APIDOC_OPTIONS,memberszundoc-memberszshow-inheritance)z.pyz.pyxZ	templatesZapidoc)filenamereturnc             C   s:   t | }x*tttddD ]}|d| krdS qW dS dS )z)Check *filename* is __init__ file or not.T)keyreverse__init__FN)r   basenamesortedPY_SUFFIXESlen)r   r   suffix r   ^/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sphinx/ext/apidoc.py	is_initpy.   s
    
r    )modnamesr   c              G   s   d td| S )zJoin module names with dots..N)joinfilter)r!   r   r   r   module_join8   s    r%   )dirnamefilesr   c             C   s8   |dkr| dkrdS |dkr&t | }tdd |D S )z+Check given *files* contains __init__ file.NFc             s   s   | ]}t |r|V  qd S )N)r    ).0fr   r   r   	<genexpr>D   s    z is_packagedir.<locals>.<genexpr>)oslistdirany)r&   r'   r   r   r   is_packagedir=   s
    
r.   )nametextoptsr   c          	   C   s   t |dd}t|jd| |jf }|jrB|s>ttd|  dS |jsht	|rh|sttd|  n2|s|ttd|  t
|}|| W dQ R X dS )z0Write the output file for module/package <name>.quietNz%s.%szWould create file %s.z!File %s already exists, skipping.zCreating file %s.)getattrr   r#   destdirr   dryrunprintr   forceisfiler   write)r/   r0   r1   r2   fnamer)   r   r   r   
write_fileG   s    
r;   )packager   r1   user_template_dirr   c             C   s`   t t}|jr d|kr |d t| |}|j |||d}t|tgd|}t	||| dS )z.Build the text of the file and write the file.zprivate-members)show_headingsr   qualnameautomodule_optionszmodule.rst_tN)
r   OPTIONSincludeprivateappendr%   
noheadingsr   template_dirrenderr;   )r<   r   r1   r=   optionsr?   contextr0   r   r   r   create_module_fileZ   s    

rI   )
rootmaster_packagesubrootpy_filesr1   subsis_namespaceexcludesr=   r   c	          
      s    fdd|D }	 fdd|D }
t t|
}
fdd|
D }
tt}jrpd|krp|d t}||	|
|jj|j	 j
d	}t|tgd|}t|| |
rjrx|
D ]}td|| qW dS )	z.Build the text of the file and write the file.c                s,   g | ]$}t t| st|qS r   )is_skipped_packager   r#   r%   )r(   pkgname)rP   rK   r1   rJ   rL   r   r   
<listcomp>r   s   z'create_package_file.<locals>.<listcomp>c                s6   g | ].}t t| st|s|d d qS )r"   r   )is_skipped_moduler   r#   r    split)r(   sub)rP   r1   rJ   r   r   rS   v   s    c                s   g | ]}t  |qS r   )r%   )r(   modname)rK   rL   r   r   rS   z   s   zprivate-members)	rR   subpackages
submodulesrO   modulefirstseparatemodulesr@   r>   maxdepthzpackage.rst_tN)r   setr   rA   rB   rC   r%   rZ   r[   rD   r\   r   rE   rF   r;   rI   )rJ   rK   rL   rM   r1   rN   rO   rP   r=   rX   rY   rG   rR   rH   r0   	submoduler   )rP   rK   r1   rJ   rL   r   create_package_filel   s0    




r_   modules)r`   r1   r/   r=   r   c             C   st   |    d}x2| dd D ]"}||d r8| | q|}qW |j|j| d}t|tgd|}t||| dS )zCreate the module's index. Nr"   )headerr\   Zdocnamesz	toc.rst_t)	sort
startswithremoverb   r\   r   rE   rF   r;   )r`   r1   r/   r=   prev_modulemodulerH   r0   r   r   r   create_modules_toc_file   s    rh   )r&   r1   rP   r   c                sd   t  sdS tt  d}tdd |D }|s@|js@dS t fdd|D r\dS dS dS )z%Check if we want to skip this module.Fz*.pyc             s   s   | ]}t |r|V  qd S )N)r    )r(   r)   r   r   r   r*      s    z%is_skipped_package.<locals>.<genexpr>Tc             3   s    | ]}t t |V  qd S )N)is_excludedr   r#   )r(   r)   )r&   rP   r   r   r*      s    N)r   isdirglobr#   r-   implicit_namespacesall)r&   r1   rP   r'   Zregular_packager   )r&   rP   r   rQ      s    

rQ   )r   r1   rP   r   c             C   s0   t | sdS t | dr(|js(dS dS dS )z%Check if we want to skip this module.T_FN)r   existsr   rd   rB   )r   r1   rP   r   r   r   rT      s
    
rT   )rootpathrP   r1   r   c             #   s   t |dd}t |dd}xptj| |dD ]^\}}tfdd|D }|rTd nd t fd	d|D |d
d
< ||fV  q(W d
S )z@Walk through the directory and list files and subdirectories up.followlinksFrB   )rq   c             3   s.   | ]&}| trtt| s|V  qd S )N)endswithr   ri   r   r#   )r(   r)   )rP   rJ   r   r   r*      s    
zwalk.<locals>.<genexpr>)r"   )r"   rn   c             3   s.   | ]&}|  stt|s|V  qd S )N)rd   ri   r   r#   )r(   rV   )exclude_prefixesrP   rJ   r   r   r*      s    N)r3   r+   walkr   )rp   rP   r1   rq   rB   rN   r'   r   )rs   rP   rJ   r   rt      s    "rt   c             C   s(   x"t | ||D ]\}}}|rdS qW dS )zACheck the given directory contains child module/s (at least one).TF)rt   )rp   rP   r1   _root_subsr'   r   r   r   has_child_module   s    rw   )rp   rP   r1   r=   r   c             C   s  t |dd}t| s|r*| tjd }nd}g }xlt| ||D ]Z\}}}	td|	}
|
 o`|}|
rxL|	dd D ]"}t|rt|	| |	d| qtW n|| kr|s|dd= qB|
s|r:|st	|	dkst
||s|t	| d tjtjd}|rt|||rt||||	|||||	 |t|| qB|| krN|dksRtxJ|	D ]B}tt| |||sX|dd }t|||| || qXW qBW |S )z`
    Look for every file in the directory tree and create the corresponding
    ReST files.
    rl   FNr      r"   )r3   r.   rU   r   seprt   r    re   insertr   rQ   lstripreplacerw   r_   rC   r%   AssertionErrorrT   r#   rI   )rp   rP   r1   r=   rl   Zroot_packageZ	toplevelsrJ   rN   r'   Zis_pkgrO   r)   Z
subpackageZpy_filerg   r   r   r   recurse_tree   sB    







r   )rJ   rP   r   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   )rJ   rP   excluder   r   r   ri      s    

ri   )r   c           	   C   s  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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d0d | jd1d2dd3td4d | jd5d6dd7td8d | jd9dd:d;td<d, | jd=d>d?d:td@d | jdAdBddCtdDd | jdEdFddGtdHd | jdIddJtdKd | jdLdMddNdOtdPd, | jdQdRddStdTd | jdUdVddWtdXd | jdYdZdd[td\d | jd]d^dd_td`d | jdadbddtdcd | jdddeddftdgd | tdh}|jdidjdkdltdmdn x2tD ]*}|jdo| dpdq| dktdr| ds qfW | tdt}|jdudvdwdxtdydz | S ){NzH%(prog)s [OPTIONS] -o <OUTPUT_PATH> <MODULE_PATH> [EXCLUDE_PATTERN, ...]z:For more information, visit <https://www.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.)usageepilogdescriptionz	--versionversionZshow_versionz%%(prog)s %s)actiondestr   module_pathzpath to module to document)helpexclude_pattern*zGfnmatch-style file and/or directory patterns to exclude from generation)nargsr   z-oz--output-dirstorer4   Tzdirectory to place all output)r   r   requiredr   z-q
store_truer2   z,no output on stdout, just warnings on stderr)r   r   r   z-dz
--maxdepthr\      z;maximum depth of submodules to show in the TOC (default: 4))r   r   typedefaultr   z-fz--forcer7   zoverwrite existing filesz-lz--follow-linksrq   FzNfollow symbolic links. Powerful when combined with collective.recipe.omelette.)r   r   r   r   z-nz	--dry-runr5   z%run the script without creating filesz-ez
--separater[   z1put documentation for each module on its own pagez-Pz	--privaterB   zinclude "_private" modulesz	--tocfiletocfiler`   z0filename of table of contents (default: modules)z-Tz--no-tocstore_falsez%don't create a table of contents filez-Ez--no-headingsrD   zedon't create headings for the module/package packages (e.g. when the docstrings already contain them)z-Mz--module-firstrZ   z7put module documentation before submodule documentationz--implicit-namespacesrl   zNinterpret module paths according to PEP-0420 implicit namespaces specificationz-sz--suffixr   rstz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--extensionsr   
extensionsrC   zenable arbitrary extensions)metavarr   r   r   z--ext-%sappend_constzsphinx.ext.%szenable %s extension)r   constr   r   zProject templatingz-tz--templatedirZTEMPLATEDIRtemplatedirz%template directory for template files)r   r   r   )argparseArgumentParserr   add_argumentr   intadd_argument_groupr   )parsergroupextr   r   r   
get_parser,  s    











r   ry   )argvr   c             C   s<  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}|jrddlm } |!  d}d}x2|D ]*}	|	|d rq |	}|d|	 7 }q W |jdd|j|j"pDd|j#pNd|j$p`|j#p`dd|j dddddgddd|j%|d||j&d}
|j'r|
d (|j' |j)rd|
d< xF|
d dd D ]2}d|kr|
d *| |
d (|d qW |js8|j+|
d|j,|jd n|j-r8t.|||j-|j dS )z+Parse and check the command line arguments.ra   localesphinxNrx   r"   ry   z%s is not a directory.)filec             S   s   g | ]}t |qS r   )r   abspath)r(   r   r   r   r   rS     s    zmain.<locals>.<listcomp>r   )
quickstartz   %s
Frn   AuthorindexTzsphinx.ext.autodoczsphinx.ext.viewcodezsphinx.ext.todoen)r   rz   dotprojectr   r   r   r   ZmasterZepubr   makefileZ	batchfileZ	make_modeZmastertocmaxdepthZmastertoctreelanguager   r   r   r2   r   )Zsilent	overwriter   )/r   r   	setlocaleLC_ALLZinit_consoler+   r   r#   r   r   
parse_argsr   r   rb   rU   rz   r   rd   rj   r6   r   sysstderrexitr5   r   r4   r   r   r   r   Z
sphinx.cmdr   rc   r   r   r   r\   r   r   extendr2   re   generater7   r   rh   )r   r   argsrp   rP   r`   qsrf   r0   rg   dr   r   r   r   main  sx    









r   __main__)NN)N)r`   N)N)9__doc__r   rk   r   r+   r   r   r   Zimportlib.machineryr   r   typingr   r   r   r	   r
   Zsphinx.localer   r   r   Zsphinx.cmd.quickstartr   r   Zsphinx.util.osutilr   r   Zsphinx.util.templater   environrU   rA   tupler   r#   rE   strboolr    r%   r.   r;   rI   r_   rh   rQ   rT   rt   rw   r   ri   r   r   r   r   r   __name__r   r   r   r   <module>
   sV   

 
(%  
*
$5a"L
