U
    3d/                     @   sj   d dl mZ d dl mZ d dlmZ d dlZG dd deZG dd deZ	dd	d
Z
G dd deZdS )    )user)key)handlerNc                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )CompleteMultiPartUploada  
    Represents a completed MultiPart Upload.  Contains the
    following useful attributes:

     * location - The URI of the completed upload
     * bucket_name - The name of the bucket in which the upload
                     is contained
     * key_name - The name of the new, completed key
     * etag - The MD5 hash of the completed, combined upload
     * version_id - The version_id of the completed upload
     * encrypted - The value of the encryption header
    Nc                 C   s.   || _ d | _d | _d | _d | _d | _d | _d S N)bucketlocationbucket_namekey_nameetag
version_idZ	encryptedselfr    r   5/tmp/pip-unpacked-wheel-d7dsrkjd/boto/s3/multipart.py__init__-   s    z CompleteMultiPartUpload.__init__c                 C   s   d| j | jf S )Nz <CompleteMultiPartUpload: %s.%s>)r	   r
   r   r   r   r   __repr__6   s    z CompleteMultiPartUpload.__repr__c                 C   s   d S r   r   r   nameattrs
connectionr   r   r   startElement:   s    z$CompleteMultiPartUpload.startElementc                 C   sP   |dkr|| _ n<|dkr || _n,|dkr0|| _n|dkr@|| _nt| || d S )NZLocationBucketKeyETag)r   r	   r
   r   setattrr   r   valuer   r   r   r   
endElement=   s    z"CompleteMultiPartUpload.endElement)N__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s
   
	r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )Parta  
    Represents a single part in a MultiPart upload.
    Attributes include:

     * part_number - The integer part number
     * last_modified - The last modified date of this part
     * etag - The MD5 hash of this part
     * size - The size, in bytes, of this part
    Nc                 C   s"   || _ d | _d | _d | _d | _d S r   )r   part_numberlast_modifiedr   sizer   r   r   r   r   U   s
    zPart.__init__c                 C   s"   t | jtrd| j S dd  S d S )Nz	<Part %d>z	<Part %s>)
isinstancer&   intr   r   r   r   r   \   s    
zPart.__repr__c                 C   s   d S r   r   r   r   r   r   r   b   s    zPart.startElementc                 C   sX   |dkrt || _n@|dkr$|| _n0|dkr4|| _n |dkrHt || _nt| || d S )NZ
PartNumberZLastModifiedr   Size)r*   r&   r'   r   r(   r   r   r   r   r   r   e   s    zPart.endElement)Nr    r   r   r   r   r%   J   s
   

r%   c                 c   s:   d}d}|r6|  d|}|D ]
}|V  q| j}| j}qdS )zG
    A generator function for listing parts of a multipart upload.
    TN)get_all_partsnext_part_number_markeris_truncated)Zmpuploadpart_number_markerZmore_resultspartpartsr   r   r   part_listerr   s    r2   c                   @   sp   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dddZ
dddZdddZdd Zdd ZdS )MultiPartUploadz2
    Represents a MultiPart Upload operation.
    Nc                 C   sR   || _ d | _d | _t| _d | _d | _d | _d | _d | _d | _	d | _
d| _d | _d S )NF)r   r	   r
   id	initiatorownerstorage_class	initiatedr/   r-   	max_partsr.   _partsr   r   r   r   r      s    zMultiPartUpload.__init__c                 C   s
   d| j  S )Nz<MultiPartUpload %s>)r
   r   r   r   r   r      s    zMultiPartUpload.__repr__c                 C   s   t | S r   )r2   r   r   r   r   __iter__   s    zMultiPartUpload.__iter__c                 C   sF   d}| D ]0}|d7 }|d|j  7 }|d|j 7 }|d7 }q|d7 }|S )Nz<CompleteMultipartUpload>
z	  <Part>
z     <PartNumber>%d</PartNumber>
z    <ETag>%s</ETag>
z
  </Part>
z</CompleteMultipartUpload>)r&   r   )r   sr0   r   r   r   to_xml   s    
zMultiPartUpload.to_xmlc                 C   sZ   |dkrt | | _| jS |dkr4t | | _| jS |dkrVt| j}| j| |S d S )NZ	InitiatorZOwnerr%   )r   ZUserr5   r6   r%   r   r:   append)r   r   r   r   r0   r   r   r   r      s    
zMultiPartUpload.startElementc                 C   s   |dkr|| _ n|dkr || _n|dkr0|| _n|dkr@|| _np|dkrP|| _n`|dkr`|| _nP|dkrtt|| _n<|dkr|d	krd
| _qd| _n|dkr|| _	nt
| || d S )Nr   r   ZUploadIdZStorageClassZPartNumberMarkerZNextPartNumberMarkerZMaxPartsZIsTruncatedtrueTFZ	Initiated)r	   r
   r4   r7   r/   r-   r*   r9   r.   r8   r   r   r   r   r   r      s*    zMultiPartUpload.endElementc                 C   s   g | _ d| j }|r |d| 7 }|r0|d| 7 }|r@|d| 7 }| jjjd| jj| j|d}| }|jdkrt	
| | }tj|| | j S dS )	a@  
        Return the uploaded parts of this MultiPart Upload.  This is
        a lower-level method that requires you to manually page through
        results.  To simplify this process, you can just use the
        object itself as an iterator and it will automatically handle
        all of the paging with S3.
        zuploadId=%sz&max-parts=%dz&part-number-marker=%sz&encoding-type=%sGET)
query_args   N)r:   r4   r   r   Zmake_requestr   r
   readstatusr   Z
XmlHandlerxmlsaxparseString)r   r9   r/   Zencoding_typerA   responsebodyhr   r   r   r,      s"    	

zMultiPartUpload.get_all_partsT
   c	                 C   sL   |dk rt dd| j|f }	| j| j}
|
j||||||d|	|d	 |
S )a_  
        Upload another part of this MultiPart Upload.

        .. note::

            After you initiate multipart upload and upload one or more parts,
            you must either complete or abort multipart upload in order to stop
            getting charged for storage of the uploaded parts. Only after you
            either complete or abort multipart upload, Amazon S3 frees up the
            parts storage and stops charging you for the parts storage.

        :type fp: file
        :param fp: The file object you want to upload.

        :type part_num: int
        :param part_num: The number of this part.

        The other parameters are exactly as defined for the
        :class:`boto.s3.key.Key` set_contents_from_file method.

        :rtype: :class:`boto.s3.key.Key` or subclass
        :returns: The uploaded part containing the etag.
           &Part numbers must be greater than zerouploadId=%s&partNumber=%dF)headersreplacecbnum_cbmd5Zreduced_redundancyrA   r(   )
ValueErrorr4   r   Znew_keyr
   Zset_contents_from_file)r   fppart_numrO   rP   rQ   rR   rS   r(   rA   r   r   r   r   upload_part_from_file   s    
   z%MultiPartUpload.upload_part_from_filec              	   C   s   |dk rt dd| j|f }|dk	rd|dk	rdd||f }	| jjj}
|dkrRi }n| }|	||
j< | jj| j|||d||dS )a  
        Copy another part of this MultiPart Upload.

        :type src_bucket_name: string
        :param src_bucket_name: Name of the bucket containing the source key

        :type src_key_name: string
        :param src_key_name: Name of the source key

        :type part_num: int
        :param part_num: The number of this part.

        :type start: int
        :param start: Zero-based byte offset to start copying from

        :type end: int
        :param end: Zero-based byte offset to copy to

        :type src_version_id: string
        :param src_version_id: version_id of source object to copy from

        :type headers: dict
        :param headers: Any headers to pass along in the request
        rL   rM   rN   Nzbytes=%s-%s)src_version_idr7   rO   rA   )	rT   r4   r   r   providercopyZcopy_source_range_headerZcopy_keyr
   )r   Zsrc_bucket_nameZsrc_key_namerV   startendrX   rO   rA   rngrY   r   r   r   copy_part_from_key  s"    

z"MultiPartUpload.copy_part_from_keyc                 C   s   |   }| j| j| j|S )a.  
        Complete the MultiPart Upload operation.  This method should
        be called when all parts of the file have been successfully
        uploaded to S3.

        :rtype: :class:`boto.s3.multipart.CompletedMultiPartUpload`
        :returns: An object representing the completed upload.
        )r=   r   Zcomplete_multipart_uploadr
   r4   )r   rE   r   r   r   complete_upload4  s
    	
 zMultiPartUpload.complete_uploadc                 C   s   | j | j| j dS )a  
        Cancels a MultiPart Upload operation.  The storage consumed by
        any previously uploaded parts will be freed. However, if any
        part uploads are currently in progress, those part uploads
        might or might not succeed. As a result, it might be necessary
        to abort a given multipart upload multiple times in order to
        completely free all storage consumed by all parts.
        N)r   Zcancel_multipart_uploadr
   r4   r   r   r   r   cancel_uploadA  s    	zMultiPartUpload.cancel_upload)N)NNN)NTNrK   NN)NNNN)r!   r"   r#   r$   r   r   r;   r=   r   r   r,   rW   r^   r_   r`   r   r   r   r   r3      s.   

  
        
$      
-r3   )N)Zboto.s3r   r   Zbotor   Zxml.saxrE   objectr   r%   r2   r3   r   r   r   r   <module>   s   +(
