U
    &%ei                     @   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)dumpsloads)randint)AnyDictIterableListOptionalTypeUnioncast   )errors)ColorColorParseErrorColorSystem	blend_rgb)Result	rich_repr)DEFAULT_TERMINAL_THEMETerminalTheme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   U/var/www/html/Darija-Ai-API/env/lib/python3.8/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   )_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 ed< eed< ee	 ed< ddddd	d
dddddg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 d0d1d2Zeeeeef  d d3d4d5Zedeeeef  ed d6d7d8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dCZ"edDZ#edEZ$e%ed-dFdGZ&ed-dHdIZ'ed-dJdKZ(e)edLdMdNZ*ee+dOdPeedQdRdSZ,eee- e-dTdUdVZ.e/d-dWdXZ0eedYdZd[Z1eedYd\d]Z2ed-d^d_Z3e%ee d-d`daZ4e%ee d-dbdcZ5e%ee d-dddeZ6e%ed-dfdgZ7e%d d-dhdiZ8e%eeef d-djdkZ9e%d d-dldmZ:ee+dndPed dodpdqZ;e+dOdPdee< edrdsdtZ=ee>d  d dudvdwZ?ed d dudxdyZ@d d-dzd{ZAe+d|dPd d-d}d~ZBdee d dddZCde)jDddeee) eedddZEdee d)dddZFe+dOdPed  d dQddZGed  d dQ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.

    _color_bgcolorr$   r#   _hash_null_meta_link_link_id_ansi_style_definition12345678921515253)r   r                        	   
         dimbolditalic	underlineblinkblink2reverseconcealstrike
underline2frameencircleoverline)rN   drO   brP   irQ   urR   rS   rT   rrU   crV   srW   uurX   rY   rZ   oN)colorbgcolorrO   rN   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   linkmetac                C   s  d | _ d | _tttf tddd}|d kr0d n||| _|d krFd n||| _t|d k	|d k	obd|d k	old|d k	ovd|d k	od|d k	od|	d k	od	|
d k	od
|d k	od|d k	od|d k	od|d k	od|d k	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 krd nt|| _|s|rtdd t| j nd| _d | _| jp|p|p|p| | _d S )N)rd   r   c                 S   s   t | tr| S t| S N)
isinstancer   parse)rd   r   r   r   _make_color   s    z#Style.__init__.<locals>._make_colorrC   rE   rI          @                     r   r   ?B  )r4   r5   r   r   strr-   r.   sumr#   r$   r2   r   r1   r   hashr3   r/   r0   )r   rd   re   rO   rN   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   rf   rg   rk   r   r   r   r    z   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)rd   re   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   rv   )__new__r   r4   r5   r-   r.   r#   r$   r2   r3   r1   r0   r/   )r|   rd   re   styler   r   r   
from_color   s    

zStyle.from_color)rg   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   ru   )r~   r   r4   r5   r-   r.   r#   r$   r2   r   r1   r   ry   r3   r/   r0   )r|   rg   r   r   r   r   	from_meta   s    

zStyle.from_meta)rg   handlersr   c                 K   s2   |dk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>  s      zStyle.on.<locals>.<dictcomp>)updateitemsr   )r|   rg   r   r   r   r   on   s    zStyle.onr   r   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   c                 C   s   | j S )z+Get a link id, used in ansi code for links.)r3   r   r   r   r   link_id  s    zStyle.link_idc                 C   s  | j dk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k	r|| jj | jdk	r|d, || jj | jr|d- || j d.|pd/| _ | j S )0z-Re-generate style definition from attributes.N   r   rO   znot boldrC   rN   znot dimrE   rP   z
not italicrI   rQ   znot underline  rl   rR   z	not blinkrm   rS   z
not blink2rn   rT   znot reversero   rU   znot concealrp   rV   z
not strike   rq   rW   znot underline2rr   rX   z	not framers   rY   znot encirclert   rZ   znot overliner   rf    none)r5   appendr#   rO   rN   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r-   namer.   r2   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.)r0   r   r   r   r   __bool__K  s    zStyle.__bool__)color_systemr   c                 C   s4  | j dk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k	r|| j|	  | j
dk	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   rC   rE   rI   rD   r   rJ   r      F
foreground;)r4   r   
_style_mapr$   r#   ranger-   extend	downgradeget_ansi_codesr.   r   )r   r   sgrr   r   r   r   r   r   r   _make_ansi_codesO  s@    

zStyle._make_ansi_codesrr   )maxsizer   r   c                 C   s8   zt | |W S  tjk
r2   |   Y S X 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)rw   rj   r   StyleSyntaxErrorstriplower)r|   r   r   r   r   	normalizez  s    zStyle.normalize)valuesr   c                 G   s&   |D ]}|dk	r|  S qt ddS )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rd| jfV  d S )Nrd   re   rO   rN   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rf   rg   )rd   re   rO   rN   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rf   r1   rg   r   r   r   r   __rich_repr__  s"    zStyle.__rich_repr__)otherr   c                 C   s   t |tstS |  | kS rh   ri   r   NotImplemented__hash__r   r   r   r   r   __eq__  s    
zStyle.__eq__c                 C   s   t |tstS |  | kS rh   r   r   r   r   r   __ne__  s    
zStyle.__ne__c                 C   s8   | j d k	r| j S t| j| j| j| j| j| jf| _ | j S rh   )r/   ry   r-   r.   r$   r#   r2   r1   r   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   rd     s    zStyle.colorc                 C   s   | j S )z.The background color or None if it is not set.)r.   r   r   r   r   re     s    zStyle.bgcolorc                 C   s   | j S )zLink text, if set.)r2   r   r   r   r   rf     s    z
Style.linkc                 C   s   | j dkp| j jS )z6Check if the style specified a transparent background.N)re   
is_defaultr   r   r   r   transparent_background  s    zStyle.transparent_backgroundc                 C   s   t | jdS )zA Style with background only.)re   )r   re   r   r   r   r   background_style  s    zStyle.background_stylec                 C   s&   | j dkri S ttttf t| j S )z=Get meta information (can not be changed after construction).N)r1   r   r   rw   r   r   r   r   r   r   rg     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   ru   rv   F)r0   r{   r~   r   r4   r5   r-   r.   r$   r#   r2   r   r3   r1   r/   r   r   r   r   r   without_color  s    
zStyle.without_colorrt   )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	
|	dk W n: tk
r }
 ztd|	d|
 dW 5 d}
~
X Y nX |	}q>|	dkrt|d}	||	}|dkrtd	|	d
||< q>|	dkr(t|d}	|	s"td|	}q>|	|kr@d|||	 < q>zt	
|	 W n< tk
r }
 ztd|	d|
 dW 5 d}
~
X Y nX |	}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   rv   zcolor expected after 'on'zunable to parse z as background color; notz,expected style attribute after 'not', found Frf   zURL expected after 'link'Tz as color; )rd   re   rf   )r   r}   STYLE_ATTRIBUTESitersplitr   nextr   r   r   rj   r   getr   )r|   r   r   rd   re   r   rf   wordsoriginal_wordworderror	attributer   r   r   r   rj     sb    









zStyle.parse)themer   c                 C   s
  |pt }g }|j}| j}| j}| jr.|| }}| jr`|dkrB|jn||}t	t
||jd}|dk	r||}|d|j  |d|j  |dk	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   rd   re   rT   rN   foreground_colorget_truecolorr   from_tripletr   background_colorhexrO   rP   rQ   rV   rZ   r   )r   r   cssr   rd   re   r   theme_colorr   r   r   get_html_style3  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   rx   r   r|   r   iter_stylesr   r   r   combineX  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   chaine  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   ru   rv   F)r0   r{   r~   r   r4   r5   r-   r.   r$   r#   r2   r   r3   r/   r1   r   r   r   r   copyr  s    
z
Style.copyro   c                 C   sh   | j r
tS | t}| j|_| j|_| j|_| j|_| j|_| j	|_	d|_
d|_| j|_d|_ d|_|S )zGet a copy of this style with link and meta information removed.

        Returns:
            Style: New style object.
        Nrv   F)r0   r{   r~   r   r4   r5   r-   r.   r$   r#   r2   r3   r/   r1   r   r   r   r   clear_meta_and_links  s    
zStyle.clear_meta_and_links)rf   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   ru   rv   NF)r~   r   r4   r5   r-   r.   r$   r#   r2   r   r3   r/   r0   r1   )r   rf   r   r   r   r   update_link  s    	
zStyle.update_linkrv   F)r   legacy_windows)textr   r   r   c                C   sd   |r|dk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;;\)r4   r   r2   r3   )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)rw   sysstdoutwriter   )r   r   r   r   r   test  s    	z
Style.testc                 C   s   |d k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| j|j|_n| jp|j|_d |_|S rh   )r0   r~   r   r4   r5   r-   r.   r$   r#   r2   r3   r1   r   rg   r/   )r   r   	new_styler   r   r   _add  s*    

z
Style._addc                 C   s   |  |}|jr| S |S rh   )r   rf   r   )r   r   combined_styler   r   r   __add__  s    
zStyle.__add__)NN)N)N)N)rv   )N)Ir&   r'   r(   r)   r
   r   __annotations__r+   r,   bytesr*   r   r   r   rw   r   r   r    classmethodr}   r   r   r   r   rO   rN   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   propertyr   r   r   r   r   r   r   	StyleTyper   r   r   r   r   r   rd   re   rf   r   r   rg   r   rj   r   r   r   r   r   r   r   r   	TRUECOLORr   r   r   r   r   r   r   r   r      s^  
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 rh   r   )r   r   r   r   r   r      s    zStyleStack.__init__rz   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    rw   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   rv   r   rd   r   r   r   r   reprr   r   terminal_themer   r   rw   r   r   r   r{   r   r   r   r   r   <module>   s&   (     `