
                         r    d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ	 ddZ
dd	Z	 dd
Zd Zd Zy)zKShared utility methods for manipulating metadata of requests and resources.    )absolute_import)print_function)division)unicode_literalsN)storage_v1_messagesc                     |rd| d<   y y )Ngzipzaccept-encoding )headers_dictcompressed_encodings     ,platform/gsutil/gslib/utils/metadata_util.pyAddAcceptEncodingGzipIfNeededr      s     '-L"#     c           	      8   | t         j                  j                  g       }| i } t        j                  |       D ][  \  }}|j
                  j                  t         j                  j                  j                  t        |      t        |                   ] |S )a  Creates a custom MetadataValue object.

  Inserts the key/value pairs in entries.

  Args:
    entries: (Dict[str, Any] or None) The dictionary containing key/value pairs
        to insert into metadata. Both the key and value must be able to be
        casted to a string type.
    custom_metadata (apitools_messages.Object.MetadataValue or None): A
        pre-existing custom metadata object to add to. If one is not provided,
        a new one will be constructed.

  Returns:
    An apitools_messages.Object.MetadataValue.
  )additionalPropertieskeyvalue)	apitools_messagesObjectMetadataValuesix	iteritemsr   appendAdditionalPropertystr)entriescustom_metadatar   r   s       r   CreateCustomMetadatar   ,   s      '..<< = !O_GMM'*jc5((//  ..AACE
 	B 	,- + 
r   c                     	 t        fd| j                  j                  D        d      }|d|fS d|fS # t        $ r d|fcY S w xY w)aE  Filters a specific element out of an object's custom metadata.

  Args:
    obj_metadata: (apitools_messages.Object) The metadata for an object.
    search_key: (str) The custom metadata key to search for.
    default_value: (Any) The default value to use for the key if it cannot be
        found.

  Returns:
    (Tuple(bool, Any)) A tuple indicating if the value could be found in
    metadata and a value corresponding to search_key (the value at the specified
    key in custom metadata, or the default value if the specified key does not
    exist in the custom metadata).
  c              3   T   K   | ]  }|j                   k(  r|j                   ! y wNr   ).0attr
search_keys     r   	<genexpr>z3GetValueFromObjectCustomMetadata.<locals>.<genexpr>Z   s)      -HdXX+ **Hs   %(NFT)nextmetadatar   AttributeError)obj_metadatar%   default_valuer   s    `  r    GetValueFromObjectCustomMetadatar,   H   sd    "  -*33HH-.24E }M!!;	  - s   .6 6 AAc                 J    | j                  d      xs | j                  d      S )zDReturns true if header (which must be lowercase) is a custom header.zx-goog-meta-zx-amz-meta-)
startswith)headers    r   IsCustomMetadataHeaderr0   d   s#    			>	*	Nf.?.?.NNr   c                 p    | j                   xr) | j                   j                         j                  d      S )zGReturns true if the apitools_messages.Object has gzip content-encoding.r	   )contentEncodinglowerendswith)r*   s    r   ObjectIsGzipEncodedr5   i   s4    

&
& @

&
&
,
,
.
7
7
?Ar   )F)NNr"   )__doc__
__future__r   r   r   r   r   "gslib.third_party.storage_apitoolsr   r   r   r   r,   r0   r5   r
   r   r   <module>r9      s=    R & %  ' 
 W-$< 48 8O
Ar   