U
    ç¥¡cf  ã                   @   sò   d Z ddlmZ ddlmZ ddlmZ zPddlm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 W nd ek
rÜ   G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZY nX G dd„ deƒZdS )aç  
Expose a memory-profiling panel to the Django Debug toolbar.

Shows process memory information (virtual size, resident set size) and model
instances for the current request.

Requires Django and Django Debug toolbar:

https://github.com/jazzband/django-debug-toolbar

Pympler adds a memory panel as a third party addon (not included in the
django-debug-toolbar). It can be added by overriding the `DEBUG_TOOLBAR_PANELS`
setting in the Django project settings::

    DEBUG_TOOLBAR_PANELS = (
        'debug_toolbar.panels.timer.TimerDebugPanel',
        'pympler.panels.MemoryPanel',
        )

Pympler also needs to be added to the `INSTALLED_APPS` in the Django settings::

    INSTALLED_APPS = INSTALLED_APPS + ('debug_toolbar', 'pympler')
é    )ÚClassTracker)ÚProcessMemoryInfo©Úpp)ÚPanel)Úapps)ÚContextÚTemplate)Úrender_to_string)ÚHttpRequest)ÚHttpResponsec                   @   s   e Zd ZdS )r   N©Ú__name__Ú
__module__Ú__qualname__© r   r   ú2/tmp/pip-unpacked-wheel-8ad_c8mj/pympler/panels.pyr   %   s   r   c                   @   s   e Zd ZdS )r	   Nr   r   r   r   r   r	   (   s   r	   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   +   s   r   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   .   s   r   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r   1   s   r   c                       st   e Zd ZdZdZdZeegZe	e
dœ‡ fdd„Zddœd	d
„Zddœdd„Zedœdd„Zeedœdd„ƒZ‡  ZS )ÚMemoryPanelZpymplerZMemoryzmemory_panel.html)ÚrequestÚreturnc                    s’   t ƒ | _t ¡ | j D ]}| j |¡ q| j d¡ |  dtƒ i¡ t	t
| ƒ |¡}|  dtƒ i¡ | j d¡ | jj}| ¡  |  d|i¡ |S )NÚbeforeÚafterÚstats)r   Ú_trackerr   Ú
get_modelsÚclassesÚtrack_classZcreate_snapshotZrecord_statsr   Úsuperr   Úprocess_requestr   Zannotate)Úselfr   ÚclsÚresponser   ©Ú	__class__r   r   r   ?   s    zMemoryPanel.process_requestN)r   c                 C   s,   t ƒ | _t ¡ | j D ]}| j |¡ qd S ©N)r   r   r   r   r   r   )r   r    r   r   r   Úenable_instrumentationM   s    z"MemoryPanel.enable_instrumentationc                 C   s   | j  ¡  d S r$   )r   Zdetach_all_classes)r   r   r   r   Údisable_instrumentationR   s    z#MemoryPanel.disable_instrumentationc                 C   sP   |   ¡ }|d }|d }|j}||j }|dkr<dt|ƒ nd}dt|ƒ|f S )Nr   r   r   z(+%s)Ú z%s %s)Ú	get_statsÚrssr   )r   Úcontextr   r   r)   Údeltar   r   r   Únav_subtitleU   s    
zMemoryPanel.nav_subtitlec                 C   sà   |   ¡ }|d }|d }|d }d|jfd|jfg}| || ¡ dd„ |D ƒ}| |j¡ g }|jd }|jD ]R}d	d„ |j| D ƒ}	|j 	|i ¡ 	d
d¡}
|	rp|	d dkrp| 
||	t|
ƒf¡ qp| ||dœ¡ t| j|ƒS )Nr   r   r   zResident set sizezVirtual sizec                 S   s   g | ]\}}|t |ƒf‘qS r   r   )Ú.0ÚkeyÚvaluer   r   r   Ú
<listcomp>h   s     z'MemoryPanel.content.<locals>.<listcomp>éÿÿÿÿc                 S   s   g | ]\}}|‘qS r   r   )r-   Ú_Zcntr   r   r   r0   n   s     Úsumr   )Úrowsr   )r(   r)   ZvszÚextendZos_specificZ	snapshotsZtracked_classesÚhistoryr   ÚgetÚappendr   Úupdater
   Útemplate)r   r*   r   r   r   r4   r   ZsnapshotÚmodelr6   Úsizer   r   r   Úcontent^   s&    ÿ

zMemoryPanel.content)r   r   r   ÚnameÚtitler:   r   r	   r   r   r   r   r%   r&   Ústrr,   Úpropertyr=   Ú__classcell__r   r   r"   r   r   5   s   	r   N)Ú__doc__Zpympler.classtrackerr   Zpympler.processr   Zpympler.util.stringutilsr   Zdebug_toolbar.panelsr   Zdjango.appsr   Zdjango.templater   r	   Zdjango.template.loaderr
   Zdjango.http.requestr   Zdjango.http.responser   ÚImportErrorÚobjectr   r   r   r   r   Ú<module>   s"   