a
    Ig1                     @   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mZ d dl	m
Z
mZmZmZmZmZm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mZ d d	lmZmZ  e Z!e
eee"  e#d
ddZ$ddddZ%eej& ej'dddZ(ej'edddZ)eee"e
f  dddZ*d7eej+e,ee"e
f dddZ-eej+ee,eee,e"f  e"f dddZ.e
e"dd d!Z/d8e
e,e j0d#d$d%Z1eee"e
f ef ee"eeee"e
f  e
f f d&d'd(Z2d9eej& dd)d*Z3d d+d,d-Z4G d.d/ d/Z5d d+d0d1Z6d2d3 Z7d4Z8d5d6 Z9dS ):    N)PrettyPrinterpformat)AnyDictListOptionalSequenceTupleUnion)Local)	QueryDict)Node)format_html)
SafeString	mark_safe)_stubssettings)frameexcluded_modulesreturnc                    s8   |sdS | j d t ts"dS t fdd|D S )NF__name__c                 3   s$   | ]} |kp  |d  V  qdS ).N)
startswith).0Zexcluded_moduleZframe_module O/var/www/html/django/DPS/env/lib/python3.9/site-packages/debug_toolbar/utils.py	<genexpr>   s   z%_is_excluded_frame.<locals>.<genexpr>)	f_globalsget
isinstancestrany)r   r   r   r   r   _is_excluded_frame   s    
r#   )r   c                   C   s   t jdtdd d S )NzNget_stack() and tidy_stacktrace() are deprecated in favor of get_stack_trace()   )
stacklevel)warningswarnDeprecationWarningr   r   r   r    _stack_trace_deprecation_warning!   s
    r)   )stackr   c           	      C   s   t   g }t d }dd | D D ]^\}}}}}t||r>q$|rPd| nd}t d rjt|jnd}||||||f q$|S )z
    Clean up stacktrace and remove all entries that are excluded by the
    HIDE_IN_STACKTRACES setting.

    ``stack`` should be a list of frame tuples from ``inspect.stack()`` or
    ``debug_toolbar.utils.get_stack()``.
    HIDE_IN_STACKTRACESc                 s   s   | ]}|d d V  qd S )N   r   )r   fr   r   r   r   6       z"tidy_stacktrace.<locals>.<genexpr> ENABLE_STACKTRACES_LOCALSN)	r)   dt_settings
get_configr#   joinstripr   f_localsappend)	r*   tracer   r   pathline_no	func_nametextframe_localsr   r   r   tidy_stacktrace*   s    

r=   )r7   r   c           
   	   C   s   t  d }d}| D ]t\}}}}}tjj|v rP|tjjd\}}	|tjj7 }nd}|}	|td||	|||7 }|r|td|7 }|d7 }qt|S )Nr0   r/      z<span class="djdt-path">{}</span><span class="djdt-file">{}</span> in <span class="djdt-func">{}</span>(<span class="djdt-lineno">{}</span>)
  <span class="djdt-code">{}</span>
z$  <pre class="djdt-locals">{}</pre>

)r1   r2   osr8   seprsplitr   r   )
r7   show_localshtmlabspathlinenofunccodeZlocals_	directoryfilenamer   r   r   render_stacktraceC   s.    
rK   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_filenameendswithco_nameget_template_contextr   r5   r    r   	Exception)Ztemplate_infoZ	cur_frameZin_utils_moduleZis_get_template_contextnoderN   r   r   r   get_template_infof   s,    




rY      )rX   rN   context_linesr   c                 C   st   t | |\}}}g }td|| }|d | }|D ]6\}	}
||	  krN|kr2n q2||	|
|	|kd q2||dS )Nr>   )numcontent	highlight)namerN   )'get_template_source_from_exception_infomaxr6   )rX   rN   r[   linesource_linesr_   Zdebug_contextstartendline_numr]   r   r   r   rV      s    rV   )rX   rN   r   c                 C   s\   |j j| jkr$|j td| j}n|jj td| j}|d }|d }|d }|||fS )NZDDTrb   rc   r_   )templateoriginZget_exception_inforW   tokenZrender_context)rX   rN   Zexception_inforb   rc   r_   r   r   r   r`      s    

r`   )objr   c                 C   sD   t | dr"| j}|j d|j S | j}t| d| jj}|d | S )z3Get the best name as `str` from a view or a object.
view_classr   __qualname__)hasattrrk   
__module__rl   getattr	__class__r   )rj   klassmod_nameZ	view_namer   r   r   get_name_from_obj   s    
rs   r>   )r   rN   r   c                 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 | }nd }}t ||| jj||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   r>   r$   N)inspectistraceback	tb_linenotb_framef_linenoisframe	TypeErrorgetsourcefilegetfile
findsourcerW   ra   minlen	TracebackrR   rU   )r   rN   rF   rJ   rd   lineslnumindexr   r   r   getframeinfo   s&    


r   )variabler   c                    sf   zFt  tr(d fddt D iW S d fddt D iW S W n ty`   d i Y S 0 dS )z`
    Get a data structure for showing a sorted list of variables from the
    request data.
    listc                    s   g | ]}|  |fqS r   )r   r   kr   r   r   
<listcomp>   r.   z/get_sorted_request_variable.<locals>.<listcomp>c                    s   g | ]}|  |fqS r   )getlistr   r   r   r   r      r.   rawN)r    dictsortedrz   r   r   r   r   get_sorted_request_variable   s    
 r   c                 C   s:   t   td}g }|r6||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()``
    r>   )r)   rO   rP   r6   r   rQ   )rN   r   	framelistr   r   r   	get_stack   s    	
r   skipc                 c   s<   | d7 } t  }|d ur8| dkr*| d8 } n|V  |j}qd S )Nr>   r   )rt   currentframerQ   )r   r   r   r   r   _stack_frames   s    
r   c                   @   sF   e Zd Ze Zdd Zdd Zddddeee	  e
edd	d
ZdS )_StackTraceRecorderc                 C   s
   i | _ d S )N)filename_cache)rM   r   r   r   __init__  s    z_StackTraceRecorder.__init__c                 C   s\   |j j}| j|}|d u rXt|}|d u r8d}|}nd}t| ||f}|| j|< |S )NFT)rR   rS   r   r   rt   r{   	linecache
checkcache)rM   r   frame_filenamevaluerJ   	is_sourcer   r   r   get_source_file  s    


z#_StackTraceRecorder.get_source_fileNFr   r   include_localsr   c                C   s   g }|d7 }t |dD ]}t||r&q| |\}}|j}|jj}	|rxt||}
|
d ur`|
jnd }t	
||| }nd}|r| j|j}nd }||||	||f q|  |S )Nr>   r   r/   )r   r#   r   rx   rR   rU   rt   	getmodule__dict__r   getliner4   pretty_printerr   r5   r6   reverse)rM   r   r   r   r7   r   rJ   r   r9   r:   modulemodule_globalsZsource_liner<   r   r   r   get_stack_trace  s*    

z#_StackTraceRecorder.get_stack_trace)r   rn   rl   r   r   r   r   r   r   r!   boolintr   r   r   r   r   r     s   
r   c                 C   sT   t  }|d sg S | d7 } ttdd}|du r<t }|t_|j|d |d | dS )a  
    Return a processed stack trace for the current call stack.

    If the ``ENABLE_STACKTRACES`` setting is False, return an empty :class:`list`.
    Otherwise return a :class:`list` of processed stack frame tuples (file name, line
    number, function name, source line, frame locals) for the current call stack.  The
    first entry in the list will be for the bottom of the stack and the last entry will
    be for the top of the stack.

    ``skip`` is an :class:`int` indicating the number of stack frames above the frame
    for this function to omit from the stack trace.  The default value of ``0`` means
    that the entry for the caller of this function will be the last entry in the
    returned stack trace.
    ZENABLE_STACKTRACESr>   stack_trace_recorderNr+   r0   r   )r1   r2   ro   _local_datar   r   r   )r   configr   r   r   r   r   C  s    r   c                   C   s   t tdrt`d S )Nr   )rm   r   r   r   r   r   r   clear_stack_trace_cachesa  s    
r   )z	text/htmlzapplication/xhtml+xmlc                 C   s@   |  dd}|  dddd }t| dd o>|dko>|tv S )NzContent-Encodingr/   zContent-Type;r   Z	streamingF)r   splitro   _HTML_TYPES)responsecontent_encodingcontent_typer   r   r   is_processable_html_responsei  s    r   )rZ   )r>   )r>   ):rt   r   os.pathr@   rO   r&   pprintr   r   typingr   r   r   r   r   r	   r
   Zasgiref.localr   Zdjango.httpr   Zdjango.templater   Zdjango.utils.htmlr   Zdjango.utils.safestringr   r   Zdebug_toolbarr   Zstubsr   r1   r   r!   r   r#   r)   ZInspectStackZTidyStackTracer=   rK   rY   ZRequestContextr   rV   r`   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sL   $	# 
(>