a
    õ£Beò2  ã                   @  s@  d Z ddlmZ ddlZ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 dd	lmZ g d
¢Zdd„ e ¡ D ƒZdddœdd„Zedddddddddd�	Zdddœdd„Zdddœdd„Ze d¡ZG dd„ deƒZejZG dd„ deƒZ e	dƒZ!d dd!œd"d#„Z"d$d%d&œd'd(„Z#G d)d%„ d%eƒZ$dS )*z-
Tool for creating styles from a dictionary.
é    )ÚannotationsN)ÚEnum)ÚHashableÚTypeVar)ÚSimpleCacheé   )ÚANSI_COLOR_NAMESÚANSI_COLOR_NAMES_ALIASESÚDEFAULT_ATTRSÚAttrsÚ	BaseStyle)ÚNAMED_COLORS)ÚStyleÚparse_colorÚPriorityÚmerge_stylesc                 C  s    i | ]\}}|  ¡ | d ¡“qS )ú#)ÚlowerÚlstrip)Ú.0ÚkÚv© r   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/styles/style.pyÚ
<dictcomp>   ó    r   Ústr)ÚtextÚreturnc                 C  sÔ   | t v r| S | tv rt|  S zt|  ¡  W S  ty<   Y n0 | dd… dkr¸| dd… }|t v rf|S |tv rvt| S t|ƒdkr†|S t|ƒdkrÄ|d d |d d  |d d  S n| dv rÄ| S td	|  ƒ‚dS )
z 
    Parse/validate color format.

    Like in Pygments, but also support the ANSI color names.
    (These will map to the colors of the 16 color palette.)
    r   r   r   Né   é   é   )Ú ÚdefaultzWrong color format %r)r   r	   Ú_named_colors_lowercaser   ÚKeyErrorÚlenÚ
ValueError)r   Úcolr   r   r   r       s*    &r   ©	ÚcolorÚbgcolorÚboldÚ	underlineÚstrikeÚitalicÚblinkÚreverseÚhiddenz	list[str])Ú	classnamer   c                 C  sF   g }|   d¡}tdt|ƒd ƒD ] }| d |d|… ¡ ¡ ¡ q |S )z…
    Split a single class name at the `.` operator, and build a list of classes.

    E.g. 'a.b.c' becomes ['a', 'a.b', 'a.b.c']
    Ú.r   N)ÚsplitÚranger&   ÚappendÚjoinr   )r3   ÚresultÚpartsÚir   r   r   Ú_expand_classname]   s
    
r<   r   )Ú	style_strr   c                 C  s   d| v rt }nt}|  ¡ D �]Þ}|dkr*q|dkr@|jdd�}q|dkrV|jdd�}q|dkrl|jdd�}q|d	kr‚|jdd�}q|d
kr˜|jdd�}q|dkr®|jdd�}q|dkrÄ|jdd�}q|dkrÚ|jdd�}q|dk� rò|jdd�}q|dk�r
|jdd�}q|dk�r"|jdd�}q|dk�r:|jdd�}q|dk�rR|jdd�}q|dk�rj|jdd�}q|dv �rvq| d¡�r„q| d¡�rž| d¡�ržq| d¡�rÄ|jt|dd… ƒd �}q| d!¡�rê|jt|dd… ƒd"�}q|jt|ƒd"�}q|S )#zd
    Take a style string, e.g.  'bg:red #88ff00 class:title'
    and return a `Attrs` instance.
    Ú	noinheritr,   T©r,   ÚnoboldFr/   ©r/   Únoitalicr-   ©r-   Únounderliner.   ©r.   Znostriker0   ©r0   Znoblinkr1   ©r1   Z	noreverser2   ©r2   Znohidden)ÚromanÚsansÚmonozborder:ú[ú]zbg:r    N©r+   zfg:©r*   )r
   Ú_EMPTY_ATTRSr5   Ú_replaceÚ
startswithÚendswithr   )r=   ÚattrsÚpartr   r   r   Ú_parse_style_strl   s\    






rV   z^[a-z0-9.\s_-]*$c                   @  s   e Zd ZdZdZdZdS )r   aÈ  
    The priority of the rules, when a style is created from a dictionary.

    In a `Style`, rules that are defined later will always override previous
    defined rules, however in a dictionary, the key order was arbitrary before
    Python 3.6. This means that the style could change at random between rules.

    We have two options:

    - `DICT_KEY_ORDER`: This means, iterate through the dictionary, and take
       the key/value pairs in order as they come. This is a good option if you
       have Python >3.6. Rules at the end will override rules at the beginning.
    - `MOST_PRECISE`: keys that are defined with most precision will get higher
      priority. (More precise means: more elements.)
    Z	KEY_ORDERÚMOST_PRECISEN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDICT_KEY_ORDERrW   r   r   r   r   r   ³   s   r   c                   @  sp   e Zd ZdZdddœdd„Zeddœdd	„ƒZeefd
dd dœdd„ƒZ	e
fddddœdd„Zddœdd„ZdS )r   aŒ  
    Create a ``Style`` instance from a list of style rules.

    The `style_rules` is supposed to be a list of ('classnames', 'style') tuples.
    The classnames are a whitespace separated string of class names and the
    style string is just like a Pygments style definition, but with a few
    additions: it supports 'reverse' and 'blink'.

    Later rules always override previous rules.

    Usage::

        Style([
            ('title', '#ff0000 bold underline'),
            ('something-else', 'reverse'),
            ('class1 class2', 'reverse'),
        ])

    The ``from_dict`` classmethod is similar, but takes a dictionary as input.
    úlist[tuple[str, str]]ÚNone)Ústyle_rulesr   c                 C  s^   g }|D ]D\}}t  |¡s&J t|ƒƒ‚t| ¡  ¡ ƒ}t|ƒ}| ||f¡ q|| _|| _	d S ©N)
ÚCLASS_NAMES_REÚmatchÚreprÚ	frozensetr   r5   rV   r7   Ú_style_rulesÚclass_names_and_attrs)Úselfr_   rf   Úclass_namesr=   Zclass_names_setrT   r   r   r   Ú__init__ã   s    zStyle.__init__©r   c                 C  s   | j S r`   )re   ©rg   r   r   r   r_   õ   s    zStyle.style_ruleszdict[str, str]r   )Ú
style_dictÚpriorityr   c                 C  sB   |t jkr.dddœdd„}| t| ¡ |d�ƒS | t| ¡ ƒƒS dS )za
        :param style_dict: Style dictionary.
        :param priority: `Priority` value.
        ztuple[str, str]Úint)Úitemr   c                 S  s   t dd„ | d  ¡ D ƒƒS )Nc                 s  s   | ]}t | d ¡ƒV  qdS )r4   N)r&   r5   )r   r;   r   r   r   Ú	<genexpr>  r   z/Style.from_dict.<locals>.key.<locals>.<genexpr>r   )Úsumr5   )ro   r   r   r   Úkey  s    zStyle.from_dict.<locals>.key)rr   N)r   rW   ÚsortedÚitemsÚlist)Úclsrl   rm   rr   r   r   r   Ú	from_dictù   s    
zStyle.from_dictr   r   ©r=   r#   r   c              	   C  s  |g}t ƒ }| jD ]\}}|s| |¡ q| ¡ D ]Ú}| d¡rúg }|dd…  ¡  d¡D ]}	| t|	ƒ¡ qZ|D ]„}
t ƒ }| t	|
gƒ¡ t
dt|ƒd ƒD ]*}t ||¡D ]}| t	||
f ƒ¡ q®qž| jD ]\}}||v rÐ| |¡ qÐ| |
¡ qrq2t|ƒ}| |¡ q2t|ƒS )z9
        Get `Attrs` for the given style string.
        zclass:r   Nú,r   )Úsetrf   r7   r5   rR   r   Úextendr<   Úaddrd   r6   r&   Ú	itertoolsÚcombinationsrV   Ú_merge_attrs)rg   r=   r#   Úlist_of_attrsrh   ÚnamesÚattrrU   Znew_class_namesÚpÚnew_nameZcombosÚcountÚc2Zinline_attrsr   r   r   Úget_attrs_for_style_str  s.    
zStyle.get_attrs_for_style_strr   c                 C  s
   t | jƒS r`   )Úidrf   rk   r   r   r   Úinvalidation_hash<  s    zStyle.invalidation_hashN)rX   rY   rZ   r[   ri   Úpropertyr_   ÚclassmethodÚdefault_priorityrw   r
   r‡   r‰   r   r   r   r   r   Í   s   ÿÿ1r   Ú_Tzlist[Attrs])r€   r   c                 C  sð   dddœdd„}t |dgdd„ | D ƒ¢R Ž |dgdd„ | D ƒ¢R Ž |d	gd
d„ | D ƒ¢R Ž |d	gdd„ | D ƒ¢R Ž |d	gdd„ | D ƒ¢R Ž |d	gdd„ | D ƒ¢R Ž |d	gdd„ | D ƒ¢R Ž |d	gdd„ | D ƒ¢R Ž |d	gdd„ | D ƒ¢R Ž d�	S )zÁ
    Take a list of :class:`.Attrs` instances and merge them into one.
    Every `Attr` in the list can override the styling of the previous one. So,
    the last one has highest priority.
    r�   )Úvaluesr   c                  W  s,   | ddd… D ]}|dur|  S qt ‚dS )z/Take first not-None value, starting at the end.Néÿÿÿÿ)r'   )rŽ   r   r   r   r   Ú_orJ  s    
z_merge_attrs.<locals>._orr"   c                 S  s   g | ]
}|j ‘qS r   rO   ©r   Úar   r   r   Ú
<listcomp>R  r   z _merge_attrs.<locals>.<listcomp>c                 S  s   g | ]
}|j ‘qS r   rN   r‘   r   r   r   r“   S  r   Fc                 S  s   g | ]
}|j ‘qS r   r?   r‘   r   r   r   r“   T  r   c                 S  s   g | ]
}|j ‘qS r   rC   r‘   r   r   r   r“   U  r   c                 S  s   g | ]
}|j ‘qS r   rE   r‘   r   r   r   r“   V  r   c                 S  s   g | ]
}|j ‘qS r   rA   r‘   r   r   r   r“   W  r   c                 S  s   g | ]
}|j ‘qS r   rF   r‘   r   r   r   r“   X  r   c                 S  s   g | ]
}|j ‘qS r   rG   r‘   r   r   r   r“   Y  r   c                 S  s   g | ]
}|j ‘qS r   rH   r‘   r   r   r   r“   Z  r   r)   )r   )r€   r�   r   r   r   r   C  s    ÷r   úlist[BaseStyle]Ú_MergedStyle©Ústylesr   c                 C  s   dd„ | D ƒ} t | ƒS )z)
    Merge multiple `Style` objects.
    c                 S  s   g | ]}|d ur|‘qS r`   r   ©r   Úsr   r   r   r“   b  r   z merge_styles.<locals>.<listcomp>)r•   )r—   r   r   r   r   ^  s    r   c                   @  sh   e Zd ZdZdddœdd„Zeddœd	d
„ƒZeddœdd„ƒZefddddœdd„Z	ddœdd„Z
dS )r•   z§
    Merge multiple `Style` objects into one.
    This is supposed to ensure consistency: if any of the given styles changes,
    then this style will be updated.
    r”   r^   r–   c                 C  s   || _ tdd�| _d S )Nr   )Úmaxsize)r—   r   Ú_style)rg   r—   r   r   r   ri   v  s    z_MergedStyle.__init__r   rj   c                   s$   ddœ‡ fdd„}ˆ j  ˆ  ¡ |¡S )z=The `Style` object that has the other styles merged together.r   rj   c                     s
   t ˆ jƒS r`   )r   r_   r   rk   r   r   Úget~  s    z'_MergedStyle._merged_style.<locals>.get)r›   rœ   r‰   )rg   rœ   r   rk   r   Ú_merged_stylez  s    z_MergedStyle._merged_styler]   c                 C  s    g }| j D ]}| |j¡ q
|S r`   )r—   r{   r_   )rg   r_   r™   r   r   r   r_   ƒ  s    
z_MergedStyle.style_rulesr   r   rx   c                 C  s   | j  ||¡S r`   )r�   r‡   )rg   r=   r#   r   r   r   r‡   Š  s    z$_MergedStyle.get_attrs_for_style_strr   c                 C  s   t dd„ | jD ƒƒS )Nc                 s  s   | ]}|  ¡ V  qd S r`   )r‰   r˜   r   r   r   rp   �  r   z1_MergedStyle.invalidation_hash.<locals>.<genexpr>)Útupler—   rk   r   r   r   r‰   �  s    z_MergedStyle.invalidation_hashN)rX   rY   rZ   r[   ri   rŠ   r�   r_   r
   r‡   r‰   r   r   r   r   r•   f  s   ÿ)%r[   Ú
__future__r   r}   ÚreÚenumr   Útypingr   r   Zprompt_toolkit.cacher   Úbaser   r	   r
   r   r   Znamed_colorsr   Ú__all__rt   r$   r   rP   r<   rV   Úcompilera   r   r\   rŒ   r   r�   r   r   r•   r   r   r   r   Ú<module>   s@   0÷D
s