o
    MEgZ                     @   s  d dl mZ d dlZd dlZd dl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mZmZmZ d dlm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 ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+m,Z, ddl-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 dd	l:mZ ddl;m<Z< e= dkZ>dZ?dZ@dddddddddee( d eeA d!eAd"eeB d#eCd$eCd%eCd&eeeD eDee gef fd'd(ZEeG d)d* d*ZFeG d+d, d,ZGeG d-d. d.ZHeG d/d0 d0ZIG d1d2 d2e0ZJG d3d4 d4ZKeLd5krHdd6l'm(Z( e( Z'd dlZd7ed&dfd8d9ZMd7ed&dfd:d;ZNd>d<d=ZOeO  dS dS )?    )absolute_importN)	dataclassfield)walk_tb)TracebackType)AnyCallableDictIterableListOptionalType)guess_lexer_for_filename)CommentKeywordNameNumberOperatorString)Text)Token   )pretty)
loop_first	loop_last)Columns)ConsoleConsoleOptionsConsoleRenderableRenderResultrender_group)	Constrain)RegexHighlighterReprHighlighter)Panel)render_scope)Style)Syntax)ThemeWindows
   P   d      FT)consolewidthextra_linestheme	word_wrapshow_localsindent_guidesr.   r/   r0   r1   r2   r3   r4   returnc           
         s   | du r
t tjdn| dtt dtdtt ddffdd d	tddf fd
d}zt }|| tj	W S  t
yL   tj	}	 t_	|	 Y S w )a  Install a rich traceback handler.

    Once installed, any tracebacks will be printed with syntax highlighting and rich formatting.


    Args:
        console (Optional[Console], optional): Console to write exception to. Default uses internal Console instance.
        width (Optional[int], optional): Width (in characters) of traceback. Defaults to 100.
        extra_lines (int, optional): Extra lines of code. Defaults to 3.
        theme (Optional[str], optional): Pygments theme to use in traceback. Defaults to ``None`` which will pick
            a theme appropriate for the platform.
        word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False.
        show_locals (bool, optional): Enable display of local variables. Defaults to False.
        indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True.

    Returns:
        Callable: The previous exception handler that was replaced.

    N)filetype_value	tracebackr5   c                    s&    tj| || d	 d S )N)r/   r0   r1   r2   r3   r4   )print	Tracebackfrom_exception)r7   r8   r9   )r0   r4   r3   r1   traceback_consoler/   r2    h/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/rich/traceback.py
excepthookH   s   zinstall.<locals>.excepthookipc              	      sl   i j  dtdtdd f fdd}dddtdtdtdd ffd	d
_|_ fdd_d S )Nargskwargsr5   c                     s   | | i | dS )zEwrap the default ip.showtraceback to store info for ip._showtracebackNr>   rB   rC   )default_showtracebacktb_datar>   r?   ipy_show_traceback_   s   zCinstall.<locals>.ipy_excepthook_closure.<locals>.ipy_show_tracebackF)	is_syntaxrH   c                    st     }| rdn|d }dd}d|rdnd}t|D ]}|du r( n|j}q  |d |d | i dS )z2Internally called traceback from ip._showtracebackN   running_compiled_codeF	tb_offsetr   r   )_get_exc_infogetrangetb_next)rH   rB   rC   	exc_tupletbcompiledrK   _)r@   rA   rF   r>   r?   ipy_display_tracebacke   s   zFinstall.<locals>.ipy_excepthook_closure.<locals>.ipy_display_tracebackc                     s    | ddi|S )NrH   Tr>   rD   )rT   r>   r?   <lambda>   s    z9install.<locals>.ipy_excepthook_closure.<locals>.<lambda>)showtracebackr   bool_showtracebackshowsyntaxerror)rA   rG   )r@   )rE   rA   rT   rF   r?   ipy_excepthook_closure[   s    z'install.<locals>.ipy_excepthook_closure)r   sysstderrr   BaseExceptionr   r   r   get_ipythonr@   	Exception)
r.   r/   r0   r1   r2   r3   r4   rZ   rA   old_excepthookr>   )r@   r0   r4   r3   r1   r=   r/   r2   r?   install)   s(   )ra   c                   @   sL   e Zd ZU eed< eed< eed< dZeed< dZee	ee
jf  ed< dS )Framefilenamelinenoname lineNlocals)__name__
__module____qualname__str__annotations__intrg   rh   r   r	   r   Noder>   r>   r>   r?   rb      s   
 rb   c                   @   s6   e Zd ZU eed< eed< eed< eed< eed< dS )_SyntaxErroroffsetrc   rg   rd   msgN)ri   rj   rk   rn   rm   rl   r>   r>   r>   r?   rp      s   
 rp   c                   @   sP   e Zd ZU eed< eed< dZee ed< dZe	ed< e
edZee ed< dS )	Stackexc_type	exc_valueNsyntax_errorFis_cause)default_factoryframes)ri   rj   rk   rl   rm   rv   r   rp   rw   rW   r   listry   r   rb   r>   r>   r>   r?   rs      s   
 rs   c                   @   s   e Zd ZU ee ed< dS )TracestacksN)ri   rj   rk   r   rs   rm   r>   r>   r>   r?   r{      s   
 r{   c                   @   s   e Zd ZdgZdS )PathHighlighterz(?P<dim>.*/)(?P<bold>.+)N)ri   rj   rk   
highlightsr>   r>   r>   r?   r}      s    
r}   c                   @   sh  e Zd ZdZddddddZddd	dd
d
deef	dee dee	 de	dee
 dededede	de	fddZedd	dd
d
deefdee dedee dee	 de	dee
 dededede	de	dd fddZed
eefdee dedee dede	de	defddZded edefd!d"Ze d#edefd$d%Zed&e
d'e
de
fd(d)Ze d*edefd+d,ZdS )-r;   a  A Console renderable that renders a traceback.

    Args:
        trace (Trace, optional): A `Trace` object produced from `extract`. Defaults to None, which uses
            the last exception.
        width (Optional[int], optional): Number of characters used to traceback. Defaults to 100.
        extra_lines (int, optional): Additional lines of code to render. Defaults to 3.
        theme (str, optional): Override pygments theme used in traceback.
        word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False.
        show_locals (bool, optional): Enable display of local variables. Defaults to False.
        indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True.
        locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
            Defaults to 10.
        locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80.
    textpythoncythonpyrex)rf   z.pyz.pxdz.pyxz.pxiNr,   r-   FTtracer/   r0   r1   r2   r3   r4   locals_max_lengthlocals_max_stringc
                 C   s   |d u r$t  \}
}}|
d u s|d u s|d u rtd| j|
|||d}|| _|| _|| _t|p2d| _	|| _
|| _|| _|| _|	| _d S )Nz9Value for 'trace' required if not called in except: blockr3   	ansi_dark)r[   exc_info
ValueErrorextractr   r/   r0   r'   	get_themer1   r2   r3   r4   r   r   )selfr   r/   r0   r1   r2   r3   r4   r   r   rt   ru   r9   r>   r>   r?   __init__   s$   
zTraceback.__init__rt   ru   r9   r5   c                 C   s,   | j ||||d}| |||||||	|
|d	S )a  Create a traceback from exception info

        Args:
            exc_type (Type[BaseException]): Exception type.
            exc_value (BaseException): Exception value.
            traceback (TracebackType): Python Traceback object.
            width (Optional[int], optional): Number of characters used to traceback. Defaults to 100.
            extra_lines (int, optional): Additional lines of code to render. Defaults to 3.
            theme (str, optional): Override pygments theme used in traceback.
            word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False.
            show_locals (bool, optional): Enable display of local variables. Defaults to False.
            indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True.
            locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
                Defaults to 10.
            locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80.

        Returns:
            Traceback: A Traceback instance that may be printed.
        r   )r/   r0   r1   r2   r3   r4   r   r   )r   )clsrt   ru   r9   r/   r0   r1   r2   r3   r4   r   r   rich_tracebackr>   r>   r?   r<      s   "zTraceback.from_exceptionc                    s  g }d}ddl m}	 dtdtfdd}
	 t|
|j|
||d	}t|tr<t|j	p*d|j
p.d
|jp2d|jp6d|jd|_|| |jj}t|D ];\}}|jj}|re|dsetj|setj|	|}t|pid
||jj|r| fdd|j D ndd}|| qIt|dd}|r|jr|j}|}|j}|rd}q|j }|r|jrt|dds|j}|}|j}|rd}q	 t!|d}|S )a  Extract traceback information.

        Args:
            exc_type (Type[BaseException]): Exception type.
            exc_value (BaseException): Exception value.
            traceback (TracebackType): Python Traceback object.
            show_locals (bool, optional): Enable display of local variables. Defaults to False.
            locals_max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
                Defaults to 10.
            locals_max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to 80.

        Returns:
            Trace: A Trace instance which you can use to construct a `Traceback`.
        Fr   )_IMPORT_CWD_objectr5   c                 S   s    zt | W S  ty   Y dS w )z1Don't allow exceptions from __str__ to propegate.z<exception str() failed>)rl   r_   )r   r>   r>   r?   safe_str;  s
   
z#Traceback.extract.<locals>.safe_strT)rt   ru   rw   ?rf   )rq   rc   rd   rg   rr   <c                    s"   i | ]\}}|t j| d qS ))
max_length
max_string)r   traverse).0keyr8   r   r   r>   r?   
<dictcomp>^  s    z%Traceback.extract.<locals>.<dictcomp>N)rc   rd   re   rh   	__cause____suppress_context__)r|   )"richr   r   rl   rs   ri   
isinstanceSyntaxErrorrp   rq   rc   rd   r   rr   rv   appendry   r   f_codeco_filename
startswithospathisabsjoinrb   co_namef_localsitemsgetattr__traceback__	__class____context__r{   )r   rt   ru   r9   r3   r   r   r|   rw   r   r   stackr   frame_summaryline_norc   framecauser   r>   r   r?   r     sx   

		



zTraceback.extractr.   optionsc                 c   sN   | j }| }|j}ti d|td|td|td|tjd|t	d|t
tdd d	|td
|ttdd d|t	d|tjd|tjd|tjd|tjd|td|td|tjtdd }t }tt| jjD ]\}}	|	jrt| |	d|dddd}
t|
| j}
|| |
V  W d    n1 sw   Y  |	jd ur|| tt| |	j|ddd| jd| jV  W d    n1 sw   Y  t|	j ddf||	jj V  n|	j!rt|	j ddf||	j!V  n
t|	j dfV  |s$|	j"rt#dV  qt#dV  qd S )Nr   pygments.textpygments.stringpygments.functionpygments.numberzrepr.indentT)dimzrepr.strz
repr.brace)boldzrepr.numberzrepr.bool_truezrepr.bool_falsez	repr.nonezscope.borderzscope.equalsz	scope.keyzscope.key.specialz7[traceback.title]Traceback [dim](most recent call last)ztraceback.border.syntax_error)r   r   )titlestyleborder_styleexpandpaddingztraceback.border)r   r   r   r   r/   z: ztraceback.exc_typezI
[i]The above exception was the direct cause of the following exception:
zH
[i]During handling of the above exception, another exception occurred:
)$r1   get_background_styleget_style_for_tokenr(   	TextTokenr   r   r   Functionr   r   r&   r   Constant	Delimiterr   r#   r   reversedr   r|   ry   r$   _render_stackr!   r/   	use_themerv   _render_syntax_errorr   assemblert   rr   ru   rw   from_markup)r   r.   r   r1   background_styletoken_styletraceback_themehighlighterlastr   stack_renderabler>   r>   r?   __rich_console__  s   
	







zTraceback.__rich_console__rv   c                 c   s    t  }t }|jdkr%tjd|j dfdt|jdfdd}||V  ||j }d|_	t
|jd	 t|}|d
||d	  |tjdd|  d dd7 }|V  d S )Nz<stdin> r   :r   r   r   r   Tr   zbold underline
u   [traceback.offset]▲[/])r#   r}   rc   r   r   rl   rd   rg   rstripno_wrapminrq   lenstylizer   )r   rv   r   path_highlighterr   syntax_error_textrq   r>   r>   r?   r     s(   


zTraceback._render_syntax_errorrc   codec                 C   sl   t j|d }|s(|d}|dkr|d | n|}|dr(d| v r(dS | j|p3t||j	}|S )Nr   z#!r   )
r   r   splitextindexr   lowerLEXERSrM   r   re   )r   rc   r   extnew_line_index
first_line
lexer_namer>   r>   r?   _guess_lexer  s   
zTraceback._guess_lexerr   c                 #   s~   t  }j}i  dtdtf fdd}dtdtt ffdd}t|jD ]\}}tj	|t|j
dd	d
t|jdfd|jdfdd	}|j
dsO|sOdV  |V  |j
dr`||E d H  q&z-||j
}	|j
|	}
t|	|
|d|jj |jj f|jhjdjdd
}dV  W n ty } zt	d| dfV  W Y d }~q&d }~ww |jrt|g||ddn|V  q&d S )Nrc   r5   c                    sV     | }|du r)t| dddd}| }W d   n1 s w   Y  | | < |S )zRead files, and cache results on filename.

            Args:
                filename (str): Filename to read

            Returns:
                str: Contents of file
            Nrtzutf-8replace)encodingerrors)rM   openread)rc   r   	code_file)
code_cacher>   r?   	read_code  s   
	
z*Traceback._render_stack.<locals>.read_coder   c                 3   s,    | j rt| j d j j jdV  d S d S )Nrh   )r   r4   r   r   )rh   r%   r4   r   r   )r   )r   r>   r?   render_locals  s   z.Traceback._render_stack.<locals>.render_localsr   r   r   r   z in r   r   r   rf   TX   F)r1   line_numbers
line_rangehighlight_linesr2   
code_widthr4   dedentr   ztraceback.errorr   )r   )r}   r1   rl   rb   r
   r   r   ry   r   r   rc   rd   re   r   r   r'   r0   r2   r4   r_   rh   r   )r   r   r   r1   r   r   firstr   r   r   r   syntaxerrorr>   )r   r   r?   r     sp   




zTraceback._render_stack)ri   rj   rk   __doc__r   LOCALS_MAX_LENGTHLOCALS_MAX_STRINGr   r{   rn   rl   rW   r   classmethodr   r   r]   r   r<   r   r   r   r   r   r    rp   r   r   rs   r   r>   r>   r>   r?   r;      s    
	

	
0h
Mr;   __main__)r   ac                 C   s   d}t ||   d S )Nr   )r:   )r  oner>   r>   r?   barV  s   r  c                 C   s   h ddd}t |  d S )N>   Duncan IdahoThufir HawatPaul AtreidesVladimir Harkonnen)NFT)
charactersatomic_types)r  )r  zedr>   r>   r?   fooZ  s   	r  c                   C   s>   zzt d W W d S    t Y W d S    tjdd Y d S )Nr   Tr   )r  slfkjsldkfjr.   print_exceptionr>   r>   r>   r?   r   g  s   r   )r5   N)P
__future__r   r   platformr[   dataclassesr   r   r9   r   typesr   typingr   r   r	   r
   r   r   r   pygments.lexersr   pygments.tokenr   r   r   r   r   r   r   r   r   rf   r   _loopr   r   columnsr   r.   r   r   r   r   r    	constrainr!   r   r"   r#   panelr$   scoper%   r   r&   r   r'   r   r1   r(   systemWINDOWSr   r   rn   rl   rW   r]   ra   rb   rp   rs   r{   r}   r;   ri   r  r  r   r>   r>   r>   r?   <module>   s    $ 	
g   



