U
    nuf<!                     @   s   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 d dl	m
Z
mZmZmZmZmZmZmZmZ d dlmZ G dd dZdS )    N)	FrameTypeTracebackType)UnionIterable)	style_with_executing_nodeOptionsLine	FrameInfoLINE_GAPVariableRepeatedFramesBlankLineRange
BlankLines)assert_c                   @   sD  e Zd Zddddddddddd	d
ddddddddddZdd Zd4ddddZd5ddddZddddZd6ee	 dddZ
d7ee	 dddZeeeef  ee	 dddZee	dd d!Zeeeef ee	 d"d#d$Zee	d%d&d'Zee	d(d)d*Zd+d, Zeee	 d%d-d.Zee	d/d0d1Ze	dd2d3ZdS )8	FormatterNFTZmonokaiz
bg:#005080^z-->z(...):z{:4} | )options	pygmentedshow_executing_nodepygments_formatter_clspygments_formatter_kwargspygments_styleexecuting_node_modifierexecuting_node_underlinecurrent_line_indicatorline_gap_stringline_number_gap_stringline_number_format_stringshow_variablesuse_code_qualnameshow_linenosstrip_leading_indenthtmlchaincollapse_repeated_framesc                C   s   |d krt  }|rR|jsR|r&t||}|d kr:ddlm} |f d|i|pJi |_|| _|| _tt|dkt	d || _
|	pd| _|
| _|| _|| _|| _|| _|| _|| _|| _|| _|| _|| _| js| jjtjkrt	dd S )Nr   )Terminal256Formatterstyle   z3executing_node_underline must be a single character z@BlankLines.SINGLE option can only be used when show_linenos=True)r   Zpygments_formatterr   Zpygments.formatters.terminal256r&   r   r   r   len
ValueErrorr   r   r   r   r   r   r!   r    r"   r#   r$   r   r%   Zblank_linesr   ZSINGLE)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%    r-   9/tmp/pip-unpacked-wheel-8ocghofw/stack_data/formatting.py__init__   sL    
 

zFormatter.__init__c                    s    fdd}|t _d S )Nc                    s     | d S N)print_exception)Z_etypeZevalueZ_tbr,   r-   r.   
excepthookN   s    z&Formatter.set_hook.<locals>.excepthook)sysr3   )r,   r3   r-   r2   r.   set_hookM   s    zFormatter.set_hookfilec                C   s   | j | ||d d S Nr6   )print_linesformat_exception)r,   er7   r-   r-   r.   r1   S   s    zFormatter.print_exceptionc                C   s*   |d krt  j}| j| ||d d S r8   )inspectcurrentframef_backr9   format_stack)r,   frame_or_tbr7   r-   r-   r.   print_stackV   s    
zFormatter.print_stackc                C   s*   |d krt j}|D ]}t||dd qd S )Nr)   )r7   end)r4   stderrprint)r,   linesr7   liner-   r-   r.   r9   \   s    zFormatter.print_lines)returnc                 c   s   |d krt  d }| jrj|jd k	r@| |jE d H  tjV  n*|jd k	rj|jsj| |jE d H  tj	V  dV  | 
|jE d H  tt||E d H  d S )Nr(   z#Traceback (most recent call last):
)r4   exc_infor$   	__cause__r:   	traceback_cause_message__context____suppress_context___context_messager?   __traceback__format_exception_onlytype)r,   r;   r-   r-   r.   r:   b   s    


zFormatter.format_exceptionc                 c   s6   |d krt  j}| tj|| j| jdE d H  d S )N)r%   )r<   r=   r>   format_stack_datar	   
stack_datar   r%   )r,   r@   r-   r-   r.   r?   s   s    
zFormatter.format_stack)stackrG   c                 c   s6   |D ],}t |tr$| |E d H  q| |V  qd S r0   )
isinstancer	   format_frameformat_repeated_frames)r,   rT   itemr-   r-   r.   rR      s    
zFormatter.format_stack_data)repeated_framesrG   c                 C   s   d |jS )Nz&    [... skipping similar frames: {}]
)formatdescription)r,   rY   r-   r-   r.   rW      s    z Formatter.format_repeated_frames)framerG   c                 c   s   t |tst|| j}| |V  |jD ]L}t |trD| |V  q(t |tr\| |V  q(t	|t
k | jd V  q(| jrz| |E d H  W n tk
r   Y nX d S )N
)rU   r	   r   format_frame_headerrE   r   format_liner   format_blank_lines_linenumbersr   r
   r   r   format_variables	Exception)r,   r\   rF   r-   r-   r.   rV      s    



zFormatter.format_frame)
frame_inforG   c                 C   s"   dj || jr|j n|jjdS )NzC File "{frame_info.filename}", line {frame_info.lineno}, in {name}
)rc   name)rZ   r    Z	executingZcode_qualnamecodeco_name)r,   rc   r-   r-   r.   r^      s    zFormatter.format_frame_header)rF   rG   c                 C   s   d}| j r0|jr| j }ndt| j  }|d7 }nd}| jrL|| j|j7 }|}||j| j| j	| j
dd 7 }| jr| js|jD ]J}|j|j }|j|j }||kr|d|t|  | j||   d 7 }q|S )Nr)       )r   Zescape_htmlr"   r]   )r   Z
is_currentr*   r!   r   rZ   linenorenderr   r#   r"   r   Zexecuting_node_rangesstartZleading_indentrB   r   )r,   rF   resultprefixZ
line_rangerk   rB   r-   r-   r.   r_      s<    

zFormatter.format_linec                 C   sP   | j rdt| j  d }nd}|j|jkr@|| j|j d S |d| j S )Nrg   rh   r]   z   {}
)r   r*   Zbegin_linenoZ
end_linenor   rZ   r   )r,   Z
blank_linerl   r-   r-   r.   r`      s    z(Formatter.format_blank_lines_linenumbersc              	   c   sF   t |jdd dD ].}z| |d V  W q tk
r>   Y qX qd S )Nc                 S   s   | j S r0   )rd   )vr-   r-   r.   <lambda>       z,Formatter.format_variables.<locals>.<lambda>)keyr]   )sorted	variablesformat_variablerb   )r,   rc   varr-   r-   r.   ra      s
    zFormatter.format_variables)ru   rG   c                 C   s   d |j| |jS )Nz{} = {})rZ   rd   format_variable_valuevalue)r,   ru   r-   r-   r.   rt      s    
zFormatter.format_variablec                 C   s   t |S r0   )repr)r,   rw   r-   r-   r.   rv      s    zFormatter.format_variable_value)N)N)N)N)__name__
__module____qualname__r/   r5   r1   rA   r9   r   strr:   r?   r   r	   r   rR   rW   r   r   rV   r^   r   r_   r`   ra   r   rt   rv   r-   r-   r-   r.   r      sJ   @	
&
r   )r<   r4   rJ   typesr   r   typingr   r   rS   r   r   r   r	   r
   r   r   r   r   Zstack_data.utilsr   r   r-   r-   r-   r.   <module>   s   ,