U
    ouf2                     @  sD  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 d
dddg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   ?/tmp/pip-unpacked-wheel-8milen2s/prompt_toolkit/styles/style.py
<dictcomp>   s      r   str)textreturnc                 C  s   | t kr| S | tkrt|  S zt|   W S  tk
r>   Y nX | dd dkr| dd }|t krh|S |tkrxt| S t|dkr|S t|dkr|d d |d d  |d d  S n| dk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*    &	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   )r2   resultpartsir   r   r   _expand_classname]   s
    
r;   r   )	style_strr   c                 C  s   d| k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krvq|drq|dr|drq|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.
    Z	noinheritr+   Tr+   ZnoboldFr.   r.   Znoitalicr,   r,   Znounderliner-   r-   Znostriker/   r/   Znoblinkr0   r0   Z	noreverser1   r1   Znohidden)ZromanZsansZmonozborder:[]zbg:r   Nr*   zfg:r)   )r
   _EMPTY_ATTRSr4   _replace
startswithendswithr   )r<   attrspartr   r   r   _parse_style_strl   s\    






rN   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_ORDERrO   r   r   r   r   r      s   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&tt|t|  }t|}|||f q|| _	|| _
d S N)CLASS_NAMES_REmatchAssertionErrorrepr	frozensetr   r4   rN   r6   _style_rulesclass_names_and_attrs)selfrW   r_   class_namesr<   Zclass_names_setrL   r   r   r   __init__   s    zStyle.__init__r   c                 C  s   | j S rX   )r^   r`   r   r   r   rW      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 )r3   N)r%   r4   )r   r:   r   r   r   	<genexpr>  s     z/Style.from_dict.<locals>.key.<locals>.<genexpr>r   )sumr4   )rh   r   r   r   key  s    zStyle.from_dict.<locals>.key)rk   N)r   rO   sorteditemslist)clsre   rf   rk   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rg }|dd  dD ]}	|t|	 qZ|D ]}
t  }|t	|
g t
dt|d D ]*}t||D ]}|t	||
f  qq| jD ]\}}||kr|| q||
 qrq2t|}|| q2t|S )z9
        Get `Attrs` for the given style string.
        zclass:r   N,r   )setr_   r6   r4   rJ   r   extendr;   addr]   r5   r%   	itertoolscombinationsrN   _merge_attrs)r`   r<   r"   list_of_attrsra   namesattrrM   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 rX   )idr_   rd   r   r   r   invalidation_hash<  s    zStyle.invalidation_hashN)rP   rQ   rR   rS   rb   propertyrW   classmethoddefault_priorityrp   r
   r   r   r   r   r   r   r      s   1_Tzlist[Attrs])ry   r   c                 C  s   ddddd}t |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	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k	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   rG   r   ar   r   r   
<listcomp>R  s     z _merge_attrs.<locals>.<listcomp>c                 S  s   g | ]
}|j qS r   rF   r   r   r   r   r   S  s     Fc                 S  s   g | ]
}|j qS r   r=   r   r   r   r   r   T  s     c                 S  s   g | ]
}|j qS r   r?   r   r   r   r   r   U  s     c                 S  s   g | ]
}|j qS r   r@   r   r   r   r   r   V  s     c                 S  s   g | ]
}|j qS r   r>   r   r   r   r   r   W  s     c                 S  s   g | ]
}|j qS r   rA   r   r   r   r   r   X  s     c                 S  s   g | ]
}|j qS r   rB   r   r   r   r   r   Y  s     c                 S  s   g | ]
}|j qS r   rC   r   r   r   r   r   Z  s     r(   )r!   )r!   )F)F)F)F)F)F)F)r   )ry   r   r   r   r   rx   C  s    rx   list[BaseStyle]_MergedStylestylesr   c                 C  s   dd | D } t | S )z)
    Merge multiple `Style` objects.
    c                 S  s   g | ]}|d k	r|qS rX   r   r   sr   r   r   r   b  s      z merge_styles.<locals>.<listcomp>)r   )r   r   r   r   r   ^  s    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   rV   r   c                 C  s   || _ tdd| _d S )Nr   )maxsize)r   r   _style)r`   r   r   r   r   rb   v  s    z_MergedStyle.__init__r   rc   c                   s$   dd fdd} j   |S )z=The `Style` object that has the other styles merged together.r   rc   c                     s
   t  jS rX   )r   rW   r   rd   r   r   get~  s    z'_MergedStyle._merged_style.<locals>.get)r   r   r   )r`   r   r   rd   r   _merged_stylez  s    z_MergedStyle._merged_stylerU   c                 C  s    g }| j D ]}||j q
|S rX   )r   rt   rW   )r`   rW   r   r   r   r   rW     s    
z_MergedStyle.style_rulesr   r   rq   c                 C  s   | j ||S rX   )r   r   )r`   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 rX   )r   r   r   r   r   ri     s     z1_MergedStyle.invalidation_hash.<locals>.<genexpr>)tupler   rd   r   r   r   r     s    z_MergedStyle.invalidation_hashN)rP   rQ   rR   rS   rb   r   r   rW   r
   r   r   r   r   r   r   r   f  s   )%rS   
__future__r   rv   reenumr   typingr   r   Zprompt_toolkit.cacher   baser   r	   r
   r   r   Znamed_colorsr   __all__rm   r#   r   rH   r;   rN   compilerY   r   rT   r   r   r   rx   r   r   r   r   r   r   <module>   sH   0D
s