U
    \ShJ                     @  s`  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mZ ddl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"dddddZ#dddddZ$dOdddd d!d"Z%ddd#d$d%d&d'Z&dPddd#dd$d(d)d*Z'g dfdddd+d#d+dd+dd$d,
d-d.Z(dQd+d#ddd$d0d1d2Z)g fdd#d+dd3d4d5Z*dd#d+dd6d7d8Z+dd+d#d9d:d;d<Z,dd+d#dd:d=d>Z-dRdd+d#dd+d?d@dAZ.dd+ddBdCdDZ/dEdFdGdHZ0ej1dId fd+dJdKdLdMZ2e3dNkr\e2  dS )Su  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/
    )annotationsN)copyfnmatch)EXTENSION_SUFFIXES)path)Any	Generator)__display_version__package_dir)
EXTENSIONS)__)FileAvoidWrite	ensuredir)ReSTRendererZSPHINX_APIDOC_OPTIONS,memberszundoc-memberszshow-inheritance)z.pyz.pyxZ	templatesZapidocstrbool)filenamereturnc                   s*   t |  t fddtttddD S )z)Check *filename* is __init__ file or not.c                 3  s   | ]} d | kV  qdS )__init__N ).0suffixbasenamer   D/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/ext/apidoc.py	<genexpr>3   s   zis_initpy.<locals>.<genexpr>T)keyreverse)r   r   anysortedPY_SUFFIXESlen)r   r   r   r   	is_initpy0   s    
r%   )modnamesr   c                  G  s   d td| S )zJoin module names with dots..N)joinfilter)r&   r   r   r   module_join9   s    r*   z
str | Nonezlist[str] | None)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%   r   fr   r   r   r   E   s      z is_packagedir.<locals>.<genexpr>)oslistdirr!   )r+   r,   r   r   r   is_packagedir>   s
    
r3   r   None)nametextoptsr   c              	   C  s   t |dd}t|j|  d|j }|jrD|s@ttd|  dS |jsjt	|rj|sttd|  n2|s~ttd|  t
|}|| W 5 Q R X dS )z0Write the output file for module/package <name>.quietNr'   zWould create file %s.z!File %s already exists, skipping.zCreating file %s.)getattrr   r(   destdirr   dryrunprintr   forceisfiler   write)r5   r6   r7   r8   fnamer0   r   r   r   
write_fileH   s    
rA   )packager   r7   user_template_dirr   c                 C  s`   t t}|jr d|kr |d t| |}|j |||d}t|tgd|}t	||| dS ).Build the text of the file and write the file.private-members)show_headingsr   qualnameautomodule_optionszmodule.rst_tN)
r   OPTIONSincludeprivateappendr*   
noheadingsr   template_dirrenderrA   )rB   r   r7   rC   optionsrG   contextr6   r   r   r   create_module_file[   s    

rQ   z	list[str])
rootmaster_packagesubrootpy_filesr7   subsis_namespaceexcludesrC   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r|
D ]}td|| qdS )	rD   c                   s,   g | ]$}t t| st|qS r   )is_skipped_packager   r(   r*   )r   pkgnamerX   rS   r7   rR   rT   r   r   
<listcomp>s   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)rX   r7   rR   r   r   r\   w   s    c                   s   g | ]}t  |qS r   )r*   )r   modname)rS   rT   r   r   r\   {   s   rE   )	rZ   subpackages
submodulesrW   modulefirstseparatemodulesrH   rF   maxdepthzpackage.rst_tN)r"   setr   rI   rJ   rK   r*   rd   re   rL   rf   r   rM   rN   rA   rQ   )rR   rS   rT   rU   r7   rV   rW   rX   rC   rb   rc   rO   rZ   rP   r6   	submoduler   r[   r   create_package_filem   s6    


ri   modules)rj   r7   r5   rC   r   c                 C  sp   |    d}| dd D ]"}||d r6| | q|}q|j|j| d}t|tgd|}t||| dS )zCreate the module's index. Nr'   )headerrf   Zdocnamesz	toc.rst_t)	sort
startswithremoverl   rf   r   rM   rN   rA   )rj   r7   r5   rC   prev_modulemodulerP   r6   r   r   r   create_modules_toc_file   s    rr   )r+   r7   rX   r   c                   sX   t  sdS tt  d}tdd |D }|s@|js@dS t fdd|D S )%Check if we want to skip this module.Fz*.pyc                 s  s   | ]}t |r|V  qd S r-   r.   r/   r   r   r   r      s      z%is_skipped_package.<locals>.<genexpr>Tc                 3  s    | ]}t t |V  qd S r-   )is_excludedr   r(   r/   r+   rX   r   r   r      s     )r   isdirglobr(   r!   implicit_namespacesall)r+   r7   rX   r,   Zregular_packager   ru   r   rY      s    

rY   )r   r7   rX   r   c                 C  s,   t | sdS t | dr(|js(dS dS )rs   T_F)r   existsr   rn   rJ   )r   r7   rX   r   r   r   r]      s
    
r]   z7Generator[tuple[str, list[str], list[str]], None, None])rootpathrX   r7   r   c                 #  s   t |dd}t |dd}tj| |dD ]^\}}tfdd|D }|rRd nd t fd	d|D |d
d
< ||fV  q&d
S )z@Walk through the directory and list files and subdirectories up.followlinksFrJ   )r}   c                 3  s.   | ]&}| trtt| s|V  qd S r-   )endswithr#   rt   r   r(   r/   )rX   rR   r   r   r      s    
zwalk.<locals>.<genexpr>)r'   )r'   rz   c                 3  s.   | ]&}|  stt|s|V  qd S r-   )rn   rt   r   r(   r_   Zexclude_prefixesrX   rR   r   r   r      s     
N)r9   r1   walkr"   )r|   rX   r7   r}   rJ   rV   r,   r   r   r   r      s    "r   c                 C  s   t dd t| ||D S )zACheck the given directory contains child module/s (at least one).c                 s  s   | ]\}}}|V  qd S r-   r   )r   _root_subsr,   r   r   r   r      s   z#has_child_module.<locals>.<genexpr>)r!   r   )r|   rX   r7   r   r   r   has_child_module   s    
r   )r|   rX   r7   rC   r   c                 C  s  t |dd}t| s|r*| tjd }nd}g }t| ||D ]V\}}}	td|	}
|
 o\|}|
r|	dd D ]"}t|rn|	| |	d| qnn|| kr|s|dd= q>|
s|r2|st	|	dkst
||s|t	| d tjtjd}|rt|||rt||||	|||||	 |t|| q>|| ks@t|dksNt|	D ]B}tt| |||sR|dd }t|||| || qRq>|S )z`
    Look for every file in the directory tree and create the corresponding
    ReST files.
    rx   FNr      r'   )r9   r3   r^   r   sepr   r%   ro   insertr$   rY   lstripreplacer   ri   rK   r*   AssertionErrorr]   r(   rQ   )r|   rX   r7   rC   rx   Zroot_packageZ	toplevelsrR   rV   r,   Zis_pkgrW   r0   Z
subpackageZpy_filerq   r   r   r   recurse_tree   sR    




     r   )rR   rX   r   c                   s   t  fdd|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".
    c                 3  s   | ]}t  |V  qd S r-   r   r   excluderR   r   r   r   "  s   zis_excluded.<locals>.<genexpr>)r!   )rR   rX   r   r   r   rt     s    rt   zargparse.ArgumentParser)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 tD ]*}|jdo| dpdq| dktdr| ds qd| 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storer:   Tzdirectory to place all output)r   r   requiredr   z-q
store_truer8   z,no output on stdout, just warnings on stderr)r   r   r   z-dz
--maxdepthrf      z;maximum depth of submodules to show in the TOC (default: 4))r   r   typedefaultr   z-fz--forcer=   zoverwrite existing filesz-lz--follow-linksr}   FzNfollow symbolic links. Powerful when combined with collective.recipe.omelette.)r   r   r   r   z-nz	--dry-runr;   z%run the script without creating filesz-ez
--separatere   z1put documentation for each module on its own pagez-Pz	--privaterJ   zinclude "_private" modulesz	--tocfiletocfilerj   z0filename of table of contents (default: modules)z-Tz--no-tocstore_falsez%don't create a table of contents filez-Ez--no-headingsrL   zedon't create headings for the module/package packages (e.g. when the docstrings already contain them)z-Mz--module-firstrd   z7put module documentation before submodule documentationz--implicit-namespacesrx   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-projectrl   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   
extensionsrK   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   r   r   )argvr   c                 C  s  t t jd tj   t }|| }t|j	}|j
dkrP|tjd |_
|jdrl|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}|D ]&}	|	|d rq|	}|d|	 7 }q|jdd|j
|jp*d|j p4d|j!pF|j pFdd|j dddddgddd|j"|d||j#d}
|j$r|
d %|j$ |j&rd|
d< |
d dd D ]2}d|kr|
d '| |
d %|d q|js|j(|
d|j)|jd n|j*rt+|||j*|j d
S )z+Parse and check the command line arguments.rk   Nr   r'   r   z%s is not a directory.)filec                 S  s   g | ]}t |qS r   )r   abspathr   r   r   r   r\     s     zmain.<locals>.<listcomp>r   )
quickstartz   %s
Frz   AuthorindexTzsphinx.ext.autodoczsphinx.ext.viewcodezsphinx.ext.todoen)r   r   dotprojectr   r   r   r   ZmasterZepubr   makefileZ	batchfileZ	make_modeZmastertocmaxdepthZmastertoctreelanguager   r   r   r8   r   )Zsilent	overwriter   ),locale	setlocaleLC_ALLsphinxZinit_consoler   
parse_argsr   r   r   rl   r^   r   r   rn   rv   r<   r   sysstderr
SystemExitr;   r   r:   r   r   r   r   Z
sphinx.cmdr   rm   r   r   r   rf   r   r   extendr8   ro   generater=   r   rr   )r   r   argsr|   rX   rj   qsrp   r6   rq   dr   r   r   r   main  s~    







r   __main__)NN)N)rj   N)N)4__doc__
__future__r   r   rw   r   r1   r   r   r   Zimportlib.machineryr   r   typingr   r	   Zsphinx.localer   r
   r   Zsphinx.cmd.quickstartr   r   Zsphinx.util.osutilr   r   Zsphinx.util.templater   environr^   rI   tupler#   r(   rM   r%   r*   r3   rA   rQ   ri   rr   rY   r]   r   r   r   rt   r   r   r   __name__r   r   r   r   <module>   s`   
	
  "(  	 7aL
