
                             S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
Jr  SS	KJr  SS
KJr  SSKrSS jr\R$                  " SS9S 5       r  SS jr  SS jrg)z1Provider-neutral tools for manipulating metadata.    N)errors)
posix_util)symlink_util)user_request_args_factory)resource_reference)yaml)function_result_cache)filesc                 B   SnU(       a  USR                  U5      -  n [        R                  " U 5      n[        U[        5      (       d  [        U[
        5      (       a  U$   [        R                  " U 5      $ ! [        R                   a2  nUSR                  [        R                  " U5      5      -  n SnANXSnAff = f! [        R                   a2  nUSR                  [        R                  " U5      5      -  n SnAOSnAff = f[        R                  " U5      e)z2Converts JSON or YAML stream to an in-memory dict.zFound invalid JSON/YAMLz in {}z

YAML Error: {}Nz

JSON Error: {})formatr   load
isinstancedictlistYAMLParseErrorsix	text_typejsonloadsJSONDecodeErrorr   InvalidUrlError)stringsource_pathcurrent_error_stringparsedes        3lib/googlecloudsdk/api_lib/storage/metadata_util.pyread_yaml_json_from_stringr       s    2HOOK88JYYvF&$:fd#;#;m $<
J::f	 
		 J077a8HIIJ
 
		 J077a8HIIJ 	344s0   AA9 #C 9B?(B::B?D(DD)maxsizec                     [         R                  R                  [         R                  R                  U 5      5      n[        R
                  " U5      n[        X S9$ )a'  Converts JSON or YAML file to an in-memory dict.

Args:
  file_path (str): Path for the file to parse passed in by the user.

Returns:
  parsed (dict): Parsed value from the provided file_path.

Raises:
  InvalidUrlError: The provided file_path either failed to load or be parsed
    as a dict.
)r   )ospathrealpath
expanduserr
   ReadFileContentsr   )	file_pathexpanded_file_pathcontentss      r   cached_read_yaml_json_filer)   5   sD     ww''(:(:9(EF##$67(	#H	DD    c                    [        U[        R                  5      nU=(       a    UnU=(       a    Un[        U=(       dD    U=(       d;    U=(       d2    U R                  =(       d    U R
                  =(       d    U R                  5      $ )z8Checks for the storage provider's custom metadata field.)r   r   FileObjectResourceboolcustom_fields_to_setcustom_fields_to_removecustom_fields_to_update)resource_argspreserve_posixpreserve_symlinksattributes_resourceknown_posixfile_resourceshould_parse_file_posixshould_parse_symlinkss           r   has_updated_custom_fieldsr9   H   s     -@@- +<}+=	 /	/	/ 
	+	+/ 
	.	.	/
 
	.	.
 r*   c                    UR                   nU(       d  g[        U[        R                  5      (       a  UOSnU (       a  U(       a  [        R
                  " S5      e[        UUR                  UR                  UUS9(       d  gUR                  =(       a    UnUR                  =(       a    Un0 nU(       d  U(       a>  U(       a  Un	O[        R                  " XQR                  S9n	[        R                  " X5        U(       a   [        R                  " XR                  5        UR                  [         R"                  :X  a  U$ UR                  (       a  UR%                  UR                  5        U$ ['        U 40 UD6n
UR(                  (       a  UR(                   H  nX;   d  M
  X	 M     UR*                  (       a  U
R%                  UR*                  5        U
$ )a  Returns a dictionary containing new custom metadata for an object.

Assumes that the custom metadata setter, clear flag, and a group containing
the update and flags are in a mutually exclusive group, meaning values can be
provided for one of these three flags/groups. The preserve POSIX flag is not a
member of this group, meaning it can be set with any of these flags.

Args:
  existing_custom_fields (dict): Existing custom metadata provided by an API.
  request_config (request_config): May contain custom metadata fields that
    should be modified.
  attributes_resource (Resource|None): If present, used for parsing POSIX and
    symlink data from a resource for the --preserve-posix and/or
    --preserve_symlink flags. This value is ignored unless it is an instance
    of FileObjectResource.
  known_posix (PosixAttributes|None): Set as custom metadata on target,
    skipping re-parsing from system.

Returns:
  Optional[dict] that should be the value of the storage provider's custom
  metadata field. `None` means that existing metadata should remain unchanged.
  Empty dictionary means it should be cleared.

Raises:
  errors.Error: If incompatible existing_custom_fields were encountered.
NzWExisting custom fields should not exist when updating custom fields using local source.)r4   r5   )r3   )r1   r   r   r,   r   Errorr9   r2   r3   r   "get_posix_attributes_from_resource1update_custom_metadata_dict_with_posix_attributesr   3update_custom_metadata_dict_with_symlink_attributes
is_symlinkr.   r   CLEARupdater   r/   r0   )existing_custom_fieldsrequest_configr4   r5   r1   r6   r7   r8   posix_metadataposix_attributescustom_fieldskeys               r   get_updated_custom_fieldsrH   a   s   @ !..-	
 
');)N)N	O	O  
  ,,	 
 
###&&-
 *99Km(::L}.+$#FF
+K+K @@* DD00 ''+D+J+JJ  ''-<<=-@@- **44		 5 **>>?	r*   )N)NN)__doc__r   r!   "googlecloudsdk.command_lib.storager   r   r   r   ,googlecloudsdk.command_lib.storage.resourcesr   googlecloudsdk.corer   googlecloudsdk.core.cacher	   googlecloudsdk.core.utilr
   r   r   lrur)   r9   rH    r*   r   <module>rQ      sn    8  	 5 9 ; H K $ ; * 
5* 4(E )E, 8 	cr*   