
    1                     V   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSK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Jr  SS jrSS jr  SS jrS rS rS rS rS rSS jr  " S S\RB                  5      r!S r"S r#S r$S r%S S jr&g)!z7Common utilities for the gcloud export/import commands.    )absolute_import)division)unicode_literalsN)message_types)messages)encoding)encoding_helper)
exceptions)log)yaml)yaml_validatorc                     SnUb  USR                  U5      -  nU R                  S[        R                  " U5      SS9  g)zAdd common export flags to the arg parser.

Args:
  parser: The argparse parser object.
  schema_path: The resource instance schema file path if there is one.
zPath to a YAML file where the configuration will be exported.
          Alternatively, you may omit this flag to write to standard output.NzL For a schema describing the export/import format, see:
          {}.
      z--destinationFhelprequiredformatadd_argumenttextwrapdedentparserschema_path	help_texts      -lib/googlecloudsdk/command_lib/export/util.pyAddExportFlagsr   '   sP    P) 

&
I 	??9%	      c                     SnUb  USR                  U5      -  nSU;   a  US-  nU R                  S[        R                  " U5      SS9  g)	zAdd common import flags to the arg parser.

Args:
  parser: The argparse parser object.
  schema_path: The resource instance schema file path if there is one.
zPath to a YAML file containing configuration export data.
          Alternatively, you may omit this flag to read from standard input.NzKFor a schema describing the export/import format, see:
          {}.
      $CLOUDSDKROOTz\

      Note: $CLOUDSDKROOT represents the Google Cloud CLI's installation directory.
      z--sourceFr   r   r   s      r   AddImportFlagsr    <   si    P) 

&
I +% 
 
i
 	??9%	  r   c                 @   [         R                  R                  [         R                  R                  [         R                  R                  [         R                  R                  [        R
                  " [        5      5      5      5      SU USR                  U5      5      nU(       au  UR                  [         R                  R                  S-   [         R                  R                  -   5      nUS:  a  U$ [         R                  R                  SSXES-   S 5      nU$ )	aL  Returns the schema installation path.

$CLOUDSDKROOT/lib/googlecloudsdk/schemas/
  {api}/{api_version}/{message_name}.yaml

Args:
  api_name: The api name.
  api_version: The API version string.
  message_name: The UpperCamelCase message name.
  for_help: Replaces the actual Cloud SDK installation root dir with
    $CLOUDSDKROOT.
schemasz{}.yamlgooglecloudsdkr   r   lib   N)
ospathjoindirnamer   Decode__file__r   rfindsep)api_nameapi_versionmessage_namefor_helpr'   rel_path_indexs         r   GetSchemaPathr3   W   s     
ggoobggoobggoo
//(
#'% & '|$
$ ZZ.> > LMNk77<<a5G5H0IJD	+r   c                 N    [         R                  " U5      R                  U 5        g)a?  Validates YAML against JSON schema.

Args:
  parsed_yaml: YAML to validate
  schema_path: JSON schema file path.

Raises:
  IOError: if schema not found in installed resources.
  files.Error: if schema file not found.
  ValidationError: if the template doesn't obey the schema.
  SchemaError: if the schema is invalid.
N)r   	ValidatorValidate)parsed_yamlr   s     r   ValidateYAMLr8   u   s     ;'00=r   c                 P    [        S [        R                  " SU 5       5       5      $ )a  Parses disallowed properties from an error message.

Args:
  error_message: The error message to parse.

Returns:
  A list of property names.

A sample error message might look like this:

Additional properties are not allowed ('id', 'createTime', 'updateTime',
'name' were unexpected)

c              3   B   #    U  H  oR                  S 5      v   M     g7f)'N)strip).0propertys     r   	<genexpr>#_ParseProperties.<locals>.<genexpr>   s      Q+OxnnT+Os   z'[^']*')listrefindall)error_messages    r   _ParsePropertiesrE      s-     
 Q+-::i+OQ 
Q Qr   c                 F    UnU H  nX4   nM	     U  H  nXS;   d  M
  X5	 M     g)zClear the given fields in a dict at a given path.

Args:
  fields: A list of fields to clear
  path_deque: A deque containing path segments
  py_dict: A nested dict from which to clear the fields
N )fields
path_dequepy_dicttmp_dictelemfields         r   _ClearFieldsrN      s/     (d~H e
/ r   c                 R    SnSnU R                   U:H  =(       a    X R                  ;   $ )zChecks if an error is due to properties that were not in the schema.

Args:
  error: A ValidationError

Returns:
  Whether the error was due to disallowed properties
additionalPropertiesz%Additional properties are not allowed)	validatormessage)errorprop_validatorprop_messages      r   _IsDisallowedPropertiesErrorrV      s)     *.8,	N	*	L|}}/LLr   c                 Z   Sn[         R                  " U5      R                  U 5       H  n[        U5      (       a,  [	        UR
                  5      n[        XCR                  U 5        O"[        R                  " UR
                  5        SnU(       d  Mj  [        R                  " S5        M     g)zFilter out fields from the yaml that are not in the schema.

Args:
  parsed_yaml: yaml to filter
  schema_path: Path to schema.
FTzDThe import command may need to be updated to handle the export data.N)
r   r5   IteraterV   rE   rR   rN   r'   r   warning)r7   r   has_warningsrS   fields_to_removes        r   _FilterYAMLr\      s     ,''4<<[Ie $E**)%--8#ZZ=	kk%-- l|	kk % &! Jr   c                 &   [         R                  " U5      nU(       a%  [        R                  " U5      R	                  U5         [
        R                  " X5      nU$ ! [         a*  n[        R                  " SR                  U5      5      eSnAff = f)aI  Reads YAML from a stream as a message.

Args:
  message_type: Type of message to load YAML into.
  stream: Input stream or buffer containing the YAML.
  schema_path: JSON schema file path. None for no YAML validation.

Raises:
  ParseError: if yaml could not be parsed as the given message type.

Returns:
  message_type object.
zCannot parse YAML: [{0}]N)r   loadr   r5   r6   api_encodingPyValueToMessage	Exceptionr
   
ParseErrorr   )message_typestreamr   r7   rR   es         r   Importrf      s|     		&!+[)22;?F++LFG 
. 
 F


 : A A! D
EEFs   A 
B&%BBc                   L   ^  \ rS rSrSrSr\S 5       rU 4S jrU 4S jr	Sr
U =r$ )_ProtoJsonApiTools   z&JSON encoder used by apitools clients.Nc                 L    U R                   c  U " 5       U l         U R                   $ N)	_INSTANCE)clss    r   Get_ProtoJsonApiTools.Get   s    
}}ecm==r   c                   >^  [        U[        R                  5      (       a  SSR                  U 4S jU 5       5      -  $ [	        U5      [
        R                  ;   a+  [
        R                  [	        U5         R                  U5      $ [        U5      n[        [
        R                  T ]/  U5      n[        X5      n[        R                  " [        R                  " U5      SS9$ )Nz[%s]z, c              3   F   >#    U  H  nTR                  U5      v   M     g 7frk   )encode_message)r=   xselfs     r   r?   4_ProtoJsonApiTools.encode_message.<locals>.<genexpr>   s      IA!4!4Q!7!7s   !T)	sort_keys)
isinstancer   	FieldListr(   typer	   _CUSTOM_MESSAGE_CODECSencoder_EncodeUnknownFieldssuperrh   rr   _EncodeCustomFieldNamesjsondumpsloads)rt   rR   result	__class__s   `  r   rr   !_ProtoJsonApiTools.encode_message   s    '8--..tyy I IIJJ G}>>>33DMBJJ
  #7+G?55((/0 $W5F::djj(D99r   c                   > [        US5       H,  nU" X5      nUR                  nUR                  (       d  M*  Us  $    [        U[        R
                  5      (       a  UR                  (       aM  U Vs/ s H?  n[        R                  " UR                  UR                  S9=(       d    UR                  PMA     nnO)[        R                  " UR                  UR                  S9nU(       a  U$ [        U[        R                  5      (       aD  [        U[        R                  5      (       d%  [        R                  " U R!                  U5      5      n["        [        R$                  U ]O  X5      $ s  snf )Nr{   )python_name)_GetFieldCodecsvaluecompleterw   r   	EnumFieldrepeatedr	   GetCustomJsonEnumMappingry   nameMessageFieldr   DateTimeFieldr   r   rr   r}   rh   encode_field)rt   rM   r   r{   r   re   remapped_valuer   s          r   r   _ProtoJsonApiTools.encode_field	  s*   "5)4u$flle		 5
 %++,,	 DI
CHa 44

0 :3466:CH 	 

 )AAJJEJJ0	5(//00um99::jj,,U34e33$$)12
s   2AE-rG   )__name__
__module____qualname____firstlineno____doc__rl   classmethodrn   rr   r   __static_attributes____classcell__)r   s   @r   rh   rh      s,    .) 
:2 2r   rh   c                 \    S nS n[         R                  " XS9U [        R                  '   U $ )z(Registers custom field codec for int64s.c                 0    [         R                  " USS9nU$ NT)r   r   r_   CodecResult)unused_fieldr   	int_values      r   _EncodeInt64Field8RegisterCustomFieldTypeCodecs.<locals>._EncodeInt64Field#  s    ((utDIr   c                 ,    [         R                  " USS9$ r   r   )r   r   s     r   _DecodeInt64Field8RegisterCustomFieldTypeCodecs.<locals>._DecodeInt64Field'  s    ##%$??r   )r{   decoder)r	   _Codecr   IntegerField)field_type_codecsr   r   s      r   RegisterCustomFieldTypeCodecsr   !  s4    @ .=-C-C.<H))*	r   c                 ^   [         R                  " [        R                  5      n[	        [         R                  " [        R
                  5      5      n[        UR                  U 5      US 5      [        UR                  [        U 5      5      US 5      /nU Vs/ s H	  oUc  M  UPM     sn$ s  snf rk   )	copydeepcopyr	   _CUSTOM_FIELD_CODECSr   _FIELD_TYPE_CODECSgetattrgetry   )rM   attrcustom_field_codecsr   r   rs   s         r   r   r   0  s    o&J&JK3
mmO6679 !%%e,dD9##DK0$=& 	-V!V	--	-s   B*!B*c                 F   [         R                  R                  [        U 5      5      nUc  U $ [         R                  " U 5      nU R                  U5      n[        U[        R                  5      (       d  [        R                  " SU-  5      eUR                  nUR                  S5      nUR                  n[        X5      n[        R                  5       nU H:  n	UR!                  XYR"                  5      n
UR%                  U	R&                  X5        M<     [)        X!/ 5        U$ )z6Remap unknown fields in message out of message.source.zInvalid pairs field %sr   )r	   _UNRECOGNIZED_FIELD_MAPPINGSr   ry   !_CopyProtoMessageVanillaProtoJsonfield_by_namerw   r   r   r
   InvalidUserInputErrorrc   variantr   rh   rn   r   r   set_unrecognized_fieldkeysetattr)rR   sourcer   pairs_field
pairs_typevalue_fieldvalue_variantpairscodecpairencoded_values              r   r|   r|   <  s    77;;DMJ&^N <<WE&%%f-+	K!6!6	7	7

*
*+C+6,7 8 8''*((1+%%-
'
"%

 
 
"%d&&{JJ?M
!!$((MI  
&"	-r   c                 6   [        [        R                  R                  [	        U 5      0 5      R                  5       5      nU(       aQ  [        R                  " U5      nU H  u  pEXA;   d  M  UR                  U5      X5'   M!     [        R                  " U5      nU$ rk   )
rA   r	   _JSON_FIELD_MAPPINGSr   ry   itemsr   r   popr   )rR   r   field_remappingsdecoded_valuer   	json_names         r   r~   r~   T  s~    **..tG}bAGGIKJJ}-M"2		%#0#4#4[#A  #3 JJ}-M	r   c                     [         R                  5       R                  U 5      n[        R                  " [
        R                  " X0S5      5      nU(       a  [        XB5        [        R                  " XAS9$ )a  Writes a message as YAML to a stream.

Args:
  message: Message to write.
  stream: Output stream, None for writing to a string and returning it.
  schema_path: JSON schema file path. If None then all message fields are
    written, otherwise only fields in the schema are written.

Returns:
  Returns the return value of yaml.dump(). If stream is None then the return
  value is the YAML data as a string.
N)rd   )
rh   rn   rr   r   r   r	   _IncludeFieldsr\   r   dump)rR   rd   r   r   message_dicts        r   Exportr   `  sU     !!#227;&$$Vd;=,*	<	//r   rk   )v1NF)NN)'r   
__future__r   r   r   r   r   r&   rB   r   apitools.base.protorpcliter   r   apitools.base.pyr   r_   r	   googlecloudsdk.api_lib.dataprocr
   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr   r    r3   r8   rE   rN   rV   r\   rf   rh   r   r   r|   r~   r   rG   r   r   <module>r      s     > &  '   	 	  4 / 5 , 6 # $ . -*6 <@ <> Q& M&8<.2;; .2b	.0	0r   