
=+Y3                 @   s   d  d l  Z  d  d l Z d  d l m Z m Z m Z e d d  Gd d   d e   Z Gd d   d e  Z d  d	 d
  Z Gd d   d e  Z	 d d   Z
 d S)    N)attr
attributesFactoryfrozenTc               @   sC   e  Z d  Z e   Z e   Z e   Z e d e e j   Z	 d S)
ImportInfodefaultN)
__name__
__module____qualname__r   namecontext_namecounterr   timestart r   r   ;/home/andreas/ai/mscthesis/disco-dop/web/import_profiler.pyr      s   			r   c               @   s4   e  Z d  Z d d   Z d d   Z d d   Z d S)ImportStackc             C   s(   g  |  _  i  |  _ i  |  _ d |  _ d  S)Nr   )_current_stack_full_stack_elapsed_counter)selfr   r   r   __init__   s    			zImportStack.__init__c             C   s   t  | | |  j  } |  j d 7_ t |  j  d k r| |  j d } | |  j k re g  |  j | <n  |  j | j |  n  |  j j |  | S)N   r   )r   r   lenr   r   append)r   r   r   infoparentr   r   r   push   s    zImportStack.pushc             C   s?   |  j  j   } | | k s! t  t j   | j |  j | <d  S)N)r   popAssertionErrorr   r   r   )r   Zimport_infotopr   r   r   r    $   s    zImportStack.popN)r   r	   r
   r   r   r    r   r   r   r   r      s   r   c       	   	   C   s   |  | k r d  S| |  } } | |  j  |  j | g | |  <| j |   xE | j |  g   D]1 } | | | 8} t | | | | | | d  qZ W| |  j |  d  S)Nr   )r   r   r   getcompute_intime)	r   
full_stackelapsedordered_visitedvisitedZdepthcumtimeintimeZchildr   r   r   r$   *   s    !r$   c               @   ss   e  Z d  Z d d   Z d d   Z d d   Z d d d	  Z d
 d   Z d d   Z d d d d d d  Z	 d S)ImportProfilerContextc             C   s   t  d |  _ t   |  _ d  S)N
__import__)__builtins___original_importerr   _import_stack)r   r   r   r   r   :   s    zImportProfilerContext.__init__c             C   s   |  j  t d <d  S)Nr,   )_profiled_importr-   )r   r   r   r   enable>   s    zImportProfilerContext.enablec             C   s   |  j  t d <d  S)Nr,   )r.   r-   )r   r   r   r   disableA   s    zImportProfilerContext.disableg      ?c             C   s5  |  j  j } |  j  j } t | j   d t j d  } i  } g  } x$ | D] } t | | | | |  qL Wg  } x | D] }	 | |	 }
 |
 d d } |
 d } |
 d } |
 d } |
 d d } | d k ry | j d j	 |  d j	 |  d	 | | f  qy qy Wd d
 l
 } t | j
 | d d d d  d
 S)z Print profiler results.

        Parameters
        ----------
        threshold : float
            import statements taking less than threshold (in ms) will not be
            displayed.
        keyr   r   i  r         z{:.1f}+Nheaderscumtime (ms)intime (ms)r   ZtablefmtZplainr   )r8   r9   zname)r/   r   r   sortedkeysoperatorZ
attrgetterr$   r   formattabulateprint)r   Z	thresholdr%   r&   r;   r(   r'   r3   lineskZnoder)   r   r   levelr*   r>   r   r   r   
print_infoD   s0    	!



z ImportProfilerContext.print_infoc             C   s   |  j    |  S)N)r1   )r   r   r   r   	__enter__o   s    
zImportProfilerContext.__enter__c             O   s   |  j    d  S)N)r2   )r   akwr   r   r   __exit__s   s    zImportProfilerContext.__exit__Nr   c       
      O   s   | d  k r d  } n- | j  d  } | d  k rB | j  d  } n  |  j j | |  }	 z# |  j | | | | | | |  SWd  |  j j |	  Xd  S)Nr   __file__)r#   r/   r   r.   r    )
r   r   globalslocalsfromlistrB   rE   rF   r   r   r   r   r   r0   v   s    	#z&ImportProfilerContext._profiled_import)
r   r	   r
   r   r1   r2   rC   rD   rG   r0   r   r   r   r   r+   9   s   +	r+   c               C   s   t    S)N)r+   r   r   r   r   profile_import   s    rL   )r<   r   r   r   r   objectr   r   r$   r+   rL   r   r   r   r   <module>   s   M