o
    NiV                     @  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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 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 ddl m!Z! er|ddl"m#Z#m$Z$ e%e&Z'dej(v rej(d )dZ*ng dZ*ddge+eR Z,e-eddZ.d^ddZ/d_d d!Z0d`dad%d&Z1dbd,d-Z2	dcddd1d2Z3	3	dedfd>d?Z4	@	dgdhdAdBZ5	3didjdCdDZ6dkdFdGZ7dldJdKZ8dmdLdMZ9	dcdndOdPZ:dodQdRZ;dpdTdUZ<G dVd* d*eZ=didqdZd[Z>e&d\kre?e>e	j@d]d dS )ru  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)EXTENSION_SUFFIXES)path)Path)TYPE_CHECKINGAnyProtocol)__display_version__package_dir)
EXTENSIONS)__)logging)FileAvoidWrite	ensuredir)ReSTRenderer)IteratorSequenceSPHINX_APIDOC_OPTIONS,)memberszundoc-memberszshow-inheritancez.pyz.pyx	templatesapidocfilename
str | Pathreturnboolc                   s*   t | j 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   E/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/ext/apidoc.py	<genexpr>;   s
    

zis_initpy.<locals>.<genexpr>T)keyreverse)r   nameanysortedPY_SUFFIXESlen)r   r   r!   r#   	is_initpy8   s   
r,   modnames
str | Nonestrc                  G  s   d td| S )zJoin module names with dots..N)joinfilter)r-   r   r   r#   module_joinA   s   r3   dirnamefileslist[str] | Nonec                 C  s8   |du r
| du r
dS |du rt | }tdd |D S )z+Check given *files* contains __init__ file.NFc                 s      | ]	}t |r|V  qd S Nr,   r   fr   r   r#   r$   M       z is_packagedir.<locals>.<genexpr>)oslistdirr(   )r4   r5   r   r   r#   is_packagedirF   s
   
r?   r'   textopts
CliOptionsr   c                 C  s   t |j|  d|j }|jr|jsttd| |S |js0|	 r0|js.ttd| |S |js;ttd| t
|}|| W d   |S 1 sPw   Y  |S )z0Write the output file for module/package <name>.r0   zWould create file %s.z!File %s already exists, skipping.zCreating file %s.N)r   destdirr    dryrunquietloggerinfor   forceis_filer   write)r'   r@   rA   fnamer;   r   r   r#   
write_fileP   s"   

rL   packager"   user_template_dirc           	      C  sp   t t}|jrd|vr|d t| |}|j |||d}|dur'|tg}ntg}t|d|}t	|||S )z.Build the text of the file and write the file.private-members)show_headingsr"   qualnameautomodule_optionsNzmodule.rst.jinja)
r   OPTIONSincludeprivateappendr3   
noheadingstemplate_dirr   renderrL   )	rM   r"   rA   rN   optionsrQ   contexttemplate_pathr@   r   r   r#   create_module_fileb   s   


r\   r   rootmaster_packagesubrootpy_files	list[str]subsis_namespaceexcludesSequence[re.Pattern[str]]
list[Path]c	              
     s   fdd|D }	 fdd|D }
t t|
}
fdd|
D }
tt}jr8d|vr8|d t}||	|
|jj|j	 j
d	}durWtg}ntg}g }t|d|}|t|| |
rjr|fd	d|
D  |S )
zBuild the text of the file and write the file.

    Also create submodules if necessary.

    :returns: list of written files
    c                   s*   g | ]}t t| st|qS r   )is_skipped_packager   r3   )r   pkgname)rd   r^   rA   r]   r_   r   r#   
<listcomp>   s    
z'create_package_file.<locals>.<listcomp>c                   s4   g | ]}t t| st|s|d d qS )r0   r   )is_skipped_moduler   r,   splitr   sub)rd   rA   r]   r   r#   ri      s    c                   s   g | ]}t  |qS r   )r3   )r   modname)r^   r_   r   r#   ri      s    rO   )	rh   subpackages
submodulesrc   modulefirstseparatemodulesrR   rP   maxdepthNzpackage.rst.jinjac                   s   g | ]	}t d | qS r8   )r\   )r   	submodule)rA   rN   r   r#   ri      s    )r)   setr   rS   rT   rU   r3   rq   rr   rV   rs   rW   r   rX   rL   extend)r]   r^   r_   r`   rA   rb   rc   rd   rN   ro   rp   rY   rh   rZ   r[   writtenr@   r   )rd   r^   rA   r]   r_   rN   r#   create_package_file|   sF   



rx   modulesc           	      C  s|   |    d}|  D ]}||d r| | q
|}q
|j|j| d}|dur-|tg}ntg}t|d|}t	|||S )zCreate the module's index. r0   )headerrs   docnamesNztoc.rst.jinja)
sortr   
startswithremover{   rs   rW   r   rX   rL   )	ry   rA   r'   rN   prev_modulemodulerZ   r[   r@   r   r   r#   create_modules_toc_file   s   
r   c                   s\   t   sdS t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  r7   r8   r9   r:   r   r   r#   r$      r<   z%is_skipped_package.<locals>.<genexpr>Tc                 3  s     | ]}t t |V  qd S r8   )is_excludedr   r:   r4   rd   r   r#   r$      s    )r   is_dirglobr/   r(   implicit_namespacesall)r4   rA   rd   r5   regular_packager   r   r#   rg      s   
rg   	_excludesc                 C  s(   t | } |  s
dS | jdo|j S )r   T_)r   existsr'   r~   rT   )r   rA   r   r   r   r#   rj      s   rj   rootpath*Iterator[tuple[str, list[str], list[str]]]c                 #  sx    t j| |jdD ]0\}}tfdd|D }|jr d 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.)followlinksc                 3  s.    | ]}| trtt| s|V  qd S r8   )endswithr*   r   r   r:   )rd   r]   r   r#   r$     s    
zwalk.<locals>.<genexpr>)r0   )r0   r   c                 3  s.    | ]}|  stt|s|V  qd S r8   )r~   r   r   rl   exclude_prefixesrd   r]   r   r#   r$     s    
N)r=   walkr   r)   rT   )r   rd   rA   rb   r5   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 r8   r   )r   _root_subsr5   r   r   r#   r$         z#has_child_module.<locals>.<genexpr>)r(   r   )r   rd   rA   r   r   r#   has_child_module  s   r   tuple[list[Path], list[str]]c                 C  s  t | s|jr| tjd }nd}g }g }t| ||D ]\}}}	t d|	}
|
 o+|j}|
rE|	 D ]}t|rC|	| |		d| q2n|| krQ|jsQ|
  q|
sU|r|sbt|	dksbt||s|t| d tjtjd}|r{t|||r|t||||	|||||	 |t|| q|| ksJ |du sJ |	D ]!}tt| |||s|dd }|t|||| || qq||fS )z`
    Look for every file in the directory tree and create the corresponding
    ReST files.
    Nr      r0   )r?   r   rk   r   sepr   r   r,   r   insertclearr+   rg   lstripreplacer   rv   rx   rU   r3   rj   r   r\   )r   rd   rA   rN   root_package	toplevelswritten_filesr]   rb   r5   is_pkgrc   r;   
subpackagepy_filer   r   r   r#   recurse_tree   sj   

 

r   c                   s   t |  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    | ]}|  V  qd S r8   )matchr   excluderoot_strr   r#   r$   p  r   zis_excluded.<locals>.<genexpr>)r/   r(   )r]   rd   r   r   r#   r   i  s   r   argparse.ArgumentParserc               	   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dQddRtdSd |jdTdUddVtdWd | jdXdYddZtd[d | jd\d]dd^td_d | jd`daddbtdcd | jdddeddtdfd | jdgdhdditdjd | tdk}|jdldmdndotdpdq tD ]}|jdr| dsdt| dntdu| dv qA| tdw}|jdxdydzd{td|d} | 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version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storerC   Tzdirectory to place all output)r   r   requiredr   z-q
store_truerE   z,no output on stdout, just warnings on stderr)r   r   r   z-dz
--maxdepthrs      z;maximum depth of submodules to show in the TOC (default: 4))r   r   typedefaultr   z-fz--forcerH   zoverwrite existing filesz-lz--follow-linksr   FzNfollow symbolic links. Powerful when combined with collective.recipe.omelette.)r   r   r   r   z-nz	--dry-runrD   z%run the script without creating filesz-ez
--separaterr   z1put documentation for each module on its own pagez-Pz	--privaterT   zinclude "_private" modulesz	--tocfiletocfilery   z0filename of table of contents (default: modules)z-Tz--no-tocstore_falsez%don't create a table of contents filez-Ez--no-headingsrV   zedon't create headings for the module/package packages (e.g. when the docstrings already contain them)z-Mz--module-firstrq   z7put module documentation before submodule documentationz--implicit-namespacesr   zNinterpret module paths according to PEP-0420 implicit namespaces specificationz-sz--suffixr    rstzfile suffix (default: rst)z--remove-old
remove_oldzERemove existing files in the output directory that were not generatedz-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-projectr{   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   
extensionsrU   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--templatedirTEMPLATEDIRtemplatedirz%template directory for template files)r   r   r   )	argparseArgumentParserr   add_argumentr
   intadd_mutually_exclusive_groupadd_argument_groupr   )parserexclusive_groupgroupextr   r   r#   
get_parsers  s  	



r   c                   @  s   e Zd ZU dZded< ded< ded< ded< d	ed
< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< d S )!rB   z'Arguments parsed from the command line.r/   r   ra   r   rC   r   rE   r   rs   rH   r   rD   rr   rT   r   rV   rq   r   r    r   r   r.   r{   r   r   r   r6   r   r   r   N)__name__
__module____qualname____doc____annotations__r   r   r   r#   rB   Q  s4   
 argvSequence[str]r   c                C  s  t t jd tj   t }|| ptjdd }t	
|j}|jdu r/|t	jd |_|jd|_t| sHttd| td|jsPt|j tdd t|jD }t||||j\}}|j r d	d
l!m"} |#  d}d}	|D ]}
|
$|d rq{|
}|	d|
 7 }	q{i d|jddddd|jd|j%pdd|j&pdd|j'p|j&pddd|j dddddg dddddddd |j(d!|	d"d#||j)d$}|j*r|d +|j* |j,rd|d%< |d dd D ]}d&|v r|d -| |d +|d& q|js|j.|d|j/|jd' n|j0r0|1t2|||j0|j |j3ru|jsut|j4d(|j D ]0}||vrsz|5  W qD t6yr } ztj7td)||j8d*d+ W Y d}~qDd}~ww qDd	S ),z+Parse and check the command line arguments.rz   r   Nr   r0   z%s is not a directory.c                 s  s&    | ]}t tt|V  qd S r8   )recompilefnmatch	translater   abspathr   r   r   r#   r$     s
    
zmain.<locals>.<genexpr>r   )
quickstartz   %s
r   r   Fdotr   projectr   Authorr   r   r    masterindexepubTr   )zsphinx.ext.autodoczsphinx.ext.viewcodezsphinx.ext.todomakefile	batchfile	make_modemastertocmaxdepthmastertoctreelanguageen)r   r   rE   r   )silent	overwriter   z**/*.zFailed to remove %s: %sautodoc)r   )9locale	setlocaleLC_ALLsphinxinit_consoler   
parse_argssysr   r   r   r   r{   rk   r   r    removeprefixr   r   rF   errorr   
SystemExitrD   r   rC   tupledictfromkeysr   r   r   r   
sphinx.cmdr   r}   r~   r   r   r   rs   r   r   rv   rE   r   generaterH   r   rU   r   r   r   unlinkOSErrorwarningstrerror)r   r   argsr   rd   r   ry   qsr   r@   r   dr   existingexcr   r   r#   mainn  s   






	


r  __main__r   )r   r   r   r   )r-   r.   r   r/   )NN)r4   r.   r5   r6   r   r   )r'   r/   r@   r/   rA   rB   r   r   r8   )
rM   r.   r"   r/   rA   rB   rN   r.   r   r   )r   N)r]   r/   r^   r.   r_   r/   r`   ra   rA   rB   rb   ra   rc   r   rd   re   rN   r.   r   rf   )ry   N)
ry   ra   rA   rB   r'   r/   rN   r.   r   r   )r   )r4   r   rA   rB   rd   re   r   r   )r   r   rA   rB   r   re   r   r   )r   r/   rd   re   rA   rB   r   r   )r   r/   rd   re   rA   rB   r   r   )
r   r/   rd   re   rA   rB   rN   r.   r   r   )r]   r   rd   re   r   r   )r   r   )r   r   r   r   )Ar   
__future__r   r   r   r   r   r=   r   r   r   importlib.machineryr   r   pathlibr   typingr   r   r	   sphinx.localer   r
   r   sphinx.cmd.quickstartr   r   sphinx.utilr   sphinx.util.osutilr   r   sphinx.util.templater   collections.abcr   r   	getLoggerr   rF   environrk   rS   r   r*   r1   rW   r,   r3   r?   rL   r\   rx   r   rg   rj   r   r   r   r   r   rB   r  r   r   r   r   r   r#   <module>   sp    



	

"G



I

 _
b