o
    MEg_g                     @   s  U d dl Zd dlZd dlmZ d dl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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"m#Z#m$Z$m%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/m0Z0m1Z1 d
dl2m3Z3 d
dl4m5Z5 d
dl6m7Z7 d
dl8mZ d
dl9m:Z: ee;df Z<e= dkZ>dZ?i e$e e%eddeeddej@eddeeddejedde"jAedde jBedde jCedde jDeddde jEeddde jFedde jGeddde jHedde jIedde jJedde jKed de#ed!de!eddejLed"dejMeddejNedd#ejOedddejPedd#ejed"deedddi	ZQee<ef eRd$< i e$e e%ed%deeddej@ed&deed dejed&de"jAed'de jBed&de jCed(de jDed&dde jEed(dde jFed&de jGed'dde jHed"de jIed"de jJed&de jKed de#ed!de!ed dejLed"dejMed(dejNedd#ejOed'ddejPedd#ejed"deedddi	ZSee<ef eRd)< eQeSd*ZTG d+d, d,eZUG d-d. d.eUZVG d/d0 d0eUZWG d1d2 d2e3ZXeYd3krd dlZZZd dl[Z[eZj\d4d5Z]e]j^d6d7d8d9 e]j^d:d;d<d=dd>d? e]j^d@dAdBd=dCdDd? e]j^dEdFdGd=dHdI e]j^dJdKe_dLddMdN e]j^dOdPdQd=dCdRd? e]j^dSdTd=dUdCdVdW e]j^dXdYdZdd[d\ e]j^d]d^d_dd`d\ e]j^dadbdcdddedf e]` Zad dglbm.Z. e.eajceajddhZ-eajedikre[jfg ZheXeheajieajjeajkeajleajmeajndjZoneXjpeajeeajjeajkeajleajmeajndkZoe-jqeoeajrdl dS dS )m    N)Lines)ABCabstractmethod)	AnyDictIterableListOptionalSetTupleTypeUnion)get_lexer_by_nameguess_lexer_for_filename)Style)get_style_by_name)
CommentErrorGenericKeywordNameNumberOperatorStringToken
Whitespace)ClassNotFound   )
loop_first)Color	blend_rgb)ConsoleConsoleOptionsJustifyMethodRenderResult)JupyterMixin)Measurement)Segment)Text.WindowsmonokaiwhitecolorTdimcyanbluemagentagreen)r-   	underline)r-   boldredbright_blueyellow
bright_red)r5   
ANSI_LIGHTbright_blackbright_cyanbright_magentabright_green	ANSI_DARK)
ansi_light	ansi_darkc                   @   s8   e Zd ZdZededefddZedefddZdS )	SyntaxThemezBase class for a syntax theme.
token_typereturnc                 C      t )z'Get a style for a given Pygments token.NotImplementedError)selfrC    rI   e/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/rich/syntax.pyget_style_for_tokenj      zSyntaxTheme.get_style_for_tokenc                 C   rE   )zGet the background color.rF   rH   rI   rI   rJ   get_background_styleo   rL   z SyntaxTheme.get_background_styleN)	__name__
__module____qualname____doc__r   	TokenTyper   rK   rN   rI   rI   rI   rJ   rB   g   s    rB   c                   @   sN   e Zd ZdZdeeee f ddfddZde	de
fdd	Zde
fd
dZdS )PygmentsSyntaxThemez.Syntax theme that delagates to Pygments theme.themerD   Nc                 C   s^   i | _ t|trzt|| _W n ty   td| _Y nw || _| jj| _t| jd| _	d S )Ndefaultbgcolor)
_style_cache
isinstancestrr   _pygments_style_classr   background_color_background_colorr   _background_style)rH   rU   rI   rI   rJ   __init__x   s   

zPygmentsSyntaxTheme.__init__rC   c                 C   s   z| j | W S  tyO   z| j|}W n ty"   t }Y n%w |d }|d }t|r2d| nd|r9d| n| j|d |d |d d}|| j |< Y |S w )	z"Get a style from a Pygments class.r-   rX   #z#000000r5   italicr4   )r-   rX   r5   rb   r4   )rY   KeyErrorr\   style_for_tokenr   nullr^   )rH   rC   pygments_stylestyler-   rX   rI   rI   rJ   rK      s(   z'PygmentsSyntaxTheme.get_style_for_tokenc                 C      | j S Nr_   rM   rI   rI   rJ   rN         z(PygmentsSyntaxTheme.get_background_style)rO   rP   rQ   rR   r   r[   r   PygmentsStyler`   rS   r   rK   rN   rI   rI   rI   rJ   rT   u   s
    rT   c                   @   sJ   e Zd ZdZdeeef ddfddZdedefdd	Zdefd
dZ	dS )ANSISyntaxThemez$Syntax theme to use standard colors.	style_maprD   Nc                 C   s$   || _ t | _t | _i | _d S ri   )rn   r   re   _missing_styler_   rY   )rH   rn   rI   rI   rJ   r`      s   


zANSISyntaxTheme.__init__rC   c                 C   sn   z| j | W S  ty6   | jj}t|}| j}|r-||}|dur%|}n|dd }|s|| j |< | Y S w )zLook up style in the style map.N)rY   rc   rn   gettuplero   )rH   rC   	get_styletokenrg   _stylerI   rI   rJ   rK      s    
z#ANSISyntaxTheme.get_style_for_tokenc                 C   rh   ri   rj   rM   rI   rI   rJ   rN      rk   z$ANSISyntaxTheme.get_background_style)
rO   rP   rQ   rR   r   rS   r   r`   rK   rN   rI   rI   rI   rJ   rm      s
    rm   c                   @   s   e Zd ZU dZee ed< eed< ede	e
ef defddZeddd	d
d
d
ddd
ddde
de
de	e
ef dedededeeeef  deee  dee dededee
 dedd
fddZededdd
d	d
d
ddd
dfde
de
de	e
ef dededeeeef  dedeee  dee dededee
 dedd fdd Zdefd!d"Zd#edee fd$d%Z	
d9de
deeeef  defd&d'Zd:d)edefd*d+Zedefd,d-Zd.edeeeef fd/d0Z 	.	1	2	3		4d;d5d6Z!d.ed2e"de#fd7d8Z$d
S )<Syntaxa  Construct a Syntax object to render syntax highlighted code.

    Args:
        code (str): Code to highlight.
        lexer_name (str): Lexer to use (see https://pygments.org/docs/lexers/)
        theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "monokai".
        dedent (bool, optional): Enable stripping of initial whitespace. Defaults to False.
        line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False.
        start_line (int, optional): Starting number for line numbers. Defaults to 1.
        line_range (Tuple[int, int], optional): If given should be a tuple of the start and end line to render.
        highlight_lines (Set[int]): A set of line numbers to highlight.
        code_width: Width of code to render (not including line numbers), or ``None`` to use all available width.
        tab_size (int, optional): Size of tabs. Defaults to 4.
        word_wrap (bool, optional): Enable word wrapping.
        background_color (str, optional): Optional background color, or None to use theme color. Defaults to None.
        indent_guides (bool, optional): Show indent guides. Defaults to False.
    r\   _themenamerD   c                 C   s2   t |tr|S |tv rtt| }|S t|}|S )zGet a syntax theme instance.)rZ   rB   RICH_SYNTAX_THEMESrm   rT   )clsrx   rU   rI   rI   rJ   	get_theme   s   
zSyntax.get_themeFr   N   )rU   dedentline_numbers
start_line
line_rangehighlight_lines
code_widthtab_size	word_wrapr]   indent_guidescode
lexer_namerU   r}   r~   r   r   r   r   r   r   r]   r   c                C   st   || _ || _|| _|| _|| _|| _|pt | _|	| _|
| _	|| _
|| _|r+t|dnt | _|| _| || _d S )NrW   )r   r   r}   r~   r   r   setr   r   r   r   r]   r   background_styler   r{   rw   )rH   r   r   rU   r}   r~   r   r   r   r   r   r   r]   r   rI   rI   rJ   r`      s   zSyntax.__init__zutf-8pathencodingc                 C   s   t |d|d}| }W d   n1 sw   Y  d}d}ztj|\}}|r8|d }t|}|j}W n	 t	yB   Y nw |du rYzt
||j}W n	 t	yX   Y nw | |||||||||	|
|||dS )a?  Construct a Syntax object from a file.

        Args:
            path (str): Path to file to highlight.
            encoding (str): Encoding of file.
            theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "emacs".
            dedent (bool, optional): Enable stripping of initial whitespace. Defaults to True.
            line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False.
            start_line (int, optional): Starting number for line numbers. Defaults to 1.
            line_range (Tuple[int, int], optional): If given should be a tuple of the start and end line to render.
            highlight_lines (Set[int]): A set of line numbers to highlight.
            code_width: Width of code to render (not including line numbers), or ``None`` to use all available width.
            tab_size (int, optional): Size of tabs. Defaults to 4.
            word_wrap (bool, optional): Enable word wrapping of code.
            background_color (str, optional): Optional background color, or None to use theme color. Defaults to None.
            indent_guides (bool, optional): Show indent guides. Defaults to False.

        Returns:
            [Syntax]: A Syntax object that may be printed to the console
        rt)r   NrV   .)rU   r}   r~   r   r   r   r   r   r   r]   r   )openreadosr   splitextlstriplowerr   rx   r   r   )rz   r   r   rU   r}   r~   r   r   r   r   r   r   r]   r   	code_filer   lexerr   _ext	extensionrI   rI   rJ   	from_path  sH   %
zSyntax.from_pathc                 C   s   | j  | j }|S )zGet the base style.)rw   rN   r   )rH   default_stylerI   rI   rJ   _get_base_styleN  s   zSyntax._get_base_stylerC   c                 C   s   | j |}|jS )zGet a color (if any) for the given token.

        Args:
            token_type (TokenType): A token type tuple from Pygments.

        Returns:
            Optional[Color]: Color from theme, or None for no color.
        )rw   rK   r-   )rH   rC   rg   rI   rI   rJ   _get_token_colorS  s   	zSyntax._get_token_colorc                    s  |   }|jr	dnd}t||| j| j d}| jj zt| jdd| jdW n t	y5   |
 Y |S w |rg|\dttttf  ffdd	dttttt f  f fd
d}||  n| fddD  | jdur|d| j  |S )a  Highlight code and return a Text instance.

        Args:
            code (str): Code to highlight.
            line_range(Tuple[int, int], optional): Optional line range to highlight.

        Returns:
            Text: A text instance containing highlighted syntax.
        rV   left)justifyrg   r   no_wrapFT)stripnlensurenltabsizerD   c                  3   s@      D ]\} }|r|d\}}}| || fV  |sqdS )zSplit tokens to one per line.
N)
get_tokens	partition)rC   rt   
line_tokennew_line)r   r   rI   rJ   line_tokenize  s   z'Syntax.highlight.<locals>.line_tokenizec                  3   s    t  } d}d }||k r(t| \}}|dfV  |dr$|d7 }||k s| D ]\}}| |fV  |drE|d7 }|krE dS q*dS )zConvert tokens to spans.r   r   Nr   )iternextendswith)tokensline_no_line_start_token_typert   rC   )_get_theme_styleline_end
line_startr   rI   rJ   tokens_to_spans  s$   



z)Syntax.highlight.<locals>.tokens_to_spansc                 3   s     | ]\}}| |fV  qd S ri   rI   ).0rC   rt   )r   rI   rJ   	<genexpr>  s
    

z#Syntax.highlight.<locals>.<genexpr>Nzon )r   transparent_backgroundr(   r   r   rw   rK   r   r   r   appendr   r   r   r[   r	   r   append_tokensr   r]   stylize)rH   r   r   
base_styler   textr   rI   )r   r   r   r   r   r   rJ   	highlight_  s@   
+ (
zSyntax.highlight333333?blendc                 C   sr   | j  | j }|j}|d u s|jrt S | tj	}|d u s#|jr)|p(t S t
| | |d}t|S )N)
cross_fade)rw   rN   r   rX   is_system_definedr   rV   r   r   r(   r    get_truecolorfrom_triplet)rH   r   r   r]   foreground_color	new_colorrI   rI   rJ   _get_line_numbers_color  s   
zSyntax._get_line_numbers_colorc                 C   s,   d}| j rtt| j| jd d }|S )z?Get the number of characters used to render the numbers column.r   r      )r~   lenr[   r   r   count)rH   column_widthrI   rI   rJ   _numbers_column_width  s   zSyntax._numbers_column_widthconsolec              	   C   s   |   }|jrt tddt fS |jdv rAt|| jtj	t| 
 d| j}t|| jtj	td| 
dd| j}n|tdd }|tdd }|||fS )z>Get background, number, and highlight styles for line numbers.Tr.   )256	truecolorr,   g?)r5   r-   F)r   r   r   re   color_systemchainrw   rK   r   r(   r   r   )rH   r   r   number_stylehighlight_number_stylerI   rI   rJ   _get_number_styles  s&   

zSyntax._get_number_stylesr!   optionsr"   r&   c                 C   s0   | j d ur| j | j }t| j|S t| j|jS ri   )r   r   r&   	max_width)rH   r   r   widthrI   rI   rJ   __rich_measure__  s   
zSyntax.__rich_measure__c                  c   sj   |   j}| jd u r| jr|j| j d n|jn| j}d}| jr-| j\}}td|d }| j	d}|r8| jn| jd }	| j
rEt
|	n|	}	|	| j}	| |	| j}
| |\}}}| js| js| js|sl|
d |   | jt tdd | j }| jr|js|
j| j|d}
d|
_|jr|j|
|j|dd	E d H  d S |j|
|j|d d
| jddd}|D ]}|E d H  qd S |
jd|d}| jr||| }| jr|js|   | jt tdd | j }td |j| j|djddd}| j}|j|d}| j!j"}t#}|d| d |}|d}|j$rdnd}t%|| j&| D ]\}}| jr;|j||jd d|| d}nt'|j|dd}|j(rL|g}n|j)||j|| dg}| jrt*|D ]@\}}|rt+|,|d d }||r||tddV  |||V  n|d|V  |||V  n|V  |E d H  |V  q`q$|D ]}|E d H  |V  qq$d S )Nr   r   r   Tr.   )rg   crop)r   )r   )r   height)rg   pad	new_lines)allow_blank z> u   ❱ )r   )rg   r    )endr   r6   r,   z  )-r   r   r   r~   r   r   r   maxr   r   r}   textwrap
expandtabsr   r   r   r   remove_suffixrw   rK   r   r   r   r   
ascii_onlywith_indent_guidesoverflowrenderupdaterender_linessplitr(   joinr   __contains__r'   legacy_windows	enumerater   listr   adjust_line_lengthr   r[   rjust) rH   r   r   r   r   line_offsetr   end_line
ends_on_nlr   r   r   r   r   rg   syntax_linessyntax_linelinesnumbers_column_widthrender_optionshighlight_line_Segmentpaddingr   line_pointerr   linewrapped_linessegmentsfirstwrapped_lineline_columnrI   rI   rJ   __rich_console__  s   












zSyntax.__rich_console__ri   )r   )r   r!   r   r"   rD   r&   )%rO   rP   rQ   rR   r   rl   __annotations__rB   classmethodr   r[   r{   DEFAULT_THEMEboolintr	   r   r
   r`   r   r   r   rS   r   r   r(   r   floatr   propertyr   r!   r   r   r"   r$   r  rI   rI   rI   rJ   rv      s   
 
	


#

	
H
N
rv   __main__z&Render syntax to the console with Rich)descriptionr   PATHzpath to file, or - for stdin)metavarhelpz-cz--force-colorforce_color
store_truezforce color for non-terminals)destactionrV   r  z-iz--indent-guidesr   Fzdisplay indent guidesz-lz--line-numbersr~   zrender line numbers)r  r  r  z-wz--widthr   z*width of output (default will auto-detect))typer  rV   r  z-rz--wrapr   zword wrap long linesz-sz--soft-wrap	soft_wrapzenable soft wrapping mode)r  r  rV   r  z-tz--themerU   zpygments theme)r  rV   r  z-bz--background-colorr]   zOveride background colorz-xz--lexerrV   r   z
Lexer name)rV   r  r  )r!   )force_terminalr   -)r   r   r~   r   rU   r]   r   )r~   r   rU   r]   r   )r  )sos.pathr   platformrich.containersr   r   abcr   r   typingr   r   r   r   r	   r
   r   r   r   pygments.lexersr   r   pygments.styler   rl   pygments.stylesr   pygments.tokenr   r   r   r   r   r   r   r   r   r   pygments.utilr   _loopr   r-   r   r    r   r!   r"   r#   r$   jupyterr%   measurer&   segmentr'   rg   r   r(   r[   rS   systemWINDOWSr  PreprocWordBuiltinFunction	NamespaceClass	Exception	DecoratorVariableConstant	AttributeTagDeletedInsertedHeading
SubheadingPromptr:   r  r?   ry   rB   rT   rm   rv   rO   argparsesysArgumentParserparseradd_argumentr  
parse_argsargsrich.consoler  r   r   stdinr   r   r   r~   r   rU   r]   r   syntaxr   printr  rI   rI   rI   rJ   <module>   s  
 ,0


	





	



*!   
)


