
    &              	          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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	Zd
ZdZdZdez  ZdZdZi ddddddddddddddddddddddddddd dd!dd"dd#dddd$d%d%d%d%d&Z G d' d(ej0                        Zd) Zd* Zd+ Zd, Zd- Zd. Zd/ Z d0 Z!d1 Z"	 d:d2Z#d3 Z$d4 Z%d5 Z&	 d;d6Z'd7 Z(d8 Z)d9 Z*y)<zUtils for resource classes.    )absolute_import)division)unicode_literalsN)storage_url)resource_reference)resource_projector         z     zgoog-reserved-file-is-symlinkz1Skipping item {} with unsupported object type: {}RPOz				LabelsACL	Autoclassz			zLocation typeMetagenerationzTime createdzTime updatedzSatisfies PZSzStorage classzDefault ACLzPlacement locationsz		zRetention PolicyzLocation constraintzVersioning enabledzLogging configurationzWebsite configurationz 			)zRequester Pays enabledzDefault KMS keyzCORS configurationzLifecycle configurationzDefault Event-Based HoldzBucket Policy Only enabledzPublic access preventionc                       e Zd ZdZy)UnsupportedObjectTypeGLACIERN)__name__
__module____qualname__r        Alib/googlecloudsdk/command_lib/storage/resources/resource_util.pyr   r   I   s    'r   r   c                     t        | t        j                        rP| j                  j                  t        j
                  j                  k(  r| j                  dk(  rt        j                  S y)an  Returns unsupported type or None if object is supported for copies.

  Currently, S3 Glacier objects are the only unsupported object type.

  Args:
    resource (ObjectResource|FileObjectResource): Check if this resource is
      supported for copies.

  Returns:
    (UnsupportedObjectType|None) If resource is unsupported, the unsupported
      type, else None.
  r   N)

isinstancer   ObjectResourcer   schemeProviderPrefixS3storage_classr   r   )resources    r   get_unsupported_object_typer$   M   sR     -<<=



%
%)C)C)F)F
F

 
 I
- (((	r   c                 8    t        j                  | t              S )z.Return json.dumps with formatting options set.)indent)jsondumpsMETADATA_LINE_INDENT_LENGTHitems    r   configured_json_dumpsr,   c   s    	D!<	==r   c                     | yt        | t        j                        st        dt        |       z        | j	                  d      S )z2Returns a formatted string from a datetime object.Nz(Expected a datetime object, but got a %sz%Y-%m-%dT%H:%M:%S%z)r   datetime
ValueErrortypestrftimedatetime_objects    r   )get_formatted_string_from_datetime_objectr4   h   sI    	OX%6%6	7
2T/5JJ  
	!	!"7	88r   c                     t        | t              rt        |       S t        | t        j                        rt	        |       S t        | t        j
                        r| j                  d      S | S )z<Converts values encountered in metadata to be JSON-parsable.z%Y-%m-%d)r   	Exceptionstrr.   r4   dater1   values    r   convert_to_json_parsable_typer;   s   sS    y!u:x(()4U;;x}}%>>*%%	,r   c                    |r| j                   }njd| j                  j                  i}| j                         }|j                  D ]3  }||v r|j                  |      }nt        | |d      }t        |      ||<   5 t        j                  |      S )a9  Makes a resource better for returning from describe and list commands.

  Display = Removes complex nested objects and makes other string tweaks.

  Args:
    resource (resource_reference.Resource): Resource to format.
    display_titles_and_defaults (namedtuple): Contains names of fields for
      display.
    display_raw_keys (bool): Displays raw API responses if True, otherwise
      standardizes metadata keys. If True, `resource` must have a metadata
      attribute.

  Returns:
    Dictionary representing input resource with optimizations described above.
  r   N)
metadatar   
url_stringget_formatted_acl_fieldsgetgetattrr;   r   MakeSerializable)r#   display_titles_and_defaultsdisplay_raw_keysdisplay_dataformatted_acl_dictfieldr:   s          r   get_display_dict_for_resourcerI      s    $ $$L "8#7#7#B#BCL!335,44	$	$"&&u-%.9%@l5 5 
	,	,\	::r   c                     | j                         }|r-| |z
  j                  t        j                  j                        S | S )z/Converts datetime object to UTC and returns it.)tzinfo)	utcoffsetreplacer.   timezoneutc)r3   offsets     r   convert_datetime_object_to_utcrQ      s@     $$&&f$--X5F5F5J5J-KK	r   c                 <    | syt        |       j                  d      S )zEConverts datetime to UTC and returns formatted string representation.Nonez%Y-%m-%dT%H:%M:%SZ)rQ   r1   r2   s    r   get_formatted_timestamp_in_utcrT      s#    		'	8	A	A
 r   c                 l    t        t        j                  t        |       j	                                     S )zBConverts datetime to UTC and returns Unix seconds-since-epoch int.)intcalendartimegmrQ   	timetupler2   s    r   get_unix_timestamp_in_utcrZ      s,    	oo
(
9
C
C
E
 r   c                 z    t        j                  t        |      t              }dj	                  t        | |      S )a  Returns metadata section with potentially multiple lines of JSON.

  Args:
    key_string (str): Key to give section.
    value_to_convert_to_json (list|object): json_dump_method run on this.

  Returns:
    String with key followed by JSON version of value.
  prefixz{indent}{key}:
{json})r&   keyr'   )textwrapr&   r,   METADATA_LINE_INDENT_STRINGformat)
key_stringvalue_to_convert_to_jsonjson_strings      r    get_metadata_json_section_stringre      sA     45(*+ 
"	(	((j{ 
) 
L Lr   c                     t        dt        t        |       z
  t        z   |z
        }dj	                  ddt        |z   z  | ||      S )z+Returns metadata line with correct padding.   z({indent}{key}:{_:>{left_spacing}}{value} r   )_r&   r^   left_spacingr:   )maxLONGEST_METADATA_KEY_LENGTHlenr)   ra   )rb   value_stringextra_indentspaces_left_of_values       r   "get_padded_metadata_key_value_linerq      sa    
 Q!<s:!N!<"=?K"L N	3	:	:
/,>?
' 
; 
 r   c                 0    t        |      }t        | |      S )zBReturns _get_padded_metadata_value_line with formatted time value.)rT   rq   )rb   
value_timeformatted_times      r   get_padded_metadata_time_lineru      s    1*=.	+J	GGr   c                     t          |  dj                  t              }t        j                  t        |      d      }| | S )zEReturns object metadata JSON(multiple lines) section in gsutil style.:rh   r\   )GSUTIL_METADATA_LINE_INDENTljust!GSUTIL_LIMIT_BREAKING_LINE_LENGTHr_   r&   r,   )rb   rc   padded_key_stringrd   s       r   .get_gsutil_object_metadata_json_section_stringr|      sR    
 66zl!DKK' 45+
 {m	,,r   c                 j    t         |    }t        j                  t        |      d      }d|  d| d| S )zEReturns bucket metadata JSON(multiple lines) section in gsutil style.z	  r\   r   rw   
))_LEFT_TAB_FOR_GSUTIL_BUCKET_METADATA_KEYSr_   r&   r,   )rb   rc   rp   rd   s       r   .get_gsutil_bucket_metadata_json_section_stringr      sG     C:N45+
 j\/0;-	@@r   c                 P    t         |z   |  dj                  t              }| | S )zBReturns object metadata line with correct padding in gsutil style.rw   )rx   ry   rz   )rb   rn   r&   r{   s       r   0get_gsutil_padded_object_metadata_key_value_liner     s:    
 &
.0A>	E
+,  |n	--r   c                 (    t         |    }d|  d| | S )zBReturns bucket metadata line with correct padding in gsutil style.r   rw   )r   )rb   rn   rp   s      r   0get_gsutil_padded_bucket_metadata_key_value_liner     s&    B:Nj\/0	??r   c                 
    | dv S )z3There are falsy values we want to keep as metadata.)r   g        Fr   r9   s    r   $should_preserve_falsy_metadata_valuer     s     
/	!!r   c                 4    | st        |       ryt        |       S )zDReturns string showing if item exists. May return 'None', '[]', etc.Present)r   r7   r*   s    r   get_exists_stringr     s    	1$7t9r   )r   )rg   )+__doc__
__future__r   r   r   rW   r.   enumr'   r_   "googlecloudsdk.command_lib.storager   ,googlecloudsdk.command_lib.storage.resourcesr   googlecloudsdk.core.resourcer   rl   rz   r)   rx   r`   SYMLINK_METADATA_KEY!UNSUPPORTED_OBJECT_WARNING_FORMATr   Enumr   r$   r,   r4   r;   rI   rQ   rT   rZ   re   rq   ru   r|   r   r   r   r   r   r   r   r   <module>r      s   " &  '      : K ; ! $& ! % !$??  7  8 "-	:-j- 
:- 	-
 X- h- H- H- X- X- 8- 6- - 6- &-  V!-" V#-$ %!# $"& $1- )8DII ,>
9	";J	L& 56H- 
A &'.@"r   