U
    nufD                     @   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	m
Z
mZ d dlmZmZmZ d dlmZmZmZmZmZmZ d dlmZ eeZG dd	 d	ZdS )
    N)Counter)escape)	FrameTypeTracebackType)UnionIterableList)style_with_executing_nodeOptionsLine	FrameInfoVariableRepeatedFrames)some_strc                   @   s  e Zd ZddddddddddddddddZd+ee d	d
dZeedddZd,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eef edddZdd ZeedddZee
e dd d!Zeed"d#d$Zd%d& Zed	d'd(Zeedd)d*ZdS )-
SerializerNFTZmonokaiz
bg:#005080)options	pygmentedshow_executing_nodepygments_formatter_clspygments_formatter_kwargspygments_styleexecuting_node_modifieruse_code_qualnamestrip_leading_indenthtmlchaincollapse_repeated_framesshow_variablesc                C   s   |d krt  }|rd|jsd|r&t||}|d krL|
r@ddlm} nddlm} |f d|i|p\i |_|| _|| _|	| _	|
| _
|| _|| _|| _|| _d S )Nr   )HtmlFormatter)Terminal256Formatterstyle)r
   Zpygments_formatterr	   Zpygments.formatters.htmlr   Zpygments.formatters.terminal256r   r   r   r   r   r   r   r   r   )selfr   r   r   r   r   r   r   r   r   r   r   r   r    r"   :/tmp/pip-unpacked-wheel-8ocghofw/stack_data/serializing.py__init__   s2    
 zSerializer.__init__)returnc                 C   s   |d krt  d }g }| jrv|jd k	rH| |j}tj |d d< n.|jd k	rv|j	sv| |j}tj
 |d d< || | |S )N   tail)sysexc_infor   	__cause__format_exception	traceback_cause_messagestrip__context____suppress_context___context_messageappendformat_traceback_part)r!   eresultr"   r"   r#   r,   J   s    
zSerializer.format_exception)r5   r%   c                 C   s4   t | |jpt d t t|jt|dddS )N   )typemessage )frames	exceptionr(   )dictformat_stack__traceback__r)   r*   r8   __name__r   )r!   r5   r"   r"   r#   r4   [   s    z Serializer.format_traceback_partc                 C   s0   |d krt  j}t| tj|| j| jdS )N)r   )	inspectcurrentframef_backlistformat_stack_datar   
stack_datar   r   )r!   Zframe_or_tbr"   r"   r#   r>   e   s    
zSerializer.format_stack)stackr%   c                 c   sZ   |D ]P}t |tr:| |sqtf ddi| |V  qtf ddi| |V  qd S )Nr8   framerepeated_frames)
isinstancer   should_include_framer=   format_frameformat_repeated_frames)r!   rG   itemr"   r"   r#   rE   s   s    

zSerializer.format_stack_data)rI   r%   c                 C   s.   t t|j dd d}tdd |D dS )Nc                 S   s   | d  | d d j fS )Nr&   r   )co_name)rN   r"   r"   r#   <lambda>       z3Serializer.format_repeated_frames.<locals>.<lambda>keyc                 S   s$   g | ]\\}}}t |j||d qS ))namelinenocount)r=   rO   ).0coderU   rV   r"   r"   r#   
<listcomp>   s   
z5Serializer.format_repeated_frames.<locals>.<listcomp>)r;   )sortedr   Z
frame_keysitemsr=   )r!   rI   countsr"   r"   r#   rM   ~   s    z!Serializer.format_repeated_frames)rH   r%   c                 C   sf   t |tst|| j}t| jr(|j n|jj|j	|j
t| |jd}| jrbt| ||d< |S )N)rT   filenamerU   lines	variables)rJ   r   r   r=   r   Z	executingZcode_qualnamerX   rO   r]   rU   rD   format_linesr^   r   format_variables)r!   rH   r6   r"   r"   r#   rL      s    

zSerializer.format_framec                 c   s@   |D ]6}t |tr.tf ddi| |V  qtddV  qd S )Nr8   lineZline_gap)r8   )rJ   r   r=   format_line)r!   r^   rb   r"   r"   r#   r`      s    
zSerializer.format_lines)rb   r%   c                 C   s$   t |j|j|j| j| j| jddS )N)r   escape_htmlr   )
is_currentrU   text)r=   re   rU   renderr   r   r   )r!   rb   r"   r"   r#   rc      s    zSerializer.format_line)
frame_infor%   c                 c   sL   z(t |jdd dD ]}| |V  qW n tk
rF   td Y nX d S )Nc                 S   s   | j S N)rT   )vr"   r"   r#   rP      rQ   z-Serializer.format_variables.<locals>.<lambda>rR   z Error in getting frame variables)rZ   r_   format_variable	Exceptionlogr<   )r!   rh   varr"   r"   r#   ra      s
    zSerializer.format_variables)rn   r%   c                 C   s"   t | |j| | |jdS )N)rT   value)r=   format_variable_partrT   format_variable_valuero   )r!   rn   r"   r"   r#   rk      s    
zSerializer.format_variablec                 C   s   | j rt|S |S d S ri   )r   rd   )r!   rf   r"   r"   r#   rp      s    zSerializer.format_variable_partc                 C   s   t |S ri   )repr)r!   ro   r"   r"   r#   rq      s    z Serializer.format_variable_valuec                 C   s   dS )NTr"   )r!   rh   r"   r"   r#   rK      s    zSerializer.should_include_frame)N)N)r@   
__module____qualname__r$   r   r=   r,   BaseExceptionr4   r>   r   r   r   r   rE   rM   r   r   rL   r`   r   rc   ra   r   rk   rp   strrq   boolrK   r"   r"   r"   r#   r      s:   2
r   )rA   loggingr)   r-   collectionsr   r   r   rd   typesr   r   typingr   r   r   rF   r	   r
   r   r   r   r   Zstack_data.utilsr   	getLoggerr@   rm   r   r"   r"   r"   r#   <module>   s    
