o
    Mfeg                     @   s   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	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 dd	lmZmZ dd
lmZmZ eedf ZG dd dZ eG dd dZ!e! Z"G dd dZ#dS )    N)	lru_cache)loadsdumps)randint)AnycastDictIterableListOptionalTypeUnion   )errors)ColorColorParseErrorColorSystem	blend_rgb)	rich_reprResult)DEFAULT_TERMINAL_THEMETerminalThemeStylec                   @   sF   e Zd ZdZdgZdeddfddZdd	d
ed	 dee	 fddZ
dS )_Bitz.A descriptor to get/set a style attribute bit.bitbit_noreturnNc                 C   s   d|> | _ d S )Nr   )r   )selfr    r   X/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/pip/_vendor/rich/style.py__init__      z_Bit.__init__objr   objtypec                 C   s    |j | j@ r|j| j@ dkS d S )Nr   )_set_attributesr   _attributes)r   r"   r#   r   r   r   __get__   s   z_Bit.__get__)__name__
__module____qualname____doc__	__slots__intr    r   r   boolr&   r   r   r   r   r      s
    "r   c                &   @   s  e Zd ZU dZee ed< ee ed< eed< eed< eed< eed< ee	 ed< g d	Z
d
dddddddddddddZi ddddddddddddddddd d d!d!d"d"d#d"d$d$d%d$d&d&d'd&d(d(d(d)d*d+d+d,Zd-d-d-d-d-d-d-d-d-d-d-d-d-d-d-d-d-d.d/eeeef  d0eeeef  dee dee dee dee d ee d!ee d"ee d$ee d&ee d(ee d)ee d*ee d+ee d1ee d2eeeef  f"d3d4Zedd6d7Ze	-dd/ee d0ee d5d fd8d9Zed2eeeef  d5d fd:d;Zedd2eeeef  d<ed5d fd=d>Zed?Zed@ZedAZedBZedCZedDZedEZedFZedGZ edHZ!edIZ"edJZ#edKZ$e%d5efdLdMZ&d5efdNdOZ'd5efdPdQZ(dRe)d5efdSdTZ*ee+dUdVdWed5efdXdYZ,edZee- d5e-fd[d\Z.d5e/fd]d^Z0d_ed5efd`daZ1d5efdbdcZ2e%d5ee fdddeZ3e%d5ee fdfdgZ4e%d5ee fdhdiZ5e%d5efdjdkZ6e%ddldmZ7e%d5eeef fdndoZ8e%ddpdqZ9ee+drdVdsed5d fdtduZ:e+dUdVddvee; d5efdwdxZ<edye=d  d5d fdzd{Z>edd|d}Z?dd~dZ@dd1ee d5d fddZA	de)jBdddedRee) ded5efddZCddee d5d-fddZDdWed  d5d fddZEd-S )r   a  A terminal style.

    A terminal style consists of a color (`color`), a background color (`bgcolor`), and a number of attributes, such
    as bold, italic etc. The attributes have 3 states: they can either be on
    (``True``), off (``False``), or not set (``None``).

    Args:
        color (Union[Color, str], optional): Color of terminal text. Defaults to None.
        bgcolor (Union[Color, str], optional): Color of terminal background. Defaults to None.
        bold (bool, optional): Enable bold text. Defaults to None.
        dim (bool, optional): Enable dim text. Defaults to None.
        italic (bool, optional): Enable italic text. Defaults to None.
        underline (bool, optional): Enable underlined text. Defaults to None.
        blink (bool, optional): Enabled blinking text. Defaults to None.
        blink2 (bool, optional): Enable fast blinking text. Defaults to None.
        reverse (bool, optional): Enabled reverse text. Defaults to None.
        conceal (bool, optional): Enable concealed text. Defaults to None.
        strike (bool, optional): Enable strikethrough text. Defaults to None.
        underline2 (bool, optional): Enable doubly underlined text. Defaults to None.
        frame (bool, optional): Enable framed text. Defaults to None.
        encircle (bool, optional): Enable encircled text. Defaults to None.
        overline (bool, optional): Enable overlined text. Defaults to None.
        link (str, link): Link URL. Defaults to None.

    _color_bgcolorr%   r$   _hash_null_meta)r.   r/   r%   r$   _link_link_id_ansi_style_definitionr0   r1   r2   12345678921515253)r   r                        	   
         dimdboldbitalici	underlineublinkblink2reverserconcealcstrikes
underline2frameencircleoverline)uur`   ra   rb   oN)colorbgcolorrQ   rO   rS   rU   rW   rX   rY   r[   r]   r_   r`   ra   rb   linkmetare   rf   rg   rh   c                C   s  d | _ d | _dtttf dtfdd}|d u rd n||| _|d u r$d n||| _t|d u|d uo2d|d uo7d|d uo<d|d uoAd|d uoFd	|	d uoKd
|
d uoPd|d uoUd|d uoZd|d uo_d|d uodd|d uoidf| _| jrt|rudpvd|rzdp{d|rdpd|rdpd|rdpd|rd	pd|	rd
pd|
rdpd|rdpd|rdpd|rdpd|rdpd|rdpdfnd| _	|| _
|rtdd nd| _|d u rd nt|| _t| j| j| j	| j|| jf| _| jp|p|p|p| | _d S )Nre   r   c                 S   s   t | tr| S t| S N)
isinstancer   parse)re   r   r   r   _make_color   s   z#Style.__init__.<locals>._make_colorrD   rF   rJ          @                     r   r   ?B  )r5   r6   r   r   strr.   r/   sumr$   r%   r3   r   r4   r   r2   hashr0   r1   )r   re   rf   rQ   rO   rS   rU   rW   rX   rY   r[   r]   r_   r`   ra   rb   rg   rh   rl   r   r   r   r    {   sj   











#













zStyle.__init__r   c                 C   s   t S )zCCreate an 'null' style, equivalent to Style(), but more performant.)
NULL_STYLE)clsr   r   r   null   s   z
Style.nullc                 C   sf   |  t}d|_d|_||_||_d|_d|_d|_d|_	d|_
t||ddddf|_|p.| |_|S )a  Create a new style with colors and no attributes.

        Returns:
            color (Optional[Color]): A (foreground) color, or None for no color. Defaults to None.
            bgcolor (Optional[Color]): A (background) color, or None for no color. Defaults to None.
        Nr   rw   )__new__r   r5   r6   r.   r/   r$   r%   r3   r4   r2   rz   r0   r1   )r|   re   rf   styler   r   r   
from_color   s*   


zStyle.from_colorc                 C   sh   |  t}d|_d|_d|_d|_d|_d|_d|_d|_	t
||_tddddd|jf|_| |_|S )zCreate a new style with meta data.

        Returns:
            meta (Optional[Dict[str, Any]]): A dictionary of meta data. Defaults to None.
        Nr   rw   )r~   r   r5   r6   r.   r/   r$   r%   r3   r4   r   r2   rz   r0   r1   )r|   rh   r   r   r   r   	from_meta   s*   


zStyle.from_metahandlersc                 K   s2   |du ri n|}| dd | D  | |S )a  Create a blank style with meta information.

        Example:
            style = Style.on(click=self.on_click)

        Args:
            meta (Optiona[Dict[str, Any]], optional): An optional dict of meta information.
            **handlers (Any): Keyword arguments are translated in to handlers.

        Returns:
            Style: A Style with meta information attached.
        Nc                 S   s   i | ]
\}}d | |qS )@r   ).0keyvaluer   r   r   
<dictcomp>!  s    zStyle.on.<locals>.<dictcomp>)updateitemsr   )r|   rh   r   r   r   r   on  s   
zStyle.onr   r   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   c                 C      | j S )z+Get a link id, used in ansi code for links.)r4   r   r   r   r   link_id2     zStyle.link_idc                 C   s  | j du rg }|j}| j}|d@ rE|d@ r|| jrdnd |d@ r+|| jr(dnd |d	@ r8|| jr5d
nd |d@ rE|| jrBdnd |d@ r|d@ rV|| jrSdnd |d@ rc|| jr`dnd |d@ rp|| j	rmdnd |d@ r}|| j
rzdnd |d@ r|| jrdnd |d@ r|d @ r|| jrd!nd" |d#@ r|| jrd$nd% |d&@ r|| jrd'nd( |d)@ r|| jrd*nd+ | jdur|| jj | jdur|d, || jj | jr|d- || j d.|pd/| _ | j S )0z-Re-generate style definition from attributes.N   r   rQ   znot boldrD   rO   znot dimrF   rS   z
not italicrJ   rU   znot underline  rm   rW   z	not blinkrn   rX   z
not blink2ro   rY   znot reverserp   r[   znot concealrq   r]   z
not strike   rr   r_   znot underline2rs   r`   z	not framert   ra   znot encircleru   rb   znot overliner   rg    none)r6   appendr$   rQ   rO   rS   rU   rW   rX   rY   r[   r]   r_   r`   ra   rb   r.   namer/   r3   join)r   
attributesr   bitsr   r   r   __str__7  sV   



zStyle.__str__c                 C   s   | j  S )z;A Style is false if it has no attributes, colors, or links.)r1   r   r   r   r   __bool__e  s   zStyle.__bool__color_systemc                 C   s0  | j du rg }|j}| j}| j| j@ }|rm|d@ r||d  |d@ r)||d  |d@ r3||d  |d@ r=||d  |d@ rUtdd	D ]}|d|> @ rT|||  qF|d
@ rmtd	dD ]}|d|> @ rl|||  q^| jdur}|| j|	  | j
dur|| j
|j	dd d|| _ | j S )zGenerate ANSI codes for this style.

        Args:
            color_system (ColorSystem): Color system.

        Returns:
            str: String containing codes.
        Nr   r   rD   rF   rJ   rE   r   rK   r      F
foreground;)r5   r   
_style_mapr%   r$   ranger.   extend	downgradeget_ansi_codesr/   r   )r   r   sgrr   r   r   r   r   r   r   _make_ansi_codesi  sD   
	

zStyle._make_ansi_codesrs   )maxsizer   c                 C   s2   zt | |W S  tjy   |   Y S w )zNormalize a style definition so that styles with the same effect have the same string
        representation.

        Args:
            style (str): A style definition.

        Returns:
            str: Normal form of style definition.
        )rx   rk   r   StyleSyntaxErrorstriplower)r|   r   r   r   r   	normalize  s
   zStyle.normalizevaluesc                 G   s"   |D ]
}|dur|  S qt d)zPick first non-None style.Nz$expected at least one non-None style)
ValueError)r|   r   r   r   r   r   
pick_first  s
   zStyle.pick_firstc                 c   s    d| j d fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  d	| jd fV  d
| j	d fV  d| j
d fV  d| jd fV  d| jd fV  d| jd fV  d| jd fV  | jrud| jfV  d S d S )Nre   rf   rQ   rO   rS   rU   rW   rX   rY   r[   r]   r_   r`   ra   rg   rh   )re   rf   rQ   rO   rS   rU   rW   rX   rY   r[   r]   r_   r`   ra   rg   r2   rh   r   r   r   r   __rich_repr__  s&   zStyle.__rich_repr__otherc                 C   sV   t |tstS | j|jko*| j|jko*| j|jko*| j|jko*| j|jko*| j|jkS ri   )	rj   r   NotImplementedr.   r/   r$   r%   r3   r2   )r   r   r   r   r   __eq__  s   





zStyle.__eq__c                 C   r   ri   )r0   r   r   r   r   __hash__  s   zStyle.__hash__c                 C   r   )z.The foreground color or None if it is not set.)r.   r   r   r   r   re     r   zStyle.colorc                 C   r   )z.The background color or None if it is not set.)r/   r   r   r   r   rf     r   zStyle.bgcolorc                 C   r   )zLink text, if set.)r3   r   r   r   r   rg     r   z
Style.linkc                 C   s   | j du p| j jS )z6Check if the style specified a transparent background.N)rf   
is_defaultr   r   r   r   transparent_background  s   zStyle.transparent_backgroundc                 C   s   t | jdS )zA Style with background only.)rf   )r   rf   r   r   r   r   background_style  s   zStyle.background_stylec                 C   s&   | j du ri S ttttf t| j S )z=Get meta information (can not be changed after construction).N)r2   r   r   rx   r   r   r   r   r   r   rh     s   &z
Style.metac                 C   st   | j rtS | t}d|_d|_d|_d|_| j|_| j	|_	| j
|_
| j
r+tdd nd|_| j|_d|_ d|_|S )z+Get a copy of the style with color removed.Nr   rv   rw   Fr1   r{   r~   r   r5   r6   r.   r/   r%   r$   r3   r   r4   r0   r2   r   r   r   r   r   without_color  s   
zStyle.without_colorru   style_definitionc                 C   s  |  dks|s|  S | j}d}d}i }d}t| }|D ]}| }	|	dkrZt|d}	|	s5tdz	t	
|	du  W n tyV }
 ztd|	d|
 dd}
~
ww |	}q|	dkryt|d}	||	}|du rttd	|	d
||< q|	dkrt|d}	|	std|	}q|	|v rd|||	 < qzt	
|	 W n ty }
 ztd|	d|
 dd}
~
ww |	}qtd|||d|}|S )a  Parse a style definition.

        Args:
            style_definition (str): A string containing a style.

        Raises:
            errors.StyleSyntaxError: If the style definition syntax is invalid.

        Returns:
            `Style`: A Style instance.
        r   Nr   rw   zcolor expected after 'on'zunable to parse z as background color; notz,expected style attribute after 'not', found Frg   zURL expected after 'link'Tz as color; )re   rf   rg   r   )r   r}   STYLE_ATTRIBUTESitersplitr   nextr   r   r   rk   r   getr   )r|   r   r   re   rf   r   rg   wordsoriginal_wordworderror	attributer   r   r   r   rk     sj   






zStyle.parsethemec                 C   s  |pt }g }|j}| j}| j}| jr||}}| jr0|du r!|jn||}t	t
||jd}|durI||}|d|j  |d|j  |dur\|j|dd}|d|j  | jrc|d | jrj|d	 | jrq|d
 | jrx|d | jr|d d|S )zGet a CSS style rule.Ng      ?zcolor: ztext-decoration-color: Fr   zbackground-color: zfont-weight: boldzfont-style: italicztext-decoration: underlineztext-decoration: line-throughztext-decoration: overlinez; )r   r   re   rf   rY   rO   foreground_colorget_truecolorr   from_tripletr   background_colorhexrQ   rS   rU   r]   rb   r   )r   r   cssr   re   rf   r   theme_colorr   r   r   get_html_styleB  s>   


zStyle.get_html_stylestylesc                 C      t |}t|t|S )zCombine styles and get result.

        Args:
            styles (Iterable[Style]): Styles to combine.

        Returns:
            Style: A new style instance.
        r   ry   r   r|   r   iter_stylesr   r   r   combineg     
zStyle.combinec                 G   r   )zCombine styles from positional argument in to a single style.

        Args:
            *styles (Iterable[Style]): Styles to combine.

        Returns:
            Style: A new style instance.
        r   r   r   r   r   chaint  r   zStyle.chainc                 C   s~   | j rtS | t}| j|_| j|_| j|_| j|_| j|_| j	|_	| j
|_
| j
r/tdd nd|_| j|_d|_ | j|_|S )zwGet a copy of this style.

        Returns:
            Style: A new Style instance with identical attributes.
        r   rv   rw   Fr   r   r   r   r   copy  s   
z
Style.copyc                 C   sp   |  t}| j|_| j|_| j|_| j|_| j|_| j|_||_|r(t	dd nd|_
| j|_d|_| j|_|S )zGet a copy with a different value for link.

        Args:
            link (str, optional): New value for link. Defaults to None.

        Returns:
            Style: A new Style instance.
        r   rv   rw   F)r~   r   r5   r6   r.   r/   r%   r$   r3   r   r4   r0   r1   r2   )r   rg   r   r   r   r   update_link  s   
	zStyle.update_linkrw   F)r   legacy_windowstextr   c                C   s^   |r|du r|S |  |}|rd| d| dn|}| jr-|s-d| j d| j d| d}|S )	aD  Render the ANSI codes for the style.

        Args:
            text (str, optional): A string to style. Defaults to "".
            color_system (Optional[ColorSystem], optional): Color system to render to. Defaults to ColorSystem.TRUECOLOR.

        Returns:
            str: A string containing ANSI style codes.
        Nz[mz[0mz]8;id=r   z\z]8;;\)r   r3   r4   )r   r   r   r   attrsrenderedr   r   r   render  s   

zStyle.renderc                 C   s(   |pt | }tj| | d dS )zWrite text with style directly to terminal.

        This method is for testing purposes only.

        Args:
            text (Optional[str], optional): Text to style or None for style name.

        
N)rx   sysstdoutwriter   )r   r   r   r   r   test  s   	z
Style.testc                 C   s   t |ts|d u stS |d u s|jr| S | jr|S | t}d |_d |_|jp)| j|_|jp0| j|_| j	|j
 @ |j	|j
@ B |_	| j
|j
B |_
|jpL| j|_|jpS| j|_|j|_| jp^|j|_| jrs|jrsti | j|j|_|S | jpx|j|_|S ri   )rj   r   r   r1   r~   r5   r6   r.   r/   r%   r$   r3   r4   r0   r2   r   rh   )r   r   	new_styler   r   r   __add__  s0   

zStyle.__add__)r   r   )NNri   )r   r   r   r   )rw   )Fr'   r(   r)   r*   r   r   __annotations__r,   r-   bytesr+   r   r   r   rx   r   r   r    classmethodr}   r   r   r   r   rQ   rO   rS   rU   rW   rX   rY   r[   r]   r_   r`   ra   rb   propertyr   r   r   r   r   r   r   	StyleTyper   r   r   r   r   re   rf   rg   r   r   rh   r   rk   r   r   r	   r   r   r   r   	TRUECOLORr   r   r   r   r   r   r   r      s  
 	
	

S  &.*A$

c                   @   s`   e Zd ZdZdgZdddZdefd	d
Zede	fddZ
de	ddfddZde	fddZdS )
StyleStackzA stack of styles._stackdefault_styler   r   Nc                 C   s   |g| _ d S ri   r   )r   r   r   r   r   r      s   zStyleStack.__init__c                 C   s   d| j dS )Nz<stylestack >r   r   r   r   r   __repr__  r!   zStyleStack.__repr__c                 C   s
   | j d S )z&Get the Style at the top of the stack.r   r   r   r   r   current  s   
zStyleStack.currentr   c                 C   s   | j | j d |  dS )z}Push a new style on to the stack.

        Args:
            style (Style): New style to combine with current style.
        r   N)r   r   r   r   r   r   push  s   zStyleStack.pushc                 C   s   | j   | j d S )z}Pop last style and discard.

        Returns:
            Style: New current style (also available as stack.current)
        r   )r   popr   r   r   r   r   
  s   

zStyleStack.pop)r   r   r   N)r'   r(   r)   r*   r+   r    rx   r   r   r   r   r   r   r   r   r   r   r     s    
r   )$r   	functoolsr   marshalr   r   randomr   typingr   r   r   r	   r
   r   r   r   rw   r   re   r   r   r   r   reprr   r   terminal_themer   r   rx   r   r   r   r{   r   r   r   r   r   <module>   s(    (     T