a
    0øa¹  ã                   @   s  d 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	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 e e¡ZG dd„ deƒZeddœdd„Zeee
e ddœdd„Zeejddœdd„Zeeddœdd„Z ee	eef dœdd„Z!dS )zÐ
    sphinx.ext.duration
    ~~~~~~~~~~~~~~~~~~~

    Measure durations of Sphinx processing.

    :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
é    )ÚdatetimeÚ	timedelta)Úislice)Ú
itemgetter)ÚAnyÚDictÚListÚcast)Únodes)ÚSphinx)ÚDomain)Ú__)Úloggingc                   @   sz   e Zd ZdZdZeeeef dœdd„ƒZ	eddœdd	„Z
ddœd
d„Zeddœdd„Zee eeef ddœdd„ZdS )ÚDurationDomainz,A domain for durations of Sphinx processing.Úduration)Úreturnc                 C   s   | j  di ¡S )NÚreading_durations)ÚdataÚ
setdefault©Úself© r   ú2lib/python3.9/site-packages/sphinx/ext/duration.pyr      s    z DurationDomain.reading_durationsN)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)r   r   c                 C   s   | j  |d ¡ d S r   )r   Úpop)r   r   r   r   r   Ú	clear_doc(   s    zDurationDomain.clear_doc)ÚdocnamesÚ	otherdatar   c                 C   s(   |  ¡ D ]\}}||v r|| j|< qd S r   )Úitemsr   )r   r    r!   r   r   r   r   r   Úmerge_domaindata+   s    zDurationDomain.merge_domaindata)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚnameÚpropertyr   Ústrr   r   r   r   r   r   r#   r   r   r   r   r      s   r   N)Ú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_inited1   s    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   r/   r   r   r   Úon_source_read:   s    r3   )r+   Údoctreer   c                 C   s:   | j j d¡}t ¡ | }tt| j  d¡ƒ}| |¡ dS )zRecord a reading duration.r0   r   N)	r   r2   Úgetr   r1   r	   r   r,   r   )r+   r4   r0   r   r-   r   r   r   Úon_doctree_read?   s    r6   )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   ZsecondsZmicroseconds)r+   r7   r-   Z	durationsr   Údr   r   r   Úon_build_finishedG   s    
rA   c                 C   sF   |   t¡ |  dt¡ |  dt¡ |  dt¡ |  dt¡ ddddœS )Nzbuilder-initedzsource-readzdoctree-readzbuild-finishedZbuiltinT)ÚversionZparallel_read_safeZparallel_write_safe)Z
add_domainr   Zconnectr.   r3   r6   rA   )r+   r   r   r   ÚsetupT   s    
ýrC   )"r'   r   r   Ú	itertoolsr   Úoperatorr   Útypingr   r   r   r	   Zdocutilsr
   Zsphinx.applicationr   Zsphinx.domainsr   Zsphinx.localer   Zsphinx.utilr   Z	getLoggerr$   r>   r   r.   r*   r3   Zdocumentr6   Ú	ExceptionrA   rC   r   r   r   r   Ú<module>   s    

	