U
    \Sh                     @  sz   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ e	rhdd	lmZ G d
d dZdS )z,Utilities parsing and analyzing Python code.    )annotationsN)import_module)path)TYPE_CHECKINGAny)PycodeError)Parser)	Signaturec                   @  s   e Zd ZU ded< ded< ded< ded< d	ed
< ded< i Zded< edddddZed)dddd dddZeddd dddZ	edd dddZ
ddddddd Zdd!d"d#Zdd!d$d%Zdd!d&d'Zd(S )*ModuleAnalyzerzdict[tuple[str, str], str]r   z dict[tuple[str, str], list[str]]	attr_docsz	list[str]finalszdict[str, list[Signature]]	overloadszdict[str, int]tagorderzdict[str, tuple[str, int, int]]tagszdict[tuple[str, str], Any]cachestrztuple[str | None, str | None])modnamereturnc              
   C  s  zt | }W n2 tk
r> } ztd|  ||W 5 d}~X Y nX t|dd}t|dd}|rt|ddrz|| }|r||fW S W n tk
r   Y nX |dkr|rt|ddrz|| }W n2 tk
r } ztd|  ||W 5 d}~X Y nX |dkr
td|  tt	|}|
 d	r\|dd
 }t|sxt|d rx|d7 }n|
 dsxtd| t|std| |dfS )zTry to find the source code for a module.

        Returns ('filename', 'source'). One of it can be None if
        no filename or source found
        zerror importing %rN
__loader____file__
get_sourceget_filenamezerror getting filename for %rzno source found for module %r)z.pyoz.pycw)z.pyz.pywzsource is not a .py file: %rzsource file is not present: %r)r   	Exceptionr   getattrr   ImportErrorr   r   normpathabspathlowerendswithisfile)r   moderrloaderfilenamesource r'   I/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/pycode/__init__.pyget_module_source   s<    "
"

z ModuleAnalyzer.get_module_source<string>)stringr   srcnamer   c                 C  s   | |||S )Nr'   )clsr+   r   r,   r'   r'   r(   
for_stringG   s    zModuleAnalyzer.for_string)r%   r   r   c              
   C  s   d|f| j kr| j d|f S z<t|}| }W 5 Q R X | |||}|| j d|f< W n2 tk
r } ztd| ||W 5 d }~X Y nX |S )Nfilezerror opening %r)r   tokenizeopenreadr   r   )r-   r%   r   fr+   objr#   r'   r'   r(   for_fileL   s    "zModuleAnalyzer.for_filec              
   C  s   d|f| j kr.| j d|f }t|tr*||S zB| |\}}|d k	rZ| |||pTd}n|d k	rn| ||}W n2 tk
r } z|| j d|f<  W 5 d }~X Y nX || j d|f< |S )Nmoduler*   )r   
isinstancer   r)   r.   r5   )r-   r   entryr%   r&   r4   r#   r'   r'   r(   
for_moduleY   s     
zModuleAnalyzer.for_moduleNone)r&   r   r,   r   c                 C  s   || _ || _|| _d| _d S )NF)r   r,   code	_analyzed)selfr&   r   r,   r'   r'   r(   __init__m   s    zModuleAnalyzer.__init__)r   c              
   C  s   | j r
dS zt| j}|  i | _|j D ].\}}|rP| dg | j|< q.dg| j|< q.|j| _|j	| _	|j
| _
|j| _|j| _d| _ W n: tk
r } ztd| jd||W 5 d}~X Y nX dS )zAnalyze the source code.N Tzparsing z	 failed: )r<   r   r;   parser   commentsitems
splitlinesr   r   r   Zdefinitionsr   Z	defordersr   r   r   r,   )r=   parserscopecommentexcr'   r'   r(   analyzev   s$    

zModuleAnalyzer.analyzec                 C  s   |    | jS )z?Find class and module-level attributes and their documentation.)rH   r   r=   r'   r'   r(   find_attr_docs   s    zModuleAnalyzer.find_attr_docsc                 C  s   |    | jS )z?Find class, function and method definitions and their location.)rH   r   rI   r'   r'   r(   	find_tags   s    zModuleAnalyzer.find_tagsN)r*   )__name__
__module____qualname____annotations__r   staticmethodr)   classmethodr.   r5   r9   r>   rH   rJ   rK   r'   r'   r'   r(   r
      s&   
*	r
   )__doc__
__future__r   r0   	importlibr   osr   typingr   r   Zsphinx.errorsr   Zsphinx.pycode.parserr   inspectr	   r
   r'   r'   r'   r(   <module>   s   