U
    \Shdf                     @  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
Z
ddlmZ ddlmZmZmZ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  ddl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z' ddlm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8 ernddl9m:Z: e/;e<Z=G dd dZ>G dd deZ?dddddZ@dOd!d!d!d"d#d$ZAG d%d& d&ZBG d'd( d(ZCdd)d*d+d,d-ZDdPd!ddd&d!d.dd.d/d0d0d!d1d2d3ZEdQd8d0d!d0d.dd.d!dd9	d:d;ZFd8d<d=d>d?ZGdRd!d0d<d@dAdBZHdSd8d0d0d<dCdDdEZIdFdGdHdIZJe
jKdJd fd8ddKdLdMZLe<dNkreL  dS )Tax  Generates reST source files for autosummary.

Usable as a library or script to generate automatic RST source files for
items referred to in autosummary:: directives.

Each generated RST file contains a single auto*:: directive which
extracts the docstring of the referred item.

Example Makefile rule::

   generate:
           sphinx-autogen -o source/generated source/*.rst
    )annotationsN)path)TYPE_CHECKINGAny
NamedTupleSequence)TemplateNotFound)SandboxedEnvironment)__display_version__package_dir)Sphinx)Builder)Config)
Documenter)import_module)ImportExceptionGroupget_documenterimport_by_nameimport_ivar_by_name)__)ModuleAnalyzerPycodeError)SphinxComponentRegistry)loggingrstsplit_full_qualified_name)getallsafe_getattr)	ensuredir)SphinxTemplateLoader)NullTranslationsc                   @  s0   e Zd ZdZdddddZdddd	d
ZdS )DummyApplicationz3Dummy Application class for sphinx-autogen command.r    None)
translatorreturnc                 C  sx   t  | _t | _g | _d| _|| _d| _d| _d| _	| j
di dd  | j
di dd  | j
dddt | j  d S )	N/r   Fautosummary_contextTautosummary_filename_mapautosummary_ignore_module_allenv)r   configr   registryZ
messagelogsrcdirr#   	verbosityZ
_warncountZwarningiserroraddboolZinit_values)selfr#    r1   R/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/ext/autosummary/generate.py__init__=   s    zDummyApplication.__init__r   )argsr$   c                 G  s   d S Nr1   )r0   r4   r1   r1   r2   emit_firstresultL   s    z!DummyApplication.emit_firstresultN)__name__
__module____qualname____doc__r3   r6   r1   r1   r1   r2   r!   :   s   r!   c                   @  s.   e Zd ZU ded< ded< ded< ded< dS )AutosummaryEntrystrnamer   templater/   	recursiveN)r7   r8   r9   __annotations__r1   r1   r1   r2   r;   P   s   
r;   r   r"   appr$   c              	   C  s`   ddl m}m}m}m}m}m}m}m}m	}	 |||||||||	g	}
|
D ]}| j
|j| qFd S )Nr   )	AttributeDocumenterClassDocumenterDataDocumenterDecoratorDocumenterExceptionDocumenterFunctionDocumenterMethodDocumenterModuleDocumenterPropertyDocumenter)sphinx.ext.autodocrC   rD   rE   rF   rG   rH   rI   rJ   rK   r+   Zadd_documenterobjtype)rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   Zdocumenters
documenterr1   r1   r2   setup_documentersW   s    ,      rO   =r<   )titleliner$   c                 C  s$   d| krt d| d |t|   S )N
zCan only underline single lines)
ValueErrorlen)rQ   rR   r1   r1   r2   
_underlinel   s    rV   c                   @  s2   e Zd ZdZdddddZdddd	d
dZdS )AutosummaryRendererzA helper class for rendering.r   r"   rA   c                 C  s   t |trtdtjtdddg}t|j|j	j
|}t|d| _tj| jjd< tj| jjd< t| jjd< |jr| jd	 | j|j d S )
Nz%Expected a Sphinx application object!extautosummary	templates)loaderescapee	underlinezjinja2.ext.i18n)
isinstancer   rT   osr   joinr   r   r,   r*   templates_pathr	   r)   r   r\   filtersrV   r#   add_extensionZinstall_gettext_translations)r0   rB   Zsystem_templates_pathr[   r1   r1   r2   r3   u   s    
zAutosummaryRenderer.__init__r<   dict)template_namecontextr$   c                 C  sf   z| j |}W nJ tk
rZ   z| j d| }W n  tk
rT   | j d}Y nX Y nX ||S )zRender a template file.zautosummary/%s.rstzautosummary/base.rst)r)   Zget_templater   render)r0   rf   rg   r>   r1   r1   r2   rh      s    zAutosummaryRenderer.renderN)r7   r8   r9   r:   r3   rh   r1   r1   r1   r2   rW   r   s   rW   c                   @  sT   e Zd ZddddddZdddddd	Zdddd
dddZd
ddddZdS )ModuleScannerr   r"   )rB   objr$   c                 C  s   || _ || _d S r5   )rB   object)r0   rB   rj   r1   r1   r2   r3      s    zModuleScanner.__init__r<   )r=   valuer$   c                 C  s   t | j|| jjS r5   )r   rB   rk   rM   )r0   r=   rl   r1   r1   r2   get_object_type   s    zModuleScanner.get_object_typer/   )r=   rl   rM   r$   c              
   C  s\   z| j d|||di W S  tk
rV } z tjtd||dd W Y dS d }~X Y nX d S Nzautodoc-skip-memberFz\autosummary: failed to determine %r to be documented, the following exception was raised:
%srY   )type)rB   r6   	Exceptionloggerwarningr   )r0   r=   rl   rM   excr1   r1   r2   
is_skipped   s    
   
  zModuleScanner.is_skipped	list[str])imported_membersr$   c           
   	   C  s,  g }zt | jj}| }W n tk
r6   i }Y nX t| j| jjD ]}zt	| j|}W n t
k
rt   d }Y nX | ||}| |||rqHzBd|f|krd}n,t|rd}nt	|d| jjkrd}nd}W n t
k
r   d}Y nX | jjj }	|s|dks|	rHdt| jkrH|| qH|S )N FTr8   __all__)r   
for_modulerk   r7   find_attr_docsr   
members_ofrB   r*   r   AttributeErrorrm   rt   inspectismoduler(   dirappend)
r0   rv   membersanalyzer	attr_docsr=   rl   rM   importedrespect_module_allr1   r1   r2   scan   sF    



zModuleScanner.scanN)r7   r8   r9   r3   rm   rt   r   r1   r1   r1   r2   ri      s   
ri   r   Sequence[str])rj   confr$   c                 C  s"   |j rt| S t| pt| S dS )zGet the members of ``obj``, possibly ignoring the ``__all__`` module attribute

    Follows the ``conf.autosummary_ignore_module_all`` setting.N)r(   r   r   )rj   r   r1   r1   r2   r{      s    r{   r/   re   z
str | None)r=   rj   parentr>   rf   rv   rB   r?   rg   modnamequalnamer$   c                   sp  t  ||ddddd fdddddfd	d
ddd fdddddfddg dfdddddd fdd}dddfdd}dGdddddfdd}i }|| jdkrDt |}|||d<  jj }|pd t|ko|}||d!h|d"\|d#< |d$< ||d%h|d"\|d&< |d'< ||d(h|d"\|d)< |d*< ||d \|d+< |d,< t|d-}|r|r|d$ |d'  |d*  |d,  }|r
d t|kr
||dhdd"\}}||7 }fd.d/|D }fd0d/|D }t	|}ng g  }}d }||||d1\}}|| |d2< || |d3< nljd%krt||d< t
t|t
|j  |d4< ||d5hd6g\|d7< |d8< ||d9d:h\|d+< |d,< |	d ksĈd krt\}	jd;krd<d=d> |d%< jd?kr}nd<d=d@ }|dA< |	|d< |dB< ||dC< j|dD< tdE |dF< |r^|||S |j|S d S )HNr   r<   r/   )rj   r=   rM   r$   c              
     sZ   z  d||| di W S  tk
rT } z tjtd||dd W Y dS d }~X Y nX d S rn   )r6   rp   rq   rr   r   )rj   r=   rM   rs   rB   r1   r2   skip_member   s    
  
  z1generate_autosummary_content.<locals>.skip_memberzdict[str, Any])rj   r$   c                   s&   t jj|  gt}dd | D S )Nc                 S  s   i | ]\}}||j qS r1   )rk   ).0r=   memberr1   r1   r2   
<dictcomp>   s      zKgenerate_autosummary_content.<locals>.get_class_members.<locals>.<dictcomp>)sphinxrX   Zautodocget_class_membersr   items)rj   r   )r   r1   r2   r      s    z7generate_autosummary_content.<locals>.get_class_membersc              	     sF   i }t |  jD ]0}zt| |||< W q tk
r>   Y qY qX q|S r5   )r{   r*   r   r|   )rj   r   r=   r   r1   r2   get_module_members   s    z8generate_autosummary_content.<locals>.get_module_membersc                   s(    j dkr| S  j dkr$| S i S )Nmoduleclass)rM   )rj   )docr   r   r1   r2   get_all_members   s
    

z5generate_autosummary_content.<locals>.get_all_membersTzset[str]ru   ztuple[list[str], list[str]])rj   typesinclude_publicr   r$   c                   s   g }g }| }|  D ]\}}t || }	|	j|kr|sLt|dd | jkr|||	j}
|
dkrdq|
dkr|| || q|| ||ks|ds|| q||fS )Nr8   TF_)r   r   rM   getattrr7   r   
startswith)rj   r   r   r   r   publicZall_membersr=   rl   rN   Zskipped)rB   r   r   r1   r2   get_members  s"    


z1generate_autosummary_content.<locals>.get_members)r   r$   c                   sz   g g  }}zRt  }| }|D ]6\}}|dkr"|| kr"|| |ds"|| q"W n tk
rp   Y nX ||fS )z'Find module attributes with docstrings.rw   r   )r   ry   rz   r   r   r   )r   attrsr   r   r   	namespace	attr_namer=   r1   r2   get_module_attrs   s    



z6generate_autosummary_content.<locals>.get_module_attrsr   zSequence[str] | None)rj   skippublic_membersr$   c           
   	     s   g }g }t | jD ]\}}}||kr(q d | }zt|}	|	rPt|	drPW qW n tk
rf   Y nX || |d k	r||kr|| q|ds|| q||fS )N.Z__sphinx_mock__r   )pkgutiliter_modules__path__r   hasattrImportErrorr   r   )
rj   r   r   r   r   r   r   Z_ispkgfullnamer   r   r1   r2   get_modules/  s&    

z1generate_autosummary_content.<locals>.get_modulesr   r   rx   function)r   Z	functionsZall_functionsr   classesZall_classes	exception
exceptionsZall_exceptions
attributesZall_attributesr   c                   s   g | ]} d  | qS r   r1   r   r   r   r1   r2   
<listcomp>r  s     z0generate_autosummary_content.<locals>.<listcomp>c                   s   g | ]} d  | qS r   r1   r   r   r1   r2   r   t  s     )r   r   modulesall_modulesZinherited_membersmethodr3   methodsZall_methods	attributeproperty)r   r   r   r      r   )r   r   Zobjnamer=   rM   rP   r^   )N)r   updaterM   ri   r   r*   r(   r   r   r   set__dict__keysr   rsplitrU   rh   )r=   rj   r   r>   rf   rv   rB   r?   rg   r   r   r   r   r   nsscannerr   	ispackager   Zimported_modulesZall_imported_modulesr   r   r   	shortnamer1   )rB   r   r   r   r   r=   r   r   r2   generate_autosummary_content   s    
	  







r   .rstFTutf-8ru   )	sources
output_dirsuffix	base_pathrv   rB   	overwriteencodingr$   c                   s  t | }t|dkr2|d d dg |dd   }ttdd|  |r`ttd|   d k	rz fdd	| D } t|}	t| }
g }|r|jj	}ni }t t
|
td
D ]}|jd krq|ptj|j}t| z&t|j\}}}}||d d}W n tk
r } zz&t|j\}}}}||d d}W n tk
r } zb|jrh|j|jg }n|j|g }tdd |D }ttd|jd| W Y W Y qW 5 d }~X Y nX W 5 d }~X Y nX i }|r||jj t||||	|j|||j|||}tj||||| }tj |rt!||d}|" }W 5 Q R X ||kr^q|rt!|d|d}|#| W 5 Q R X |$| qt!|d|d}|#| W 5 Q R X |$| q|rt%||| |||d d S )N   
   z...iz,[autosummary] generating autosummary for: %sz, z[autosummary] writing to %sc                   s   g | ]}t j |qS r1   )r`   r   ra   )r   filenamer   r1   r2   r     s     z-generate_autosummary_docs.<locals>.<listcomp>)keyr   rw   c                 S  s"   h | ]}d t |j d| qS z* z: ro   r7   r   r]   r1   r1   r2   	<setcomp>  s     z,generate_autosummary_docs.<locals>.<setcomp>z5[autosummary] failed to import %s.
Possible hints:
%srS   )r   w)r   r   r   rv   rB   r   )&sortedrU   rq   infor   ra   rW   find_autosummary_in_filesr*   r'   r   r<   r   r`   abspathr   r   r=   replacer   r   r   	__cause__r   listrr   r   r&   r   r>   r?   getisfileopenreadwriter   generate_autosummary_docs)r   r   r   r   rv   rB   r   r   Zshowed_sourcesr>   r   Z	new_filesZfilename_mapentryr   r=   rj   r   r   r   rs   Zexc2r   errorsrg   contentr   fZold_contentr1   r   r2   r     s    



 2    
  r   zlist[AutosummaryEntry])	filenamesr$   c              
   C  sJ   g }| D ]<}t |ddd$}|  }|t||d W 5 Q R X q|S )z^Find out what items are documented in source/*.rst.

    See `find_autosummary_in_lines`.
    r   ignore)r   r   r   )r   r   
splitlinesextendfind_autosummary_in_lines)r   
documentedr   r   linesr1   r1   r2   r     s    r   )r=   r   r$   c           	   
   C  s   z.t | \}}}}t| }t|| |dW S  tk
rB   Y nj tk
r } z.ddd |jD }t	d|  d|  W 5 d}~X Y n  t
k
r   t	d|   Y nX g S )	znFind out what items are documented in the given object's docstring.

    See `find_autosummary_in_lines`.
    )r   r   rS   c                 S  s"   h | ]}d t |j d| qS r   r   r   r1   r1   r2   r     s     z0find_autosummary_in_docstring.<locals>.<setcomp>zFailed to import z.
Possible hints:
Nz_Failed to import '%s'; the module executes module level statement and it might call sys.exit().)r   pydocgetdocr   r   r|   r   ra   r   print
SystemExit)	r=   r   Z	real_namerj   r   r   r   rs   r   r1   r1   r2   find_autosummary_in_docstring  s    $r   )r   r   r   r$   c                 C  s  t d}t d}t d}t d}t d}t d}t d}	g }
d}d	}d	}|}d}d
}| D ]}|rv||}|rd}qf||}|r|d}|rftjtj||}qf|	|}|r|d }qf| 	drqf||}|rX|d }|	dr|dd	 }|rB|	|d sB| d| }|

t|||| qf| rf|	|d rrqfd}||}|rd}|d}d}d	}d	}qf||}|r|d }|
t||d qf||}|rf|d}qfqf|
S )a  Find out what items appear in autosummary:: directives in the
    given lines.

    Returns a list of (name, toctree, template) where *name* is a name
    of an object and *toctree* the :toctree: path of the corresponding
    autosummary directive (relative to the root of the file name), and
    *template* the value of the :template: option. *toctree* and
    *template* ``None`` if the directive does not have the
    corresponding options set.
    z^(\s*)\.\.\s+autosummary::\s*z.^\s*\.\.\s+automodule::\s*([A-Za-z0-9_.]+)\s*$z4^\s*\.\.\s+(current)?module::\s*([a-zA-Z0-9_.]+)\s*$z%^\s+(~?[_a-zA-Z][a-zA-Z0-9_.]*)\s*.*?z^\s+:recursive:\s*$z^\s+:toctree:\s*(.*?)\s*$z^\s+:template:\s*(.*?)\s*$FNrw   Tr   :~r    r      )recompilematchgroupr`   r   ra   dirnamestripr   r   r;   searchr   r   )r   r   r   Zautosummary_reZautomodule_reZ	module_reZautosummary_item_reZrecursive_arg_reZtoctree_arg_reZtemplate_arg_rer   r?   Ztoctreer>   Zcurrent_moduleZin_autosummarybase_indentrR   mr=   r1   r1   r2   r     s    













 

r   zargparse.ArgumentParser)r$   c                  C  s   t jdtdtdd} | jddddt d	 | jd
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d tdd | jddd d!d"td#d | jd$d%d d&d"td'd | S )(Nz#%(prog)s [OPTIONS] <SOURCE_FILE>...z:For more information, visit <https://www.sphinx-doc.org/>.a  
Generate ReStructuredText using autosummary directives.

sphinx-autogen is a frontend to sphinx.ext.autosummary.generate. It generates
the reStructuredText files from the autosummary directives contained in the
given input files.

The format of the autosummary directive is documented in the
``sphinx.ext.autosummary`` Python module and can be read using::

  pydoc sphinx.ext.autosummary
)usageepilogdescriptionz	--versionversionZshow_versionz%%(prog)s %s)actiondestr	  source_file+z&source files to generate rST files for)nargshelpz-oz--output-dirstorer   z directory to place all output in)r
  r  r  z-sz--suffixr   r   z/default suffix for files (default: %(default)s))r
  r  defaultr  z-tz--templatesrZ   z0custom template directory (default: %(default)s)z-iz--imported-members
store_truerv   Fz0document imported members (default: %(default)s)z-az--respect-module-allr   zPdocument exactly the members in module __all__ attribute. (default: %(default)s))argparseArgumentParserr   add_argumentr
   )parserr1   r1   r2   
get_parser~  sD    


 
 r  r   )argvr$   c                 C  s   t t jd tj   ttj  }t|t	j
t	j t| t | }|jrh|jjt|j |j |j_t|j|jd|j |j|d d S )Nrw   r   )rv   rB   )locale	setlocaleLC_ALLr   Zinit_consoler!   Zget_translatorr   setupsysstdoutstderrrO   r  
parse_argsrZ   r*   rb   r   r   r   r   r(   r   r  r   r   rv   )r  rB   r4   r1   r1   r2   main  s    

r!  __main__)rP   )NN)Nr   NFNTr   )N)NN)Mr:   
__future__r   r  r}   r  r`   r   r   r   r  r   typingr   r   r   r   Zjinja2r   Zjinja2.sandboxr	   Zsphinx.localer   r
   r   Zsphinx.applicationr   Zsphinx.buildersr   Zsphinx.configr   rL   r   Zsphinx.ext.autodoc.importerr   Zsphinx.ext.autosummaryr   r   r   r   r   Zsphinx.pycoder   r   Zsphinx.registryr   Zsphinx.utilr   r   r   Zsphinx.util.inspectr   r   Zsphinx.util.osutilr   Zsphinx.util.templater   gettextr    	getLoggerr7   rq   r!   r;   rO   rV   rW   ri   r{   r   r   r   r   r   r  r  r!  r1   r1   r1   r2   <module>   sx   
&>  & ?           \    `.
