a
    baini                     @   s   d dl 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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dddddd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)
attrgetter)dumpsloads)randint)AnyDictIterableListOptionalTypeUnioncast   )errors)ColorColorParseErrorColorSystem	blend_rgb)Result	rich_repr)DEFAULT_TERMINAL_THEMETerminalTheme_color_bgcolor_attributes_set_attributes_link_metaStylec                   @   s@   e Zd ZdZdgZeddddZded ee	 dd	d
Z
dS )_Bitz.A descriptor to get/set a style attribute bit.bitN)bit_noreturnc                 C   s   d|> | _ d S )Nr   )r!   )selfr"    r%   P/var/www/html/SiteWeb/venv/lib/python3.9/site-packages/pip/_vendor/rich/style.py__init__   s    z_Bit.__init__r   )objobjtyper#   c                 C   s    |j | j@ r|j| j@ dkS d S )Nr   )r   r!   r   )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 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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'eeeef  eeeef  ee ee ee ee ee ee ee ee ee ee ee ee ee ee eeeef  d'd(d)Zed d*d+d,Zedee ee d d-d.d/Zeeeeef  d d0d1d2Zedeeeef  ed d3d4d5Zed6Zed7Zed8Zed9Zed:Zed;Zed<Zed=Zed>Z ed?Z!ed@Z"edAZ#edBZ$e%ed*dCdDZ&ed*dEdFZ'ed*dGdHZ(e)edIdJdKZ*ee+dLdMeedNdOdPZ,eee- e-dQdRdSZ.e/d*dTdUZ0eedVdWdXZ1eedVdYdZZ2ed*d[d\Z3e%ee d*d]d^Z4e%ee d*d_d`Z5e%ee d*dadbZ6e%ed*dcddZ7e%d d*dedfZ8e%eeef d*dgdhZ9e%d d*didjZ:ee+dkdMed dldmdnZ;e+dLdMdee< edodpdqZ=ee>d  d drdsdtZ?ed d drdudvZ@d d*dwdxZAe+dydMd d*dzd{ZBdee d d|d}d~ZCde)jDddeee) eedddZEdee d&dddZFe+dLdMed  d dNddZGed  d dNddZHd&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.

    r   r   r   r   _hash_nullr   )r   r   r   r   r   _link_id_ansi_style_definitionr2   r3   r   123456789Z21Z51Z52Z53)r   r                        	   
         dimbolditalic	underlineblinkblink2reverseconcealstrike
underline2frameencircleoverline)rK   drL   brM   irN   urO   rP   rQ   rrR   crS   srT   uurU   rV   rW   oN)colorbgcolorrL   rK   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   linkmetac                C   s  d | _ d | _tttf tddd}|d u r0d n||| _|d u rFd n||| _t|d u|d uobd|d uold|d uovd|d uod|d uod|	d uod	|
d uod
|d uod|d uod|d uod|d uod|d uodf| _| jrt|rdpd|rdpd|odpd|rdpd|rdpd|r*dp,d|	r8d	p:d|
rFd
pHd|rTdpVd|rbdpdd|rpdprd|r~dpd|rdpdfnd| _	|| _
|d u rd nt|| _|s|rtdd t| j nd| _d | _| jp|p|p|p| | _d S )N)ra   r#   c                 S   s   t | tr| S t| S N)
isinstancer   parse)ra   r%   r%   r&   _make_color   s    z#Style.__init__.<locals>._make_colorr@   rB   rF          @                     r   r   ?B  )r5   r6   r   r   strr   r   sumr   r   r   r   r   r   hashr4   r2   r3   )r$   ra   rb   rL   rK   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rc   rd   rh   r%   r%   r&   r'      s\    











#

&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.null)ra   rb   r#   c                 C   sV   |  t}d|_d|_||_||_d|_d|_d|_d|_	d|_
|pF| |_d|_|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   rs   )__new__r   r5   r6   r   r   r   r   r   r4   r   r3   r2   )ry   ra   rb   styler%   r%   r&   
from_color   s    

zStyle.from_color)rd   r#   c                 C   sj   |  t}d|_d|_d|_d|_d|_d|_d|_t	||_
tdd t|j
 |_d|_| |_|S )zCreate a new style with meta data.

        Returns:
            meta (Optional[Dict[str, Any]]): A dictionary of meta data. Defaults to None.
        Nr   rr   )r{   r   r5   r6   r   r   r   r   r   r   r   r   rv   r4   r2   r3   )ry   rd   r|   r%   r%   r&   	from_meta   s    

zStyle.from_meta)rd   handlersr#   c                 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 (Optional[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>      zStyle.on.<locals>.<dictcomp>)updateitemsr~   )ry   rd   r   r%   r%   r&   on   s    zStyle.onr   r   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   c                 C   s   | j S )z+Get a link id, used in ansi code for links.)r4   r$   r%   r%   r&   link_id  s    zStyle.link_idc                 C   s  | j du rg }|j}| j}|d@ r|d@ r>|| jr8dnd |d@ rX|| jrRdnd |d	@ rr|| jrld
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 |d@ r|| jrdnd |d@ r|d @ rD|| jr>d!nd" |d#@ rb|| jr\d$nd% |d&@ r|| jrzd'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   rL   znot boldr@   rK   znot dimrB   rM   z
not italicrF   rN   znot underline  ri   rO   z	not blinkrj   rP   z
not blink2rk   rQ   znot reverserl   rR   znot concealrm   rS   z
not strike   rn   rT   znot underline2ro   rU   z	not framerp   rV   znot encirclerq   rW   znot overliner   rc    none)r6   appendr   rL   rK   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   r   namer   r   join)r$   
attributesr   bitsr%   r%   r&   __str__"  sV    







zStyle.__str__c                 C   s   | j  S )z;A Style is false if it has no attributes, colors, or links.)r3   r   r%   r%   r&   __bool__P  s    zStyle.__bool__)color_systemr#   c                 C   s4  | j du r.g }|j}| j}| j| j@ }|r|d@ r@||d  |d@ rT||d  |d@ rh||d  |d@ r|||d  |d@ rtdd	D ]}|d|> @ r|||  q|d
@ rtd	dD ]}|d|> @ r|||  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   r@   rB   rF   rA   r   rG   r      FZ
foreground;)r5   r   
_style_mapr   r   ranger   extendZ	downgradeZget_ansi_codesr   r   )r$   r   Zsgrr   r   r   r!   r%   r%   r&   _make_ansi_codesT  s@    

zStyle._make_ansi_codesro   )maxsizer|   r#   c                 C   s6   zt | |W S  tjy0   |   Y S 0 dS )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.
        N)rt   rg   r   StyleSyntaxErrorstriplower)ry   r|   r%   r%   r&   	normalize  s    zStyle.normalize)valuesr#   c                 G   s&   |D ]}|dur|  S qt ddS )zPick first non-None style.Nz$expected at least one non-None style)
ValueError)ry   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rd| jfV  d S )Nra   rb   rL   rK   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rc   rd   )ra   rb   rL   rK   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rc   r   rd   r   r%   r%   r&   __rich_repr__  s"    zStyle.__rich_repr__)otherr#   c                 C   s   t |tstS |  | kS re   rf   r   NotImplemented__hash__r$   r   r%   r%   r&   __eq__  s    
zStyle.__eq__c                 C   s   t |tstS |  | kS re   r   r   r%   r%   r&   __ne__  s    
zStyle.__ne__c                 C   s$   | j d ur| j S tt| | _ | j S re   )r2   rv   _hash_getterr   r%   r%   r&   r     s    
zStyle.__hash__c                 C   s   | j S )z.The foreground color or None if it is not set.)r   r   r%   r%   r&   ra     s    zStyle.colorc                 C   s   | j S )z.The background color or None if it is not set.)r   r   r%   r%   r&   rb     s    zStyle.bgcolorc                 C   s   | j S )zLink text, if set.)r   r   r%   r%   r&   rc     s    z
Style.linkc                 C   s   | j du p| j jS )z6Check if the style specified a transparent background.N)rb   Z
is_defaultr   r%   r%   r&   transparent_background  s    zStyle.transparent_backgroundc                 C   s   t | jdS )zA Style with background only.)rb   )r   rb   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)r   r   r   rt   r   r   r   r%   r%   r&   rd     s    z
Style.metac                 C   sr   | j r
tS | t}d|_d|_d|_d|_| j|_| j	|_	| j
|_
| j
rVtdd nd|_d|_ d|_d|_|S )z+Get a copy of the style with color removed.Nr   rr   rs   F)r3   rx   r{   r   r5   r6   r   r   r   r   r   r   r4   r   r2   r$   r|   r%   r%   r&   without_color  s    
zStyle.without_colorrq   )style_definitionr#   c                 C   s  |  dks|s|  S | j}d}d}i }d}t| }|D ]P}| }	|	dkrt|d}	|	sltdzt	
|	 W n< ty }
 z$td|	d|
 dW Y d}
~
n
d}
~
0 0 |	}q>|	dkrt|d}	||	}|du rtd	|	d
||< q>|	dkr&t|d}	|	s td|	}q>|	|v r>d|||	 < q>zt	
|	 W n> ty }
 z$td|	d|
 dW Y d}
~
n
d}
~
0 0 |	}q>tf |||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   rs   zcolor expected after 'on'zunable to parse z as background color; notz,expected style attribute after 'not', found Frc   zURL expected after 'link'Tz as color; )ra   rb   rc   )r   rz   STYLE_ATTRIBUTESitersplitr   nextr   r   r   rg   r   getr   )ry   r   r   ra   rb   r   rc   wordsZoriginal_wordworderror	attributer|   r%   r%   r&   rg     sb    









zStyle.parse)themer#   c                 C   s
  |pt }g }|j}| j}| j}| jr.|| }}| jr`|du rB|jn||}t	t
||jd}|dur||}|d|j  |d|j  |dur|j|dd}|d|j  | jr|d | jr|d	 | jr|d
 | jr|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   ra   rb   rQ   rK   foreground_colorZget_truecolorr   Zfrom_tripletr   Zbackground_colorhexrL   rM   rN   rS   rW   r   )r$   r   cssr   ra   rb   r   Ztheme_colorr%   r%   r&   get_html_style/  s>    

zStyle.get_html_style)stylesr#   c                 C   s   t |}t|t|S )zCombine styles and get result.

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

        Returns:
            Style: A new style instance.
        r   ru   r   ry   r   Ziter_stylesr%   r%   r&   combineT  s    
zStyle.combinec                 G   s   t |}t|t|S )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&   chaina  s    
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   rr   rs   F)r3   rx   r{   r   r5   r6   r   r   r   r   r   r   r4   r2   r   r   r%   r%   r&   copyn  s    
z
Style.copyrl   c                 C   sf   | j r
tS | t}| j|_| j|_| j|_| j|_| j|_| j	|_	d|_
d|_d|_d|_ d|_|S )zGet a copy of this style with link and meta information removed.

        Returns:
            Style: New style object.
        Nrs   F)r3   rx   r{   r   r5   r6   r   r   r   r   r   r4   r2   r   r   r%   r%   r&   clear_meta_and_links  s    
zStyle.clear_meta_and_links)rc   r#   c                 C   sn   |  t}| j|_| j|_| j|_| j|_| j|_| j|_||_|rPt	dd nd|_
d|_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   rr   rs   NF)r{   r   r5   r6   r   r   r   r   r   r   r4   r2   r3   r   )r$   rc   r|   r%   r%   r&   update_link  s    	
zStyle.update_linkrs   F)r   legacy_windows)textr   r   r#   c                C   sd   |r|du r|S | j p| |}|r6d| 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;;\)r5   r   r   r4   )r$   r   r   r   attrsrenderedr%   r%   r&   render  s    
zStyle.render)r   r#   c                 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)rt   sysstdoutwriter   )r$   r   r%   r%   r&   test  s    	z
Style.testc                 C   s   |d u s|j r| S | j r|S | t}d |_d |_|jp<| j|_|jpJ| j|_| j|j @ |j|j@ B |_| j|jB |_|j	p| j	|_	|j
p| j
|_
|j |_ | jr|jrti | j|j|_n| jp|j|_d |_|S re   )r3   r{   r   r5   r6   r   r   r   r   r   r4   r   r   rd   r2   )r$   r|   Z	new_styler%   r%   r&   _add  s*    

z
Style._addc                 C   s   |  |}|jr| S |S re   )r   rc   r   )r$   r|   Zcombined_styler%   r%   r&   __add__  s    
zStyle.__add__)NN)N)N)N)rs   )N)Ir+   r,   r-   r.   r   r   __annotations__r0   r1   bytesr/   r   r   r   rt   r   r   r'   classmethodrz   r}   r~   r   r    rL   rK   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   propertyr   r   r   r   r   r   r   	StyleTyper   r   r   r   r   r   ra   rb   rc   r   r   rd   r   rg   r   r   r	   r   r   r   r   r   Z	TRUECOLORr   r   r   r   r%   r%   r%   r&   r   #   sD  
L ".+A$ c                   @   sd   e Zd ZdZdgZdddddZedd	d
Zee	dddZ
e	ddddZe	dddZdS )
StyleStackzA stack of styles._stackr   N)default_styler#   c                 C   s   |g| _ d S re   r   )r$   r   r%   r%   r&   r'     s    zStyleStack.__init__rw   c                 C   s   d| j dS )Nz<stylestack >r   r   r%   r%   r&   __repr__  s    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-   r.   r/   r'   rt   r   r   r   r   r   r   r%   r%   r%   r&   r     s   r   )'r   	functoolsr   operatorr   pickler   r   randomr   typingr   r   r	   r
   r   r   r   r   rs   r   ra   r   r   r   r   reprr   r   Zterminal_themer   r   r   rt   r   r    r   rx   r   r%   r%   r%   r&   <module>   s.   (     W