U
    cc2                     @   s\  d Z ddlZddlZddlZddlmZ ddl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mZ ddlmZ d	d
ddgZdd e D Zeeddd
Zedddddddddd	Zee	e dddZee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%e	e edddZ&e	e dd d!dZ'G d"d deZ(dS )#z-
Tool for creating styles from a dictionary.
    N)Enum)DictHashableListSetTuple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   U/var/www/html/project/venv/lib/python3.8/site-packages/prompt_toolkit/styles/style.py
<dictcomp>   s      r   )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hidden)	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   )r4   resultpartsir   r   r   _expand_classname\   s
    
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.
    	noinheritr-   Tr-   noboldFr0   r0   noitalicr.   r.   nounderliner/   r/   Znostriker1   r1   Znoblinkr2   r2   Z	noreverser3   r3   Znohidden)romansansmonozborder:[]zbg:r!   Nr,   zfg:r+   )r   _EMPTY_ATTRSr6   _replace
startswithendswithr   )r>   attrspartr   r   r   _parse_style_strk   s\    






rW   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_ORDERrX   r   r   r   r   r      s   c                   @   s   e Zd ZdZeeeef  ddddZeeeeef  dddZ	e
efeeef ed d	d
dZefeeedddZe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.
    N)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   r6   rW   r8   _style_rulesclass_names_and_attrs)selfr^   rf   class_namesr>   Zclass_names_setrU   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_rules)
style_dictpriorityr   c                 C   sJ   |t jkr6tttf tddd}| t| |dS | t| S dS )za
        :param style_dict: Style dictionary.
        :param priority: `Priority` value.
        )itemr   c                 S   s   t dd | d  D S )Nc                 s   s   | ]}t |d V  qdS )r5   N)r'   r6   )r   r<   r   r   r   	<genexpr>  s     z/Style.from_dict.<locals>.key.<locals>.<genexpr>r   )sumr6   )rn   r   r   r   key  s    zStyle.from_dict.<locals>.key)rq   N)r   rX   r   strintsorteditemslist)clsrl   rm   rq   r   r   r   	from_dict   s    
zStyle.from_dict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
   )setrf   r8   r6   rS   r   extendr=   addrd   r7   r'   	itertoolscombinationsrW   _merge_attrs)rg   r>   r$   list_of_attrsrh   namesattrrV   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_strc                 C   s
   t | jS r_   )idrf   rk   r   r   r   invalidation_hash;  s    zStyle.invalidation_hash)rY   rZ   r[   r\   r   r   rr   ri   propertyr^   classmethoddefault_priorityr   r   rx   r   r   r   r   r   r   r   r   r   r      s$   
  1_T)r   r   c                 C   s   t t 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.
    )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   _orI  s    
z_merge_attrs.<locals>._orr#   c                 S   s   g | ]
}|j qS r   rP   r   ar   r   r   
<listcomp>Q  s     z _merge_attrs.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   rO   r   r   r   r   r   R  s     Fc                 S   s   g | ]
}|j qS r   r@   r   r   r   r   r   S  s     c                 S   s   g | ]
}|j qS r   rD   r   r   r   r   r   T  s     c                 S   s   g | ]
}|j qS r   rF   r   r   r   r   r   U  s     c                 S   s   g | ]
}|j qS r   rB   r   r   r   r   r   V  s     c                 S   s   g | ]
}|j qS r   rG   r   r   r   r   r   W  s     c                 S   s   g | ]
}|j qS r   rH   r   r   r   r   r   X  s     c                 S   s   g | ]
}|j qS r   rI   r   r   r   r   r   Y  s     r*   )r#   )r#   )F)F)F)F)F)F)F)r   r   )r   r   r   r   r   r   B  s    r   _MergedStylestylesr   c                 C   s   dd | D } t | S )z)
    Merge multiple `Style` objects.
    c                 S   s   g | ]}|d k	r|qS r_   r   r   sr   r   r   r   a  s      z merge_styles.<locals>.<listcomp>)r   )r   r   r   r   r   ]  s    c                   @   sx   e Zd ZdZee ddddZeedddZ	eee
eef  dd	d
ZefeeedddZe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.
    Nr   c                 C   s   || _ tdd| _d S )Nr
   )maxsize)r   r	   _style)rg   r   r   r   r   ri   u  s    z_MergedStyle.__init__rj   c                    s$   t d fdd} j  |S )z=The `Style` object that has the other styles merged together.rj   c                      s
   t  jS r_   )r   r^   r   rk   r   r   get}  s    z'_MergedStyle._merged_style.<locals>.get)r   r   r   r   )rg   r   r   rk   r   _merged_styley  s    z_MergedStyle._merged_stylec                 C   s    g }| j D ]}||j q
|S r_   )r   r|   r^   )rg   r^   r   r   r   r   r^     s    
z_MergedStyle.style_rulesry   c                 C   s   | j ||S r_   )r   r   )rg   r>   r$   r   r   r   r     s    z$_MergedStyle.get_attrs_for_style_strc                 C   s   t dd | jD S )Nc                 s   s   | ]}|  V  qd S r_   )r   r   r   r   r   ro     s     z1_MergedStyle.invalidation_hash.<locals>.<genexpr>)tupler   rk   r   r   r   r     s    z_MergedStyle.invalidation_hash)rY   rZ   r[   r\   r   r   ri   r   r   r   r   rr   r^   r   r   r   r   r   r   r   r   r   r   e  s    ))r\   r~   resysenumr   typingr   r   r   r   r   r   Zprompt_toolkit.cacher	   baser   r   r   r   r   Znamed_colorsr   __all__ru   r%   rr   r   rQ   r=   rW   compiler`   r   r]   r   r   r   r   r   r   r   r   r   r   <module>   sH    0D
s