o
    NiR                     @  sX  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	m
Z
 ddlmZ ddlmZmZmZm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 ertddlmZmZm Z m!Z! ddl"m#Z# ddl$m%Z% e&e'Z(dAdBddZ)dCddZ*dDd!d"Z+dEd&d'Z,dFd+d,Z-dGd1d2Z.dHd8d9Z/G d:d; d;eZ0dId?d@Z1dS )JzCheck Python modules and C API for coverage.

Mostly written by Josip Dzolonga for the Google Highly Open Participation
contest.
    )annotationsN)import_module)path)IOTYPE_CHECKINGAnyTextIO)Builder)__)logging)red)safe_getattr)IterableIteratorSequenceSet)Sphinx)ExtensionMetadata-fIO[str]textstrcharreturnNonec                 C  s(   |  |d  |  |t| d  d S )N
z

)writelen)r   r   r    r   G/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/ext/coverage.pywrite_header$   s   r!   nameexpslist[re.Pattern[str]]c              	   C  sJ   g }|D ]}z
| t| W q ty"   ttd||  Y qw |S )Nzinvalid regex %r in %s)appendrecompile	Exceptionloggerwarningr
   )r"   r#   lstexpr   r   r    compile_regex_list)   s   r-   tablelist[list[str]]Iterator[str]c                 #  sj     fddt t d D }t|dV  t| d dE d H   dd  D ]}t||dE d H  q'd S )Nc                   s&   g | ] t  fd dD d qS )c                 3  s    | ]	}t |  V  qd S N)r   .0xcolumnr   r    	<genexpr>4   s    z*_write_table.<locals>.<listcomp>.<genexpr>   )max)r3   r.   r5   r    
<listcomp>4   s   & z _write_table.<locals>.<listcomp>r   r   =r8   )ranger   	_add_line_add_row)r.   sizesrowr   r:   r    _write_table3   s   rB   r@   	list[int]	separatorc                   s   dd  fdd| D  S )N+ c                 3  s     | ]} |d   d V  qdS )r8   rE   Nr   )r3   sizerD   r   r    r7   >   s    z_add_line.<locals>.<genexpr>)join)r@   rD   r   rH   r    r>   =   s   r>   
col_widthscolumns	list[str]c                 #  s4    d  fddt|D d V  t |V  d S )NrF   c                 3  s*    | ]\}}d |d |  V  qdS )z| z <Nr   )r3   ir6   rJ   r   r    r7   B   s   ( z_add_row.<locals>.<genexpr>|)rI   	enumerater>   )rJ   rK   rD   r   rN   r    r?   A   s   "r?   mod_nameignored_module_expsIterable[re.Pattern[str]]Set[str]c                   s   t  fdd|D rt S t } h}|jdu r|S |jj}t|D ]/\}}dkr/q%|r>|t  d |O }q%t fdd|D rJq%|  d  q%|S )am  Recursively load all submodules.

    :param mod_name: The name of a module to load submodules for.
    :param ignored_module_exps: A list of regexes for modules to ignore.
    :returns: A set of modules names including the provided module name,
        ``mod_name``
    :raises ImportError: If the module indicated by ``mod_name`` could not be
        loaded.
    c                 3      | ]}|  V  qd S r1   matchr3   r,   )rQ   r   r    r7   P       z _load_modules.<locals>.<genexpr>N__main__.c                 3  rU   r1   rV   rX   )sub_mod_namer   r    r7   a   rY   )	anysetr   __spec__submodule_search_locationspkgutiliter_modules_load_modulesadd)rQ   rR   modmodulessearch_locations_sub_mod_ispkgr   )rQ   r\   r    rc   F   s    

rc   coverage_modulesSequence[str]seen_modulespy_undocdict[str, dict[str, Any]]c           	      C  s   | st |S t }| D ],}z	|t||O }W q ty7 } zttd|| d|i||< W Y d}~qd}~ww ||  }rIttdd| ||  }rZttdd| t |S )a3  Return a sorted list of modules to check for coverage.

    Figure out which of the two operating modes to use:

    - If 'coverage_modules' is not specified, we check coverage for all modules
      seen in the documentation tree. Any objects found in these modules that are
      not documented will be noted. This will therefore only identify missing
      objects, but it requires no additional configuration.

    - If 'coverage_modules' is specified, we check coverage for all modules
      specified in this configuration value. Any objects found in these modules
      that are not documented will be noted. In addition, any objects from other
      modules that are documented will be noted. This will therefore identify both
      missing modules and missing objects, but it requires manual configuration.
    #module %s could not be imported: %serrorNzSthe following modules are documented but were not specified in coverage_modules: %sz, zOthe following modules are specified in coverage_modules but were not documented)sortedr^   rc   ImportErrorr)   r*   r
   rI   )	rj   rl   rR   rm   rf   rQ   erradditional_modulesmissing_modulesr   r   r    _determine_py_coverage_modulesh   s.   rv   c                   @  s   e Zd ZdZdZedej d Zd#ddZ	d$d
dZ
d%ddZd#ddZd#ddZd&ddZd#ddZd'ddZd#ddZd#d d!Zd"S )(CoverageBuilderz:
    Evaluates coverage of code in the documentation.
    coveragezNTesting of coverage in the sources finished, look at the results in %(outdir)szpython.txt.r   r   c              	   C  s   g | _ | jjD ]}t| j|}| j t| qg | _| jj	
 D ]"\}}z| j|t|f W q# tyE   ttd| Y q#w i | _| jj
 D ]\}}td|| j|< qOtd| jj| _td| jj| _td| jj| _td| jj| _d S )Nz&invalid regex %r in coverage_c_regexescoverage_ignore_c_itemscoverage_ignore_modulescoverage_ignore_classescoverage_ignore_functionscoverage_ignore_pyobjects)c_sourcefilesconfigcoverage_c_pathr   rI   srcdirextendglob	c_regexescoverage_c_regexesitemsr%   r&   r'   r(   r)   r*   r
   c_ignorexpsry   r-   rz   mod_ignorexpsr{   cls_ignorexpsr|   fun_ignorexpsr}   py_ignorexps)selfpatternr"   r,   r#   r   r   r    init   s8   
zCoverageBuilder.initr   c                 C  s   dS )Nzcoverage overviewr   )r   r   r   r    get_outdated_docs   s   z!CoverageBuilder.get_outdated_docs	_docnamesrT   c                 C  s<   i | _ i | _i | _|   |   i | _|   |   d S r1   )rm   py_undocumentedpy_documentedbuild_py_coveragewrite_py_coveragec_undocbuild_c_coveragewrite_c_coverage)r   r   r   r   r    write_documents   s   zCoverageBuilder.write_documentsc              	   C  s  i }| j jj D ]
}|d ||d < q	| jD ]g}t }t|ddM}|D ]B}| jD ]<\}}||}	|	rf|		 d }
||vrG|
||
f q*|
|| vre| j|g D ]	}||
r] nqT|
||
f q*q*q%W d    n1 srw   Y  |r~|| j|< qd S )Nr8      utf-8encodingr   )envdomainsc_domainget_objectsr~   r^   openr   rW   groupsrd   r   getr   )r   	c_objectsobjfilenameundocr   linekeyregexrW   r"   r,   r   r   r    r      s:   



z CoverageBuilder.build_c_coveragec              
   C  s  t | jd}t|dddj}| jjrt|dd |d | j	 D ]L\}}t|| t
|D ]9\}}|d||f  | jjri| jjrPttd	||| q0ttd
d d d|d|    td |  q0|d q#W d    d S 1 s{w   Y  d S )Nzc.txtwr   r   zUndocumented C API elementsr<   r   z * %-50s [%9s]
z&undocumented c api: %s [%s] in file %sundocumented  zc   z
api       %-30sz [%9s]z - in file )r   rI   outdirr   r   coverage_write_headliner!   r   r   r   rq   coverage_show_missing_itemsappquietr)   r*   r
   infor   )r   output_fileopr   r   typr"   r   r   r    r      s2   


"z CoverageBuilder.write_c_coverage	full_nameboolc                   s   t  fdd| jD S )Nc                 3  rU   r1   )searchrX   r   r   r    r7      s
    
z/CoverageBuilder.ignore_pyobj.<locals>.<genexpr>)r]   r   )r   r   r   r   r    ignore_pyobj   s   zCoverageBuilder.ignore_pyobjc                 C  s  t | jjd d }t | jjd d }| jj}t| jj|| j| j}|D ]A}d}| jD ]}|	|r8d} nq-|s@| 
|rAq%zt|}W n" tyi }	 zttd||	 d|	i| j|< W Y d }	~	q%d }	~	ww t }
t }g }i }t|D ]\}}|d d	krqyt|d
sqy|j|krqy| d| }| 
|rqyt|r||vr| jD ]	}|	|r nq|r|jsqy|| || qy|
| qyt|rT| jD ]	}|	|r nxq||vr|r|jsqyg ||< qyg }t|D ]Z}||jvrqzt||}W n
 ty   Y qw t |st|sq|d d	kr!q|r)|js)q| d| }| 
|r7q||vrG|| || q|
| q|rT|||< qy||d| j|< || j!|< |
| j"|< q%d S )Npyobjectsrf   FTro   rp   r   rh   
__module__r[   )funcsclasses)#	frozensetr   
domaindatar   coverage_skip_undoc_in_sourcerv   rj   r   rm   rW   r   r   rr   r)   r*   r
   r^   inspect
getmembershasattrr   
isfunctionr   __doc__r%   rd   isclassr   dir__dict__r   AttributeErrorismethodr   r   )r   seen_objectsrl   
skip_undocrf   rQ   ignorer,   re   rs   documented_objectsundocumented_objectsr   r   r"   r   r   attrs	attr_nameattrfull_attr_namer   r   r    r     s   


















z!CoverageBuilder.build_py_coverager   r   c           
   	   C  s6  t | j | j B }t }t }|D ]}|| j| | j| B O }|| j| O }qg dg}t|D ]1}| j| | j| B }t|rQdt| j|  t| }nd}||d| dt| j|  g q3|r|ddt| t| ddt|t|  g n|g d	 t|D ]
}	|	|	 d
 qdS )zOutputs the table of ``op``.)ModuleCoverageUndocumentedg      Y@z%.2f%%z%dTOTALd   z.2f%)r   1000r   N)
r   r   keysr   r^   rq   r   r%   rB   r   )
r   r   all_modulesall_objectsall_documented_objectsmoduler.   module_objectsvaluer   r   r   r    _write_py_statisticsm  s.   
$z$CoverageBuilder._write_py_statisticsc              
   C  s  t | jd}g }t|ddd_}| jjrt|dd | jjr&| t	j
 | jjr9t|d | | |d t| j }|D ]
}| j| }d	|v rX|||d	 f qB|d
 sa|d saqBt|| |d r|d |dd |d D  | jjr| jjr|d D ]}ttd|| qn|d D ]}ttdd d d|  td |  q|d |d
 rM|d t|d
  D ]\}}	|	s|d|  | jjr| jjrttd|| qttdd d d|  td |  q|d|  |dd |	D  | jjrG| jjr(|	D ]}
ttd|||
 qq|	D ]}
ttdd d d|d |
   td |  q*q|d qB|rht|d |dd |D  W d    d S W d    d S 1 stw   Y  d S ) Nz
python.txtr   r   r   zUndocumented Python objectsr<   
Statisticsr   rp   r   r   zFunctions:
c                 s      | ]}d | V  qdS ) * %s
Nr   r2   r   r   r    r7         z4CoverageBuilder.write_py_coverage.<locals>.<genexpr>z&undocumented python function: %s :: %sr   zpy  z
function  r   z - in module z	Classes:
r   z#undocumented python class: %s :: %sz
class     z * %s -- missing methods:

c                 s  r   )z   - %s
Nr   r2   r   r   r    r7     r   z*undocumented python method: %s :: %s :: %sz
method    r[   zModules that failed to importc                 s  r   )z * %s -- %s
Nr   r2   r   r   r    r7     r   )r   rI   r   r   r   r   r!   coverage_statistics_to_stdoutr   sysstdoutcoverage_statistics_to_reportr   rq   rm   r   r%   
writelinesr   r   r   r)   r*   r
   r   r   r   )r   r   failedr   r   r"   r   func
class_namemethodsmethr   r   r    r     s   
















E$z!CoverageBuilder.write_py_coveragec                 C  sZ   t | jd}t|d}t| j| j| j| j	f| W d    d S 1 s&w   Y  d S )Nzundoc.picklewb)
r   rI   r   r   pickledumprm   r   r   r   )r   
picklepathdumpfiler   r   r    finish  s   "zCoverageBuilder.finishN)r   r   )r   r   )r   rT   r   r   )r   r   r   r   )r   r   r   r   )__name__r   __qualname__r   r"   r
   r   sepepilogr   r   r   r   r   r   r   r   r   r   r   r   r   r    rw      s$    







j
 Lrw   r   r   r   c                 C  s   |  t | jdddtthd | dg d | dg d | dg d | dg d | d	g d | d
i d | di d | ddd | dddt | dddt | ddd | ddd tjddS )Nrj   r   rF   )typesrz   r|   r{   r}   r   r   ry   r   Tr   r   r   Fr   )versionparallel_read_safe)add_builderrw   add_config_valuetuplelistr   sphinx__display_version__)r   r   r   r    setup  s   
r  )r   )r   r   r   r   r   r   r   r   )r"   r   r#   r   r   r$   )r.   r/   r   r0   )r@   rC   rD   r   r   r   )rJ   rC   rK   rL   rD   r   r   r0   )rQ   r   rR   rS   r   rT   )
rj   rk   rl   rT   rR   rS   rm   rn   r   rL   )r   r   r   r   )2r   
__future__r   r   r   r   ra   r&   r   	importlibr   osr   typingr   r   r   r   r  sphinx.buildersr	   sphinx.localer
   sphinx.utilr   sphinx.util.consoler   sphinx.util.inspectr   collections.abcr   r   r   r   sphinx.applicationr   sphinx.util.typingr   	getLoggerr   r)   r!   r-   rB   r>   r?   rc   rv   rw   r  r   r   r   r    <module>   sB    








"5  F