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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 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Z$dZ%dZ&dZ'dZ(dZ)e *dZ+G dd deZdS )    N)handler)	ResultSet)GSResponseError)InvalidAclError)ACLCannedACLStrings)SupportedPermissionsVersionedBucketListResultSet)Cors)EncryptionConfig)LifecycleConfig)Key)Policy)Bucket)get_utf8_value)sixZdefaultObjectAclaclcorsZencryptionConfigZ	lifecycleZstorageClassz"<Details>(?P<details>.*)</Details>c                       s  e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
d
ef fdd	Zdd Zdd ZdkddZdl fdd	ZdmddZdd ZdnddZdoddZdpd d!Zd"d# Zd$d% Zdqd&d'Zdrd(d)Zdsd*d+Zdtd,d-Zdud/d0Zdvd1d2Zdwd3d4Zdxd5d6Z dyd7d8Z!dzd9d:Z"d{d;d<Z#d|d=d>Z$d}d?d@Z%d~dAdBZ&ddCdDZ'ddEdFZ(ddGdHZ)ddIdJZ*ddKdLZ+ddMdNZ,ddOdPZ-ddQdRZ.ddSdTZ/ddUdVZ0ddWdXZ1ddYdZZ2dd[d\Z3dd]d^Z4dd_d`Z5ddadbZ6ddcddZ7ddedfZ8ddgdhZ9ddidjZ:  Z;S )r   z)Represents a Google Cloud Storage bucket.zu<?xml version="1.0" encoding="UTF-8"?>
<BillingConfiguration><RequesterPays>%s</RequesterPays></BillingConfiguration>z\<?xml version="1.0" encoding="UTF-8"?>
<EncryptionConfiguration>%s</EncryptionConfiguration>z)<DefaultKmsKeyName>%s</DefaultKmsKeyName>zF<?xml version="1.0" encoding="UTF-8"?>
<StorageClass>%s</StorageClass>zm<?xml version="1.0" encoding="UTF-8"?>
<VersioningConfiguration><Status>%s</Status></VersioningConfiguration>zX<?xml version="1.0" encoding="UTF-8"?>
<WebsiteConfiguration>%s%s</WebsiteConfiguration>z#<MainPageSuffix>%s</MainPageSuffix>z<NotFoundPage>%s</NotFoundPage>Nc                    s   t t| ||| d S N)superr   __init__)self
connectionname	key_class	__class__ 2/tmp/pip-unpacked-wheel-d7dsrkjd/boto/gs/bucket.pyr   K   s    zBucket.__init__c                 C   s   d S r   r   )r   r   attrsr   r   r   r   startElementN   s    zBucket.startElementc                 C   s0   |dkr|| _ n|dkr || _nt| || d S )NNameZCreationDate)r   Zcreation_datesetattr)r   r   valuer   r   r   r   
endElementQ   s
    zBucket.endElementc              
   C   s   g }|r| d|  |rFt|D ] \}}| d|t|f  q$z| j|||d\}	}
W nH tk
r } z*|jdkrd|jkrd| j	|f |_ W 5 d}~X Y nX |	S )af  Returns a Key instance for an object in this bucket.

         Note that this method uses a HEAD request to check for the existence of
         the key.

        :type key_name: string
        :param key_name: The name of the key to retrieve

        :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/06N3b for details.

        :type version_id: string
        :param version_id: Unused in this subclass.

        :type generation: int
        :param generation: A specific generation number to fetch the key at. If
            not specified, the latest generation is fetched.

        :rtype: :class:`boto.gs.key.Key`
        :returns: A Key object from this bucket.
        generation=%sz%s=%s)query_args_l  	ForbiddenzAccess denied to 'gs://%s/%s'.N)
appendr   	iteritemsurllibquoteZ_get_key_internalr   statusreasonr   )r   key_nameheaders
version_idresponse_headers
generationr'   Zrkrvkeyresper   r   r   get_keyY   s"    zBucket.get_keySTANDARDFc                    s:   |r|	p
i }	t ||	d< tt| j||||||||	|
d	S )a  Create a new key in the bucket by copying an existing key.

        :type new_key_name: string
        :param new_key_name: The name of the new key

        :type src_bucket_name: string
        :param src_bucket_name: The name of the source bucket

        :type src_key_name: string
        :param src_key_name: The name of the source key

        :type src_generation: int
        :param src_generation: The generation number of the source key to copy.
            If not specified, the latest generation is copied.

        :type metadata: dict
        :param metadata: Metadata to be associated with new key.  If
            metadata is supplied, it will replace the metadata of the
            source key being copied.  If no metadata is supplied, the
            source key's metadata will be copied to the new key.

        :type version_id: string
        :param version_id: Unused in this subclass.

        :type storage_class: string
        :param storage_class: The storage class of the new key.  By
            default, the new key will use the standard storage class.
            Possible values are: STANDARD | DURABLE_REDUCED_AVAILABILITY

        :type preserve_acl: bool
        :param preserve_acl: If True, the ACL from the source key will
            be copied to the destination key.  If False, the
            destination key will have the default ACL.  Note that
            preserving the ACL in the new key object will require two
            additional API calls to GCS, one to retrieve the current
            ACL and one to set that ACL on the new object.  If you
            don't care about the ACL (or if you have a default ACL set
            on the bucket), a value of False will be significantly more
            efficient.

        :type encrypt_key: bool
        :param encrypt_key: Included for compatibility with S3. This argument is
            ignored.

        :type headers: dict
        :param headers: A dictionary of header name/value pairs.

        :type query_args: string
        :param query_args: A string of additional querystring arguments
            to append to the request

        :rtype: :class:`boto.gs.key.Key`
        :returns: An instance of the newly created key object
        zx-goog-copy-source-generation)metadatastorage_classpreserve_aclencrypt_keyr1   
query_args)strr   r   copy_key)r   Znew_key_nameZsrc_bucket_nameZsrc_key_namer;   Zsrc_version_idr<   r=   r>   r1   r?   Zsrc_generationr   r   r   rA      s    :
      zBucket.copy_key c                 C   s   t | |||||S )a  
        List versioned objects within a bucket.  This returns an
        instance of an VersionedBucketListResultSet that automatically
        handles all of the result paging, etc. from GCS.  You just need
        to keep iterating until there are no more results.  Called
        with no arguments, this will return an iterator object across
        all keys within the bucket.

        :type prefix: string
        :param prefix: allows you to limit the listing to a particular
            prefix.  For example, if you call the method with
            prefix='/foo/' then the iterator will only cycle through
            the keys that begin with the string '/foo/'.

        :type delimiter: string
        :param delimiter: can be used in conjunction with the prefix
            to allow you to organize and browse your keys
            hierarchically. See:
            https://developers.google.com/storage/docs/reference-headers#delimiter
            for more details.

        :type marker: string
        :param marker: The "marker" of where you are in the result set

        :type generation_marker: string
        :param generation_marker: The "generation marker" of where you are in
            the result set.

        :type headers: dict
        :param headers: A dictionary of header name/value pairs.

        :rtype:
            :class:`boto.gs.bucketlistresultset.VersionedBucketListResultSet`
        :return: an instance of a BucketListResultSet that handles paging, etc.
        r	   )r   prefix	delimitermarkergeneration_markerr1   r   r   r   list_versions   s
    % zBucket.list_versionsc              	   C   s   |  |ddddddg dS )z9
        See documentation in boto/s3/bucket.py.
        Zversion_id_markerrD   rE   rF   rC   Zmax_keysN)Zvalidate_kwarg_names)r   paramsr   r   r    validate_get_all_versions_params   s      z'Bucket.validate_get_all_versions_paramsc                 C   s.   g }|r| d|  | j|||||d dS )a  
        Deletes a key from the bucket.

        :type key_name: string
        :param key_name: The key name to delete

        :type headers: dict
        :param headers: A dictionary of header name/value pairs.

        :type version_id: string
        :param version_id: Unused in this subclass.

        :type mfa_token: tuple or list of strings
        :param mfa_token: Unused in this subclass.

        :type generation: int
        :param generation: The generation number of the key to delete. If not
            specified, the latest generation number will be deleted.

        :rtype: :class:`boto.gs.key.Key`
        :returns: A key object holding information on what was
            deleted.
        r&   )r1   r2   	mfa_tokenr'   N)r*   Z_delete_key_internal)r   r0   r1   r2   rJ   r4   r'   r   r   r   
delete_key   s     zBucket.delete_keyc                 C   sT   t |trtdn<t |tr:| j| |||||d n| j||||||d dS )a  Sets or changes a bucket's or key's ACL.

        :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 key_name: string
        :param key_name: A key name within the bucket to set the ACL for. If not
            specified, the ACL for the bucket will be set.

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

        :type version_id: string
        :param version_id: Unused in this subclass.

        :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.
        "Attempt to set S3 Policy on GS ACL)r1   r4   if_generationif_metagenerationN)
isinstancer   r   r   set_xml_aclto_xmlset_canned_acl)r   
acl_or_strr0   r1   r2   r4   rM   rN   r   r   r   set_acl  s     



zBucket.set_aclc                 C   sD   t |trtdn,t |tr2| j| |d n| j||d dS )aF  Sets or changes a bucket's default ACL.

        :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.
        rL   r1   N)rO   r   r   r   set_def_xml_aclrQ   set_def_canned_acl)r   rS   r1   r   r   r   set_def_aclE  s
    



zBucket.set_def_aclc                 C   s   | j jd| j|||d}| }|jdkr|jdkrjt|}|rL|dnd}|rjd| }t	t||}| j j
|j|j||S )zBProvides common functionality for get_xml_acl and _get_acl_helper.GETr?   r1      r(   detailsNzP<Details>%s. Note that Full Control access is required to access ACLs.</Details>)r   make_requestr   readr.   ERROR_DETAILS_REGEXsearchgroupresubproviderstorage_response_errorr/   )r   r0   r1   r?   responsebodymatchr\   r   r   r   _get_xml_acl_helperV  s(    


  zBucket._get_xml_acl_helperc                 C   s4   |  |||}t| }t|| }tj|| |S )z:Provides common functionality for get_acl and get_def_acl.)ri   r   r   
XmlHandlerxmlsaxparseString)r   r0   r1   r?   rg   r   hr   r   r   _get_acl_helperi  s
    zBucket._get_acl_helperc                 C   s"   t }|r|d| 7 }| |||S )a  Returns the ACL of the bucket or an object in the bucket.

        :param str key_name: The name of the object to get the ACL for. If not
            specified, the ACL for the bucket will be returned.

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

        :type version_id: string
        :param version_id: Unused in this subclass.

        :param int generation: If specified, gets the ACL for a specific
            generation of a versioned object. If not specified, the current
            version is returned. This parameter is only valid when retrieving
            the ACL of an object, not a bucket.

        :rtype: :class:`.gs.acl.ACL`
        &generation=%s)STANDARD_ACLro   r   r0   r1   r2   r4   r?   r   r   r   get_aclq  s    zBucket.get_aclc                 C   s"   t }|r|d| 7 }| |||S )a  Returns the ACL string of the bucket or an object in the bucket.

        :param str key_name: The name of the object to get the ACL for. If not
            specified, the ACL for the bucket will be returned.

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

        :type version_id: string
        :param version_id: Unused in this subclass.

        :param int generation: If specified, gets the ACL for a specific
            generation of a versioned object. If not specified, the current
            version is returned. This parameter is only valid when retrieving
            the ACL of an object, not a bucket.

        :rtype: str
        rp   )rq   ri   rr   r   r   r   get_xml_acl  s    zBucket.get_xml_aclc                 C   s   |  d|tS )zReturns the bucket's default ACL.

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

        :rtype: :class:`.gs.acl.ACL`
        rB   )ro   DEF_OBJ_ACLr   r1   r   r   r   get_def_acl  s    zBucket.get_def_aclc	                 C   s   |pi }d}	|r ||| j jj< n|}	|r4|d| 7 }|dk	rL|dkrLtd|s`|sX|r`td|dk	rtt||d< |dk	rt||d< | j jdt| jt|t|	||d	}
|
 }|
j	d
kr| j j
|
j	|
j|dS )zProvides common functionality for set_acl, set_xml_acl,
        set_canned_acl, set_def_acl, set_def_xml_acl, and
        set_def_canned_acl().rB   rp   NzReceived if_metageneration argument with no if_generation argument. A metageneration has no meaning without a content generation.zXReceived if_generation or if_metageneration parameter while setting the ACL of a bucket.zx-goog-if-generation-matchzx-goog-if-metageneration-matchPUT)datar1   r?   r[   )r   rd   Z
acl_header
ValueErrorr@   r]   r   r   r^   r.   re   r/   )r   rS   r0   r1   r?   r4   rM   rN   cannedry   rf   rg   r   r   r   _set_acl_helper  s<        
  zBucket._set_acl_helperr   c	           	   	   C   s   | j |||||||dS )a  Sets a bucket's or objects's ACL to an XML string.

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

        :type key_name: string
        :param key_name: A key name within the bucket to set the ACL for. If not
            specified, the ACL for the bucket will be set.

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

        :type version_id: string
        :param version_id: Unused in this subclass.

        :type query_args: str
        :param query_args: The query parameters to pass with 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.
        )r0   r1   r?   r4   rM   rN   )r|   )	r   acl_strr0   r1   r2   r?   r4   rM   rN   r   r   r   rP     s    #
zBucket.set_xml_aclc           	   
   C   s2   |t krtd| t}| j|||||||ddS )a  Sets a bucket's or 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 key_name: string
        :param key_name: A key name within the bucket to set the ACL for. If not
            specified, the ACL for the bucket will be set.

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

        :type version_id: string
        :param version_id: Unused in this subclass.

        :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.
        -Provided canned ACL string (%s) is not valid.T)r{   )r   rz   rq   r|   )	r   r}   r0   r1   r2   r4   rM   rN   r?   r   r   r   rR     s    !  zBucket.set_canned_aclc              
   C   s2   |t krtd| t}| j|d||dddddS )a)  Sets a bucket's default 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.
        r~   rB   NT)r4   rM   rN   r{   )r   rz   ru   r|   )r   r}   r1   r?   r   r   r   rW   !  s    
  zBucket.set_def_canned_aclc                 C   s   | j |d|tdS )zSets a bucket's default 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.
        rB   )r?   )rP   ru   )r   r}   r1   r   r   r   rV   3  s    	
zBucket.set_def_xml_aclc                 C   sf   | j jd| jt|d}| }|jdkrLt }t|| }t	j
|| |S | j j|j|j|dS )zReturns a bucket's CORS XML document.

        :param dict headers: Additional headers to send with the request.
        :rtype: :class:`~.cors.Cors`
        rY   rZ   r[   N)r   r]   r   CORS_ARGr^   r.   r   r   rj   rk   rl   rm   rd   re   r/   )r   r1   rf   rg   r   rn   r   r   r   get_cors?  s    
  zBucket.get_corsc                 C   sL   | j jdt| jt|t|d}| }|jdkrH| j j|j|j	|dS )zSets a bucket's CORS XML document.

        :param str cors: A string containing the CORS XML.
        :param dict headers: Additional headers to send with the request.
        rx   ry   r?   r1   r[   N)
r   r]   r   r   r   r^   r.   rd   re   r/   )r   r   r1   rf   rg   r   r   r   set_corsS  s       
  zBucket.set_corsc                 C   sj   | j jd| jt|d}| }|jdkrPt| }t|| }t	j
|| |jS | j j|j|j|dS )z
        Returns the StorageClass for the bucket.

        :rtype: str
        :return: The StorageClass for the bucket.
        rY   rZ   r[   N)r   r]   r   STORAGE_CLASS_ARGr^   r.   r   r   rj   rk   rl   rm   ZStorageClassrd   re   r/   )r   r1   rf   rg   rsrn   r   r   r   get_storage_classa  s    
  zBucket.get_storage_classc                 C   s"   | j t| }| jt||d dS )z
        Sets a bucket's storage class.

        :param str storage_class: A string containing the storage class.
        :param dict headers: Additional headers to send with the request.
        rU   N)StorageClassBodyr   set_subresourcer   )r   r<   r1   req_bodyr   r   r   set_storage_classu  s    zBucket.set_storage_classc                 C   sb   |t kr| jjd| | j|d}||| | j||d |r^| D ]}|j|||d qHdS )a  
        Convenience method that provides a quick way to add an email grant
        to a bucket. 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 GCS.

        :type permission: string
        :param permission: The permission being granted. Should be one of:
                           (READ, WRITE, FULL_CONTROL).

        :type email_address: string
        :param email_address: The email address associated with the GS
                              account your are granting the permission to.

        :type recursive: bool
        :param recursive: A boolean value to controls whether the call
                          will apply the grant to all keys within the bucket
                          or not.  The default value is False.  By passing a
                          True value, the call will iterate through all keys
                          in the bucket and apply the same grant to each key.
                          CAUTION: If you have a lot of keys, this could take
                          a long time!
        Unknown Permission: %srU   N)GSPermissionsr   rd   storage_permissions_errorrs   add_email_grantrT   r   
permissionZemail_address	recursiver1   r   r6   r   r   r   r     s    zBucket.add_email_grantc                 C   sb   |t kr| jjd| | j|d}||| | j||d |r^| D ]}|j|||d qHdS )a  
        Convenience method that provides a quick way to add a canonical user
        grant to a bucket. This method retrieves the current ACL, creates a new
        grant based on the parameters passed in, adds that grant to the ACL and
        then PUTs the new ACL back to GCS.

        :type permission: string
        :param permission:  The permission being granted.  Should be one of:
                            (READ|WRITE|FULL_CONTROL)

        :type user_id: string
        :param user_id:     The canonical user id associated with the GS account
                            you are granting the permission to.

        :type recursive: bool
        :param recursive: A boolean value to controls whether the call
                          will apply the grant to all keys within the bucket
                          or not.  The default value is False.  By passing a
                          True value, the call will iterate through all keys
                          in the bucket and apply the same grant to each key.
                          CAUTION: If you have a lot of keys, this could take
                          a long time!
        r   rU   N)r   r   rd   r   rs   add_user_grantrT   )r   r   Zuser_idr   r1   r   r6   r   r   r   r     s    zBucket.add_user_grantc                 C   sb   |t kr| jjd| | j|d}||| | j||d |r^| D ]}|j|||d qHdS )a  
        Convenience method that provides a quick way to add an email group
        grant to a bucket. 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 GCS.

        :type permission: string
        :param permission: The permission being granted. Should be one of:
            READ|WRITE|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.

        :type recursive: bool
        :param recursive: A boolean value to controls whether the call
                          will apply the grant to all keys within the bucket
                          or not.  The default value is False.  By passing a
                          True value, the call will iterate through all keys
                          in the bucket and apply the same grant to each key.
                          CAUTION: If you have a lot of keys, this could take
                          a long time!
        r   rU   N)r   r   rd   r   rs   add_group_email_grantrT   r   r   r   r   r     s    zBucket.add_group_email_grantc                 C   s   | j |d}|jS )zReturns the ACL entries applied to this bucket.

        :param dict headers: Additional headers to send with the request.
        :rtype: list containing :class:`~.gs.acl.Entry` objects.
        rU   )rs   entries)r   r1   r   r   r   r   list_grants  s    zBucket.list_grantsc                 C   s   d}| j d||d dS )zsDisable logging on this bucket.

        :param dict headers: Additional headers to send with the request.
        z0<?xml version="1.0" encoding="UTF-8"?><Logging/>loggingrU   N)r   )r   r1   xml_strr   r   r   disable_logging  s    zBucket.disable_loggingc                 C   sL   t |tr|j}d}|d|  }|r0|d|  }|d }| jd||d dS )a  Enable logging on a bucket.

        :type target_bucket: bucket or string
        :param target_bucket: The bucket to log to.

        :type target_prefix: string
        :param target_prefix: The prefix which should be prepended to the
            generated log files written to the target_bucket.

        :param dict headers: Additional headers to send with the request.
        z/<?xml version="1.0" encoding="UTF-8"?><Logging>z<LogBucket>%s</LogBucket>z%<LogObjectPrefix>%s</LogObjectPrefix>z
</Logging>r   rU   N)rO   r   r   r   )r   Ztarget_bucketZtarget_prefixr1   r   r   r   r   enable_logging  s    
zBucket.enable_loggingc                 C   st   | j jd| jd|d}| }tj| |jdkrJ| j j	|j|j
|tj }tj|d}|| ||fS )aM  Returns the current status of logging configuration on the bucket as
        unparsed XML.

        :param dict headers: Additional headers to send with the request.

        :rtype: 2-Tuple
        :returns: 2-tuple containing:

            1) A dictionary containing the parsed XML response from GCS. The
              overall structure is:

              * Logging

                * LogObjectPrefix: Prefix that is prepended to log objects.
                * LogBucket: Target bucket for log objects.

            2) Unparsed XML describing the bucket's logging configuration.
        rY   r   rZ   r[   Nr   r]   r   r^   botologdebugr.   rd   re   r/   ZjsonresponseElementrj   parser   r1   rf   rg   r8   rn   r   r   r   get_logging_config_with_xml  s     
  

z"Bucket.get_logging_config_with_xmlc                 C   s   |  |d S )a  Returns the current status of logging configuration on the bucket.

        :param dict headers: Additional headers to send with the request.

        :rtype: dict
        :returns: A dictionary containing the parsed XML response from GCS. The
            overall structure is:

            * Logging

              * LogObjectPrefix: Prefix that is prepended to log objects.
              * LogBucket: Target bucket for log objects.
        r   )r   rv   r   r   r   get_logging_config<  s    zBucket.get_logging_configc                 C   s   |r| j | }nd}|r$| j| }nd}| j||f }| jjdt| jt|d|d}| }|jdkrldS | jj	
|j|j|dS )aQ  Configure this bucket to act as a website

        :type main_page_suffix: str
        :param main_page_suffix: Suffix that is appended to a request that is
            for a "directory" on the website endpoint (e.g. if the suffix is
            index.html and you make a request to samplebucket/images/ the data
            that is returned will be for the object with the key name
            images/index.html). The suffix must not be empty and must not
            include a slash character. This parameter is optional and the
            property is disabled if excluded.

        :type error_key: str
        :param error_key: The object key name to use when a 400 error occurs.
            This parameter is optional and the property is disabled if excluded.

        :param dict headers: Additional headers to send with the request.
        rB   rx   websiteConfigr   r[   TN)WebsiteMainPageFragmentWebsiteErrorFragmentWebsiteBodyr   r]   r   r   r^   r.   rd   re   r/   )r   Zmain_page_suffixZ	error_keyr1   Zmain_page_fragZ
error_fragrg   rf   r   r   r   configure_websiteL  s,       
  zBucket.configure_websitec                 C   s   |  |d S )a6  Returns the current status of website configuration on the bucket.

        :param dict headers: Additional headers to send with the request.

        :rtype: dict
        :returns: A dictionary containing the parsed XML response from GCS. The
            overall structure is:

            * WebsiteConfiguration

              * MainPageSuffix: suffix that is appended to request that
                is for a "directory" on the website endpoint.
              * NotFoundPage: name of an object to serve when site visitors
                encounter a 404.
        r   )"get_website_configuration_with_xmlrv   r   r   r   get_website_configurationt  s    z Bucket.get_website_configurationc                 C   st   | j jd| jd|d}| }tj| |jdkrJ| j j	|j|j
|tj }tj|d}|| ||fS )a  Returns the current status of website configuration on the bucket as
        unparsed XML.

        :param dict headers: Additional headers to send with the request.

        :rtype: 2-Tuple
        :returns: 2-tuple containing:

            1) A dictionary containing the parsed XML response from GCS. The
              overall structure is:

              * WebsiteConfiguration

                * MainPageSuffix: suffix that is appended to request that is for
                  a "directory" on the website endpoint.
                * NotFoundPage: name of an object to serve when site visitors
                  encounter a 404

            2) Unparsed XML describing the bucket's website configuration.
        rY   r   rZ   r[   Nr   r   r   r   r   r     s      
  

z)Bucket.get_website_configuration_with_xmlc                 C   s   | j |d dS )zRemove the website configuration from this bucket.

        :param dict headers: Additional headers to send with the request.
        rU   N)r   rv   r   r   r   delete_website_configuration  s    z#Bucket.delete_website_configurationc                 C   s   | j jd| jd|d}| }tj| |jdkrJ| j j	|j|j
|tj }tj|d| |d }d|ko|d dkS )	zdReturns the current status of versioning configuration on the bucket.

        :rtype: bool
        rY   
versioningrZ   r[   NZVersioningConfigurationZStatusEnabledr   )r   r1   rf   rg   Z	resp_jsonr   r   r   get_versioning_status  s     
  
zBucket.get_versioning_statusc                 C   s2   |dkr| j d }n
| j d }| jd||d dS )zConfigure versioning for this bucket.

        :param bool enabled: If set to True, enables versioning on this bucket.
            If set to False, disables versioning.

        :param dict headers: Additional headers to send with the request.
        Tr   Z	Suspendedr   rU   N)VersioningBodyr   )r   Zenabledr1   r   r   r   r   configure_versioning  s    
zBucket.configure_versioningc                 C   sr   | j jd| jt|d}| }tj| |jdkrXt	 }t
|| }tj|| |S | j j|j|j|dS )a  
        Returns the current lifecycle configuration on the bucket.

        :rtype: :class:`boto.gs.lifecycle.LifecycleConfig`
        :returns: A LifecycleConfig object that describes all current
            lifecycle rules in effect for the bucket.
        rY   rZ   r[   N)r   r]   r   LIFECYCLE_ARGr^   r   r   r   r.   r   r   rj   rk   rl   rm   rd   re   r/   )r   r1   rf   rg   lifecycle_configrn   r   r   r   get_lifecycle_config  s      
  zBucket.get_lifecycle_configc                 C   sX   |  }| jjdt| jt|t|d}| }|jdkr>dS | jj	|j|j
|dS )z
        Configure lifecycle for this bucket.

        :type lifecycle_config: :class:`boto.gs.lifecycle.LifecycleConfig`
        :param lifecycle_config: The lifecycle configuration you want
            to configure for this bucket.
        rx   r   r[   TN)rQ   r   r]   r   r   r   r^   r.   rd   re   r/   )r   r   r1   rk   rf   rg   r   r   r   configure_lifecycle  s        
  zBucket.configure_lifecyclec                 C   s   |  |d S )at  Returns the current status of billing configuration on the bucket.

        :param dict headers: Additional headers to send with the request.

        :rtype: dict
        :returns: A dictionary containing the parsed XML response from GCS. The
            overall structure is:

            * BillingConfiguration

              * RequesterPays: Enabled/Disabled.
        r   )"get_billing_configuration_with_xmlrv   r   r   r   get_billing_config  s    zBucket.get_billing_configc                 C   st   | j jd| jd|d}| }tj| |jdkrJ| j j	|j|j
|tj }tj|d}|| ||fS )a  Returns the current status of billing configuration on the bucket as
        unparsed XML.

        :param dict headers: Additional headers to send with the request.

        :rtype: 2-Tuple
        :returns: 2-tuple containing:

            1) A dictionary containing the parsed XML response from GCS. The
              overall structure is:

              * BillingConfiguration

                * RequesterPays: Enabled/Disabled.

            2) Unparsed XML describing the bucket's website configuration.
        rY   billingrZ   r[   Nr   r   r   r   r   r     s     
  

z)Bucket.get_billing_configuration_with_xmlc                 C   s2   |dkr| j d }n
| j d }| jd||d dS )a  Configure billing for this bucket.

        :param bool requester_pays: If set to True, enables requester pays on
            this bucket. If set to False, disables requester pays.

        :param dict headers: Additional headers to send with the request.
        Tr   ZDisabledr   rU   N)BillingBodyr   )r   Zrequester_paysr1   r   r   r   r   configure_billing(  s    
zBucket.configure_billingc                 C   sf   | j jd| jt|d}| }|jdkrLt }t|| }t	j
|| |S | j j|j|j|dS )zReturns a bucket's EncryptionConfig.

        :param dict headers: Additional headers to send with the request.
        :rtype: :class:`~.encryption_config.EncryptionConfig`
        rY   rZ   r[   N)r   r]   r   ENCRYPTION_CONFIG_ARGr^   r.   r   r   rj   rk   rl   rm   rd   re   r/   )r   r1   rf   rg   Zencryption_configrn   r   r   r   get_encryption_config6  s"       
  zBucket.get_encryption_configc                 C   s   |r| j | }nd}| j| S )a  Creates an XML document for setting a bucket's EncryptionConfig.

        This method is internal as it's only here for testing purposes. As
        managing Cloud KMS resources for testing is complex, we settle for
        testing that we're creating correctly-formed XML for setting a bucket's
        encryption configuration.

        :param str default_kms_key_name: A string containing a fully-qualified
            Cloud KMS key name.
        :rtype: str
        rB   )&EncryptionConfigDefaultKeyNameFragmentEncryptionConfigBody)r   default_kms_key_nameZdefault_kms_key_name_fragr   r   r    _construct_encryption_config_xmlI  s    z'Bucket._construct_encryption_config_xmlc                 C   sX   | j |d}| jjdt| jt|t|d}| }|jdkrT| jj	|j|j
|dS )zSets a bucket's EncryptionConfig XML document.

        :param str default_kms_key_name: A string containing a fully-qualified
            Cloud KMS key name.
        :param dict headers: Additional headers to send with the request.
        )r   rx   r   r[   N)r   r   r]   r   r   r   r^   r.   rd   re   r/   )r   r   r1   rg   rf   r   r   r   set_encryption_config_  s"       
  zBucket.set_encryption_config)NNNN)NNr:   FFNNN)rB   rB   rB   rB   N)NNNN)rB   NNNNN)N)rB   NNN)rB   NNN)N)F)rB   NNr   NNN)rB   NNNNN)N)N)N)N)N)N)FN)FN)FN)N)N)NN)N)N)NNN)N)N)N)N)N)N)N)N)N)FN)N)N)NN)<__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   GSKeyr   r!   r%   r9   rA   rG   rI   rK   rT   rX   ri   ro   rs   rt   rw   r|   rP   rR   rW   rV   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r   4   s       
-             B    
)    
       
-
  
  

 
%       
)       
)





   
%  
#  
)
	


"
  
(

#






!


r   ),rb   r,   Zxml.saxrk   r   r   Zboto.resultsetr   Zboto.exceptionr   r   Zboto.gs.aclr   r   r   r   Zboto.gs.bucketlistresultsetr
   Zboto.gs.corsr   Zboto.gs.encryptionconfigr   Zboto.gs.lifecycler   Zboto.gs.keyr   r   Zboto.s3.aclr   Zboto.s3.bucketr   ZS3BucketZ
boto.utilsr   Zboto.compatr   ru   rq   r   r   r   r   compiler_   r   r   r   r   <module>   s4   
