B
    EDbS                 @   s>   d dl Z d dlZd dlmZmZ G dd deZdddZdS )	    N)NonExistingAttachmentInvalidAttachmentReferencec               @   s4   e Zd Zdi dfddZdd Zdd Zd	d
 ZdS )
attachmentNzUTF-8c             C   s  ddl m} i | _|| _xJ| D ]>\}}t||kr@||}t||krV||}|| j|< q"W || _| jj| _t	| jdr| jj
| _
t	| jdr| jj| _t	| jdr| jj| _t	| jdr| jj| _t	| jdr| jj| _t	| jdr| jj| _t	| jd	r| jj| _| jj| _d
S )z^
		attachment objects consist of a binary file-like buffer object and a header
		dict. The 
		r   )PORTABLE_BYTESreadline	readlines
xreadlinesseekreadintoflushclosedN)Zmysite.ladon.compatr   headerssizeitemstypedecode	bufferobjreadhasattrr   r   r   r	   r
   r   r   close)selfr   Zbytesizer   Zheader_encodingr   kv r   6G:\easytimepro\master/mysite/ladon\types\attachment.py__init__   s4    









zattachment.__init__c             C   s   | j r| j   d S )N)r   r   )r   r   r   r   __del__'   s    zattachment.__del__c             C   s   || j kr| j | S d S d S )N)r   )r   namer   r   r   header+   s    

zattachment.headerc             C   s   | j S )N)r   )r   r   r   r   r   1   s    zattachment.headers)__name__
__module____qualname__r   r   r   r   r   r   r   r   r      s    r    c             C   sF  t dt j}t dt j}d }x||gD  ]}|| }|r||kr| d |}	|	rd|kr|	|d kr|d |	 }
tt|
d d|
d |
d |S t||d	|	 P |r,||kr,t	| d |}d
|kr||d
 kr|d
 | }
tt|
d d|
d |
d |S t||d| P q,W |sBt
||d|  d S )Nz
^cid:(.+)$z^cidx:(\d+)$r   Zattachments_by_idpathrbr   r   z2Attachment reference %s is not part of the requestZattachmentsz3Attachment with index:%d is not part of the requestzMAttachment reference %s has invalid format, must be cid:<ref> or cidx:<index>)recompileImatchgroupsencoder   openr   intr   )Za_referenceZexport_dictZreq_encodingZifnameZsnameZrx_cidZrx_cidxmrxZcidZattachment_infoZcidxr   r   r   extract_attachment_reference5   s*    
  r/   )r"   r"   )osr%   Z"mysite.ladon.exceptions.dispatcherr   r   objectr   r/   r   r   r   r   <module>   s   0