o
    ;iv                     @   s   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZ dd	lmZ e d
edefddZdddZdddZdS )z=Helper functions related to displaying tracebacks in the CLI.    N)Optional)RenderResultgroup)Syntax)Text)PathHighlighterStack	Tracebackinstall   )OutputManagerstackreturnc                 #   s   t  }| j}i  t|di }d}dtdtf fdd}d}| jdkr3t| jd t|j| jd  }d	}t|jD ]\}	}
|rH|	|v rHd
}q:|r`|dusPJ t	dt| ddddV  d	}|	dk}|
j
dr~d|
j
v r~|
j
dd\}|d}n|
j
d}tfdd| jD }||kr|}dV  t	d| ddddV  t	j|t	dddt|
jdfd|
jdfd d}ds|sdV  |V  |sSz+|}| |}t|||d
|
j| j |
j| j f|
jh| jd!| jd	d"
}dV  W nW tyO } zJ||
jf}|rBz | |}dV  t|||d
d#|
jh| jd!| jd	|
jd$V  W n tyA   t	d%| dfV  Y nw dV  W Y d}~q:d}~ww |V  q:dS )&zPatched variant of rich.Traceback._render_stack that uses the line from the modal StackSummary,
    when the file isn't available to be read locally.
line_cacheNfilenamer   c                    sT     | }|d u r(t| ddd}| }W d    n1 sw   Y  | | < |S )Nzutf-8replace)encodingerrors)getopenread)r   code	code_file)
code_cache H/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/_traceback.py	read_code   s   

z _render_stack.<locals>.read_coder   r   FTz
... z frames hidden ...centerztraceback.error)justifystyle<:   z<>c                 3   s    | ]}  |V  qd S N)
startswith).0path)frame_filenamer   r   	<genexpr>A   s    z _render_stack.<locals>.<genexpr> z"...Remote call to Modal Function (z)...greenzpygments.string)r   )r!   pygments.textzpygments.numberz in zpygments.functionr+   X   )themeline_numbers
line_rangehighlight_lines	word_wrap
code_widthindent_guidesdedent)r   r"   )	r-   r.   r/   r0   r1   r2   r3   r4   
start_line
)r   r-   getattrstr
max_framesrangelenframes	enumerater   r   r$   splitstripanysuppressassemblelinenoname_guess_lexerr   extra_linesr1   r3   	Exceptionr   )selfr   path_highlighterr-   r   task_idr   exclude_framesexcludedframe_indexframefirstnext_task_id
suppressedtextr   
lexer_namesyntaxerrorliner   )r   r'   r   _render_stack   s   





rW   c                     sb   t j tt j fdd} tt _| t _dd l}dd l}dd l}dd l}t	||||gdd d S )Nc                    s<   t |di } | |g|R i |}|jjD ]}||_q|S )zPatch from_exception to grab the Modal line_cache and store it with the
        Stack object, so it's available to render_stack at display time.__line_cache__)r7   tracestacksr   )exc_type	exc_valueargskwargsr   tbr   from_exceptionr   r   _from_exception   s
   z-setup_rich_traceback.<locals>._from_exceptionr   r"   )rA   rF   )
r	   ra   	functoolswrapsrW   clickgrpclibsynchronicitytyperr
   )rb   re   rf   rg   rh   r   r`   r   setup_rich_traceback   s   

ri   c                     s   t j d fdd	} | t _dS )zLPatch the warnings module to delegate warning display to the output manager.Nc                    s   t  | |||  d S r#   )r   r   show_warning)warningcategoryr   rC   filerV   base_showwarningr   r   showwarning   s   z-highlight_modal_warnings.<locals>.showwarning)NN)warningsrp   )rp   r   rn   r   highlight_modal_warnings   s   
rr   )r   N)__doc__rc   rq   typingr   rich.consoler   r   rich.syntaxr   	rich.textr   rich.tracebackr   r   r	   r
   outputr   rW   ri   rr   r   r   r   r   <module>   s   
x