U
    3d#                     @   s|   d dl Z d dlZd dlZd dl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 d dlmZ G dd	 d	e
Z	dS )
    N)StringIO)BotoClientError)Key)KeyFile)compute_hash)get_utf8_valuec                
       s  e Zd ZdZd; fdd	Zdd Zdd Zd<d
dZdd Zdd Z	d= fdd	Z
d>ddZd?ddZd@ddZdAddZdBddZdd Zd d! ZdCd"d#Zd$d% ZdDd'd(ZdEd)d*ZdFd+d,Z fd-d.ZdGd/d0ZdHd1d2ZdId3d4ZdJd5d6ZdKd7d8ZdLd9d:Z  ZS )Mr   a  
    Represents a key (object) in a GS bucket.

    :ivar bucket: The parent :class:`boto.gs.bucket.Bucket`.
    :ivar name: The name of this Key object.
    :ivar metadata: A dictionary containing user metadata that you
        wish to store with the object or that has been retrieved from
        an existing object.
    :ivar cache_control: The value of the `Cache-Control` HTTP header.
    :ivar content_type: The value of the `Content-Type` HTTP header.
    :ivar content_encoding: The value of the `Content-Encoding` HTTP header.
    :ivar content_disposition: The value of the `Content-Disposition` HTTP
        header.
    :ivar content_language: The value of the `Content-Language` HTTP header.
    :ivar etag: The `etag` associated with this object.
    :ivar last_modified: The string timestamp representing the last
        time this object was modified in GS.
    :ivar owner: The ID of the owner of this object.
    :ivar storage_class: The storage class of the object. Currently, one of:
        STANDARD | DURABLE_REDUCED_AVAILABILITY.
    :ivar md5: The MD5 hash of the contents of the object.
    :ivar size: The size, in bytes, of the object.
    :ivar generation: The generation number of the object.
    :ivar metageneration: The generation number of the object metadata.
    :ivar encrypted: Whether the object is encrypted while at rest on
        the server.
    :ivar cloud_hashes: Dictionary of checksums as supplied by the storage
        provider.
    Nc                    s0   t t| j||d || _d | _i | _d | _d S )N)bucketname)superr   __init__
generationZmeta_generationcloud_hashescomponent_count)selfr   r	   r   	__class__ //tmp/pip-unpacked-wheel-d7dsrkjd/boto/gs/key.pyr   A   s
    zKey.__init__c                 C   sN   | j r| jrd| j | jf }nd}| jr<d| jj| j|f S d| j|f S d S )Nz#%s.%s z<Key: %s,%s%s>z<Key: None,%s%s>)r   metagenerationr   r	   )r   Zver_strr   r   r   __repr__H   s    zKey.__repr__c                 C   s   |dkr|| _ n|dkr || _n|dkr@|dkr8d| _qd| _nz|dkrP|| _nj|dkrdt|| _nV|d	krt|| _nF|d
kr~n<|dkr|| _n,|dkr|| _n|dkr|| _	nt
| || d S )Nr   ETagZIsLatesttrueTFZLastModifiedSizeZStorageClassZOwnerZ	VersionIdZ
GenerationZMetaGeneration)r	   etagZ	is_latestZlast_modifiedintsizestorage_class
version_idr   r   setattr)r   r	   value
connectionr   r   r   
endElementR   s.    zKey.endElementFc                 C   s    | dd | _| dd | _d S )Nzx-goog-metagenerationx-goog-generation)	getheaderr   r   )r   respforcer   r   r   handle_version_headersm   s    zKey.handle_version_headersc                 C   s   d S )Nr   )r   responser   r   r   handle_restore_headersq   s    zKey.handle_restore_headersc                 C   s   |D ]\}}|dkrJ| dD ](}|  dd\}}t|| j|< qq|dkr^t|| _q|dkrn|| _q|dkr~|| _q|dkrt|| _	q|d	kr|| _
qd S )
Nzx-goog-hash,=   zx-goog-component-countr#   zx-goog-stored-content-encodingzx-goog-stored-content-lengthzx-goog-storage-class)splitstripbinascii
a2b_base64r   r   r   r   content_encodingr   r   )r   headerskeyr    Z	hash_pairZalg
b64_digestr   r   r   handle_addl_headerst   s    zKey.handle_addl_headersr   c                    s<   | j r |r|d7 }|d| j  7 }tt| j||||d dS )a  
        Open this key for reading

        :type headers: dict
        :param headers: Headers to pass in the web request

        :type query_args: string
        :param query_args: Arguments to pass in the query string
            (ie, 'torrent')

        :type override_num_retries: int
        :param override_num_retries: If not None will override configured
            num_retries parameter for underlying GET.

        :type response_headers: dict
        :param response_headers: A dictionary containing HTTP
            headers/values that will override any headers associated
            with the stored object in the response.  See
            http://goo.gl/EWOPb for details.
        &generation=%s)r2   
query_argsoverride_num_retriesresponse_headersN)r   r
   r   	open_read)r   r2   r8   r9   r:   r   r   r   r;      s    zKey.open_read
   c
              
   C   s4   d }
| j rd| j  g}
| j|||||||	|
d d S )Nr7   )r2   cbnum_cbr9   r:   	hash_algsr8   )r   Z_get_file_internal)r   fpr2   r=   r>   torrentr   r9   r:   r?   r8   r   r   r   get_file   s    zKey.get_filec
           
   
   C   sH   | j dk	rD|r*|j| |||||||	d n| j||||||||	d dS )aG  
        Retrieve an object from GCS using the name of the Key object as the
        key in GCS. Write the contents of the object to the file pointed
        to by 'fp'.

        :type fp: File -like object
        :param fp:

        :type headers: dict
        :param headers: additional HTTP headers that will be sent with
            the GET request.

        :type cb: function
        :param cb: a callback function that will be called to report
            progress on the upload. The callback should accept two
            integer parameters, the first representing the number of
            bytes that have been successfully transmitted to GCS and
            the second representing the size of the to be transmitted
            object.

        :type cb: int
        :param num_cb: (optional) If a callback is specified with the
            cb parameter this parameter determines the granularity of
            the callback by defining the maximum number of times the
            callback will be called during the file transfer.

        :type torrent: bool
        :param torrent: If True, returns the contents of a torrent
            file as a string.

        :type res_upload_handler: ResumableDownloadHandler
        :param res_download_handler: If provided, this handler will
            perform the download.

        :type response_headers: dict
        :param response_headers: A dictionary containing HTTP
            headers/values that will override any headers associated
            with the stored object in the response. See
            http://goo.gl/sMkcC for details.
        N)rA   r   r?   )rA   r   r:   r?   )r   rB   )
r   r@   r2   r=   r>   rA   r   Zres_download_handlerr:   r?   r   r   r   get_contents_to_file   s    /
zKey.get_contents_to_filec                 C   s"   t |||d\}}}|| _||fS )at  
        :type fp: file
        :param fp: File pointer to the file to hash. The file
            pointer will be reset to the same position before the
            method returns.

        :type algorithm: zero-argument constructor for hash objects that
            implements update() and digest() (e.g. hashlib.md5)

        :type size: int
        :param size: (optional) The Maximum number of bytes to read
            from the file pointer (fp). This is useful when uploading
            a file in multiple parts where the file is being split
            in place into different parts. Less bytes may be available.
        )r   Zhash_algorithm)r   r   )r   r@   	algorithmr   
hex_digestr4   	data_sizer   r   r   r      s      zKey.compute_hashc	           	   
   C   s   | j ||||||||d dS )a;  
        Upload a file to GCS.

        :type fp: file
        :param fp: The file pointer to upload. The file pointer must
            point at the offset from which you wish to upload.
            ie. if uploading the full file, it should point at the
            start of the file. Normally when a file is opened for
            reading, the fp will point at the first byte. See the
            bytes parameter below for more info.

        :type headers: dict
        :param headers: The headers to pass along with the PUT request

        :type num_cb: int
        :param num_cb: (optional) If a callback is specified with the
            cb parameter this parameter determines the granularity of
            the callback by defining the maximum number of times the
            callback will be called during the file
            transfer. Providing a negative integer will cause your
            callback to be called with each buffer read.

        :type query_args: string
        :param query_args: Arguments to pass in the query string.

        :type chunked_transfer: boolean
        :param chunked_transfer: (optional) If true, we use chunked
            Transfer-Encoding.

        :type size: int
        :param size: (optional) The Maximum number of bytes to read
            from the file pointer (fp). This is useful when uploading
            a file in multiple parts where you are splitting the file
            up into different ranges to be uploaded. If not specified,
            the default behaviour is to read all bytes from the file
            pointer. Less bytes may be available.

        :type hash_algs: dictionary
        :param hash_algs: (optional) Dictionary of hash algorithms and
            corresponding hashing class that implements update() and digest().
            Defaults to {'md5': hashlib.md5}.
        )r2   r=   r>   r8   chunked_transferr   r?   N)Z_send_file_internal)	r   r@   r2   r=   r>   r8   rG   r   r?   r   r   r   	send_file  s    - zKey.send_filec                 C   s   | j j| j| j| j|dS )N)r   r   r2   )r   Z
delete_keyr	   r   r   )r   r2   r   r   r   delete=  s    z
Key.deletec                 C   s"   |   }||| | | dS )a  
        Convenience method that provides a quick way to add an email grant to a
        key. This method retrieves the current ACL, creates a new grant based on
        the parameters passed in, adds that grant to the ACL and then PUT's the
        new ACL back to GS.

        :type permission: string
        :param permission: The permission being granted. Should be one of:
            READ|FULL_CONTROL
            See http://code.google.com/apis/storage/docs/developer-guide.html#authorization
            for more details on permissions.

        :type email_address: string
        :param email_address: The email address associated with the Google
                              account to which you are granting the permission.
        N)get_acladd_email_grantset_acl)r   
permissionemail_addressaclr   r   r   rK   B  s    zKey.add_email_grantc                 C   s"   |   }||| | | dS )a  
        Convenience method that provides a quick way to add a canonical user
        grant to a key. This method retrieves the current ACL, creates a new
        grant based on the parameters passed in, adds that grant to the ACL and
        then PUT's the new ACL back to GS.

        :type permission: string
        :param permission: The permission being granted. Should be one of:
            READ|FULL_CONTROL
            See http://code.google.com/apis/storage/docs/developer-guide.html#authorization
            for more details on permissions.

        :type user_id: string
        :param user_id: The canonical user id associated with the GS account to
             which you are granting the permission.
        N)rJ   add_user_grantrL   )r   rM   Zuser_idrO   r   r   r   rP   W  s    zKey.add_user_grantc                 C   s*   | j |d}||| | j||d dS )a  
        Convenience method that provides a quick way to add an email group
        grant to a key. This method retrieves the current ACL, creates a new
        grant based on the parameters passed in, adds that grant to the ACL and
        then PUT's the new ACL back to GS.

        :type permission: string
        :param permission: The permission being granted. Should be one of:
            READ|FULL_CONTROL
            See http://code.google.com/apis/storage/docs/developer-guide.html#authorization
            for more details on permissions.

        :type email_address: string
        :param email_address: The email address associated with the Google
            Group to which you are granting the permission.
        )r2   N)rJ   add_group_email_grantrL   )r   rM   rN   r2   rO   r   r   r   rQ   l  s    zKey.add_group_email_grantc                 C   s"   |   }||| | | dS )a  
        Convenience method that provides a quick way to add a canonical group
        grant to a key. This method retrieves the current ACL, creates a new
        grant based on the parameters passed in, adds that grant to the ACL and
        then PUT's the new ACL back to GS.

        :type permission: string
        :param permission: The permission being granted. Should be one of:
            READ|FULL_CONTROL
            See http://code.google.com/apis/storage/docs/developer-guide.html#authorization
            for more details on permissions.

        :type group_id: string
        :param group_id: The canonical group id associated with the Google
            Groups account you are granting the permission to.
        N)rJ   add_group_grantrL   )r   rM   Zgroup_idrO   r   r   r   rR     s    zKey.add_group_grantTc                 C   s  | j jj}|r|	rtd|p i }|r0|||j< |
rD|dtj nbt|t	s|
 }|dtj |
 |kr|dtj |
 |kr|| td|| t|dr|j| _| j dk	rt|t	r| }|j|
  }	|	| _td|jr|jd}|tt|f}|	r*|	| _n4|
 }|dtj |
 | | _|| | j}	|dkrt| ||	}|d | _|d | _| jdkr| j| _|s| j | jrdS |dk	rt||d	< |r|| |||| n| j|||||	d
 dS )a   
        Store an object in GS using the name of the Key object as the
        key in GS and the contents of the file pointed to by 'fp' as the
        contents.

        :type fp: file
        :param fp: The file whose contents are to be uploaded.

        :type headers: dict
        :param headers: (optional) Additional HTTP headers to be sent with the
            PUT request.

        :type replace: bool
        :param replace: (optional) If this parameter is False, the method will
            first check to see if an object exists in the bucket with the same
            key. If it does, it won't overwrite it. The default value is True
            which will overwrite the object.

        :type cb: function
        :param cb: (optional) Callback function that will be called to report
            progress on the upload. The callback should accept two integer
            parameters, the first representing the number of bytes that have
            been successfully transmitted to GS and the second representing the
            total number of bytes that need to be transmitted.

        :type num_cb: int
        :param num_cb: (optional) If a callback is specified with the cb
            parameter, this parameter determines the granularity of the callback
            by defining the maximum number of times the callback will be called
            during the file transfer.

        :type policy: :class:`boto.gs.acl.CannedACLStrings`
        :param policy: (optional) A canned ACL policy that will be applied to
            the new key in GS.

        :type md5: tuple
        :param md5: (optional) A tuple containing the hexdigest version of the
            MD5 checksum of the file as the first element and the
            Base64-encoded version of the plain checksum as the second element.
            This is the same format returned by the compute_md5 method.

            If you need to compute the MD5 for any reason prior to upload, it's
            silly to have to do it twice so this param, if present, will be
            used as the MD5 values of the file. Otherwise, the checksum will be
            computed.

        :type res_upload_handler: :py:class:`boto.gs.resumable_upload_handler.ResumableUploadHandler`
        :param res_upload_handler: (optional) If provided, this handler will
            perform the upload.

        :type size: int
        :param size: (optional) The Maximum number of bytes to read from the
            file pointer (fp). This is useful when uploading a file in multiple
            parts where you are splitting the file up into different ranges to
            be uploaded. If not specified, the default behaviour is to read all
            bytes from the file pointer. Less bytes may be available.

            Notes:

                1. The "size" parameter currently cannot be used when a
                   resumable upload handler is given but is still useful for
                   uploading part of a file as implemented by the parent class.
                2. At present Google Cloud Storage does not support multipart
                   uploads.

        :type rewind: bool
        :param rewind: (optional) If True, the file pointer (fp) will be
            rewound to the start before any bytes are read from it. The default
            behaviour is False which reads from the current position of the
            file pointer (fp).

        :type if_generation: int
        :param if_generation: (optional) If set to a generation number, the
            object will only be written to if its current generation number is
            this value. If set to the value 0, the object will only be written
            if it doesn't already exist.

        :rtype: int
        :return: The number of bytes written to the key.

        TODO: At some point we should refactor the Bucket and Key classes,
        to move functionality common to all providers into a parent class,
        and provider-specific functionality into subclasses (rather than
        just overriding/sharing code the way it currently works).
        z1"size" param not supported for resumable uploads.r   z8fp is at EOF. Use rewind option or seek() to data start.r	   Nz^"[a-fA-F0-9]{32}"$"r,   x-goog-if-generation-match)r   ) r   r!   providerr   Z
acl_headerseekosSEEK_SET
isinstancer   tellSEEK_ENDAttributeErrorhasattrr	   pathZgetkeyr   rematchr   r.   base64	b64encoder/   	unhexlifyZcompute_md5md5	base64md5lookupstrrH   )r   r@   r2   replacer=   r>   policyrd   res_upload_handlerr   rewindif_generationrU   sposr3   r   r   r   r   set_contents_from_file  sd    Y










zKey.set_contents_from_filec                 C   s<   i | _ t|d"}| j||||||||	|
d	 W 5 Q R X dS )a 
  
        Store an object in GS using the name of the Key object as the
        key in GS and the contents of the file named by 'filename'.
        See set_contents_from_file method for details about the
        parameters.

        :type filename: string
        :param filename: The name of the file that you want to put onto GS.

        :type headers: dict
        :param headers: (optional) Additional headers to pass along with the
            request to GS.

        :type replace: bool
        :param replace: (optional) If True, replaces the contents of the file
            if it already exists.

        :type cb: function
        :param cb: (optional) Callback function that will be called to report
            progress on the upload. The callback should accept two integer
            parameters, the first representing the number of bytes that have
            been successfully transmitted to GS and the second representing the
            total number of bytes that need to be transmitted.

        :type num_cb: int
        :param num_cb: (optional) If a callback is specified with the cb
            parameter this parameter determines the granularity of the callback
            by defining the maximum number of times the callback will be called
            during the file transfer.

        :type policy: :py:attribute:`boto.gs.acl.CannedACLStrings`
        :param policy: (optional) A canned ACL policy that will be applied to
            the new key in GS.

        :type md5: tuple
        :param md5: (optional) A tuple containing the hexdigest version of the
            MD5 checksum of the file as the first element and the
            Base64-encoded version of the plain checksum as the second element.
            This is the same format returned by the compute_md5 method.

            If you need to compute the MD5 for any reason prior to upload, it's
            silly to have to do it twice so this param, if present, will be
            used as the MD5 values of the file. Otherwise, the checksum will be
            computed.

        :type res_upload_handler: :py:class:`boto.gs.resumable_upload_handler.ResumableUploadHandler`
        :param res_upload_handler: (optional) If provided, this handler will
            perform the upload.

        :type if_generation: int
        :param if_generation: (optional) If set to a generation number, the
            object will only be written to if its current generation number is
            this value. If set to the value 0, the object will only be written
            if it doesn't already exist.
        rbrl   N)Zlocal_hashesopenrn   )r   filenamer2   rh   r=   r>   ri   rd   Zreduced_redundancyrj   rl   r@   r   r   r   set_contents_from_filenameH  s    >  zKey.set_contents_from_filenamec	              
   C   s>   d| _ d| _tt|}	| j|	|||||||d}
|	  |
S )aA	  
        Store an object in GCS using the name of the Key object as the
        key in GCS and the string 's' as the contents.
        See set_contents_from_file method for details about the
        parameters.

        :type headers: dict
        :param headers: Additional headers to pass along with the
                        request to AWS.

        :type replace: bool
        :param replace: If True, replaces the contents of the file if
                        it already exists.

        :type cb: function
        :param cb: a callback function that will be called to report
                   progress on the upload. The callback should accept
                   two integer parameters, the first representing the
                   number of bytes that have been successfully
                   transmitted to GCS and the second representing the
                   size of the to be transmitted object.

        :type cb: int
        :param num_cb: (optional) If a callback is specified with
                       the cb parameter this parameter determines the
                       granularity of the callback by defining
                       the maximum number of times the callback will
                       be called during the file transfer.

        :type policy: :class:`boto.gs.acl.CannedACLStrings`
        :param policy: A canned ACL policy that will be applied to the
                       new key in GCS.

        :type md5: A tuple containing the hexdigest version of the MD5
                   checksum of the file as the first element and the
                   Base64-encoded version of the plain checksum as the
                   second element. This is the same format returned by
                   the compute_md5 method.
        :param md5: If you need to compute the MD5 for any reason prior
                    to upload, it's silly to have to do it twice so this
                    param, if present, will be used as the MD5 values
                    of the file. Otherwise, the checksum will be computed.

        :type if_generation: int
        :param if_generation: (optional) If set to a generation number, the
            object will only be written to if its current generation number is
            this value. If set to the value 0, the object will only be written
            if it doesn't already exist.
        Nrp   )rd   re   r   r   rn   close)r   sr2   rh   r=   r>   ri   rd   rl   r@   rr   r   r   set_contents_from_string  s    6 zKey.set_contents_from_stringc                    sJ   | dd}|dk	r4|di }t||d< ||d< tt| j|| dS )a1
  
        Store an object using the name of the Key object as the key in
        cloud and the contents of the data stream pointed to by 'fp' as
        the contents.

        The stream object is not seekable and total size is not known.
        This has the implication that we can't specify the
        Content-Size and Content-MD5 in the header. So for huge
        uploads, the delay in calculating MD5 is avoided but with a
        penalty of inability to verify the integrity of the uploaded
        data.

        :type fp: file
        :param fp: the file whose contents are to be uploaded

        :type headers: dict
        :param headers: additional HTTP headers to be sent with the
            PUT request.

        :type replace: bool
        :param replace: If this parameter is False, the method will first check
            to see if an object exists in the bucket with the same key. If it
            does, it won't overwrite it. The default value is True which will
            overwrite the object.

        :type cb: function
        :param cb: a callback function that will be called to report
            progress on the upload. The callback should accept two integer
            parameters, the first representing the number of bytes that have
            been successfully transmitted to GS and the second representing the
            total number of bytes that need to be transmitted.

        :type num_cb: int
        :param num_cb: (optional) If a callback is specified with the
            cb parameter, this parameter determines the granularity of
            the callback by defining the maximum number of times the
            callback will be called during the file transfer.

        :type policy: :class:`boto.gs.acl.CannedACLStrings`
        :param policy: A canned ACL policy that will be applied to the new key
            in GS.

        :type size: int
        :param size: (optional) The Maximum number of bytes to read from
            the file pointer (fp). This is useful when uploading a
            file in multiple parts where you are splitting the file up
            into different ranges to be uploaded. If not specified,
            the default behaviour is to read all bytes from the file
            pointer. Less bytes may be available.

        :type if_generation: int
        :param if_generation: (optional) If set to a generation number, the
            object will only be written to if its current generation number is
            this value. If set to the value 0, the object will only be written
            if it doesn't already exist.
        rl   Nr2   rT   )popgetrg   r
   r   set_contents_from_stream)r   argskwargsrl   r2   r   r   r   rz     s    9zKey.set_contents_from_streamc                 C   s(   | j dk	r$| j j|| j||||d dS )a  Sets the ACL for this object.

        :type acl_or_str: string or :class:`boto.gs.acl.ACL`
        :param acl_or_str: A canned ACL string (see
            :data:`~.gs.acl.CannedACLStrings`) or an ACL object.

        :type headers: dict
        :param headers: Additional headers to set during the request.

        :type generation: int
        :param generation: If specified, sets the ACL for a specific generation
            of a versioned object. If not specified, the current version is
            modified.

        :type if_generation: int
        :param if_generation: (optional) If set to a generation number, the acl
            will only be updated if its current generation number is this value.

        :type if_metageneration: int
        :param if_metageneration: (optional) If set to a metageneration number,
            the acl will only be updated if its current metageneration number is
            this value.
        Nr2   r   rl   if_metageneration)r   rL   r	   )r   Z
acl_or_strr2   r   rl   r~   r   r   r   rL     s    
zKey.set_aclc                 C   s"   | j dk	r| j j| j||dS dS )aQ  Returns the ACL of this object.

        :param dict headers: Additional headers to set during the request.

        :param int generation: If specified, gets the ACL for a specific
            generation of a versioned object. If not specified, the current
            version is returned.

        :rtype: :class:`.gs.acl.ACL`
        Nr2   r   )r   rJ   r	   r   r2   r   r   r   r   rJ   ,  s    
zKey.get_aclc                 C   s"   | j dk	r| j j| j||dS dS )aG  Returns the ACL string of this object.

        :param dict headers: Additional headers to set during the request.

        :param int generation: If specified, gets the ACL for a specific
            generation of a versioned object. If not specified, the current
            version is returned.

        :rtype: str
        Nr   )r   get_xml_aclr	   r   r   r   r   r   ;  s    
zKey.get_xml_aclc                 C   s(   | j dk	r$| j j|| j||||dS dS )a`  Sets this objects's ACL to an XML string.

        :type acl_str: string
        :param acl_str: A string containing the ACL XML.

        :type headers: dict
        :param headers: Additional headers to set during the request.

        :type generation: int
        :param generation: If specified, sets the ACL for a specific generation
            of a versioned object. If not specified, the current version is
            modified.

        :type if_generation: int
        :param if_generation: (optional) If set to a generation number, the acl
            will only be updated if its current generation number is this value.

        :type if_metageneration: int
        :param if_metageneration: (optional) If set to a metageneration number,
            the acl will only be updated if its current metageneration number is
            this value.
        Nr}   )r   set_xml_aclr	   r   Zacl_strr2   r   rl   r~   r   r   r   r   J  s    
zKey.set_xml_aclc                 C   s(   | j dk	r$| j j|| j||||dS dS )a  Sets this objects's ACL using a predefined (canned) value.

        :type acl_str: string
        :param acl_str: A canned ACL string. See
            :data:`~.gs.acl.CannedACLStrings`.

        :type headers: dict
        :param headers: Additional headers to set during the request.

        :type generation: int
        :param generation: If specified, sets the ACL for a specific generation
            of a versioned object. If not specified, the current version is
            modified.

        :type if_generation: int
        :param if_generation: (optional) If set to a generation number, the acl
            will only be updated if its current generation number is this value.

        :type if_metageneration: int
        :param if_metageneration: (optional) If set to a metageneration number,
            the acl will only be updated if its current metageneration number is
            this value.
        Nr}   )r   set_canned_aclr	   r   r   r   r   r   h  s    
zKey.set_canned_aclc           	      C   s   g }|D ]H}|j j| j jkr$tdd}|jr<dt|j }|d|j|f  qdd| }|pfi }|rt||d< | j jjdt	| j jt	| j|dt	|d	}|j
d
k s|j
dkr| j jj|j
|j| |dS )a  Create a new object from a sequence of existing objects.

        The content of the object representing this Key will be the
        concatenation of the given object sequence. For more detail, visit

            https://developers.google.com/storage/docs/composite-objects

        :type components list of Keys
        :param components List of gs.Keys representing the component objects

        :type content_type (optional) string
        :param content_type Content type for the new composite object.
        z+GCS does not support inter-bucket composingr   z<Generation>%s</Generation>z(<Component><Name>%s</Name>%s</Component>z#<ComposeRequest>%s</ComposeRequest>zContent-TypePUTcompose)r2   r8   data   i+  r#   )r   r	   r   r   rg   appendjoinr!   Zmake_requestr   statusrU   Zstorage_response_errorreasonreadr$   )	r   
componentscontent_typer2   Zcompose_reqr3   Zgeneration_tagZcompose_req_xmlr%   r   r   r   r     sF     
  
  zKey.compose)NNN)F)Nr   NN)NNr<   FNNNN)NNr<   FNNNN)N)NNr<   NFNN)N)N)
NTNr<   NNNNFN)	NTNr<   NNNNN)NTNr<   NNN)NNNN)NN)NN)NNNN)NNNN)NN)__name__
__module____qualname____doc__r   r   r"   r'   r)   r5   r;   rB   rC   r   rH   rI   rK   rP   rQ   rR   rn   rs   rw   rz   rL   rJ   r   r   r   r   __classcell__r   r   r   r   r   "   s   

              
         
;
       
2

              
 3           
E         
@@    


    
    
#r   )ra   r/   rW   r_   Zboto.compatr   Zboto.exceptionr   Zboto.s3.keyr   ZS3KeyZboto.s3.keyfiler   Z
boto.utilsr   r   r   r   r   r   <module>   s   