
    Fe.N                       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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jD                  e#      Z$dejJ                  v rejJ                  d   jM                  d      Z'ng dZ'd e(e      z   Z) ejT                  edd      Z+d&dZ,d'dZ-d(d)dZ.d*dZ/	 d+	 	 	 d,dZ0	 	 d-	 	 	 	 	 	 	 	 	 	 	 	 	 d.dZ1	 	 d/	 	 	 d0dZ2	 d1	 	 	 d2dZ3d3dZ4	 	 d4dZ5d5dZ6	 d+	 	 	 d6d Z7d7d!Z8d8d"Z9d1d9d#Z:e#d$k(  r e; e:e	jx                  d%d             y):u  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)TYPE_CHECKINGAny)__display_version__package_dir)
EXTENSIONS)__)logging)FileAvoidWrite	ensuredir)ReSTRenderer)	GeneratorSequenceSPHINX_APIDOC_OPTIONS,)memberszundoc-memberszshow-inheritance)z.pyz.pyx	templatesapidocc                ~    t        j                  |       t        fdt        t        t
        d      D              S )z)Check *filename* is __init__ file or not.c              3  .   K   | ]  }d |z   k(    yw)__init__N ).0suffixbasenames     3/usr/lib/python3/dist-packages/sphinx/ext/apidoc.py	<genexpr>zis_initpy.<locals>.<genexpr>:   s#       	J''s   T)keyreverse)r   r   anysortedPY_SUFFIXESlen)filenamer   s    @r   	is_initpyr(   7   s4    }}X&H [c4@      c                 8    dj                  t        d|             S )zJoin module names with dots..N)joinfilter)modnamess    r   module_joinr/   @   s    88F4*++r)   c                ^    || y|t        j                  |       }t        d |D              S )z+Check given *files* contains __init__ file.Fc              3  8   K   | ]  }t        |      s|  y wNr(   r   fs     r   r    z is_packagedir.<locals>.<genexpr>L   s     0Q9Q<q0   )oslistdirr#   )dirnamefiless     r   is_packagedirr;   E   s2    }}

7#0%000r)   c                   t        |dd      }t        j                  |j                  |  d|j                         }|j
                  r"|st        j                  t        d      |       y|j                  s8t        j                  |      r#|s t        j                  t        d      |       yy|st        j                  t        d      |       t        |      5 }|j                  |       ddd       y# 1 sw Y   yxY w)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loggerinfor   forceisfiler   write)nametextoptsr=   fnamer5   s         r   
write_filerJ   O   s    D'4(EIIdlltfAdkk]$;<E{{KK23U;::$++e,KK>?G  KK./7E" 	aGGDM	 	 	s   C66C?c                   t        t              }|j                  rd|vr|j                  d       t	        | |      }|j
                   |||d}|	|t        g}nt        g}t        |      j                  d|      }t        |||       y).Build the text of the file and write the file.private-members)show_headingsr   qualnameautomodule_optionsNzmodule.rst_t)
r   OPTIONSincludeprivateappendr/   
noheadingstemplate_dirr   renderrJ   )	packager   rH   user_template_diroptionsrO   contexttemplate_pathrG   s	            r   create_module_filer\   b   s     7mG0?()7H-H!__,%	G $*L9%&--ngFDxt$r)   c	           
     .   |D 	cg c]0  }	t        t        j                  | |	      ||      st        |||	      2 }
}	|D cg c]B  }t	        t        j                  | |      ||      st        |      s|j                  d      d   D }}t        t        |            }|D cg c]  }t        |||       }}t        t              }|j                  rd|vr|j                  d       t        ||      }	|	|
|||j                  |j                  ||j                   |j                   d	}|	|t"        g}nt"        g}t%        |      j'                  d|      }t)        |	||       |r#|j                  r|D ]  }t+        d|||        yyyc c}	w c c}w c c}w )rL   r+   r   rM   )	pkgnamesubpackages
submodulesis_namespacemodulefirstseparatemodulesrP   rN   maxdepthNzpackage.rst_t)is_skipped_packager   r,   r/   is_skipped_moduler(   splitr$   setr   rQ   rR   rS   rb   rc   rT   rd   rU   r   rV   rJ   r\   )rootmaster_packagesubrootpy_filesrH   subsra   excludesrX   r^   r_   subr`   modnamerY   rZ   r[   rG   	submodules                      r   create_package_filerr   x   s    #'X,TYYtW-EtXV ~w@ XK X 08 &*499T3+?xPn ))C.# &J & J(J!+- ngw? -J -7mG0?().'2G" $''//%!__,MM
G $*L9%&--owGDwd#d**# 	IItY6GH	I +zCX&-s   5F AF!Fc                4   | j                          d}| dd D ]*  }|j                  |dz         r| j                  |       )|}, |j                  |j                  | d}|	|t
        g}nt
        g}t        |      j                  d|      }t        |||       y)zCreate the module's index. Nr+   )headerrd   docnamesz	toc.rst_t)	sort
startswithremoveru   rd   rU   r   rV   rJ   )	modulesrH   rF   rX   prev_modulemodulerZ   r[   rG   s	            r   create_modules_toc_filer}      s     LLNK!* ![3./NN6" K! ++MMG
 $*L9%&--k7CDtT4 r)   c                     t        j                         syt        j                  t        j                   d            }t	        d |D              }|s|j
                  syt         fd|D              S )%Check if we want to skip this module.Fz*.pyc              3  8   K   | ]  }t        |      s|  y wr2   r3   r4   s     r   r    z%is_skipped_package.<locals>.<genexpr>   s     ;il!;r6   Tc              3  ^   K   | ]$  }t        t        j                  |             & y wr2   )is_excludedr   r,   )r   r5   r9   rn   s     r   r    z%is_skipped_package.<locals>.<genexpr>   s#     K{499Wa0(;Ks   *-)r   isdirglobr,   r#   implicit_namespacesall)r9   rH   rn   r:   regular_packages   ` `  r   re   re      s^     ::gIIdii01E;U;;O4#;#; KUKKKr)   c                    t        j                  |       syt        j                  |       j                  d      r|j                  syy)r   T_F)r   existsr   rx   rR   )r'   rH   	_excludess      r   rf   rf      s6    ;;x }}X))#.t7J7Jr)   c              #     K   t        |dd      }t        |dd      }t        j                  | |      D ]B  \  }}t        fd|D              }|rdndt        fd|D              |d	d	 ||f D y	w)
z@Walk through the directory and list files and subdirectories up.followlinksFrR   )r   c              3     K   | ];  }|j                  t              r$t        t        j                  |            s| = y wr2   )endswithr%   r   r   r,   )r   r5   rn   ri   s     r   r    zwalk.<locals>.<genexpr>   s;      FQ**[1&tyyq'98D  Fs   AA)r+   )r+   r   c              3     K   | ]8  }|j                        rt        t        j                  |            s| : y wr2   )rx   r   r   r,   )r   ro   exclude_prefixesrn   ri   s     r   r    zwalk.<locals>.<genexpr>   s;      JCNNCS4T(4)=xH  Js
   A'AN)r>   r7   walkr$   )	rootpathrn   rH   r   rR   rm   r:   r   ri   s	    `     @@r   r   r      s      $u5KT#3U;NWWX;G  dE F% F F 06) J J JQ D%! s   A8A=c                <    t        d t        | ||      D              S )zACheck the given directory contains child module/s (at least one).c              3  (   K   | ]
  \  }}}|  y wr2   r   )r   _root_subsr:   s       r   r    z#has_child_module.<locals>.<genexpr>   s      E5% 	s   )r#   r   )r   rn   rH   s      r   has_child_moduler      s%     #'(D#A  r)   c                   t        |dd      }t        |       s|r#| j                  t        j                        d   }nd}g }t        | ||      D ]t  \  }}}	t        d|	      }
|
 xr |}|
r:|	dd D ]1  }t        |      s|	j                  |       |	j                  d|       3 n|| k7  r|s|dd= d|
s|r|st        |	      dkD  st        ||      r|t        |       d j                  t        j                        j                  t        j                  d      }|rt        |||      st        ||||	|||||	       |j                  t!        ||             || k(  sJ |J |	D ]W  }t#        t        j$                  | |      ||      r%|j                  d      d   }t'        ||||       |j                  |       Y w |S )z`
    Look for every file in the directory tree and create the corresponding
    ReST files.
    r   FNr      r+   )r>   r;   rg   r   sepr   r(   ry   insertr&   re   lstripreplacer   rr   rS   r/   rf   r,   r\   )r   rn   rH   rX   r   root_package	toplevelsri   rm   r:   is_pkgra   r5   
subpackagepy_filer|   s                   r   recurse_treer      s    "$(=uE X"5~~dhh/3 I!(Hd; "-dEtU+!z9&91X 'Q<LLOLLA&' X&G\s5zA~-?d-K!#h-.188BGDHHc*  $'7h'M'lJ(-tT<(9; $$[z%JK 8###'''  -(8W)EtXV$]]3/2F&|VTCTU$$V,	-="-H r)   c                ,     t         f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  @   K   | ]  }|j                          y wr2   )match)r   excluderi   s     r   r    zis_excluded.<locals>.<genexpr>8  s     ;ww}}T";s   )r#   )ri   rn   s   ` r   r   r   2  s     ;(;;;r)   c                 4   t        j                  dt        d      t        d            } | j                  ddddt        z  	       | j                  d
t        d             | j                  ddt        d             | j                  dddddt        d             | j                  dddt        d             | j                  ddddt
        d t        d!      "       | j                  d#d$dd%t        d&             | j                  d'd(dd)d*t        d+      ,       | j                  d-d.dd/t        d0             | j                  d1d2dd3t        d4             | j                  d5d6dd7t        d8             | j                  d9dd:d;t        d<      ,       | j                  d=d>d?d:t        d@             | j                  dAdBddCt        dD             | j                  dEdFddGt        dH             | j                  dIddJt        dK             | j                  dLdMddNdOt        dP      ,       | j                  dQdRddSt        dT             | j                  dUdVddWt        dX             | j                  dYdZdd[t        d\             | j                  d]d^dd_t        d`             | j                  dadbddt        dc             | j                  dddeddft        dg             | j                  t        dh            }|j                  didjdkdlt        dm      n       t        D ]*  }|j                  do|z  dpdq|z  dkt        dr      |z  s       , | j                  t        dt            }|j                  dudvdwdxt        dy      z       | 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storer?   Tzdirectory to place all output)r   r   requiredr   z-q
store_truer=   z,no output on stdout, just warnings on stderr)r   r   r   z-dz
--maxdepthrd      z;maximum depth of submodules to show in the TOC (default: 4))r   r   typedefaultr   z-fz--forcerC   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
--separaterc   z1put documentation for each module on its own pagez-Pz	--privaterR   zinclude "_private" modulesz	--tocfiletocfilerz   z0filename of table of contents (default: modules)z-Tz--no-tocstore_falsez%don't create a table of contents filez-Ez--no-headingsrT   zedon't create headings for the module/package packages (e.g. when the docstrings already contain them)z-Mz--module-firstrb   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-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-projectru   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   
extensionsrS   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_argument_groupr   )parsergroupexts      r   
get_parserr   ;  s   $$'NO J K	LF IN .1D D  F  <=  ?
) != >  ? nW9!% ?@  B \ NO  Q
l7 ! !/ 0  1 i7 :;  =
.|*E !C D  E k,X GH  J
l<. ST  V k,- <=  ? G)Y RS  U
jY GH  J
ol) !0 1  2
 .|* !0 1  2 /2 !D E  F jx % <=  ? h|& PQ  S
0- [\  ^ ogH JK  M
nW8 NO  Q
ogI LM  O
ogI !< =  > %%b)<&=>E	~|,&R0M-N  P A:+N!03!6\ "#8 9C ? 	 	AA
 %%b)=&>?E	t_m)FG  I Mr)   c                  t        j                  t         j                  d       t        j                   j	                          t               }|j                  | xs t        j                  dd       }t        j                  |j                        }|j                  '|j                  t        j                        d   |_        |j                  j!                  d      r|j                  dd |_        t        j"                  |      s*t$        j'                  t)        d      |       t+        d      |j,                  st/        |j0                         t3        d t4        j7                  |j8                        D              }t;        ||||j<                        }|j>                  rdd	l m!} |jE                          d}d}|D ]!  }	|	j!                  |dz         r|	}|d
|	z  z  }# i d|j0                  ddddd|j                  d|jF                  xs dd|jH                  xs dd|jJ                  xs |jH                  xs ddd|j                  z   dddddg dddddddd|jL                  d |d!d"||jN                  d#}
|jP                  r|
d   jS                  |jP                         |jT                  rd|
d$<   |
d   dd D ]>  }d%|v s|
d   jW                  |       |
d   jS                  |j                  d%             @ |j,                  s)|jY                  |
d|jZ                  |j<                  &       y|j\                  r"t_        |||j\                  |j<                         y)'z+Parse and check the command line arguments.rt   r   Nr   r+   z%s is not a directory.c              3     K   | ]?  }t        j                  t        j                  t	        j
                  |                   A y wr2   )recompilefnmatch	translater   abspath)r   r   s     r   r    zmain.<locals>.<genexpr>  s5       	

7$$T\\'%:;<s   AA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   r=   r   )silent	overwriter   )0locale	setlocaleLC_ALLsphinxinit_consoler   
parse_argssysargvr   r   r   ru   rg   r   r   rx   r   rA   errorr   
SystemExitr@   r   r?   tupledictfromkeysr   r   r   r   
sphinx.cmdr   rw   r   r   r   rd   r   r   extendr=   ry   generaterC   r   r}   )r   r   argsr   rn   rz   qsr{   rG   r|   dr   s               r   mainr     sg   
V]]B'
MM \FT1SXXab\2D||D,,-H {{nnTXX.r2{{c"kk!"o::hR018<m;;$,, }}T%9%9: H 8XtT5E5EFGyy/ 	'F  s!23 KI&&D		'

DLL
5
 3
 t{{	

 dkk-X
 t||)r
 t||9t||9r
 cDKK'
 g
 D
  .
 
 
 
   !
" T#
$ %
& $"11)
, ??lO""4??3::AgJ\?1% 	7Ccz,&&s+,&&syy~6	7
 {{KK$$**$($4$4  6
  
t||T=M=MNr)   __main__r   )r'   strreturnbool)r.   
str | Noner   r   )NN)r9   r  r:   zlist[str] | Noner   r  )rF   r   rG   r   rH   r   r   Noner2   )
rW   r  r   r   rH   r   rX   r  r   r  )r   N)ri   r   rj   r  rk   r   rl   	list[str]rH   r   rm   r  ra   r  rn   Sequence[re.Pattern[str]]rX   r  r   r  )rz   N)
rz   r  rH   r   rF   r   rX   r  r   r  )r   )r9   r   rH   r   rn   r  r   r  )r'   r   rH   r   r   r  r   r  )r   r   rn   r  rH   r   r   z7Generator[tuple[str, list[str], list[str]], None, None])r   r   rn   r  rH   r   r   r  )
r   r   rn   r  rH   r   rX   r  r   r  )ri   r   rn   r  r   r  )r   zargparse.ArgumentParser)r   zSequence[str]r   r   )=__doc__
__future__r   r   r   r   r   r7   r   r   r   importlib.machineryr   r   typingr   r   sphinx.localer   r	   r
   sphinx.cmd.quickstartr   r   sphinx.utilr   sphinx.util.osutilr   r   sphinx.util.templater   collections.abcr   r   	getLogger__name__rA   environrg   rQ   r   r%   r,   rU   r(   r/   r;   rJ   r\   rr   r}   re   rf   r   r   r   r   r   r   r   r   r   r)   r   <module>r     s  	 #     	 	 
  2  %  3 ,   8 -3			8	$ bjj(jj0177<GG &8 99tyyk8<,
1( 8<%*4%@D%2 ?A8<	+I"++I!+I)2+IBF+I #<+I ,6	+I
 "+I\ HQ<@!/9!EI!4 >@L!:LDHL  E 2 264$.4:C4n<^BK^ z
T#((12,'
(( r)   