U
    d                     @   s   d dl Z d dlmZ d dlmZmZ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ZdZd	Ze d
eeeZdd ZG dd deZG dd deZG dd deZdS )    N)warn)AliasBoolStrictStringIntegerMatchPatternTyped)Serialisable)Element)escapeunescapez&"(?P<font>.+)"z&K(?P<color>[A-F0-9]{6})z&(?P<size>\d+\s?)z{0}|{1}|{2}c                 C   sX   t dt jt jB }|| }z| }W n( tk
rR   td dddd}Y nX |S )z
    Split the combined (decoded) string into left, center and right parts

    # See http://stackoverflow.com/questions/27711175/regex-with-multiple-optional-groups for discussion
    zN
    (&L(?P<left>.+?))?
    (&C(?P<center>.+?))?
    (&R(?P<right>.+?))?
    $z3Cannot parse header or footer so it will be ignored )leftrightcenter)recompileVERBOSEDOTALLmatch	groupdictAttributeErrorr   )textZ
ITEM_REGEXmparts r   D/tmp/pip-unpacked-wheel-rwywncvq/openpyxl/worksheet/header_footer.py_split_string   s    

r   c                   @   sd   e Zd ZdZeddZeddZeddZdZ	e
de	dZdddZd	d
 Zdd Zedd ZdS )_HeaderFooterParta  
    Individual left/center/right header/footer part

    Do not use directly.

    Header & Footer ampersand codes:

    * &A   Inserts the worksheet name
    * &B   Toggles bold
    * &D or &[Date]   Inserts the current date
    * &E   Toggles double-underline
    * &F or &[File]   Inserts the workbook name
    * &I   Toggles italic
    * &N or &[Pages]   Inserts the total page count
    * &S   Toggles strikethrough
    * &T   Inserts the current time
    * &[Tab]   Inserts the worksheet name
    * &U   Toggles underline
    * &X   Toggles superscript
    * &Y   Toggles subscript
    * &P or &[Page]   Inserts the current page number
    * &P+n   Inserts the page number incremented by n
    * &P-n   Inserts the page number decremented by n
    * &[Path]   Inserts the workbook path
    * &&   Escapes the ampersand character
    * &"fontname"   Selects the named font
    * &nn   Selects the specified 2-digit font point size

    Colours are in RGB Hex
    T
allow_nonez^[A-Fa-f0-9]{6}$)r!   patternNc                 C   s   || _ || _|| _|| _d S N)r   fontsizecolor)selfr   r$   r%   r&   r   r   r   __init__]   s    z_HeaderFooterPart.__init__c                 C   s^   g }| j r|d| j  | jr4|d| j | jrL|d| j d|| jg S )zI
        Convert to Excel HeaderFooter miniformat minus position
        z&"{0}"z&{0} z&K{0}r   )r$   appendformatr%   r&   joinr   )r'   fmtr   r   r   __str__d   s    z_HeaderFooterPart.__str__c                 C   s
   t | jS r#   )boolr   r'   r   r   r   __bool__q   s    z_HeaderFooterPart.__bool__c                    s:   d t  fddt|D }td||d< | f |S )z3
        Convert from miniformat to object
        )r$   r&   r%   c                 3   s.   | ]&}t  |D ]\}}|r||fV  qqd S r#   )zip).0r   kvkeysr   r   	<genexpr>|   s
      z-_HeaderFooterPart.from_str.<locals>.<genexpr>r   r   )dictFORMAT_REGEXfindallsub)clsr   kwr   r5   r   from_strv   s    z_HeaderFooterPart.from_str)NNNN)__name__
__module____qualname____doc__r   r   r$   r   r%   ZRGBr   r&   r(   r-   r0   classmethodr>   r   r   r   r   r   5   s   



r   c                   @   sh   e Zd ZdZeedZeedZedZ	eedZ
dZdddZdd	 Zd
d Zdd Zedd ZdS )HeaderFooterItemz 
    Header or footer item

    )expected_typer   )LCRNc                 C   s@   |d krt  }|| _|d kr"t  }|| _|d kr6t  }|| _d S r#   )r   r   r   r   )r'   r   r   r   r   r   r   r(      s    zHeaderFooterItem.__init__c              	      s   ddddddddd	 t d
dd  D } fdd}g }t| j| j| j| jgD ](\}}|jdk	rX|	d
|t| qXd|}|||}t|S )z1
        Pack parts into a single string
        z&Az&Nz&Dz&Zz&Pz&Tz&Fz&G)z&[Tab]z&[Pages]z&[Date]z&[Path]z&[Page]z&[Time]z&[File]z
&[Picture]|c                 S   s   g | ]}d  t|qS )z({0}))r*   r   r   )r2   r3   r   r   r   
<listcomp>   s   z,HeaderFooterItem.__str__.<locals>.<listcomp>c                    s   |  d} | S )zn
            Callback for re.sub
            Replace expanded control with mini-format equivalent
            r   )group)r   r;   Z	TRANSFORMr   r   replace   s    
z)HeaderFooterItem.__str__.<locals>.replaceNz&{0}{1}r   )r   r   r+   r1   _HeaderFooterItem__keysr   r   r   r   r)   r*   strr;   r   )r'   Z
SUBS_REGEXrM   txtkeypartr   rL   r   r-      s*       
 

zHeaderFooterItem.__str__c                 C   s   t | j| j| jgS r#   )anyr   r   r   r/   r   r   r   r0      s    zHeaderFooterItem.__bool__c                 C   s   t |}t| |_|S )z$
        Return as XML node
        )r   rO   r   )r'   tagnameelr   r   r   to_tree   s    
zHeaderFooterItem.to_treec                 C   sR   |j rNt|j }t|}| D ]\}}|d k	r t|||< q | f |}|S d S r#   )r   r   r   itemsr   r>   )r<   noder   r   r3   r4   r'   r   r   r   	from_tree   s    

zHeaderFooterItem.from_tree)NNN)r?   r@   rA   rB   r	   r   r   r   r   Zcentrer   rN   r(   r-   r0   rV   rC   rY   r   r   r   r   rD      s   



	rD   c                
   @   s   e Zd ZdZeddZeddZeddZeddZe	e
ddZe	e
ddZe	e
ddZe	e
ddZe	e
ddZe	e
ddZdZdddZd	d
 ZdS )HeaderFooterZheaderFooterTr    )rE   r!   )	oddHeader	oddFooter
evenHeader
evenFooterfirstHeaderfirstFooterNc                 C   s   || _ || _|| _|| _|d kr&t }|| _|d kr:t }|| _|d krNt }|| _|d krbt }|| _|	d krvt }	|	| _	|
d krt }
|
| _
d S r#   )differentOddEvendifferentFirstscaleWithDocalignWithMarginsrD   r[   r\   r]   r^   r_   r`   )r'   ra   rb   rc   rd   r[   r\   r]   r^   r_   r`   r   r   r   r(      s,    zHeaderFooter.__init__c                    s"    fdd j  j D }t|S )Nc                    s   g | ]}t  |qS r   )getattr)r2   attrr/   r   r   rJ     s     z)HeaderFooter.__bool__.<locals>.<listcomp>)	__attrs____elements__rS   )r'   r   r   r/   r   r0     s    zHeaderFooter.__bool__)
NNNNNNNNNN)r?   r@   rA   rT   r   ra   rb   rc   rd   r	   rD   r[   r\   r]   r^   r_   r`   rh   r(   r0   r   r   r   r   rZ      s0   



          
$rZ   )r   warningsr   Zopenpyxl.descriptorsr   r   r   r   r   r   r	   Z!openpyxl.descriptors.serialisabler
   Zopenpyxl.xml.functionsr   Zopenpyxl.utils.escaper   r   ZFONT_PATTERNZCOLOR_PATTERNZ
SIZE_REGEXr   r*   r9   r   r   rD   rZ   r   r   r   r   <module>   s   $	OR