U
    H4f‰  ã                   @   sd   d Z ddlZddlZejdkr,ddlmZ nddlmZ ddlmZ G dd„ dƒZG dd	„ d	ƒZ	dS )
z^Basic implementation to support SOAP-Attachments

See https://www.w3.org/TR/SOAP-attachments

é    N)é   é   )Úcached_property)ÚCaseInsensitiveDictc                   @   sD   e Zd Zdd„ Zdd„ Zedd„ ƒZdd„ Zed	d
„ ƒZ	dd„ Z
dS )ÚMessagePackc                 C   s
   || _ d S ©N©Ú_parts)ÚselfÚparts© r   ú9/tmp/pip-unpacked-wheel-04z2hmy8/zeep/wsdl/attachments.pyÚ__init__   s    zMessagePack.__init__c                 C   s   dd  dd„ | jD ƒ¡ S )Nz<MessagePack(attachments=[%s])>z, c                 s   s   | ]}t |ƒV  qd S r   )Úrepr)Ú.0Úar   r   r   Ú	<genexpr>   s     z'MessagePack.__repr__.<locals>.<genexpr>)ÚjoinÚattachments©r
   r   r   r   Ú__repr__   s    ÿzMessagePack.__repr__c                 C   s   | j S r   ©Ú_rootr   r   r   r   Úroot   s    zMessagePack.rootc                 C   s
   || _ d S r   r   )r
   r   r   r   r   Ú	_set_root   s    zMessagePack._set_rootc                 C   s   dd„ | j D ƒS )zKReturn a list of attachments.

        :rtype: list of Attachment

        c                 S   s   g | ]}t |ƒ‘qS r   )Ú
Attachment)r   Úpartr   r   r   Ú
<listcomp>)   s     z+MessagePack.attachments.<locals>.<listcomp>r   r   r   r   r   r   "   s    zMessagePack.attachmentsc                 C   s"   | j D ]}|j|kr|  S qdS )z‰get_by_content_id

        :param content_id: The content-id to return
        :type content_id: str
        :rtype: Attachment

        N)r   Ú
content_id)r
   r   Ú
attachmentr   r   r   Úget_by_content_id+   s    

zMessagePack.get_by_content_idN)Ú__name__Ú
__module__Ú__qualname__r   r   Úpropertyr   r   r   r   r    r   r   r   r   r      s   

r   c                   @   s(   e Zd Zdd„ Zdd„ Zedd„ ƒZdS )r   c                    sb   |j pd‰ t‡ fdd„|j ¡ D ƒƒ| _| j dd ¡| _| j dd ¡| _| j dd ¡| _|| _d S )Nzutf-8c                    s"   i | ]\}}|  ˆ ¡|  ˆ ¡“qS r   )Údecode)r   ÚkÚv©Úencodingr   r   Ú
<dictcomp><   s      z'Attachment.__init__.<locals>.<dictcomp>zContent-Typez
Content-IDzContent-Location)	r)   r   ÚheadersÚitemsÚgetÚcontent_typer   Úcontent_locationÚ_part)r
   r   r   r(   r   r   9   s    
ÿzAttachment.__init__c                 C   s   d| j | jf S )Nz<Attachment(%r, %r)>)r   r.   r   r   r   r   r   C   s    zAttachment.__repr__c                 C   sB   | j  dd¡}| jj}|dkr(t |¡S |dkr:| d¡S |S dS )zLReturn the content of the attachment

        :rtype: bytes or str

        zContent-Transfer-EncodingNÚbase64Úbinarys   
)r+   r-   r0   Úcontentr1   Ú	b64decodeÚstrip)r
   r)   r3   r   r   r   r3   F   s    

zAttachment.contentN)r!   r"   r#   r   r   r   r3   r   r   r   r   r   8   s   
r   )
Ú__doc__r1   ÚsysÚversion_infoÚ	functoolsr   Zrequests.structuresr   r   r   r   r   r   r   Ú<module>   s   
&