a
    ތxdo"                     @   s<  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l	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 zd dlZW n ey   dZY n0 ejeje	jZd	d
 Zdd e d D Zdd Zdd Z dd Z!dd Z"d&ddZ#dd Z$dd Z%d'ddZ&d d! Z'd(d"d#Z(G d$d% d%Z)dS ))    N)import_module)chain)ImproperlyConfigured)Node)render_to_string)	mark_safe)settingsc              
   C   sp   zt | }W n2 ty> } ztd|W Y d }~n6d }~0 0 t|}|dr`tj	|}tj
|S d S )Nz'Error importing HIDE_IN_STACKTRACES: {}z__init__.py)r   ImportErrorr   formatinspectgetsourcefileendswithospathdirnamerealpath)module_namemoduleesource_path r   K/var/www/html/Ranjet/env/lib/python3.9/site-packages/debug_toolbar/utils.pyget_module_path   s    $

r   c                 C   s   g | ]}t |qS r   )r   ).0r   r   r   r   
<listcomp>&   s   r   ZHIDE_IN_STACKTRACESc                    s   t  fddtD S )Nc                 3   s   | ]}  |V  qd S N)
startswith)r   Zhidden_pathr   r   r   	<genexpr>-       zomit_path.<locals>.<genexpr>)anyhidden_pathsr   r   r   r   	omit_path,   s    r"   c                 C   sx   g }dd | D D ]`\}}}}}t tj|r2q|rDd| nd}t d rZ|jnd}|	|||||f q|S )a4  
    Clean up stacktrace and remove all entries that:
    1. Are part of Django (except contrib apps)
    2. Are part of socketserver (used by Django's dev server)
    3. Are the last entry (which is part of our stacktracing code)

    ``stack`` should be a list of frame tuples from ``inspect.stack()``
    c                 s   s   | ]}|d d V  qd S )N   r   )r   fr   r   r   r   :   r   z"tidy_stacktrace.<locals>.<genexpr> ENABLE_STACKTRACES_LOCALSN)
r"   r   r   r   joinstripdt_settings
get_configf_localsappend)stacktraceframer   line_no	func_nametextZframe_localsr   r   r   tidy_stacktrace0   s    	
r3   c              	   C   s   g }| D ]h}dd t |d tjjd|dd  D }dd t|D }z|| W q tyn   Y qY q0 qtt	d|t
 d d	S )
Nc                 s   s   | ]
}|V  qd S r   r   )r   vr   r   r   r   J   r   z$render_stacktrace.<locals>.<genexpr>r      c                 S   s   i | ]\}}t ||qS r   )str)r   idxr4   r   r   r   
<dictcomp>K   r   z%render_stacktrace.<locals>.<dictcomp>z(debug_toolbar/panels/sql_stacktrace.htmlr&   )
stacktraceshow_locals)r   rsplitr   r   sep	enumerater,   KeyErrorr   r   r)   r*   )r.   r9   r/   paramsparams_dictr   r   r   render_stacktraceG   s     ,
rA   c                  C   s   d } t  j}zr|d ur~|jjd}|jjtjk}|r@|r@q~n6|jjdkrv|j	d }|j	d }t
|trvt||} q~|j}qW n ty   Y n0 ~| S )Nz/debug_toolbar/utils.pyrenderselfcontext)sys	_getframef_backf_codeco_filenamer   co_nameget_template_context__name__r+   
isinstancer   	Exception)Ztemplate_infoZ	cur_frameZin_utils_moduleZis_get_template_contextnoderD   r   r   r   get_template_info^   s,    




rP      c                 C   st   t | |\}}}g }td|| }|d | }|D ]6\}	}
||	  krN|kr2n q2||	|
|	|kd q2||dS )Nr5   )numcontent	highlight)namerD   )'get_template_source_from_exception_infomaxr,   )rO   rD   Zcontext_lineslinesource_linesrU   Zdebug_contextstartendline_numrS   r   r   r   rK   z   s    rK   c                 C   s6   |j td| j}|d }|d }|d }|||fS )NZDDTrX   rY   rU   )templateZget_exception_inforN   token)rO   rD   Zexception_inforX   rY   rU   r   r   r   rV      s
    rV   c                 C   s:   t | dr| j}n| jj}t | dr6| j}d||}|S )NrL   
__module__z{}.{})hasattrrL   	__class__r_   r
   )objrU   r   r   r   r   get_name_from_obj   s    

rc   r5   c                    s  t | r| j}| j} n| j}t | s0tdt | pBt | }|dkr|d |d  }zt 	| \}}W n t
y   d } }}Y q0 t|d}tdt|t|| }|dd }||||  }|d | }nd } }}|rNt|d trNd |dd D ].}	td|	}
|
r|
dd  q<q fdd	|D }tt d
rpt ||| jj||S ||| jj||fS dS )a  
    Get information about a frame or traceback object.

    A tuple of five things is returned: the filename, the line number of
    the current line, the function name, a list of lines of context from
    the source code, and the index of the current line within that list.
    The optional second argument specifies the number of lines of context
    to return, which are centered around the current line.

    This originally comes from ``inspect`` but is modified to handle issues
    with ``findsource()``.
    z&arg is not a frame or traceback objectr   r5      Nasciis   coding[:=]\s*([-\w.]+)c                    s   g | ]}|  d qS )replace)decode)r   rX   encodingr   r   r      r   z getframeinfo.<locals>.<listcomp>	Traceback)r   istraceback	tb_linenotb_framef_linenoisframe	TypeErrorr   getfile
findsourcerN   rW   minlenrM   bytesresearchgrouprg   r`   rj   rH   rJ   )r/   rD   linenofilenamerZ   lineslnumZfirst_linesindexrX   matchr   rh   r   getframeinfo   s<    



r   c                    s:   t  tr  fddt D S  fddt D S dS )z?
    Get a sorted list of variables from the request data.
    c                    s   g | ]}|  |fqS r   )getr   kvariabler   r   r      r   z/get_sorted_request_variable.<locals>.<listcomp>c                    s   g | ]}|  |fqS r   )getlistr   r   r   r   r      r   N)rM   dictsortedr   r   r   r   get_sorted_request_variable   s    
r   c                 C   s4   t d}g }|r0||ft||   |j}q|S )a.  
    Get a list of records for a frame and all higher (calling) frames.

    Each record contains a frame object, filename, line number, function
    name, a list of lines of context, and index within the context.

    Modified version of ``inspect.stack()`` which calls our own ``getframeinfo()``
    r5   )rE   rF   r,   r   rG   )rD   r/   	framelistr   r   r   	get_stack   s    	
r   c                   @   s2   e Zd Zdd Zd
ddZdddZddd	ZdS )ThreadCollectorc                 C   s   t d u rtdi | _d S )NzGthreading module is not available, this panel cannot be used without it)	threadingNotImplementedErrorcollections)rC   r   r   r   __init__   s
    zThreadCollector.__init__Nc                 C   s.   |du rt  }|| jvr$g | j|< | j| S )z
        Returns a list of collected items for the provided thread, of if none
        is provided, returns a list for the current thread.
        Nr   currentThreadr   rC   threadr   r   r   get_collection   s
    

zThreadCollector.get_collectionc                 C   s&   |d u rt  }|| jv r"| j|= d S r   r   r   r   r   r   clear_collection  s    
z ThreadCollector.clear_collectionc                 C   s   |  || d S r   )r   r,   )rC   itemr   r   r   r   collect
  s    zThreadCollector.collect)N)N)N)rL   r_   __qualname__r   r   r   r   r   r   r   r   r      s   

r   )rQ   )r5   )r5   )*r   os.pathr   rv   rE   	importlibr   	itertoolsr   djangoZdjango.core.exceptionsr   Zdjango.templater   Zdjango.template.loaderr   Zdjango.utils.safestringr   Zdebug_toolbarr   r)   r   r	   r   r   r   __file__Zdjango_pathr   r*   r!   r"   r3   rA   rP   rK   rV   rc   r   r   r   r   r   r   r   r   <module>   s>   



7

