U
    d                     @   sd  d 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mZmZmZ ddlmZ ddlmZmZmZ d	d
lmZ G dd dee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G dd de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 eded ed!ed"ed#e d$iZ!d%d& e!" D Z#G d'd( d(eZ$d)S )*uD   Implementation of custom properties see § 22.3 in the specification    )warn)Strict)Serialisable)Sequence)AliasStringIntegerFloatDateTimeBool)
NestedText)CUSTPROPS_NS	VTYPES_NSCPROPS_FMTID   )NestedDateTimec                   @   s   e Zd ZdZdS )NestedBoolTextzX
    Descriptor for handling nested elements with the value stored in the text part
    N)__name__
__module____qualname____doc__ r   r   =/tmp/pip-unpacked-wheel-rwywncvq/openpyxl/packaging/custom.pyr       s   r   c                   @   s   e Zd ZdZdZdZeddZee	de
dZeede
dZeede
dZede
dZeede
dZee	ddZe Ze Zdd	edfd
dZedd ZdS )_CustomDocumentPropertyzW
    Low-level representation of a Custom Document Property.
    Not used directly
    propertyNT
allow_none)expected_typer   	namespace)r   r   )r   r   r   c           	      K   sl   || _ || _|| _d | _|| _| D ] \}}t| || t| d| q&| jD ]}||krNt| |d  qNd S )N_typ)fmtidpidnamer   
linkTargetitemssetattr__elements__)	selfr"   r!   r    r#   kwkver   r   r   __init__<   s    
z _CustomDocumentProperty.__init__c                 C   sD   | j d k	r| j S | jD ]}t| |d k	r|  S q| jd k	r@dS d S )Nr#   )r   r&   getattrr#   )r'   ar   r   r   typeP   s    



z_CustomDocumentProperty.type)r   r   r   r   tagnamer   r   r"   r   strr   lpwstrinti4floatr8r   filetimer   boolr#   r    r   r!   r   r,   r   r/   r   r   r   r   r   (   s&   

r   c                       sL   e Zd ZdZdZeeedZe	dZ
dddZdd	 Zd fdd	Z  ZS )_CustomDocumentPropertyListzG
    Parses and seriliases property lists but is not used directly
    Z
Properties)r   r   r   r   c                 C   s
   || _ d S Nr   )r'   r   r   r   r   r,   g   s    z$_CustomDocumentPropertyList.__init__c                 C   s
   t | jS r:   )lenr   r'   r   r   r   __len__k   s    z#_CustomDocumentPropertyList.__len__Nc                    s<   t | jdD ]\}}||_qt |||}|dt |S )N   xmlns)	enumerater   r!   superto_treesetr   )r'   r0   idxr   ptree	__class__r   r   rC   o   s
    z#_CustomDocumentPropertyList.to_tree)r   )NNN)r   r   r   r   r0   r   r   r   r   r   ZcustomPropsr,   r>   rC   __classcell__r   r   rH   r   r9   [   s   
r9   c                   @   s*   e Zd Ze Zdd Zdd Zdd ZdS )_TypedPropertyc                 C   s   || _ || _d S r:   r"   value)r'   r"   rM   r   r   r   r,   |   s    z_TypedProperty.__init__c                 C   s   | j |j ko| j|jkS r:   rL   )r'   otherr   r   r   __eq__   s    z_TypedProperty.__eq__c                 C   s   | j j d| j d| j S )Nz, name=z, value=)rI   r   r"   rM   r=   r   r   r   __repr__   s    z_TypedProperty.__repr__N)r   r   r   r   r"   r,   rO   rP   r   r   r   r   rK   x   s   rK   c                   @   s   e Zd Ze ZdS )IntPropertyN)r   r   r   r   rM   r   r   r   r   rQ      s   rQ   c                   @   s   e Zd Ze ZdS )FloatPropertyN)r   r   r   r	   rM   r   r   r   r   rR      s   rR   c                   @   s   e Zd ZeddZdS )StringPropertyTr   Nr   r   r   r   rM   r   r   r   r   rS      s   rS   c                   @   s   e Zd Ze ZdS )DateTimePropertyN)r   r   r   r
   rM   r   r   r   r   rU      s   rU   c                   @   s   e Zd Ze ZdS )BoolPropertyN)r   r   r   r   rM   r   r   r   r   rV      s   rV   c                   @   s   e Zd Ze ZdS )LinkPropertyNrT   r   r   r   r   rW      s   rW   r2   r4   r6   r7   r8   r#   c                 C   s   i | ]\}}||qS r   r   ).0r)   r*   r   r   r   
<dictcomp>   s      rY   c                   @   sn   e Zd ZeedZdd Zedd Zdd Z	dd	 Z
d
d Zedd Zdd Zdd Zdd Zdd ZdS )CustomPropertyList)r   c                 C   s
   g | _ d S r:   propsr=   r   r   r   r,      s    zCustomPropertyList.__init__c           
      C   s   t |}|  }|jD ]f}|j}t|d}|sBtd|j  qt||}|j	}|dk	rdt
}|j	}||j|d}	||	 q|S )z0
        Create list from OOXML element
        NzUnknown type for rL   )r9   	from_treer   r/   XML_MAPPINGgetr   r"   r-   r#   rW   append)
clsrG   	prop_listZ	new_propspropattrtyprM   linkZnew_propr   r   r   r]      s     


zCustomPropertyList.from_treec                 C   s8   |j | jkrtd|j  d| j}|| || _d S )NProperty with name z already exists)r"   names
ValueErrorr\   r`   )r'   rc   r\   r   r   r   r`      s
    
zCustomPropertyList.appendc                 C   sp   g }| j D ]R}t|jd }|s(tdt|jd}t|||j t	|t
rRd|_|| q
t|d}| S )NzUnknown adapter for {p}r"    r;   )r\   CLASS_MAPPINGr_   rI   	TypeErrorr   r"   r%   rM   
isinstancerW   r2   r`   r9   rC   )r'   r\   rF   rd   nprb   r   r   r   rC      s    


zCustomPropertyList.to_treec                 C   s
   t | jS r:   )r<   r\   r=   r   r   r   r>      s    zCustomPropertyList.__len__c                 C   s   dd | j D S )zList of property namesc                 S   s   g | ]
}|j qS r   rj   )rX   rF   r   r   r   
<listcomp>   s     z,CustomPropertyList.names.<locals>.<listcomp>r[   r=   r   r   r   rh      s    zCustomPropertyList.namesc                 C   s2   | j D ]}|j|kr|  S qtd| ddS )z&
        Get property by name
        rg   
 not foundN)r\   r"   KeyError)r'   r"   rF   r   r   r   __getitem__   s    


zCustomPropertyList.__getitem__c                 C   sD   t | jD ]$\}}|j|kr
| j|  dS q
td| ddS )z*
        Delete a propery by name
        Nrg   rq   )rA   r\   r"   poprr   )r'   r"   rE   rF   r   r   r   __delitem__  s
    
zCustomPropertyList.__delitem__c                 C   s   | j j d| j S )Nz containing )rI   r   r\   r=   r   r   r   rP     s    zCustomPropertyList.__repr__c                 C   s
   t | jS r:   )iterr\   r=   r   r   r   __iter__  s    zCustomPropertyList.__iter__N)r   r   r   r   rK   r\   r,   classmethodr]   r`   rC   r>   r   rh   rs   ru   rP   rw   r   r   r   r   rZ      s   



rZ   N)%r   warningsr   Zopenpyxl.descriptorsr   Z!openpyxl.descriptors.serialisabler   Zopenpyxl.descriptors.sequencer   r   r   r   r	   r
   r   Zopenpyxl.descriptors.nestedr   Zopenpyxl.xml.constantsr   r   r   corer   r   r   r9   rK   rQ   rR   rS   rU   rV   rW   rl   r$   r^   rZ   r   r   r   r   <module>   sB    3      	