U
    aP                     @   sb  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m	Z	m
Z
mZ d dl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 d dlmZ d dlmZ edddidZejdej dZ!dZ"ee#j$Z%G dd dZ&dd Z'dd Z(d+ddZ)e * dd Z+dd  Z,G d!d" d"Z-G d#d$ d$e-Z.G d%d& d&Z/d'd( Z0d)d* Z1dS ),    N)Path)settings)Http404HttpResponseHttpResponseNotFound)ContextEngineTemplateDoesNotExist)pprint)resolve)timezone)MultiValueDict)	force_str)import_string)get_docs_versionTZi18nzdjango.templatetags.i18n)debug	librariesz#API|TOKEN|KEY|SECRET|PASS|SIGNATURE)flagsz********************c                   @   s    e Zd ZdZdd Zdd ZdS )CallableSettingWrapperz
    Object to wrap callable appearing in settings.
    * Not to call in the debug page (#21345).
    * Not to break the debug page if the callable forbidding to set attributes
      (#23070).
    c                 C   s
   || _ d S N)_wrapped)selfZcallable_setting r   /home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/django/views/debug.py__init__)   s    zCallableSettingWrapper.__init__c                 C   s
   t | jS r   )reprr   )r   r   r   r   __repr__,   s    zCallableSettingWrapper.__repr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   "   s   r   c                 C   sd   z6t | rt}n"t|tr0dd | D }n|}W n tk
rN   |}Y nX t|r`t|}|S )z
    Cleanse an individual setting key/value of sensitive content. If the value
    is a dictionary, recursively cleanse the keys in that dictionary.
    c                 S   s   i | ]\}}|t ||qS r   )cleanse_setting).0kvr   r   r   
<dictcomp>:   s      z#cleanse_setting.<locals>.<dictcomp>)	HIDDEN_SETTINGSsearchCLEANSED_SUBSTITUTE
isinstancedictitems	TypeErrorcallabler   )keyvaluecleansedr   r   r   r!   0   s    


r!   c                  C   s2   i } t tD ] }| rt|tt|| |< q| S )z{
    Return a dictionary of the settings module with values of sensitive
    settings replaced with stars (*********).
    )dirr   isupperr!   getattr)Zsettings_dictr#   r   r   r   get_safe_settingsH   s
    r4     c                 C   sF   t | |||}|  r,| }t||ddS | }t||ddS dS )z
    Create a technical server error response. The last three arguments are
    the values returned from sys.exc_info() and friends.
    ztext/plain; charset=utf-8)statuscontent_type	text/htmlN)ExceptionReporterZis_ajaxget_traceback_textr   get_traceback_html)requestexc_type	exc_valuetbstatus_codeZreportertexthtmlr   r   r   technical_500_responseT   s    rC   c                   C   s   t tj S r   )r   r   Z!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   %get_default_exception_reporter_filterb   s    rD   c                 C   s   t  }t| d|S )NZexception_reporter_filter)rD   r3   )r<   Zdefault_filterr   r   r   get_exception_reporter_filterh   s    rE   c                   @   s    e Zd ZdZdd Zdd ZdS )ExceptionReporterFilterzv
    Base for all exception reporter filter classes. All overridable hooks
    contain lenient default behaviors.
    c                 C   s   |d kri S |j S d S r   )POSTr   r<   r   r   r   get_post_parameterss   s    z+ExceptionReporterFilter.get_post_parametersc                 C   s   t |j S r   )listf_localsr+   )r   r<   tb_framer   r   r   get_traceback_frame_variablesy   s    z5ExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r   r    rI   rM   r   r   r   r   rF   m   s   rF   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )SafeExceptionReporterFilterz
    Use annotations made by the sensitive_post_parameters and
    sensitive_variables decorators to filter out sensitive information.
    c                 C   s
   t jdkS )a  
        This filter is to add safety in production environments (i.e. DEBUG
        is False). If DEBUG is True then your site is not safe anyway.
        This hook is provided as a convenience to easily activate or
        deactivate the filter on a per request basis.
        F)r   DEBUGrH   r   r   r   	is_active   s    z%SafeExceptionReporterFilter.is_activec                 C   s@   t |dg }| |r<|r<| }|D ]}||kr&t||< q&|S )z
        Replace the keys in a MultiValueDict marked as sensitive with stars.
        This mitigates leaking sensitive POST parameters if something like
        request.POST['nonexistent_key'] throws an exception (#21098).
        sensitive_post_parameters)r3   rP   copyr(   )r   r<   ZmultivaluedictrQ   paramr   r   r   get_cleansed_multivaluedict   s    
z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc                 C   sx   |dkri S t |dg }| |rn|rn|j }|dkrN|D ]}t||< q<|S |D ]}||krRt||< qR|S n|jS dS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        NrQ   __ALL__)r3   rP   rG   rR   r(   )r   r<   rQ   r0   r#   rS   r   r   r   rI      s    


z/SafeExceptionReporterFilter.get_post_parametersc              
   C   sV   zt |t}W n2 tk
r@ } zd|| W Y S d }~X Y nX |rR| ||}|S )Nz{!r} while evaluating {!r})r)   r   	ExceptionformatrT   )r   r<   r/   Zis_multivalue_dicter   r   r   cleanse_special_types   s    "z1SafeExceptionReporterFilter.cleanse_special_typesc           	      C   s  |j }d}|dk	rH|jjdkr@d|jkr@|jd }t|dd}qH|j }q
i }| |r|r|dkrx|jD ]}t||< qhq|j D ]*\}}||krt}n| ||}|||< qn$|j D ]\}}| ||||< q|jjdkrd|jkrt|d< t|d< | S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrappersensitive_variablesrU   	func_argsZfunc_kwargs)	f_backf_codeco_namerK   r3   rP   r(   r+   rY   )	r   r<   rL   current_framerZ   wrapperr0   namer/   r   r   r   rM      s8    

z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)	r   r   r   r    rP   rT   rI   rY   rM   r   r   r   r   rN   }   s   	rN   c                   @   sD   e Zd ZdZdddZdd Zdd Zd	d
 ZdddZdd Z	dS )r9   z0Organize and coordinate reporting on exceptions.Fc                 C   sJ   || _ t| j | _|| _|| _|| _|| _t| jdd | _d| _	d | _
d S )NZtemplate_debugF)r<   rE   filterr=   r>   r?   is_emailr3   template_infotemplate_does_not_exist
postmortem)r   r<   r=   r>   r?   rc   r   r   r   r      s    zExceptionReporter.__init__c                 C   sP  | j r*t| j tr*d| _| jjp&| jg| _|  }t|D ]p\}}d|krg }|d D ]B\}}t	|}t
|dkrd|dd t
|f }|||f qV||d< |||< q:d}| j r0t| j tr0t| jdd}t| jd	d}	|dk	r0|	dk	r0| jjd
 }
t|
t|d dt|	d t
|
 ddd}ddlm} | jdkrNd}n,zt| jj}W n tk
rx   d}Y nX | j||| j|t| j| j t tjdtj dd  t!" | tj#| j$| j| jd}| jdk	r| jj% |d< | jj& |d< | jj' |d< | j r$| j j(|d< | jr:t| j|d< |rL|d |d< |S )z5Return a dictionary containing traceback information.Tvarsi   u   %s… <trimmed %d bytes string>r    startNend      asciireplace)errors)get_versionz%[unable to retrieve the current user]z%d.%d.%d   )rc   unicode_hintframesr<   user_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoZserver_timeZdjango_version_infosys_pathrd   re   rf   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_COOKIES_itemsZexception_typeZexception_valueZ	lastframe))r=   
issubclassr	   re   r>   chainrf   get_traceback_frames	enumerater
   lenappendUnicodeErrorr3   argsr   maxmindjangorp   r<   struserrV   rc   rJ   rb   rI   r+   r4   sys
executableversion_infor   nowpathrd   GETZFILESZCOOKIESr   )r   rs   iframeZ
frame_varsr#   r$   rr   ri   rj   Zunicode_strrp   rt   cr   r   r   get_traceback_data  sv    
" 
z$ExceptionReporter.get_traceback_datac              	   C   sH   t tddjdd}t| }W 5 Q R X t|  dd}||S )z1Return HTML version of debug 500 HTTP error page.	templatesztechnical_500.htmlutf-8encodingF)use_l10n	r   CURRENT_DIRopenDEBUG_ENGINEfrom_stringreadr   r   renderr   fhtr   r   r   r   r;   I  s    z$ExceptionReporter.get_traceback_htmlc              	   C   sJ   t tddjdd}t| }W 5 Q R X t|  ddd}||S )z7Return plain text version of debug 500 HTTP error page.r   ztechnical_500.txtr   r   F)Z
autoescaper   r   r   r   r   r   r:   P  s    z$ExceptionReporter.get_traceback_textNc              	      s:  d}t |drBz||}W n tk
r0   Y nX |dk	rB| }|dkrz&t|d}|  }W 5 Q R X W n tk
r   Y nX |dkrdg dg fS t|d trd |dd D ](}t	
d|}	|	r|	dd  qq fd	d
|D }td|| }
|| }||
| }|| }||d | }|
|||fS )z
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        N
get_sourcerbr   rm      s   coding[:=]\s*([-\w.]+)rk   c                    s   g | ]}t | d qS )rn   )r   )r"   sliner   r   r   
<listcomp>y  s     z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)hasattrr   ImportError
splitlinesr   r   OSErrorr)   bytesrer'   groupdecoder   )r   filenamelinenoZcontext_linesloadermodule_namesourcefplinematchZlower_boundZupper_boundpre_contextcontext_linepost_contextr   r   r   _get_lines_from_fileW  s<    
z&ExceptionReporter._get_lines_from_filec                 C   sn  dd }g }| j }|r4|| ||}||krq4qg }|s@|S | }|sR| jn|j}|d k	rj|jjdrx|j}qX|jj	j
}|jj	j}|jd }|jjd}	|jjdpd}
| ||d|	|
\}}}}|d kr|}g }d	}g }|||t|d
d||
drdnd|||d | j| j|jt|||||d d |jsb|rb| }|j}qX|j}qX|S )Nc                 S   s    t | dd }t | dd }|p|S )N	__cause____context__)r3   )r>   explicitZimplicitr   r   r   explicit_or_implicit_cause  s    zJExceptionReporter.get_traceback_frames.<locals>.explicit_or_implicit_causeZ__traceback_hide__rk   
__loader__r   rh      z<source code not available>r   Tzdjango.r   r   )Z	exc_causeZexc_cause_explicitr?   typer   functionr   rg   idr   r   r   pre_context_lineno)r>   r|   popr?   __traceback__rL   rK   gettb_nextr]   co_filenamer^   	tb_lineno	f_globalsr   r3   
startswithrb   rM   r<   r   )r   r   
exceptionsr>   rs   r?   r   r   r   r   r   r   r   r   r   r   r   r   ry     sl    




    
z&ExceptionReporter.get_traceback_frames)F)NN)
r   r   r   r    r   r   r;   r:   r   ry   r   r   r   r   r9      s   
G
-r9   c              
   C   s  z|j d d }W n( tttfk
r:   | jdd }Y nX z|j d d }W n tttfk
rl   g }Y nlX |r| jdkrt|dkrt|d dkrt|d d ddt|d d d	d  krd
krn nt| S t| dt	j
}t|tjr|j}d}zt| j}W n tk
r    Y n\X |j}t|dr<|j}n"t|dr^t|jdr^|jj}t|dr||j}d||f }ttddjdd}	t|	 }
W 5 Q R X t|t	j
||t|| t |d}t|
|ddS )zBCreate a technical 404 error response. `exception` is the Http404.r   r   rk   Ntried/Zapp_namerh   	namespaceZadminurlconfr   	__class__r   z%s.%sr   ztechnical_404.htmlr   r   )r   Zroot_urlconfrequest_pathZurlpatternsreasonr<   r   Zraising_view_namer8   r7   ) r~   
IndexErrorr,   KeyErrorZ	path_infor   r{   r3   default_urlconfr   ZROOT_URLCONFr)   types
ModuleTyper   r   r   funcr   r   r   r   r   r   r   r   r   r   r   r4   r   r   )r<   	exceptionZ	error_urlr   r   ZcallerZresolver_matchobjmoduler   r   r   r   r   r   technical_404_response  sd    

 

r   c              	   C   sN   t tddjdd}t| }W 5 Q R X tdt i}t|	|ddS )z+Create an empty URLconf 404 error response.r   zdefault_urlconf.htmlr   r   versionr8   r   )
r   r   r   r   r   r   r   r   r   r   )r<   r   r   r   r   r   r   r     s     r   )r5   )2	functoolsr   r   r   pathlibr   Zdjango.confr   Zdjango.httpr   r   r   Zdjango.templater   r   r	   Zdjango.template.defaultfiltersr
   Zdjango.urlsr   Zdjango.utilsr   Zdjango.utils.datastructuresr   Zdjango.utils.encodingr   Zdjango.utils.module_loadingr   Zdjango.utils.versionr   r   compile
IGNORECASEr&   r(   __file__parentr   r   r!   r4   rC   	lru_cacherD   rE   rF   rN   r9   r   r   r   r   r   r   <module>   sD   


w X8