U
    +ife"                     @  sP   d Z ddlmZ ddlZddlZG dd deZdddd	ZG d
d dZdS )zN
Utilities for interpreting CSS from Stylers for formatting non-HTML outputs.
    )annotationsNc                   @  s   e Zd ZdZdS )
CSSWarningz5
    This CSS syntax cannot currently be parsed.
    N)__name__
__module____qualname____doc__ r   r   H/home/mars/bis/venv/lib/python3.8/site-packages/pandas/io/formats/css.pyr   
   s   r   strprop_fmtc                   s   dd fdd}|S )Nr
   )valuec              
   3  sz   |  }z| jt| }W n0 tk
rJ   td| d| dt Y d S X t| j|D ]\}} 	||| fV  qXd S )NzCould not expand "z: ")
splitSIDE_SHORTHANDSlenKeyErrorwarningswarnr   zipSIDESformat)selfpropr   tokensmappingkeyidxr   r   r	   expand   s    z_side_expander.<locals>.expandr   )r   r   r   r   r	   _side_expander   s    
r   c                   @  sj  e Zd ZdZ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gddddgddddgdddd gd!Z
d"ZdEd$d%d&d'd(d)Zd&d&d&d*d+d,Zd&d&d&d*d-d.Zd&d/d0d1d2Zd$d3d4d5d6Zd&d&d0d7d8Zd#efd9d:Zd;d< Zed=Zed>Zed?Zed@ZedAZd$dBdCdDZd#S )FCSSResolverzH
    A callable for parsing and resolving CSS to atomic properties.
    )pt   )em      ?)r!   g      ?)r!   H   )ingL&d2?)r&   g
BP(?)mmg      ?)r#   r   )	remexpxZpcr&   cmr'   q	!!default)r#   g{Gz?)r(   r$   )r(   g      ?)r(   g?)r(      )r(   g      ?)r(   g      ?)r(      )r#   g?)r#   g333333?)r#   r.   )%zxx-smallzx-smallZsmallmediumZlargezx-largezxx-largeZsmallerZlargerr-   none)r!   r   )r*      )r*   r/   )r*   r.   )r2   Zthickr1   Zthinr   r.   r/      )r.   r/   r4   r3   )toprightbottomleftNr
   zdict[str, str] | Nonezdict[str, str])declarations_str	inheritedreturnc                 C  sB   t | | |}|dkr i }| ||}| ||}| |S )a  
        The given declarations to atomic properties.

        Parameters
        ----------
        declarations_str : str
            A list of CSS declarations
        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)dictatomizeparse_update_initial_update_font_size_update_other_units)r   r9   r:   propsr   r   r	   __call__X   s    *zCSSResolver.__call__)rB   r:   r;   c                 C  sl   |  D ]\}}||kr|||< q| }|  D ]4\}}|dkrN||d}|dkr^||= q2|||< q2|S )Ninheritinitial)rE   N)itemscopyget)r   rB   r:   r   valZ	new_propsr   r   r	   r?      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   rB   r:   r   r   r	   r@      s    

zCSSResolver._update_font_sizezfloat | None)rB   r;   c                 C  s    | dr|d }| |S d S )NrJ   )rH   _get_float_font_size_from_pt)r   rB   font_size_stringr   r   r	   rN      s    

zCSSResolver._get_font_sizefloat)rQ   r;   c                 C  s   | dstt|dS )Nr!   )endswithAssertionErrorrR   rstrip)r   rQ   r   r   r	   rP      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-z-width)em_ptrL   zmargin-zpadding-)rN   r   rM   BORDER_WIDTH_RATIOSMARGIN_RATIOS)r   rB   Z	font_sizeZsider   r   r   r	   rA      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 dt t jd dS )NzUnhandled size: z
1!!defaultrK   )r   r   reprr   rM   r   rL   in_valr   r   r	   _error   s    z&CSSResolver.size_to_pt.<locals>._errorz^(\S*?)([a-zA-Z%!].*) r.   r!   r#   r(      df)rematchgroupsrR   
ValueErrorr   roundint)
r   r[   rV   rL   r\   rb   rI   unitmulZsize_fmtr   rZ   r	   rM      s8    

zCSSResolver.size_to_ptc              	   c  sn   |D ]d\}}d| dd }zt| |}W n tk
rH   ||fV  Y qX |||D ]\}}||fV  qTqd S )NZexpand_-_)replacegetattrAttributeError)r   Zdeclarationsr   r   attrr   r   r   r	   r=      s    zCSSResolver.atomizezborder-{:s}-colorzborder-{:s}-stylezborder-{:s}-widthzmargin-{:s}zpadding-{:s})r9   c                 c  sl   | dD ]\}| sq
|d\}}}|  }|  }|rP||fV  q
tdt| t 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 N)r   strip	partitionlowerr   r   rY   r   )r   r9   declr   seprI   r   r   r	   r>     s    
zCSSResolver.parse)N)r   r   r   r   ZUNIT_RATIOSrG   rO   updaterX   rW   r   r   rC   r?   r@   rN   rP   rA   rM   r=   r   Zexpand_border_colorZexpand_border_styleZexpand_border_widthZexpand_marginZexpand_paddingr>   r   r   r   r	   r       sr   



 2)r    )	r   
__future__r   ra   r   UserWarningr   r   r    r   r   r   r	   <module>   s   