B
    ÿ‹d/  ã               @   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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'Measure durations of Sphinx processing.é    )ÚdatetimeÚ	timedelta)Úislice)Ú
itemgetter)ÚAnyÚDictÚListÚcast)ÚnodesN)Ú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   ú`/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/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 )N)r   Úclear)r   r   r   r   r      s    zDurationDomain.clear)r   r   c             C   s   | j  |d ¡ d S )N)r   Úpop)r   r   r   r   r   Ú	clear_doc!   s    zDurationDomain.clear_doc)ÚdocnamesÚ	otherdatar   c             C   s,   x&|  ¡ D ]\}}||kr
|| j|< q
W d S )N)Ú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   )Ú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   Ú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_read3   s    r1   )r)   Údoctreer   c             C   s:   | j j d¡}t ¡ | }tt| j  d¡ƒ}| |¡ dS )zRecord a reading duration.r.   r   N)	r   r0   Úgetr   r/   r	   r   r*   r   )r)   r2   r.   r   r+   r   r   r   Úon_doctree_read8   s    r4   )r)   Úerrorr   c             C   s~   t t| j d¡ƒ}t|j ¡ tdƒdd}|s2dS t 	d¡ t 	t
dƒ¡ x.t|dƒD ] \}}t 	d	|j|jd
 |¡ qVW d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)   r5   r+   Z	durationsr   Údr   r   r   Úon_build_finished@   s    
rA   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,   r1   r4   rA   ÚsphinxZ__display_version__)r)   r   r   r   ÚsetupM   s    
rE   )#r%   r   r   Ú	itertoolsr   Úoperatorr   Útypingr   r   r   r	   Zdocutilsr
   rD   Zsphinx.applicationr   Zsphinx.domainsr   Zsphinx.localer   Zsphinx.utilr   Ú	getLoggerr"   r<   r   r,   r(   r1   Zdocumentr4   Ú	ExceptionrA   rE   r   r   r   r   Ú<module>   s"   
	