
    t                         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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ZddZ ej,                  d      d        Z	 	 ddZ	 	 ddZy)z1Provider-neutral tools for manipulating metadata.    )absolute_import)division)unicode_literalsN)errors)
posix_util)symlink_util)user_request_args_factory)resource_reference)yaml)function_result_cache)filesc                    d}|r|dj                  |      z  }	 t        j                  |       }t        |t              st        |t
              r|S 	 	 t        j                  |       S # t        j                  $ r1}|dj                  t        j                  |            z  }Y d}~Ud}~ww xY w# t        j                  $ r1}|dj                  t        j                  |            z  }Y d}~nd}~ww xY wt        j                  |      )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4s/   6A( B/ (B,;'B''B,/C3'C..C3)maxsizec                     t         j                  j                  t         j                  j                  |             }t	        j
                  |      }t        ||       S )a7  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,   8   sF     ww''(:(:9(EF##$67(	#H)	DD    c                     t        |t        j                        }|xr |}|xr |}t        |xs0 |xs, |xs( | j                  xs | j
                  xs | j                        S )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_fieldsr<   K   s     -@@- +<}+=	 /	/	/ 
	+	+/ 
	.	.	/
 
	.	.
 r-   c                 $   |j                   }|syt        |t        j                        r|nd}| r|rt	        j
                  d      t        ||j                  |j                  ||      sy|j                  xr |}|j                  xr |}i }|s|r<|r|}	n!t        j                  ||j                        }	t        j                  ||	       |r t        j                  ||j                         |j                  t         j"                  k(  r|S |j                  r|j%                  |j                         |S t'        | fi |}
|j(                  r|j(                  D ]
  }||
v s|
|=  |j*                  r|
j%                  |j*                         |
S )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.)r7   r8   )r6   )r4   r   r
   r/   r   Errorr<   r5   r6   r   "get_posix_attributes_from_resource1update_custom_metadata_dict_with_posix_attributesr   3update_custom_metadata_dict_with_symlink_attributes
is_symlinkr1   r	   CLEARupdater   r2   r3   )existing_custom_fieldsrequest_configr7   r8   r4   r9   r:   r;   posix_metadataposix_attributescustom_fieldskeys               r    get_updated_custom_fieldsrK   d   s   @ !..-	
 
');)N)N	O  
  ,,	 
 
###&&-
 *99Km(::L}.+$#FF
>+K+K @@(* DD00 ''+D+J+JJ  ''-<<=-@@- **44		# 5 **>>?	r-   )N)NN)__doc__
__future__r   r   r   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,   r<   rK    r-   r    <module>rU      sx    8 &  '  	 5 9 ; H K $ ; * 
5* 4(E )E, 8 	cr-   