B
    `/;                 @   s\  d 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	Z	ddl
mZ ddlZddlmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZ ed	ejZed
ejejB ZedejZ dZ!e"e#fZ$ye$e%f7 Z$W n e&k
r   Y nX edZ'edZ(edZ)dddZ*dddZ+G dd de,Z-G dd de,Z.G dd de,Z/dS )z
    werkzeug.debug.tbtools
    ~~~~~~~~~~~~~~~~~~~~~~

    This module provides various traceback related utility functions.

    :copyright: (c) 2011 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD.
    N)reify)render)
TokenError)exec_native_string_typestext_	text_typexrange_)Console)EXC_ROUTE_NAMEROOT_ROUTE_NAMESTATIC_PATHescapes   coding[:=]  # All encoding definitions end with 'coding'. See PEP 263
    \s*              # Not interested in whitespaces
    ([-\w.]+)        # The encoding we need
    s   ^(.*?)$ # an entire linez^(\s*def\s)         # The start of a function is either 'def'
    |                       # or
    (.*(?<!\w)lambda(:|\s)) # it's a lambda
    |                       # or
    ^(\s*@)s   ﻿z<div class="frame" id="frame-%(id)s">
  <h4>File <cite class="filename">"%(filename)s"</cite>,
      line <em class="line">%(lineno)s</em>,
      in <code class="function">%(function_name)s</code></h4>
  <pre>%(current_line)s</pre>
</div>
z<table class=source>%s</table>zV<tr class="%(classes)s">
  <td class=lineno>%(lineno)s</td>
  <td>%(code)s</td>
</tr>
Fc             C   s   t  }t|| ||dS )a  Get the current exception info as `Traceback` object.  Per default
    calling this method will reraise system exceptions such as generator exit,
    system exit or others.  This behavior can be disabled by passing `False`
    to the function as first parameter.
    )ignore_system_exceptionsshow_hidden_framesskip)sysexc_infoget_traceback)r   r   r   info r   d/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid_debugtoolbar/tbtools.pyget_current_tracebackX   s    r   c             C   sX   | \}}}|r|t kr x"t|D ]}|jd kr2P |j}q"W t|||}|sT|  |S )N)system_exceptionsr
   tb_next	Tracebackfilter_hidden_frames)r   r   r   r   exc_type	exc_valuetbxr   r   r   r   i   s    


r   c               @   s4   e Zd ZdZdZdd Zdd ZeeZdd Zd	S )
LinezHelper for the source renderer.)linenocodein_framecurrentc             C   s   || _ || _d| _d| _d S )NF)r#   r$   r%   r&   )selfr#   r$   r   r   r   __init__~   s    zLine.__init__c             C   s*   dg}| j r|d | jr&|d |S )Nlinezin-framer&   )r%   appendr&   )r'   rvr   r   r   classes   s    

zLine.classesc             C   s$   t td| j| jt| jd S )N )r,   r#   r$   )SOURCE_LINE_HTMLr   joinr,   r#   r   r$   )r'   r   r   r   r      s    zLine.renderN)	__name__
__module____qualname____doc__	__slots__r(   r,   propertyr   r   r   r   r   r"   y   s   r"   c               @   s   e Zd ZdZdd Zdd Zdd ZeeZdd	 ZeeZdddZ	dd Z
dddZdddZdd Zedd Zedd Zd
S )r   zWraps a traceback.c             C   sn   || _ || _t|ts6|j}|jdkr:|jd | }n|}|| _g | _x"|rh| jt	||| |j
}qHW d S )N)__builtin__
exceptions.)r   r   
isinstancestrr0   r1   exception_typeframesr*   Framer   )r'   r   r   r    r;   r   r   r   r(      s    

zTraceback.__init__c             C   s   | j s
dS g }d}xt| j D ]j}|j}|dkr@g }d}|dkrzqn:|dkrXd}|dkrzqn"|dkrpd}|d	krzqn
|s|rzq|| qW t|d
kr| j d jdkr| j dd= n| j d |kr|| j dd< dS )z.Remove the frames according to the paste spec.NF)beforebefore_and_thisr?   )resetreset_and_thisrA   )afterafter_and_thisTrC      r   Zcodeop)r<   hider*   lenmodule)r'   Z
new_frameshiddenframerF   r   r   r   r      s4    zTraceback.filter_hidden_framesc             C   s   t | jtS )zIs it a syntax error?)r9   r   SyntaxError)r'   r   r   r   is_syntax_error   s    zTraceback.is_syntax_errorc             C   s&   t | j| j}td| ddS )z'String representation of the exception. zutf-8replace)	tracebackformat_exception_onlyr   r   r   r/   strip)r'   bufr   r   r   	exception   s    zTraceback.exceptionNc             C   s2   |dkrt j}| jdd d }|| dS )z+Log the ASCII traceback into a file object.Nzutf-8rN   
)r   stderr	plaintextencoderstripwrite)r'   logfiler    r   r   r   log   s    zTraceback.logc             C   s(   ddl m} |d| }|jd| jS )z'Create a paste and return the paste id.r   )ServerProxyz	%sxmlrpc/Zpytb)	xmlrpclibr\   ZpastesZnewPasterV   )r'   lodgeit_urlr\   Zsrvr   r   r   paste   s    zTraceback.pasteTc       	      C   s   d}g }dg}| j s|d |r:| jr2td}ntd}xD| j D ]:}|td|jrhtdt|j ntd| f  qBW | jrtd}ntd	}td
||rtd| ntdtd||t| j d}td||dS )z1Render the traceback for the interactive console.rM   rO   znoframe-tracebackzSyntax Errorz0Traceback <small>(most recent call last)</small>z<li%s>%sz title="%s"z<pre class=syntaxerror>%s</pre>z<blockquote>%s</blockquote>r-   z<h3 class="traceback">%s</h3>rT   )r,   titler<   descriptionz8pyramid_debugtoolbar:templates/exception_summary.dbtmako)request)	r<   r*   rL   r   r   r   r   r/   rS   )	r'   include_titlerb   r`   r<   r,   rJ   Zdescription_wrappervarsr   r   r   render_summary   s8    


zTraceback.render_summaryc             C   s   | t}|t}t| j}| jd|d}|jjj	}|jt
|jd}|jjj}	|	rXdpZddt|||t| j|| jtdd| j| j||||j|d}
td	|
|d
S )z2Render the Full HTML page with the traceback info.F)rc   rb   )
request_idtruefalsez-{2,}-)evalexconsoler^   r`   rS   r;   summaryrV   Zplaintext_csZtraceback_idstatic_path	root_path
pdtb_tokenrf   urlz0pyramid_debugtoolbar:templates/exception.dbtmako)rb   )Z
static_urlr   Z	route_urlr   r   rS   re   registryZparent_registryro   r   Zpdtb_idZpdtb_eval_excr;   rV   resubidr   )r'   rb   r^   rm   rn   excrl   tokenrp   rj   rd   r   r   r   render_full  s4    





zTraceback.render_fullc             c   sZ   t dV  x<| jD ]2}t d|j|j|jf V  t d|j  V  qW t | jdV  dS )z4Like the plaintext attribute but returns a generatorz"Traceback (most recent call last):z  File "%s", line %s, in %sz    zutf-8N)r   r<   filenamer#   function_namecurrent_linerQ   rS   )r'   rJ   r   r   r   generate_plaintext_traceback9  s    
z&Traceback.generate_plaintext_tracebackc             C   s   t d|  S )NrT   )r   r/   r{   )r'   r   r   r   rV   D  s    zTraceback.plaintextc             C   s   t t| S )N)r:   rt   )r!   r   r   r   <lambda>H      zTraceback.<lambda>)N)TN)N)r0   r1   r2   r3   r(   r   rL   r5   rS   r[   r_   re   rw   r{   r   rV   rt   r   r   r   r   r      s   #

0
 r   c               @   sj   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZe	dd Z
edd Ze	dd Zedd ZdS )r=   zA single frame in a traceback.c             C   s   |j | _|jjj| _|jj| _|jj| _	t
|p:t
|}|dd  dkrX|d d }tj|rptj|}|| _| j	d| _| j	d| _|jj| _| jdd| _| jd}|d k	rt|d	d
}|| _d S )N)z.pyoz.pycrE   r0   
__loader__Z__traceback_hide__FZ__traceback_info__rN   )errors)	tb_linenor#   tb_framef_codeco_namery   f_localslocals	f_globalsglobalsinspectgetsourcefilegetfileospathisfilerealpathrx   getrH   loaderr$   rF   r   r   )r'   r   r   r    fnr   r   r   r   r(   N  s$    


zFrame.__init__c             C   s.   t | jt| j| jt| jt| j d S )z%Render a single frame in a traceback.)rt   rx   r#   ry   rz   )
FRAME_HTMLrt   r   rx   r#   ry   rz   rQ   )r'   r   r   r   r   f  s    zFrame.renderc             C   s   dd t | jD }t| jdr| jjd }x&|dkrRt|| jrHP |d8 }q.W y$tt	dd ||d D }W n t
k
r   d}Y nX x||||  D ]
}d|_qW yd|| jd  _W n tk
r   Y nX |S )	z:Helper function that returns lines with extra information.c             S   s   g | ]\}}t |d  |qS )rD   )r"   ).0idxr!   r   r   r   
<listcomp>r  s    z-Frame.get_annotated_lines.<locals>.<listcomp>co_firstlinenorD   r   c             S   s   g | ]}|j d  qS )rT   )r$   )r   r!   r   r   r   r   }  s    NT)	enumeratesourcelineshasattrr$   r   _funcdef_rematchrG   r   getblockr   r%   r#   r&   
IndexError)r'   linesr#   offsetr)   r   r   r   get_annotated_linesp  s&    
"

zFrame.get_annotated_linesc             C   s    t tddd |  D  S )zRender the sourcecode.rT   c             s   s   | ]}|  V  qd S )N)r   )r   r)   r   r   r   	<genexpr>  s    z&Frame.render_source.<locals>.<genexpr>)SOURCE_TABLE_HTMLr   r/   r   )r'   r   r   r   render_source  s    zFrame.render_sourcesinglec             C   sZ   t |tr.t |tr"t|d }t|d|}|dkrFt|| j| jS t	|| j| j dS )z*Evaluate code in the context of the frame.zutf-8z<interactive>execN)
r9   r   r	   UTF8_COOKIErW   compileevalr   r   r   )r'   r$   moder   r   r   r     s    

z
Frame.evalc             C   sV  d}| j dk	r^y:t| j dr,| j | j}nt| j drF| j | j}W n tk
r\   Y nX |dkryt| jd}W n t	k
r   g S X z|
 }W d|  X t|tr| S d}|tr|dd }nJxHtt|D ]6\}}t| }|dk	r|d}P |dkrP qW yt| W n tk
rD   d}Y nX ||d S )	z6The sourcecode of the file as list of unicode strings.N
get_sourceget_source_by_coderbzutf-8   rD   rN   )r   r   r   rH   r   r$   	Exceptionopenrx   IOErrorreadcloser9   r	   
splitlines
startswithr   r   _line_refinditer
_coding_researchgroupcodecslookupLookupErrordecode)r'   sourcefcharsetr   r   r   r   r   r     sD    






zFrame.sourcelinesc             C   s.   y| j | jd  S  tk
r(   tdS X d S )NrD   rM   )r   r#   r   r   )r'   r   r   r   rz     s    zFrame.current_linec             C   s   t | j| jS )N)r   r   r   )r'   r   r   r   rk     s    zFrame.consolec             C   s   t t| S )N)r:   rt   )r!   r   r   r   r|     r}   zFrame.<lambda>N)r   )r0   r1   r2   r3   r(   r   r   r   r   r   r   r5   rz   rk   rt   r   r   r   r   r=   K  s   


4r=   )FFr   )FFr   )0r3   r   r   r   Zpyramid.decoratorr   Zpyramid.renderersr   rr   r   tokenizer   rO   Zpyramid_debugtoolbar.compatr   r   r   r   r	   r
   Zpyramid_debugtoolbar.consoler   Zpyramid_debugtoolbar.utilsr   r   r   r   r   VERBOSEr   	MULTILINEr   r   r   
SystemExitKeyboardInterruptr   GeneratorExit	NameErrorr   r   r.   r   r   objectr"   r   r=   r   r   r   r   <module>
   sJ    	

 6