B
    't\01              
   @   s  d Z ddlmZmZ ddlZddlZddlZddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZ dd	d
dgZedd e D Zdd	 ZedddddddddZdd Zdd ZedZG dd
 d
ZejdkrejZnejZG dd deZ dd Z!dd Z"G dd deZ#dS )z-
Tool for creating styles from a dictionary.
    )unicode_literalsabsolute_importN   )	BaseStyleDEFAULT_ATTRSANSI_COLOR_NAMESANSI_COLOR_NAMES_ALIASESAttrs)NAMED_COLORS)SimpleCacheStyleparse_colorPrioritymerge_stylesc             c   s$   | ]\}}|  |d fV  qdS )#N)lowerlstrip).0kv r   :lib/python3.7/site-packages/prompt_toolkit/styles/style.py	<genexpr>   s    r   c             C   s   | t kr| S | tkrt|  S yt|   S  tk
r<   Y nX | dd dkr| dd }|t krf|S |tkrvt| 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)textcolr   r   r   r      s*    &)colorbgcolorbold	underlineitalicblinkreversehiddenc             C   sJ   g }|  d}x6tdt|d D ] }|d|d|   q"W |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   )Z	classnameresultpartsir   r   r   _expand_classnameK   s
    
 r4   c             C   s  d| krt }nt}x|  D ]}|dkr.q|dkrD|jdd}q|dkrZ|jdd}q|dkrp|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rJq|drXq|drr|drrq|dr|jt|dd d}q|dr|jt|dd d}q|jt|d}qW |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noblinkr*   )r*   Z	noreverser+   )r+   Znohidden)ZromanZsansZmonozborder:[]zbg:r   N)r%   zfg:)r$   )r   _EMPTY_ATTRSr-   _replace
startswithendswithr   )	style_strattrspartr   r   r   _parse_style_strZ   sT    




r>   z^[a-z0-9.\s_-]*$c               @   s    e Zd ZdZdZdZeeg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_ORDERr?   _ALLr   r   r   r   r      s   )r   r   c               @   sH   e Zd ZdZdd Zedd ZeefddZ	e
fdd	Z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.
    c             C   sp   t |tstg }xL|D ]D\}}t|s6tt|t|  }t	|}|
||f qW || _|| _d S )N)
isinstancelistAssertionErrorCLASS_NAMES_REmatchrepr	frozensetr   r-   r>   r/   _style_rulesclass_names_and_attrs)selfstyle_rulesrN   class_namesr;   r<   r   r   r   __init__   s    zStyle.__init__c             C   s   | j S )N)rM   )rO   r   r   r   rP      s    zStyle.style_rulesc             C   sH   |t jkst|t jkr4dd }| t| |dS | t| S dS )za
        :param style_dict: Style dictionary.
        :param priority: `Priority` value.
        c             S   s   t dd | d  D S )Nc             s   s   | ]}t |d V  qdS )r,   N)r    r-   )r   r3   r   r   r   r      s    z/Style.from_dict.<locals>.key.<locals>.<genexpr>r   )sumr-   )itemr   r   r   key   s    zStyle.from_dict.<locals>.key)rU   N)r   rE   rH   r?   sorteditemsrG   )clsZ
style_dictZpriorityrU   r   r   r   	from_dict   s
    
zStyle.from_dictc          	   C   s4  |g}t  }x | jD ]\}}|s|| qW x| D ]}|drg }x,|dd  dD ]}	|t|	 qdW x|D ]}
t  }|t	|
g xDt
dt|d D ].}x(t||D ]}|t	||
f  qW qW x$| jD ]\}}||kr|| qW ||
 qW q8t|}|| q8W t|S )z9
        Get `Attrs` for the given style string.
        zclass:r   N,r   )setrN   r/   r-   r9   r   extendr4   addrL   r.   r    	itertoolscombinationsr>   _merge_attrs)rO   r;   r   list_of_attrsrQ   namesattrr=   Znew_class_namespnew_nameZcomboscountZc2Zinline_attrsr   r   r   get_attrs_for_style_str   s.    
zStyle.get_attrs_for_style_strc             C   s
   t | jS )N)idrN   )rO   r   r   r   invalidation_hash*  s    zStyle.invalidation_hashN)r@   rA   rB   rC   rR   propertyrP   classmethoddefault_priorityrY   r   rg   ri   r   r   r   r   r      s   /c             C   s   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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.
    c              W   s(   x"| ddd D ]}|dk	r|S qW dS )z1 Take first not-None value, starting at the end. Nr   )valuesr   r   r   r   _or4  s    z_merge_attrs.<locals>._orr   c             S   s   g | ]
}|j qS r   )r$   )r   ar   r   r   
<listcomp>;  s    z _merge_attrs.<locals>.<listcomp>c             S   s   g | ]
}|j qS r   )r%   )r   rp   r   r   r   rq   <  s    Fc             S   s   g | ]
}|j qS r   )r&   )r   rp   r   r   r   rq   =  s    c             S   s   g | ]
}|j qS r   )r'   )r   rp   r   r   r   rq   >  s    c             S   s   g | ]
}|j qS r   )r(   )r   rp   r   r   r   rq   ?  s    c             S   s   g | ]
}|j qS r   )r)   )r   rp   r   r   r   rq   @  s    c             S   s   g | ]
}|j qS r   )r*   )r   rp   r   r   r   rq   A  s    c             S   s   g | ]
}|j qS r   )r+   )r   rp   r   r   r   rq   B  s    )r$   r%   r&   r'   r(   r)   r*   r+   )r   )r   )F)F)F)F)F)F)r	   )ra   ro   r   r   r   r`   .  s    r`   c             C   s   dd | D } t | S )z)
    Merge multiple `Style` objects.
    c             S   s   g | ]}|d k	r|qS )Nr   )r   sr   r   r   rq   I  s    z merge_styles.<locals>.<listcomp>)_MergedStyle)stylesr   r   r   r   E  s    c               @   sD   e Zd ZdZdd Zedd Zedd Zefdd	Z	d
d Z
dS )rs   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.
    c             C   s,   t dd |D st|| _tdd| _d S )Nc             s   s   | ]}t |tV  qd S )N)rF   r   )r   Zstyler   r   r   r   ]  s    z(_MergedStyle.__init__.<locals>.<genexpr>r   )maxsize)allrH   rt   r   _style)rO   rt   r   r   r   rR   \  s    z_MergedStyle.__init__c                s    fdd} j   |S )z? The `Style` object that has the other styles merged together. c                  s
   t  jS )N)r   rP   r   )rO   r   r   gete  s    z'_MergedStyle._merged_style.<locals>.get)rw   rx   ri   )rO   rx   r   )rO   r   _merged_styleb  s    z_MergedStyle._merged_stylec             C   s$   g }x| j D ]}||j qW |S )N)rt   r\   rP   )rO   rP   rr   r   r   r   rP   i  s    z_MergedStyle.style_rulesc             C   s   | j ||S )N)ry   rg   )rO   r;   r   r   r   r   rg   p  s    z$_MergedStyle.get_attrs_for_style_strc             C   s   t dd | jD S )Nc             s   s   | ]}|  V  qd S )N)ri   )r   rr   r   r   r   r   t  s    z1_MergedStyle.invalidation_hash.<locals>.<genexpr>)tuplert   )rO   r   r   r   ri   s  s    z_MergedStyle.invalidation_hashN)r@   rA   rB   rC   rR   rj   ry   rP   r   rg   ri   r   r   r   r   rs   M  s   
rs   )$rC   Z
__future__r   r   r^   resysbaser   r   r   r   r	   Znamed_colorsr
   Zprompt_toolkit.cacher   __all__dictrW   r   r   r7   r4   r>   compilerI   r   version_inforD   rl   r?   r   r`   r   rs   r   r   r   r   <module>   s6   0
@

q