U
    sVc1                     @  s   d Z ddlmZ ddlZddlmZmZmZmZ ddl	Z	ddl
mZ ddlmZ ddd	d
dZddddddZG dd dZdS )zN
Utilities for interpreting CSS from Stylers for formatting non-HTML outputs.
    )annotationsN)Callable	GeneratorIterableIterator)
CSSWarning)find_stack_levelstrr   )prop_fmtreturnc                   s   ddd fdd}|S )a)  
    Wrapper to expand shorthand property into top, right, bottom, left properties

    Parameters
    ----------
    side : str
        The border side to expand into properties

    Returns
    -------
        function: Return to call when a 'border(-{side}): {value}' string is encountered
    r	   &Generator[tuple[str, str], None, None]valuer   c              	   3  s   |  }z| jt| }W n6 tk
rP   tjd| d| dtt d Y dS X t| j	|D ]\}} 
||| fV  q^dS )a<  
        Expand shorthand property into side-specific property (top, right, bottom, left)

        Parameters
        ----------
            prop (str): CSS property name
            value (str): String token for property

        Yields
        ------
            Tuple (str, str): Expanded property, value
        zCould not expand "z: "
stacklevelN)splitSIDE_SHORTHANDSlenKeyErrorwarningswarnr   r   zipSIDESformat)selfpropr   tokensmappingkeyidxr
    9/tmp/pip-unpacked-wheel-xj8nt62q/pandas/io/formats/css.pyexpand!   s    z_side_expander.<locals>.expandr"   )r
   r$   r"   r!   r#   _side_expander   s    r%    )sider   c                   s*    dkrd   ddd fdd}|S )a.  
    Wrapper to expand 'border' property into border color, style, and width properties

    Parameters
    ----------
    side : str
        The border side to expand into properties

    Returns
    -------
        function: Return to call when a 'border(-{side}): {value}' string is encountered
    r&   -r	   r   r   c                 3  s   |  }t|dks t|dkr:tjd| dtt d d ddd d	d
d ddi}|D ]^   | jkr |d d	< qft fdd| j	D r |d d< qf |d d< qf| 
| E dH  dS )aV  
        Expand border into color, style, and width tuples

        Parameters
        ----------
            prop : str
                CSS property name passed to styler
            value : str
                Value passed to styler for property

        Yields
        ------
            Tuple (str, str): Expanded property, value
        r      zToo many tokens provided to "z" (expected 1-3)r   borderz-colorZblackz-stylenone-widthmediumc                 3  s   | ]}|   kV  qd S N)lower).0Zratiotokenr"   r#   	<genexpr>n   s     z3_border_expander.<locals>.expand.<locals>.<genexpr>N)r   r   r   r   r   r   r/   BORDER_STYLESanyBORDER_WIDTH_RATIOSatomizeitems)r   r   r   r   Zborder_declarationsr'   r1   r#   r$   N   s*    

 
 
 z _border_expander.<locals>.expandr"   )r'   r$   r"   r9   r#   _border_expander>   s    
)r:   c                   @  s  e Zd ZdZddddddddd	d
ddZe Zedddddddddddd e Zeddi e Z	e	ddddd dddd d!d"d#d$d%d&d'd(d)d*d+d,d-gZ
d.d.d.d.gd.d/d.d/gd.d/d0d/gd.d/d0d1gd2Zd3Zd4d5 d6D d7d5 d8D ed9ed:d;Zd^d=d>d?d@dAdBZd?d?d?dCdDdEZd?d?d?dCdFdGZd?dHdIdJdKZdLdMdNdOdPZd?d?dIdQdRZd<efdSdTZdUdVdWdXdYZdLdZd[d\d]Zd<S )_CSSResolverzH
    A callable for parsing and resolving CSS to atomic properties.
    )pt   )emr=   )r<      )r>         ?)r<   g      ?)r<   H   )ingL&d2?)rB   g
BP(?)mmg      ?)r>   r   )r<   r>   remexpxZpcrB   cmrC   q	!!default)r>   g{Gz?)rD   r@   )rD   g      ?)rD   g?)rD   r=   )rD   g      ?)rD   g      ?)rD      )r>   g?)r>   g333333?)%zxx-smallzx-smallsmallr-   Zlargezx-largezxx-largeZsmallerZlargerrI   r+   )r<   r   )rF      )rF   rJ   )rF   r=   )r+   Zthickr-   ZthinhiddenZdottedZdashedZsoliddoubleZgrooveZridgeZinsetZoutsetZmediumdashdotZ
dashdotdotZhairZmediumdashdotdotZdashdotZslantdashdotZmediumdashedr   r=   rJ   r)   )r=   rJ   r)   rM   )toprightbottomleftc                 C  s*   i | ]"}d  |rd|gndgt|qS )r(   r*   )joinr:   r0   r   r"   r"   r#   
<dictcomp>   s    zCSSResolver.<dictcomp>)r&   rP   rQ   rR   rS   c                 C  s$   i | ]}d  d|gtd| qS )r(   r*   zborder-{:s}-)rT   r%   rU   r"   r"   r#   rV      s    )colorstylewidthzmargin-{:s}zpadding-{:s})marginpaddingNzstr | Iterable[tuple[str, str]]zdict[str, str] | Nonezdict[str, str])declarations	inheritedr   c                 C  sP   t |tr| |}t| |}|dkr.i }| ||}| ||}| |S )a  
        The given declarations to atomic properties.

        Parameters
        ----------
        declarations_str : str | Iterable[tuple[str, str]]
            A CSS string or set of CSS declaration tuples
            e.g. "font-weight: bold; background: blue" or
            {("font-weight", "bold"), ("background", "blue")}
        inherited : dict, optional
            Atomic properties indicating the inherited style context in which
            declarations_str is to be resolved. ``inherited`` should already
            be resolved, i.e. valid output of this method.

        Returns
        -------
        dict
            Atomic CSS 2.2 properties.

        Examples
        --------
        >>> resolve = CSSResolver()
        >>> inherited = {'font-family': 'serif', 'font-weight': 'bold'}
        >>> out = resolve('''
        ...               border-color: BLUE RED;
        ...               font-size: 1em;
        ...               font-size: 2em;
        ...               font-weight: normal;
        ...               font-weight: inherit;
        ...               ''', inherited)
        >>> sorted(out.items())  # doctest: +NORMALIZE_WHITESPACE
        [('border-bottom-color', 'blue'),
         ('border-left-color', 'red'),
         ('border-right-color', 'red'),
         ('border-top-color', 'blue'),
         ('font-family', 'serif'),
         ('font-size', '24pt'),
         ('font-weight', 'bold')]
        N)
isinstancer	   parsedictr7   _update_initial_update_font_size_update_other_units)r   r\   r]   propsr"   r"   r#   __call__   s    ,

zCSSResolver.__call__)rd   r]   r   c                 C  sl   |  D ]\}}||kr|||< q| }|  D ]4\}}|dkrN||d}|dkr^||= q2|||< q2|S )NZinheritinitial)rf   N)r8   copyget)r   rd   r]   r   valZ	new_propsr"   r"   r#   ra     s    

zCSSResolver._update_initialc                 C  s.   | dr*| j|d | || jd|d< |S )N	font-sizeconversions)rh   
size_to_pt_get_font_sizeFONT_SIZE_RATIOS)r   rd   r]   r"   r"   r#   rb   %  s    

zCSSResolver._update_font_sizezfloat | None)rd   r   c                 C  s    | dr|d }| |S d S )Nrj   )rh   _get_float_font_size_from_pt)r   rd   font_size_stringr"   r"   r#   rn   3  s    

zCSSResolver._get_font_sizer	   float)rq   r   c                 C  s   | dstt|dS )Nr<   )endswithAssertionErrorrr   rstrip)r   rq   r"   r"   r#   rp   9  s    z(CSSResolver._get_float_font_size_from_ptc                 C  s   |  |}| jD ]n}d| d}||krB| j|| || jd||< d| d| fD ]&}||krV| j|| || jd||< qVq|S )Nzborder-r,   )em_ptrl   zmargin-zpadding-)rn   r   rm   r6   MARGIN_RATIOS)r   rd   Z	font_sizer'   r   r"   r"   r#   rc   =  s"    


zCSSResolver._update_other_unitsc           
        s   fdd}t d}|d kr*| S | \}}|dkrDd}n(zt|}W n tk
rj   |  Y S X |dkr|dkr|d krd}ql||9 }d}qlz | \}}W n tk
r   |  Y S X ||9 }qlt|d	}t||krt|d
d}	n|dd}	|	S )Nc                     s*   t jdt tt d jd dS )NzUnhandled size: r   z
1!!defaultrk   )r   r   reprr   r   rm   r"   rl   in_valr   r"   r#   _errorT  s    z&CSSResolver.size_to_pt.<locals>._errorz^(\S*?)([a-zA-Z%!].*)r&   r=   r<   r>   rD      df)rematchgroupsrr   
ValueErrorr   roundint)
r   rz   rv   rl   r{   r   ri   unitmulZsize_fmtr"   ry   r#   rm   S  s8    

zCSSResolver.size_to_ptr   r   )r\   r   c                 c  sT   |D ]J\}}|  }|  }|| jkrD| j| }|| ||E d H  q||fV  qd S r.   )r/   CSS_EXPANSIONS)r   r\   r   r   r$   r"   r"   r#   r7     s    

zCSSResolver.atomizezIterator[tuple[str, str]])declarations_strr   c                 c  sr   | dD ]b}| sq
|d\}}}|  }|  }|rP||fV  q
tjdt| tt d q
dS )z
        Generates (prop, value) pairs from declarations.

        In a future version may generate parsed tokens from tinycss/tinycss2

        Parameters
        ----------
        declarations_str : str
        ;:z-Ill-formatted attribute: expected a colon in r   N)	r   strip	partitionr/   r   r   rx   r   r   )r   r   declr   sepri   r"   r"   r#   r_     s    
zCSSResolver.parse)N)__name__
__module____qualname____doc__ZUNIT_RATIOSrg   ro   updaterw   r6   r4   r   r   r%   r   re   ra   rb   rn   rp   rc   rm   r7   r_   r"   r"   r"   r#   r;   z   s   



 6-
r;   )r&   )r   
__future__r   r   typingr   r   r   r   r   Zpandas.errorsr   Zpandas.util._exceptionsr   r%   r:   r;   r"   r"   r"   r#   <module>   s   +<