
    U                     P   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
 dZ G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zy) z?Classes for cloud/file references yielded by storage iterators.    )absolute_import)division)unicode_literalsN)errors)resource_util_NOT_SUPPORTED_DO_NOT_DISPLAYc                   B    e Zd ZdZdZd Zd Zd Zd Zd Z	e
d        Zy	)
Resourcea  Base class for a reference to one fully expanded iterator result.

  This allows polymorphic iteration over wildcard-iterated URLs.  The
  reference contains a fully expanded URL string containing no wildcards and
  referring to exactly one entity (if a wildcard is contained, it is assumed
  this is part of the raw string and should never be treated as a wildcard).

  Each reference represents a Bucket, Object, or Prefix.  For filesystem URLs,
  Objects represent files and Prefixes represent directories.

  The metadata_object member contains the underlying object as it was retrieved.
  It is populated by the calling iterator, which may only request certain
  fields to reduce the number of server requests.

  For filesystem and prefix URLs, metadata_object is not populated.

  Attributes:
    TYPE_STRING (str): String representing the resource's content type.
    storage_url (StorageUrl): A StorageUrl object representing the resource.
  resourcec                     || _         y)zInitialize the Resource object.

    Args:
      storage_url_object (StorageUrl): A StorageUrl object representing the
          resource.
    N)storage_url)selfstorage_url_objects     Flib/googlecloudsdk/command_lib/storage/resources/resource_reference.py__init__zResource.__init__6   s     *D    c                     t        j                  t        j                  d| j                  j
                  fd| j                  fg            S )z&Formats resource for printing as JSON.urltype)r   configured_json_dumpscollectionsOrderedDictr   
url_stringTYPE_STRINGr   s    r   get_json_dumpzResource.get_json_dump?   sJ    ..D$$//0T%%&!
 	 r   c                 .    | j                   j                  S N)r   r   r   s    r   __repr__zResource.__repr__G   s     &&&r   c                 d    t        || j                        xr | j                  |j                  k(  S r   )
isinstance	__class__r   )r   others     r   __eq__zResource.__eq__M   s.    5$..) 	.E---r   c                     t        d      )Nz is_container must be overridden.NotImplementedErrorr   s    r   is_containerzResource.is_containerS   s    
@
AAr   c                      y)z+Returns whether this resource is a symlink.F r   s    r   
is_symlinkzResource.is_symlinkV   s     r   N)__name__
__module____qualname____doc__r   r   r   r   r$   r(   propertyr+   r*   r   r   r
   r
      s<    ( +*'B  r   r
   c                   *    e Zd ZdZdZed        Zd Zy)CloudResourcea5  For Resource classes with CloudUrl's.

  Attributes:
    TYPE_STRING (str): String representing the resource's content type.
    scheme (storage_url.ProviderPrefix): Prefix indicating what cloud provider
        hosts the bucket.
    storage_url (StorageUrl): A StorageUrl object representing the resource.
  cloud_resourcec                 .    | j                   j                  S r   )r   schemer   s    r   r5   zCloudResource.schemeg   s    """r   c                     i S )aU  Returns provider specific formatting for the acl fields.

    Provider specific resource classses can override this method to return
    provider specific formatting for acl fields. If not overriden, acl values
    are displayed as-is if present.

    Returns:
      Dictionary with acl fields as key and corresponding formatted values.
    r*   r   s    r   get_formatted_aclzCloudResource.get_formatted_aclk   s	     Ir   N)r,   r-   r.   r/   r   r0   r5   r7   r*   r   r   r2   r2   \   s%     !+# #
r   r2   c                        e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	 fd	Zed        Zed        Zed        Z	 fdZ
d Z xZS )
BucketResourceap
  Class representing a bucket.

  Warning: After being run through through output formatter utils (e.g. in
  `shim_format_util.py`), these fields may all be strings.

  Attributes:
    TYPE_STRING (str): String representing the resource's content type.
    storage_url (StorageUrl): A StorageUrl object representing the bucket.
    name (str): Name of bucket.
    scheme (storage_url.ProviderPrefix): Prefix indicating what cloud provider
      hosts the bucket.
    acl (dict|CloudApiError|None): ACLs dict or predefined-ACL string for the
      bucket. If the API call to fetch the data failed, this can be an error
      string.
    cors_config (dict|CloudApiError|None): CORS configuration for the bucket.
      If the API call to fetch the data failed, this can be an error string.
    creation_time (datetime|None): Bucket's creation time in UTC.
    default_event_based_hold (bool|None): Prevents objects in bucket from being
      deleted. Currently GCS-only but needed for generic copy logic.
    default_storage_class (str|None): Default storage class for objects in
      bucket.
    etag (str|None): HTTP version identifier.
    labels (dict|None): Labels for the bucket.
    lifecycle_config (dict|CloudApiError|None): Lifecycle configuration for
      bucket. If the API call to fetch the data failed, this can be an error
      string.
    location (str|None): Represents region bucket was created in.
      If the API call to fetch the data failed, this can be an error string.
    logging_config (dict|CloudApiError|None): Logging configuration for bucket.
      If the API call to fetch the data failed, this can be an error string.
    metadata (object|dict|None): Cloud-provider specific data type for holding
      bucket metadata.
    metageneration (int|None): The generation of the bucket's metadata.
    requester_pays (bool|CloudApiError|None): "Requester pays" status of bucket.
      If the API call to fetch the data failed, this can be an error string.
    retention_period (int|None): Default time to hold items in bucket before
      before deleting in seconds. Generated from retention_policy.
    retention_policy (dict|None): Info about object retention within bucket.
    retention_policy_is_locked (bool|None): True if a retention policy is
      locked.
    update_time (str|None): Bucket's update time.
    versioning_enabled (bool|CloudApiError|None): Whether past object versions
      are saved. If the API call to fetch the data failed, this can be an error
      string.
    website_config (dict|CloudApiError|None): Website configuration for bucket.
      If the API call to fetch the data failed, this can be an error string.
  cloud_bucketc                    t         t        |   |       || _        || _        || _        || _        || _        || _        || _	        |	| _
        |
| _        || _        || _        || _        || _        || _        || _        || _        || _        y6Initializes resource. Args are a subset of attributes.N)superr9   r   aclcors_configcreation_timedefault_event_based_holddefault_storage_classetaglabelslifecycle_configlocationlogging_configmetadatametagenerationrequester_paysretention_policyupdate_timeversioning_enabledwebsite_config)r   r   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rJ   rI   rK   rL   rM   rN   rO   r"   s                      r   r   zBucketResource.__init__   s    ( 
.$();<DH"D&D$<D!!6DDIDK,DDM(DDM(D(D,D"D0D(Dr   c                 .    | j                   j                  S r   r   bucket_namer   s    r   namezBucketResource.name       '''r   c                      y r   r*   r   s    r   retention_periodzBucketResource.retention_period        r   c                      y r   r*   r   s    r   retention_policy_is_lockedz)BucketResource.retention_policy_is_locked   rW   r   c                    t         t        |   |      xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j
                  |j
                  k(  xr | j                  |j                  k(  xr| | j                  |j                  k(  xr` | j                  |j                  k(  xrD | j                  |j                  k(  xr( | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xrj | j                   |j                   k(  xrO | j"                  |j"                  k(  xr4 | j$                  |j$                  k(  xr | j&                  |j&                  k(  S r   )r>   r9   r$   r?   r@   rA   rB   rC   rD   rG   rE   rF   rH   rI   rJ   rK   rL   rM   rN   rO   r   r#   r"   s     r   r$   zBucketResource.__eq__   s   .$.u5 8HH		!8&*&6&6%:K:K&K8%"5"558 ))U-K-KK8 &&%*E*EE	8
 II#8
 )-(G8 KK5<<'8 !!U%;%;;8 MMU^^+8 5#7#778 MMU^^+8 5#7#778 5#7#778 !!U%;%;;8  1 118 ##u'?'??8  5#7#77!9r   c                      yNTr*   r   s    r   r(   zBucketResource.is_container       r   )NNNNNNNNNNNNNNNNN)r,   r-   r.   r/   r   r   r0   rS   rV   rY   r$   r(   __classcell__r"   s   @r   r9   r9   x   s    .^ + !(,%) $""" $"&"%%)N ( (    9&r   r9   c                        e Zd ZdZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zed        Zed        Zed        Z	ed        Z
 fdZd	 Zd
 Z xZS )ObjectResourcea
  Class representing a cloud object confirmed to exist.

  Warning: After being run through through output formatter utils (e.g. in
  `shim_format_util.py`), these fields may all be strings.

  Attributes:
    TYPE_STRING (str): String representing the resource's type.
    storage_url (StorageUrl): A StorageUrl object representing the object.
    scheme (storage_url.ProviderPrefix): Prefix indicating what cloud provider
      hosts the object.
    bucket (str): Bucket that contains the object.
    name (str): Name of object.
    generation (str|None): Generation (or "version") of the underlying object.
    acl (dict|str|None): ACLs dict or predefined-ACL string for the objects. If
      the API call to fetch the data failed, this can be an error string.
    cache_control (str|None): Describes the object's cache settings.
    component_count (int|None): Number of components, if any.
    content_disposition (str|None): Whether the object should be displayed or
      downloaded.
    content_encoding (str|None): Encodings that have been applied to the object.
    content_language (str|None): Language used in the object's content.
    content_type (str|None): A MIME type describing the object's content.
    custom_time (str|None): A timestamp in RFC 3339 format specified by the user
      for an object. Currently, GCS-only, but not in provider-specific class
      because generic daisy chain logic uses the field.
    crc32c_hash (str|None): Base64-encoded digest of crc32c hash.
    creation_time (datetime|None): Time the object was created.
    custom_fields (dict|None): Custom key-value pairs set by users.
    decryption_key_hash_sha256 (str|None): Digest of a customer-supplied
      encryption key.
    encryption_algorithm (str|None): Encryption algorithm used for encrypting
      the object if CSEK is used.
    etag (str|None): HTTP version identifier.
    event_based_hold (bool|None): Event based hold information for the object.
      Currently, GCS-only, but left generic because can affect copy logic.
    kms_key (str|None): Resource identifier of a Google-managed encryption key.
    md5_hash (str|None): Base64-encoded digest of md5 hash.
    metadata (object|dict|None): Cloud-specific metadata type.
    metageneration (int|None): Generation object's metadata.
    noncurrent_time (datetime|None): Noncurrent time value for the object.
    retention_expiration (datetime|None): Retention expiration information.
    size (int|None): Size of object in bytes (equivalent to content_length).
    storage_class (str|None): Storage class of the bucket.
    temporary_hold (bool|None): Temporary hold information for the object.
    update_time (datetime|None): Time the object was updated.
  cloud_objectc                    t         t        |   |       || _        || _        || _        || _        || _        || _        || _	        |	| _
        |
| _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        yr<   )r>   rb   r   r?   cache_controlcomponent_countcontent_dispositioncontent_encodingcontent_languagecontent_typecrc32c_hashrA   custom_fieldscustom_timedecryption_key_hash_sha256encryption_algorithmrD   event_based_holdkms_keymd5_hashrJ   rI   noncurrent_timeretention_expirationsizestorage_classtemporary_holdrM   )r   r   r?   re   rf   rg   rh   ri   rj   rk   rA   rl   rm   rn   ro   rD   rp   rq   rr   rI   rJ   rs   rt   ru   rv   rw   rM   r"   s                              r   r   zObjectResource.__init__'  s    8 
.$();<DH&D*D2D,D,D$D"D&D&D"D&@D# 4DDI,DDLDM(DDM*D 4DDI&D(D"Dr   c                 .    | j                   j                  S r   rQ   r   s    r   bucketzObjectResource.bucket^  rT   r   c                 .    | j                   j                  S r   r   resource_namer   s    r   rS   zObjectResource.nameb      )))r   c                 .    | j                   j                  S r   )r   
generationr   s    r   r   zObjectResource.generationf  s    &&&r   c                     | j                   rt        j                  | j                   vry| j                   t        j                     j                         dk(  S )z)Returns whether this object is a symlink.Ftrue)rl   r   SYMLINK_METADATA_KEYlowerr   s    r   r+   zObjectResource.is_symlinkj  sK     --T5G5GG===>DDF&Pr   c                    t         t        |   |      xr | j                  |j                  k(  xr | j                  |j                  k(  xrx | j
                  |j
                  k(  xr\ | j                  |j                  k(  xr@ | j                  |j                  k(  xr$ | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr| | j                  |j                  k(  xr` | j                  |j                  k(  xrD | j                   |j                   k(  xr( | j"                  |j"                  k(  xr | j$                  |j$                  k(  xr | j&                  |j&                  k(  xr | j(                  |j(                  k(  xr | j*                  |j*                  k(  xr | j,                  |j,                  k(  xr | j.                  |j.                  k(  xrj | j0                  |j0                  k(  xrO | j2                  |j2                  k(  xr4 | j4                  |j4                  k(  xr | j6                  |j6                  k(  S r   )r>   rb   r$   r?   re   rf   rg   rh   ri   rj   rk   rA   rl   rm   rn   ro   rD   rp   rq   rr   rI   rJ   rs   rt   ru   rv   rw   rM   r[   s     r   r$   zObjectResource.__eq__v  s   nd*51 	.dhh%))6K 	.e111	. 5 55	. 	  E$=$==	. 	!7!77		.
 	!7!77	. 	U///	. 	E---	. 	e111	. 	e111	. 	E---	. 	''5+K+KK	. 	!!U%?%??	. 			UZZ	. 	!7!77	. 	%	. +/--5>>*I	.  	'!	." 	u333#	.$ 	 5 55%	.& 	!!U%?%??'	.( 			UZZ)	.* 	e111+	., 	u333-	.. 	E---1/r   c                      yNFr*   r   s    r   r(   zObjectResource.is_container      r   c                     t         r   r&   r   s    r   is_encryptedzObjectResource.is_encrypted  s    
r   )NNNNNNNNNNNNNNNNNNNNNNNNN)r,   r-   r.   r/   r   r   r0   ry   rS   r   r+   r$   r(   r   r_   r`   s   @r   rb   rb      s    -\ + !##' $ $ !!*.$( $"#$(!"55#n ( ( * * ' ' 	 	/6r   rb   c                   ,     e Zd ZdZdZ fdZd Z xZS )PrefixResourcezClass representing a  cloud object.

  Attributes:
    TYPE_STRING (str): String representing the resource's content type.
    storage_url (StorageUrl): A StorageUrl object representing the prefix.
    prefix (str): A string representing the prefix.
  prefixc                 :    t         t        |   |       || _        y)zInitialize the PrefixResource object.

    Args:
      storage_url_object (StorageUrl): A StorageUrl object representing the
          prefix.
      prefix (str): A string representing the prefix.
    N)r>   r   r   r   )r   r   r   r"   s      r   r   zPrefixResource.__init__  s     
.$();<DKr   c                      yr]   r*   r   s    r   r(   zPrefixResource.is_container  r^   r   )r,   r-   r.   r/   r   r   r(   r_   r`   s   @r   r   r     s     +	r   r   c                   j     e Zd ZdZdZ	 	 	 	 d fd	Zed        Zed        Zed        Z	 fdZ
 xZS )	ManagedFolderResourcez$Class representing a managed folder.managed_folderc                 z    t         t        |   ||j                         || _        || _        || _        || _        y r   )r>   r   r   r|   create_timerI   rJ   rM   r   r   r   rI   rJ   rM   r"   s         r   r   zManagedFolderResource.__init__  sB     

/.<< #DDM(D"Dr   c                 .    | j                   j                  S r   rQ   r   s    r   ry   zManagedFolderResource.bucket  rT   r   c                      yr   r*   r   s    r   r+   z ManagedFolderResource.is_symlink      r   c                 .    | j                   j                  S r   r{   r   s    r   rS   zManagedFolderResource.name  r}   r   c                 8   t         t        |   |      xr | j                  |j                  k(  xrj | j                  |j                  k(  xrO | j
                  |j
                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )r>   r   r$   r   r   rI   rJ   rM   r[   s     r   r$   zManagedFolderResource.__eq__  s    #T1%8 	2 1 11	2 1 11	2 MMU^^+	2 5#7#77		2
  1 11r   NNNNr,   r-   r.   r/   r   r   r0   ry   r+   rS   r$   r_   r`   s   @r   r   r     sd    , +
 #  ( (   * * r   r   c                   j     e Zd ZdZdZ	 	 	 	 d fd	Zed        Zed        Zed        Z	 fdZ
 xZS )	FolderResourcezClass representing a folder.folderc                 z    t         t        |   ||j                         || _        || _        || _        || _        y r   )r>   r   r   r|   r   rI   rJ   rM   r   s         r   r   zFolderResource.__init__  sA     
.$(.<< #DDM(D"Dr   c                 .    | j                   j                  S r   rQ   r   s    r   ry   zFolderResource.bucket  rT   r   c                      yr   r*   r   s    r   r+   zFolderResource.is_symlink  r   r   c                 .    | j                   j                  S r   r{   r   s    r   rS   zFolderResource.name  r}   r   c                 8   t         t        |   |      xr | j                  |j                  k(  xrj | j                  |j                  k(  xrO | j
                  |j
                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )r>   r   r$   r   r   rI   rJ   rM   r[   s     r   r$   zFolderResource.__eq__  s    nd*51 	2 1 11	2 1 11	2 MMU^^+	2 5#7#77		2
  1 11r   r   r   r`   s   @r   r   r     sd    $+
 #  ( (   * * r   r   c                   N     e Zd ZdZdZd fd	Zd Zed        Zed        Z	 xZ
S )FileObjectResourcea  Wrapper for a filesystem file.

  Attributes:
    TYPE_STRING (str): String representing the resource's content type.
    size (int|None): Size of local file in bytes or None if pipe or stream.
    storage_url (StorageUrl): A StorageUrl object representing the resource.
    md5_hash (bytes): Base64-encoded digest of MD5 hash.
    is_symlink (bool|None): Whether this file is known to be a symlink.
  file_objectc                 H    t         t        |   |       || _        || _        yr<   )r>   r   r   rr   _is_symlink)r   r   rr   r+   r"   s       r   r   zFileObjectResource.__init__  s#    	
d,-?@DM!Dr   c                      yr   r*   r   s    r   r(   zFileObjectResource.is_container  r   r   c                     | j                   j                  ryt        j                  j	                  | j                   j
                        S )z,Returns file size or None if pipe or stream.N)r   	is_streamospathgetsizer|   r   s    r   ru   zFileObjectResource.size  s5     !!77??4++99::r   c                     | j                   8t        j                  j                  | j                  j
                        | _         | j                   S )z'Returns whether this file is a symlink.)r   r   r   islinkr   r|   r   s    r   r+   zFileObjectResource.is_symlink%  s=     (8(8(F(FGdr   )NN)r,   r-   r.   r/   r   r   r(   r0   ru   r+   r_   r`   s   @r   r   r   	  sA     +" ; ;  r   r   c                   D     e Zd ZdZd fd	Zed        Zed        Z xZS )FileSymlinkPlaceholderResourcezjA file to a symlink that should be preserved as a placeholder.

  Attributes:
    Refer to super class.
  c                 0    t         t        |   ||d       y)r=   TN)r>   r   r   )r   r   rr   r"   s      r   r   z'FileSymlinkPlaceholderResource.__init__4  s    	
($8Hdr   c                     t        t        j                  | j                  j                        j                  d            S )zEReturns the length of the symlink target to be used as a placeholder.zutf-8)lenr   readlinkr   r|   encoder   s    r   ru   z#FileSymlinkPlaceholderResource.size:  s/     r{{4++99:AA'JKKr   c                      yr]   r*   r   s    r   r+   z)FileSymlinkPlaceholderResource.is_symlink?  s    r   r   )	r,   r-   r.   r/   r   r0   ru   r+   r_   r`   s   @r   r   r   -  s7     L L  r   r   c                       e Zd ZdZdZd Zy)FileDirectoryResourcez$Wrapper for a File system directory.file_directoryc                      yr]   r*   r   s    r   r(   z"FileDirectoryResource.is_containerH  r^   r   Nr,   r-   r.   r/   r   r(   r*   r   r   r   r   D  s    , +r   r   c                       e Zd ZdZdZd Zy)UnknownResourcez0Represents a resource that may or may not exist.unknownc                 ,    t        j                  d      )Nz6Unknown whether or not UnknownResource is a container.)r   ValueCannotBeDeterminedErrorr   s    r   r(   zUnknownResource.is_containerP  s    

-
-@B Br   Nr   r*   r   r   r   r   L  s    8+Br   r   c                 v    t        | t              r| j                  j                         S | j	                         S )z@Returns if resource is a known or unverified container resource.)r!   r   r   	is_bucketr(   )r   s    r   !is_container_or_has_container_urlr   U  s0    /*))++				  r   )r/   
__future__r   r   r   r   r   "googlecloudsdk.command_lib.storager   ,googlecloudsdk.command_lib.storage.resourcesr   NOT_SUPPORTED_DO_NOT_DISPLAYobjectr
   r2   r9   rb   r   r   r   r   r   r   r   r   r*   r   r   <module>r      s    F &  '  	 5 F  ? :v :zH 8{] {|_] _D] 2)N )X)^ )X! !H%7 .H Bh B!r   