B
    u9aj                 @   s  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gZ	G dd de
eZG dd dZG dd	 d	Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zedkrd dlZyd dlZW n ek
r   Y nX G dd dejZee jdkre jd ZndZyTeeZx"e jdd D ]Ze e q.W e!dej"d e#  e!dej"d W n e$k
r~   Y nX dS )     N)Enum)
cmp_to_keyStatsSortKeyc               @   s8   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
d ZdS )r   )callsncalls)
cumulativecumtime)filenamemodulelinenamenflpcallsstdname)timetottimec             G   s>   t | }|d |_x|dd  D ]}|| j|< q"W ||_|S )Nr      )str__new___value__value2member_map_Z_all_values)clsvaluesobjZother_value r   /usr/lib/python3.7/pstats.pyr   -   s    

zSortKey.__new__N)__name__
__module____qualname__ZCALLSZ
CUMULATIVEZFILENAMEZLINENAMEZNFLZPCALLSZSTDNAMEZTIMEr   r   r   r   r   r   "   s   c               @   s   e Zd ZddddZdd Zdd Zd	d
 Zdd Zdd ZddddddddddddddZ	dd Z
dd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd6d0d1Zd2d3 Zd4d5 ZdS )7r   N)streamc            G   sF   |pt j| _t|sd }n|d }|dd  }| | | j|  d S )Nr   r   )sysstdoutr!   leninitadd)selfr!   argsargr   r   r   __init__Y   s    
zStats.__init__c             C   s   d | _ g | _d | _d| _d| _d| _d| _t | _i | _	i | _
| | y|   W n8 tk
r   td| jrx| jd nd | jd  Y nX d S )Nr   zInvalid timing data %s )file)all_calleesfilesfcn_listtotal_tttotal_calls
prim_callsmax_name_lenset	top_levelstatssort_arg_dict
load_statsget_top_level_stats	Exceptionprintr!   )r'   r)   r   r   r   r%   c   s"    
z
Stats.initc          	   C   s   |d kri | _ d S t|trxt|d}t|| _ W d Q R X y"t|}t	|j
d | }W n   Y nX |g| _n t|dr|  |j | _ i |_ | j std| j|f d S )Nrbz    create_statsz.Cannot create or construct a %r object from %r)r7   
isinstancer   openmarshalloadosstatr   Zctimest_mtimer/   hasattrr>   	TypeError	__class__)r'   r)   fZ
file_statsr   r   r   r9   v   s(    



zStats.load_statsc             C   s   x~| j  D ]p\}\}}}}}|  j|7  _|  j|7  _|  j|7  _d|kr\| j| tt|| j	krtt|| _	qW d S )N)Zjprofiler   Zprofiler)
r7   itemsr2   r3   r1   r6   r&   r$   func_std_stringr4   )r'   funcccncttctcallersr   r   r   r:      s    zStats.get_top_level_statsc             G   s   |s| S xt |D ]}t| t|kr.t|}|  j|j7  _|  j|j7  _|  j|j7  _|  j|j7  _x|jD ]}| j| qvW | j	|j	k r|j	| _	d | _
xJ|j D ]<\}}|| jkr| j| }nddddi f}t||| j|< qW qW | S )Nr   )reversedtyper   r/   r2   r3   r1   r6   r&   r4   r0   r7   rJ   add_func_stats)r'   arg_listitemrL   rD   Zold_func_statr   r   r   r&      s(    
z	Stats.addc          	   C   s(   t |d}t| j| W d Q R X d S )Nwb)r@   rA   dumpr7   )r'   r
   rI   r   r   r   
dump_stats   s    zStats.dump_stats)))r   r+   z
call count)))   r+   zcumulative time)))   r   z	file name)))   r   zline number)))   r   zfunction name)))r]   r   )r[   r   )r\   r   zname/file/line)))r   r+   zprimitive call count)))   r   zstandard name)))   r+   zinternal time)r   r   r	   r   r
   r   r   r   r   r   r   r   r   c             C   s   | j szi  | _ }i }xP| j D ]B\}}|}x4|r`|s8P ||krJd||< P |||< |d d }q.W q W x|D ]
}||= qlW | j S )Nr   r+   )r8   sort_arg_dict_defaultrJ   )r'   dictZbad_listwordtupZfragmentr   r   r   get_sort_arg_defs   s     


zStats.get_sort_arg_defsc             G   sl  |sd| _ | S t|dkrBt|d trBddddd|d  g}n>t|dkrx0|dd  D ] }t|t|d kr\td	q\W |  }d
}d| _d}xJ|D ]B}t|tr|j	}||| d  }|  j||| d  7  _d}qW g }xB| j
 D ]4\}\}	}
}}}||	|
||f| t||f  qW |jtt|jd g  | _ }x|D ]}||d  qPW | S )Nr   r   r   r   r   r   )r+   r   r   r_   r_   zCan't have mixed argument typer   r,   z, )keyr+   )r0   r$   r?   intrS   rG   rd   	sort_typer   valuer7   rJ   appendrK   sortr   	TupleCompcompare)r'   Zfieldr)   Zsort_arg_defsZ
sort_tupleZ	connectorrb   Z
stats_listrL   rM   rN   rO   rP   rQ   r0   tupler   r   r   
sort_stats   s>    



zStats.sort_statsc             C   s   | j r| j   | S )N)r0   reverse)r'   r   r   r   reverse_order  s    
zStats.reverse_orderc             C   s   | j }i  | _ }d}x| D ]\}\}}}}}	t|}
tt|
|krTtt|
}i }x |	 D ]\}}||t|< qbW |
|krt||
 |||||f||
< q|||||f||
< qW | j}t  | _}x|D ]}|t| qW || _	d | _
d | _| S )Nr   )r7   rJ   func_strip_pathr$   rK   rT   r6   r5   r&   r4   r0   r.   )r'   ZoldstatsZnewstatsr4   rL   rM   rN   rO   rP   rQ   ZnewfuncZ
newcallersfunc2callerZold_topZnew_topr   r   r   
strip_dirs  s.    

zStats.strip_dirsc       
      C   s|   | j r
d S i  | _ }xb| j D ]T\}\}}}}}||krBi ||< x0| D ]$\}}	||krdi ||< |	|| |< qLW q W d S )N)r.   r7   rJ   )
r'   r.   rL   rM   rN   rO   rP   rQ   rr   rs   r   r   r   calc_callees#  s    
zStats.calc_calleesc             C   s  |}t |trpyt|}W n& tjk
rB   |d| 7 }||fS X g }x|D ]}|t|rN|| qNW nzt|}t |t	rd|  krdk rn nt
|| d }|d | }n2t |t
rd|  kr|k rn n|}|d | }t|t|kr|dt|t||f 7 }||fS )Nz#   <Invalid regular expression %r>
g        g      ?g      ?r   z6   List reduced from %r to %r due to restriction <%r>
)r?   r   recompileerrorsearchrK   ri   r$   floatrf   )r'   ZsellistmsgZnew_listZrexrL   countr   r   r   eval_print_amount6  s,    


""zStats.eval_print_amountc             C   s   | j }| jr*| jd d  }d| j d }nt| j }d}x|D ]}| |||\}}qBW t|}|spd|fS t|| j	d |t| jk rd}x(|D ] }tt
||krtt
|}qW |d |fS )Nz   Ordered by: 
z!   Random listing order was used
r   )r-   r_   )r4   r0   rg   r{   r7   keysr~   r$   r<   r!   rK   )r'   Zsel_listwidthZ	stat_listr|   Z	selectionr}   rL   r   r   r   get_print_listP  s$    

zStats.get_print_listc             G   s   x| j D ]}t|| jd qW | j r0t| jd d}x"| jD ]}t|t|| jd q<W t|| jdd| jd | j| jkrtd| j d| jd td| j | jd t| jd | |\}}|r| 	  x|D ]}| 
| qW t| jd t| jd | S )N)r-   z        zfunction calls )endr-   z(%d primitive calls)zin %.3f seconds)r/   r<   r!   r6   func_get_function_namer2   r3   r1   r   print_title
print_line)r'   amountr
   indentrL   r   r{   r   r   r   print_statsh  s(    
zStats.print_statsc             G   s~   |  |\}}|rz|   | |d x:|D ]2}|| jkrP| ||| j|  q,| ||i  q,W t| jd t| jd | S )Nz	called...)r-   )r   ru   print_call_headingr.   print_call_liner<   r!   )r'   r   r   r{   rL   r   r   r   print_callees  s    

zStats.print_calleesc       
      G   sl   |  |\}}|rh| |d x0|D ](}| j| \}}}}}	| |||	d q$W t| jd t| jd | S )Nzwas called by...z<-)r-   )r   r   r7   r   r<   r!   )
r'   r   r   r{   rL   rM   rN   rO   rP   rQ   r   r   r   print_callers  s    
zStats.print_callersc       
      C   sx   t d|| | jd d}x<| j D ].\}}}}}|r(tt| }	t|	t}P q(W |rtt d| d | jd d S )Nz	Function )r-   Fr   z    ncalls  tottime  cumtime)	r<   ljustr!   r7   r   nextiterr?   rm   )
r'   	name_sizeZcolumn_titleZ	subheaderrM   rN   rO   rP   rQ   rh   r   r   r   r     s    
zStats.print_call_heading->c             C   s  t t||| d| jd |s2t | jd d S t| }d}x|D ]}t|}|| }	t|	tr|	\}
}}}|
|krd|
|f }n
d|
f }d|dd	t	|  t
|t
||f }|d
 }n$d||	t
| j| d f }|d }t || | | jd d}qHW d S )Nr   )r   r-   )r-   r,   z%d/%dz%dz%s %s %s  %sr^   r_   r   z	%s(%r) %srZ   )r<   rK   r   r!   sortedr   r?   rm   rjustr$   f8r7   )r'   r   sourceZ	call_dictZarrowZclistr   rL   r   rh   rN   rM   rO   rP   ZsubstatsZ
left_widthr   r   r   r     s*    



zStats.print_call_linec             C   s"   t dd| jd t d| jd d S )Nz-   ncalls  tottime  percall  cumtime  percallr   )r   r-   zfilename:lineno(function))r-   )r<   r!   )r'   r   r   r   r     s    zStats.print_titlec             C   s   | j | \}}}}}t|}||kr4|d t| }t|dd| jd tt|d| jd |dkrxtdd| jd ntt|| d| jd tt|d| jd |dkrtdd| jd ntt|| d| jd tt|| jd d S )N/	   r   )r   r-   r   z        )r-   )r7   r   r<   r   r!   r   rK   )r'   rL   rM   rN   rO   rP   rQ   cr   r   r   r     s    zStats.print_line)r   )r   r   r   r*   r%   r9   r:   r&   rY   r`   rd   rn   rp   rt   ru   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   7   sB   "

'
c               @   s   e Zd Zdd Zdd ZdS )rk   c             C   s
   || _ d S )N)comp_select_list)r'   r   r   r   r   r*     s    zTupleComp.__init__c             C   sB   x<| j D ]2\}}|| }|| }||k r.| S ||kr|S qW dS )Nr   )r   )r'   leftrightindex	directionlrr   r   r   rl     s    zTupleComp.compareN)r   r   r   r*   rl   r   r   r   r   rk     s   rk   c             C   s   | \}}}t j|||fS )N)rC   pathbasename)	func_namer
   r   r   r   r   r   rq     s    
rq   c             C   s   | d S )Nr_   r   )rL   r   r   r   r     s    r   c             C   sN   | d d dkrB| d }| dr<|dr<d|dd  S |S nd|  S d S )	Nr_   )~r   <>z{%s}r   r+   z	%s:%d(%s))
startswithendswith)r   r   r   r   r   rK     s    rK   c             C   s@   |\}}}}}| \}}}	}
}|| || ||	 ||
 t ||fS )N)add_callers)targetr   rM   rN   rO   rP   rQ   Zt_ccZt_ncZt_ttZt_ctZ	t_callersr   r   r   rT     s    rT   c             C   s   i }x|   D ]\}}|||< qW xb|  D ]V\}}||krzt|trhtdd t||| D ||< q||  |7  < q,|||< q,W |S )Nc             s   s   | ]\}}|| V  qd S )Nr   ).0ijr   r   r   	<genexpr>  s    zadd_callers.<locals>.<genexpr>)rJ   r?   rm   zip)r   r   Znew_callersrL   rs   r   r   r   r     s    
"r   c             C   s"   d}x|   D ]}||7 }qW |S )Nr   )r   )rQ   rN   r   r   r   r   count_calls  s    r   c             C   s   d|  S )Nz%8.3fr   )xr   r   r   r   (  s    r   __main__c               @   s   e Zd Zd6ddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZdS )7ProfileBrowserNc             C   s6   t j|  d| _d | _tj| _|d k	r2| | d S )Nz% )	cmdCmdr*   promptr7   r"   r#   r!   do_read)r'   profiler   r   r   r*   7  s    zProfileBrowser.__init__c          	   C   s   |  }g }x|D ]}y|t| wW n tk
r>   Y nX y8t|}|dksZ|dk rjtd| jd w|| wW n tk
r   Y nX || qW | jrt| j||  ntd| jd dS )Nr   r   z#Fraction argument must be in [0, 1])r-   zNo statistics object is loaded.)	splitri   rf   
ValueErrorrz   r<   r!   r7   getattr)r'   fnr   r(   Z	processedZtermZfracr   r   r   generic?  s,    

zProfileBrowser.genericc             C   sX   t d| jd t d| jd t d| jd t d| jd t d| jd t d| jd d S )NzArguments may be:)r-   z0* An integer maximum number of entries to print.z:* A decimal fractional number between 0 and 1, controllingz-  what fraction of selected entries to print.z8* A regular expression; only entries with function namesz  that match it are printed.)r<   r!   )r'   r   r   r   generic_helpW  s    zProfileBrowser.generic_helpc          
   C   sd   | j rRy| j | W q` tk
rN } ztd||f | jd W d d }~X Y q`X ntd| jd dS )Nz$Failed to load statistics for %s: %s)r-   zNo statistics object is loaded.r   )r7   r&   OSErrorr<   r!   )r'   r   er   r   r   do_add_  s    *zProfileBrowser.do_addc             C   s   t d| jd d S )Nz>Add profile info from given file to current statistics object.)r-   )r<   r!   )r'   r   r   r   help_addh  s    zProfileBrowser.help_addc             C   s   |  d|S )Nr   )r   )r'   r   r   r   r   
do_calleesk  s    zProfileBrowser.do_calleesc             C   s   t d| jd |   d S )Nz6Print callees statistics from the current stat object.)r-   )r<   r!   r   )r'   r   r   r   help_calleesm  s    zProfileBrowser.help_calleesc             C   s   |  d|S )Nr   )r   )r'   r   r   r   r   
do_callersq  s    zProfileBrowser.do_callersc             C   s   t d| jd |   d S )Nz6Print callers statistics from the current stat object.)r-   )r<   r!   r   )r'   r   r   r   help_callerss  s    zProfileBrowser.help_callersc             C   s   t d| jd dS )Nr,   )r-   r   )r<   r!   )r'   r   r   r   r   do_EOFw  s    zProfileBrowser.do_EOFc             C   s   t d| jd d S )NzLeave the profile brower.)r-   )r<   r!   )r'   r   r   r   help_EOFz  s    zProfileBrowser.help_EOFc             C   s   dS )Nr   r   )r'   r   r   r   r   do_quit}  s    zProfileBrowser.do_quitc             C   s   t d| jd d S )NzLeave the profile brower.)r-   )r<   r!   )r'   r   r   r   	help_quit  s    zProfileBrowser.help_quitc          
   C   s   |ryt || _W nn tk
rH } zt|jd | jd d S d }~X Y n: tk
r } zt|jjd || jd d S d }~X Y nX |d | _	n6t
| j	dkr| j	d d }| | ntd| jd dS )	Nr   )r-   :z% r_   z1No statistics object is current -- cannot reload.r   )r   r7   r   r<   r(   r!   r;   rH   r   r   r$   r   )r'   r   errr   r   r   r     s    zProfileBrowser.do_readc             C   s    t d| jd t d| jd d S )Nz+Read in profile data from a specified file.)r-   z*Without argument, reload the current file.)r<   r!   )r'   r   r   r   	help_read  s    zProfileBrowser.help_readc             C   s$   | j r| j   ntd| jd dS )NzNo statistics object is loaded.)r-   r   )r7   rp   r<   r!   )r'   r   r   r   r   
do_reverse  s    zProfileBrowser.do_reversec             C   s   t d| jd d S )Nz/Reverse the sort order of the profiling report.)r-   )r<   r!   )r'   r   r   r   help_reverse  s    zProfileBrowser.help_reversec                s   | j std| jd d S | j   |rRt fdd| D rR| j j|   n@td| jd x0tj	 D ]"\}}td||d f | jd qlW dS )	NzNo statistics object is loaded.)r-   c             3   s   | ]}| kV  qd S )Nr   )r   r   )abbrevsr   r   r     s    z)ProfileBrowser.do_sort.<locals>.<genexpr>z/Valid sort keys (unique prefixes are accepted):z%s -- %sr   r   )
r7   r<   r!   rd   allr   rn   r   r`   rJ   )r'   r   re   rh   r   )r   r   do_sort  s    
zProfileBrowser.do_sortc             C   s    t d| jd t d| jd d S )Nz.Sort profile data according to specified keys.)r-   z3(Typing `sort' without arguments lists valid keys.))r<   r!   )r'   r   r   r   	help_sort  s    zProfileBrowser.help_sortc                s    fddt jD S )Nc                s   g | ]}|  r|qS r   )r   )r   a)textr   r   
<listcomp>  s    z0ProfileBrowser.complete_sort.<locals>.<listcomp>)r   r`   )r'   r   r(   r   )r   r   complete_sort  s    zProfileBrowser.complete_sortc             C   s   |  d|S )Nr   )r   )r'   r   r   r   r   do_stats  s    zProfileBrowser.do_statsc             C   s   t d| jd |   d S )Nz.Print statistics from the current stat object.)r-   )r<   r!   r   )r'   r   r   r   
help_stats  s    zProfileBrowser.help_statsc             C   s$   | j r| j   ntd| jd d S )NzNo statistics object is loaded.)r-   )r7   rt   r<   r!   )r'   r   r   r   r   do_strip  s    zProfileBrowser.do_stripc             C   s   t d| jd d S )Nz<Strip leading path information from filenames in the report.)r-   )r<   r!   )r'   r   r   r   
help_strip  s    zProfileBrowser.help_stripc             C   s   t d| jd d S )NzShow help for a given command.)r-   )r<   r!   )r'   r   r   r   	help_help  s    zProfileBrowser.help_helpc             C   s   |r|S d S )Nr   )r'   stopr   r   r   r   postcmd  s    zProfileBrowser.postcmd)N)r   r   r   r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   6  s4   
	r   r   r_   z*Welcome to the profile statistics browser.)r-   zGoodbye.)%r"   rC   r   rA   rv   enumr   	functoolsr   __all__r   r   r   rk   rq   r   rK   rT   r   r   r   r   r   readlineImportErrorr   r   r$   argvZinitprofileZbrowserr   r   r<   r!   ZcmdloopKeyboardInterruptr   r   r   r   <module>   sP       
 