U
    \ªShC  ã                   @  s  d Z ddlmZ ddlmZm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Zdd	lmZ dd
lmZ ddlmZ ddlmZ e e¡ZG dd„ deƒZdddœdd„Zdddddœdd„Zddddœdd„Zddddœdd „Zdd!dœd"d#„ZdS )$z'Measure durations of Sphinx processing.é    )Úannotations)ÚdatetimeÚ	timedelta)Úislice)Ú
itemgetter)ÚAnyÚcast)ÚnodesN)ÚSphinx)ÚDomain)Ú__)Úloggingc                   @  sf   e Zd ZdZdZeddœdd„ƒZddd	œd
d„Zddœdd„Zdddœdd„Z	ddddœdd„Z
dS )ÚDurationDomainz,A domain for durations of Sphinx processing.Údurationzdict[str, timedelta])Úreturnc                 C  s   | j  di ¡S )NÚreading_durations)ÚdataÚ
setdefault©Úself© r   úF/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/ext/duration.pyr      s    z DurationDomain.reading_durationsr   ÚNone)r   r   c                 C  s   || j | jj< d S ©N)r   ÚenvÚdocname)r   r   r   r   r   Únote_reading_duration   s    z$DurationDomain.note_reading_durationc                 C  s   | j  ¡  d S r   )r   Úclearr   r   r   r   r       s    zDurationDomain.clearÚstr)r   r   c                 C  s   | j  |d ¡ d S r   )r   Úpop)r   r   r   r   r   Ú	clear_doc#   s    zDurationDomain.clear_docú	list[str])ÚdocnamesÚ	otherdatar   c                 C  s(   |  ¡ D ]\}}||kr|| j|< qd S r   )Úitemsr   )r   r"   r#   r   r   r   r   r   Úmerge_domaindata&   s    zDurationDomain.merge_domaindataN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚnameÚpropertyr   r   r   r    r%   r   r   r   r   r      s   r   r
   r   )Úappr   c                 C  s   t t| j d¡ƒ}| ¡  dS )zTInitialize DurationDomain on bootstrap.

    This clears results of last build.
    r   N)r   r   r   Ú
get_domainr   )r,   Údomainr   r   r   Úon_builder_inited,   s    r/   r   r!   )r,   r   Úcontentr   c                 C  s   t  ¡ | jjd< dS )z"Start to measure reading duration.Ú
started_atN)r   Únowr   Ú	temp_data)r,   r   r0   r   r   r   Úon_source_read5   s    r4   znodes.document)r,   Údoctreer   c                 C  s8   | j jd }t ¡ | }tt| j  d¡ƒ}| |¡ dS )zRecord a reading duration.r1   r   N)r   r3   r   r2   r   r   r-   r   )r,   r5   r1   r   r.   r   r   r   Úon_doctree_read:   s    r6   Ú	Exception)r,   Úerrorr   c                 C  sz   t t| j d¡ƒ}t|j ¡ tdƒdd}|s2dS t 	d¡ t 	t
dƒ¡ t|dƒD ] \}}t 	d	|j|jd
 |¡ qTdS )z*Display duration ranking on current build.r   é   T)ÚkeyÚreverseNÚ zH====================== slowest reading durations =======================é   z
%d.%03d %siè  )r   r   r   r-   Úsortedr   r$   r   ÚloggerÚinfor   r   ÚsecondsÚmicroseconds)r,   r8   r.   Z	durationsr   Údr   r   r   Úon_build_finishedB   s    
rD   zdict[str, Any]c                 C  sH   |   t¡ |  dt¡ |  dt¡ |  dt¡ |  dt¡ tjdddœS )Nzbuilder-initedzsource-readzdoctree-readzbuild-finishedT)ÚversionZparallel_read_safeZparallel_write_safe)	Z
add_domainr   Úconnectr/   r4   r6   rD   ÚsphinxZ__display_version__)r,   r   r   r   ÚsetupO   s    
ýrH   ) r)   Ú
__future__r   r   r   Ú	itertoolsr   Úoperatorr   Útypingr   r   Zdocutilsr	   rG   Zsphinx.applicationr
   Zsphinx.domainsr   Zsphinx.localer   Zsphinx.utilr   Ú	getLoggerr&   r?   r   r/   r4   r6   rD   rH   r   r   r   r   Ú<module>   s$   
	