
    R1                     2   S 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\R:                  5      rS rS rS r S r!SS jr"g)z7Common utilities for the gcloud export/import commands.    N)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.NL 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.Nr   $CLOUDSDKROOTz\

      Note: $CLOUDSDKROOT represents the Google Cloud CLI's installation directory.
      z--sourceFr   r   r   s      r   AddImportFlagsr   9   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   GetSchemaPathr1   T   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   ValidateYAMLr6   r   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   _ParsePropertiesrC      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   _ClearFieldsrL      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   _IsDisallowedPropertiesErrorrT      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
   r3   IteraterT   rC   rP   rL   r%   r   warning)r5   r   has_warningsrQ   fields_to_removes        r   _FilterYAMLrZ      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
   r3   r4   api_encodingPyValueToMessage	Exceptionr   
ParseErrorr   )message_typestreamr   r5   rP   es         r   Importrd      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ri   )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superrf   rp   _EncodeCustomFieldNamesjsondumpsloads)rr   rP   result	__class__s   `  r   rp   !_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 )Nry   )python_name)_GetFieldCodecsvaluecompleteru   r   	EnumFieldrepeatedr   GetCustomJsonEnumMappingrw   nameMessageFieldr   DateTimeFieldr}   r   rp   r{   rf   encode_field)rr   rK   r   ry   r   rc   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-rE   )__name__
__module____qualname____firstlineno____doc__rj   classmethodrl   rp   r   __static_attributes____classcell__)r   s   @r   rf   rf      s,    .) 
:2 2r   rf   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   )ry   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 ri   )	copydeepcopyr   _CUSTOM_FIELD_CODECSr   _FIELD_TYPE_CODECSgetattrgetrw   )rK   attrcustom_field_codecsr   r   rq   s         r   r   r   -  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   rw   !_CopyProtoMessageVanillaProtoJsonfield_by_nameru   r   r   r   InvalidUserInputErrorra   variantr   rf   rl   r   r   set_unrecognized_fieldkeysetattr)rP   sourcer   pairs_field
pairs_typevalue_fieldvalue_variantpairscodecpairencoded_values              r   rz   rz   9  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$ ri   )
r?   r   _JSON_FIELD_MAPPINGSr   rw   itemsr}   r   popr~   )rP   r   field_remappingsdecoded_valuer   	json_names         r   r|   r|   Q  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)rb   )
rf   rl   rp   r}   r   r   _IncludeFieldsrZ   r	   dump)rP   rb   r   r   message_dicts        r   Exportr   ]  sU     !!#227;&$$Vd;=,*	<	//r   ri   )v1NF)NN)#r   r   r}   r$   r@   r   apitools.base.protorpcliter   r   apitools.base.pyr   r]   r   googlecloudsdk.api_lib.dataprocr   googlecloudsdk.corer   r	   r
   googlecloudsdk.core.utilr   r   r1   r6   rC   rL   rT   rZ   rd   rf   r   r   rz   r|   r   rE   r   r   <module>r      s     >   	 	  4 / 5 , 6 # $ . -*6 <@ <> Q& M&8<.2;; .2b	.0	0r   