U
    .R”iV  ã                   @   sè   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l
mZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZmZ G dd„ deƒZG dd„ deƒZ G dd„ deƒZ!G dd„ deƒZ"dS )é    )Úsafe_string)ÚTypedÚIntegerÚBoolÚStringÚSequence)ÚExtensionList)ÚSerialisableé   )ÚPatternFillÚFill)ÚFont)ÚBorder)Ú	Alignment)Ú
Protection)ÚNumberFormatDescriptorÚBUILTIN_FORMATS_MAX_SIZEÚBUILTIN_FORMATS_REVERSE)Ú
StyleArrayÚ	CellStylec                	       s¶   e Zd ZdZeed�Zeed�Zee	d�Z
eed�Zeƒ Zeed�Zedd�Zedd�Zeƒ ZdZeƒ Zddd	„Z‡ fd
d„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!‡  Z"S )Ú
NamedStylez#
    Named and editable styles
    ©Úexpected_typeT©Ú
allow_noneNÚNormalFc
           
      C   sf   || _ |ptƒ | _|ptƒ | _|p&tƒ | _|p2tƒ | _|| _	|pDt
ƒ | _|| _|	| _d | _tƒ | _d S ©N)Únamer   Úfontr   Úfillr   Úborderr   Ú	alignmentÚnumber_formatr   Ú
protectionÚ	builtinIdÚhiddenÚ_wbr   Ú_style)
Úselfr   r   r   r    r!   r"   r#   r$   r%   © r)   ú@/tmp/pip-unpacked-wheel-r0v169r1/openpyxl/styles/named_styles.pyÚ__init__2   s    zNamedStyle.__init__c                    s.   t ƒ  ||¡ t| dd ƒr*|dkr*|  ¡  d S )Nr&   )r   r   r    r!   r"   r#   )ÚsuperÚ__setattr__ÚgetattrÚ_recalculate)r(   ÚattrÚvalue©Ú	__class__r)   r*   r-   J   s    zNamedStyle.__setattr__c                 c   s0   dD ]&}t | |d ƒ}|d k	r|t|ƒfV  qd S )N©r   r$   r%   ÚxfId)r.   r   )r(   Úkeyr1   r)   r)   r*   Ú__iter__R   s    zNamedStyle.__iter__c                 C   s   || _ |  ¡  dS )z2
        Bind a named style to a workbook
        N)r&   r/   )r(   Úwbr)   r)   r*   ÚbindY   s    zNamedStyle.bindc                 C   sœ   | j j | j¡| j_| j j | j¡| j_| j j	 | j
¡| j_| j j | j¡| j_| j j | j¡| j_| j}|tkr|t| }n| j j | j¡t }|| j_d S r   )r&   Z_fontsÚaddr   r'   ZfontIdZ_bordersr    ZborderIdZ_fillsr   ZfillIdZ_protectionsr#   ZprotectionIdZ_alignmentsr!   ZalignmentIdr"   r   Z_number_formatsr   ZnumFmtId)r(   Úfmtr)   r)   r*   r/   a   s    
ÿzNamedStyle._recalculatec                 C   s   | j S )z3Return a style array representing the current style)r'   ©r(   r)   r)   r*   Úas_tuplep   s    zNamedStyle.as_tuplec                 C   sJ   t  | j¡}d|_d|_d|_| jtƒ kr2| j|_| jt	ƒ krF| j|_|S )z+
        Return equivalent XfStyle
        N)
r   Z
from_arrayr'   r5   ZpivotButtonZquotePrefixr!   r   r#   r   )r(   Zxfr)   r)   r*   Úas_xfu   s    zNamedStyle.as_xfc                 C   s   t | j| j| j| jjd�}|S )z.
        Return relevant named style

        r4   )Ú_NamedCellStyler   r$   r%   r'   r5   )r(   Únamedr)   r)   r*   Úas_name„   s    üzNamedStyle.as_name)	r   NNNNNNNF)#Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r    r   r!   r   r"   r   r#   r   r$   r   r%   r   r   r&   r   r'   r+   r-   r7   r9   r/   r=   r>   rA   Ú__classcell__r)   r)   r2   r*   r      s:   






         ÷
r   c                       sF   e Zd ZdZd‡ fdd„	Zedd„ ƒZ‡ fdd„Z‡ fd	d
„Z‡  Z	S )ÚNamedStyleListzÅ
    Named styles are editable and can be applied to multiple objects

    As only the index is stored in referencing objects the order mus
    be preserved.

    Returns a list of NamedStyles
    r)   c                    s0   t |t| ƒƒD ]\}}||j_qtƒ  |¡ dS )zN
        Allow a list of named styles to be passed in and index them.
        N)Ú	enumerateÚlenr'   r5   r,   r+   )r(   ÚiterableÚidxÚsr2   r)   r*   r+   œ   s    
zNamedStyleList.__init__c                 C   s   dd„ | D ƒS )Nc                 S   s   g | ]
}|j ‘qS r)   )r   )Ú.0rL   r)   r)   r*   Ú
<listcomp>¨   s     z(NamedStyleList.names.<locals>.<listcomp>r)   r<   r)   r)   r*   Únames¦   s    zNamedStyleList.namesc                    sP   t |tƒrtƒ  |¡S t| jƒD ]\}}||kr | |   S q td |¡ƒ‚d S )Nz&No named style with the name{0} exists)Ú
isinstanceÚintr,   Ú__getitem__rH   rO   ÚKeyErrorÚformat)r(   r6   rK   r   r2   r)   r*   rR   «   s    
zNamedStyleList.__getitem__c                    sL   t |tƒstdƒ‚n|j| jkr0td |j¡ƒ‚t| ƒ|j_	t
ƒ  |¡ d S )Nz&Only NamedStyle instances can be addedzStyle {0} exists already)rP   r   Ú	TypeErrorr   rO   Ú
ValueErrorrT   rI   r'   r5   r,   Úappend)r(   Ústyler2   r)   r*   rW   ¶   s    

zNamedStyleList.append)r)   )
rB   rC   rD   rE   r+   ÚpropertyrO   rR   rW   rF   r)   r)   r2   r*   rG   ’   s   	

rG   c                   @   sb   e Zd ZdZdZeƒ Zeƒ Zedd�Z	edd�Z
edd�Zedd�Zeedd�ZdZd
dd	„ZdS )r?   z�
    Pointer-based representation of named styles in XML
    xfId refers to the corresponding CellStyleXfs

    Not used in client code.
    Ú	cellStyleTr   )r   r   r)   Nc                 C   s(   || _ || _|| _|| _|| _|| _d S r   )r   r5   r$   ÚiLevelr%   ÚcustomBuiltin)r(   r   r5   r$   r[   r%   r\   ÚextLstr)   r)   r*   r+   Õ   s    	z_NamedCellStyle.__init__)NNNNNNN)rB   rC   rD   rE   Útagnamer   r   r   r5   r$   r[   r   r%   r\   r   r   r]   Z__elements__r+   r)   r)   r)   r*   r?   ¿   s$   



       ùr?   c                   @   sJ   e Zd ZdZdZedd�Zeed�Z	dZ
dd	d
„Zedd„ ƒZdd„ ZdS )Ú_NamedCellStyleListzM
    Container for named cell style objects

    Not used in client code
    Z
cellStylesTr   r   )ÚcountNr)   c                 C   s
   || _ d S r   )rZ   )r(   r`   rZ   r)   r)   r*   r+   ô   s    z_NamedCellStyleList.__init__c                 C   s
   t | jƒS r   )rI   rZ   r<   r)   r)   r*   r`   û   s    z_NamedCellStyleList.countc                 C   sh   dd„ }g }t ƒ }t ƒ }t| j|d�D ]<}|j|ks&|j|kr@q&| |j¡ | |j¡ | |¡ q&|S )a  
        Some applications contain duplicate definitions either by name or
        referenced style.

        As the references are 0-based indices, styles are sorted by
        index.

        Returns a list of style references with duplicates removed
        c                 S   s   | j S r   )r5   )Úvr)   r)   r*   Úsort_fn  s    z6_NamedCellStyleList.remove_duplicates.<locals>.sort_fn)r6   )ÚsetÚsortedrZ   r5   r   r:   rW   )r(   rb   ZstylesrO   ÚidsÚnsr)   r)   r*   Úremove_duplicates   s    z%_NamedCellStyleList.remove_duplicates)Nr)   )rB   rC   rD   rE   r^   r   r`   r   r?   rZ   Ú	__attrs__r+   rY   rg   r)   r)   r)   r*   r_   æ   s   

  þ

r_   N)#Zopenpyxl.compatr   Zopenpyxl.descriptorsr   r   r   r   r   Zopenpyxl.descriptors.excelr   Z!openpyxl.descriptors.serialisabler	   Zfillsr   r   Zfontsr   Zbordersr   r!   r   r#   r   Znumbersr   r   r   Z
cell_styler   r   r   ÚlistrG   r?   r_   r)   r)   r)   r*   Ú<module>   s   s-'