o
    Mfri                     @   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 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/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:mZ ddl;m<Z< ee=df Z>e? dkZ@dZAi e&e e'eddeeddejBedde!edde!jedde$jCedde"jDedde"jEedde"jFeddde"jGeddde"jHedde"jIeddde"jJed de"jKed de"jLedde"jMed!de%ed"de#edde jNed#de jOedde jPedd$e jQeddde jRedd$e jed#deed ddi	ZSee>ef eTd%< i e&e e'ed&deeddejBed'de!ed!de!jed'de$jCed(de"jDed'de"jEed)de"jFed'dde"jGed)dde"jHed'de"jIed(dde"jJed#de"jKed#de"jLed'de"jMed!de%ed"de#ed!de jNed#de jOed)de jPedd$e jQed(dde jRedd$e jed#deed ddi	ZUee>ef eTd*< eSeUd+ZVG d,d- d-eZWG d.d/ d/eWZXG d0d1 d1eWZYG d2d3 d3e5ZZe[d4krd dl\Z\d dl]Z]e\j^d5d6Z_e_j`d7d8d9d: e_j`d;d<d=d>dd?d@ e_j`dAdBdCd>dDdEd@ e_j`dFdGdHd>dIdJ e_j`dKdLeadMddNdO e_j`dPdQdRd>dDdSd@ e_j`dTdUd>dVdDdWdX e_j`dYdZd[dd\d] e_j`d^d_d`ddad] e_j`dbdcdddedfdg e_b Zcd dhldm0Z0 e0ecjeecjfdiZ/ecjgdjkre]jhi ZjeZejecjkecjlecjmecjnecjoecjpdkZqneZjrecjgecjlecjmecjnecjoecjpdlZqe/jseqecjtdm dS dS )n    N)Lines)ABCabstractmethod)	AnyDictIterableListOptionalSetTupleTypeUnion)Lexer)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)r6   
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)selfrD    rJ   Y/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/pip/_vendor/rich/syntax.pyget_style_for_tokenk      zSyntaxTheme.get_style_for_tokenc                 C   rF   )zGet the background color.rG   rI   rJ   rJ   rK   get_background_stylep   rM   z SyntaxTheme.get_background_styleN)	__name__
__module____qualname____doc__r   	TokenTyper   rL   rO   rJ   rJ   rJ   rK   rC   h   s    rC   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 delegates to Pygments theme.themerE   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)rI   rV   rJ   rJ   rK   __init__y   s   

zPygmentsSyntaxTheme.__init__rD   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.   rY   #z#000000r6   italicr5   )r.   rY   r6   rc   r5   )rZ   KeyErrorr]   style_for_tokenr   nullr_   )rI   rD   pygments_stylestyler.   rY   rJ   rJ   rK   rL      s(   z'PygmentsSyntaxTheme.get_style_for_tokenc                 C      | j S Nr`   rN   rJ   rJ   rK   rO         z(PygmentsSyntaxTheme.get_background_style)rP   rQ   rR   rS   r   r\   r   PygmentsStylera   rT   r   rL   rO   rJ   rJ   rJ   rK   rU   v   s
    rU   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_maprE   Nc                 C   s$   || _ t | _t | _i | _d S rj   )ro   r   rf   _missing_styler`   rZ   )rI   ro   rJ   rJ   rK   ra      s   


zANSISyntaxTheme.__init__rD   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)rZ   rd   ro   gettuplerp   )rI   rD   	get_styletokenrh   _stylerJ   rJ   rK   rL      s    
z#ANSISyntaxTheme.get_style_for_tokenc                 C   ri   rj   rk   rN   rJ   rJ   rK   rO      rl   z$ANSISyntaxTheme.get_background_style)
rP   rQ   rR   rS   r   rT   r   ra   rL   rO   rJ   rJ   rJ   rK   rn      s
    rn   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	ee
f 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edee fd&d'Z	
d;d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 d0e!deeeef fd1d2Z"	0	3	4	5		6d=d7d8Z#d0e!d4e$de%fd9d:Z&d
S )>Syntaxa  Construct a Syntax object to render syntax highlighted code.

    Args:
        code (str): Code to highlight.
        lexer (Lexer | 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namerE   c                 C   s2   t |tr|S |tv rtt| }|S t|}|S )zGet a syntax theme instance.)r[   rC   RICH_SYNTAX_THEMESrn   rU   )clsry   rV   rJ   rJ   rK   	get_theme   s   
zSyntax.get_themeFr   N   )rV   dedentline_numbers
start_line
line_rangehighlight_lines
code_widthtab_size	word_wrapr^   indent_guidescodelexerrV   r~   r   r   r   r   r   r   r   r^   r   c                C   st   || _ || _|| _|| _|| _|| _|pt | _|	| _|
| _	|| _
|| _|r+t|dnt | _|| _| || _d S )NrX   )r   _lexerr~   r   r   r   setr   r   r   r   r^   r   background_styler   r|   rx   )rI   r   r   rV   r~   r   r   r   r   r   r   r   r^   r   rJ   rJ   rK   ra      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   NrW   .)rV   r~   r   r   r   r   r   r   r   r^   r   )openreadosr   splitextlstriplowerr   ry   r   r   )r{   r   r   rV   r~   r   r   r   r   r   r   r   r^   r   	code_filer   r   
lexer_name_ext	extensionrJ   rJ   rK   	from_path  sH   %
zSyntax.from_pathc                 C   s   | j  | j }|S )zGet the base style.)rx   rO   r   )rI   default_stylerJ   rJ   rK   _get_base_styleO  s   zSyntax._get_base_stylerD   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.
        )rx   rL   r.   )rI   rD   rh   rJ   rJ   rK   _get_token_colorT  s   	zSyntax._get_token_colorc                 C   s>   t | jtr	| jS zt| jdd| jdW S  ty   Y dS w )zThe lexer for this syntax, or None if no lexer was found.

        Tries to find the lexer by name if a string was passed to the constructor.
        FT)stripnlensurenltabsizeN)r[   r   r   r   r   r   rN   rJ   rJ   rK   r   `  s   zSyntax.lexerc                    s   |   }|jr	dnd}t||| j| j d}| jj | jdu r(| |S |rY|\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rv|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.
        rW   left)justifyrh   r   no_wrapNrE   c                  3   sH    sJ   D ]\} }|r!|d\}}}| || fV  |sq
dS )zSplit tokens to one per line.
N)
get_tokens	partition)rD   ru   
line_tokennew_line)r   r   rJ   rK   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_typeru   rD   )_get_theme_styleline_end
line_startr   rJ   rK   tokens_to_spans  s$   



z)Syntax.highlight.<locals>.tokens_to_spansc                 3   s     | ]\}}| |fV  qd S rj   rJ   ).0rD   ru   )r   rJ   rK   	<genexpr>  s
    

z#Syntax.highlight.<locals>.<genexpr>zon )r   transparent_backgroundr)   r   r   rx   rL   r   appendr   r   r   r\   r	   r   append_tokensr   r^   stylize)rI   r   r   
base_styler   textr   rJ   )r   r   r   r   r   r   rK   	highlights  s2   
- (	
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)rx   rO   r   rY   is_system_definedr    rW   r   r   r)   r!   get_truecolorfrom_triplet)rI   r   r   r^   foreground_color	new_colorrJ   rJ   rK   _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)rI   column_widthrJ   rJ   rK   _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?)r6   r.   F)r   r   r   rf   color_systemchainrx   rL   r   r)   r   r   )rI   r   r   number_stylehighlight_number_stylerJ   rJ   rK   _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 rj   )r   r   r'   	max_width)rI   r   r   widthrJ   rJ   rK   __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/   )rh   crop)r   )r   )r   height)rh   pad	new_lines)allow_blank z> u   ❱ )r   )rh   r    )endr   r7   r-   z  )-r   r   r   r   r   r   r   maxr   r   r~   textwrap
expandtabsr   r   r   r   remove_suffixrx   rL   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) rI   r   r   r   r   line_offsetr   end_line
ends_on_nlr   r   r   r   r   rh   syntax_linessyntax_linelinesnumbers_column_widthrender_optionshighlight_line_Segmentpaddingr   line_pointerr   linewrapped_linessegmentsfirstwrapped_lineline_columnrJ   rJ   rK   __rich_console__  s   












zSyntax.__rich_console__rj   )r   )r   r"   r   r#   rE   r'   )'rP   rQ   rR   rS   r   rm   __annotations__rC   classmethodr   r\   r|   DEFAULT_THEMEr   boolintr	   r   r
   ra   r   r   r   rT   r    r   propertyr   r)   r   floatr   r   r"   r   r   r#   r%   r  rJ   rJ   rJ   rK   rw      s   
 

	


#

	
H
L
rw   __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actionrW   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  rW   r  z-rz--wrapr   zword wrap long linesz-sz--soft-wrap	soft_wrapzenable soft wrapping mode)r  r  rW   r  z-tz--themerV   zpygments theme)r  rW   r  z-bz--background-colorr^   zOverride background colorz-xz--lexerrW   r   z
Lexer name)rW   r  r  )r"   )force_terminalr   -)r   r   r   r   rV   r^   r   )r   r   rV   r^   r   )r  )uos.pathr   platformpip._vendor.rich.containersr   r   abcr   r   typingr   r   r   r   r	   r
   r   r   r   pip._vendor.pygments.lexerr   pip._vendor.pygments.lexersr   r   pip._vendor.pygments.styler   rm   pip._vendor.pygments.stylesr   pip._vendor.pygments.tokenr   r   r   r   r   r   r   r   r   r   pip._vendor.pygments.utilr   _loopr   r.   r    r!   r   r"   r#   r$   r%   jupyterr&   measurer'   segmentr(   rh   r   r)   r\   rT   systemWINDOWSr  PreprocWordBuiltinFunction	NamespaceClass	Exception	DecoratorVariableConstant	AttributeTagDeletedInsertedHeading
SubheadingPromptr;   r  r@   rz   rC   rU   rn   rw   rP   argparsesysArgumentParserparseradd_argumentr
  
parse_argsargspip._vendor.rich.consoler  r   r   stdinr   r   r   r   r   rV   r^   r   syntaxr   printr  rJ   rJ   rJ   rK   <module>   s  
 ,0


	





	



*!   
:


