
    :                     
   S r SSKJr  SSKJr  SSKJ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KJr  SSKrSrSrSrSSSSSSS.r " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      rS rS  rS! r S" r!S# r"S$ r#S% r$S& r%S' r&S( r'S) r(S* r)S+ r*S, r+S- r,S. r-S/ r.S0 r/S1 r0S2 r1S3 r2S4 r3S5 r4S6 r5S7 r6S8 r7S9 r8S: r9S; r:S< r;S= r<g)>z.Argument processors for DLP surface arguments.    )absolute_import)division)unicode_literalsN)apis)arg_parsers)base)	arg_utils)
exceptions)log)
properties)	resources)files)timesdlpv2zColors should be specified as a string of `r,g,b` float values in the interval
[0,1] representing the amount of red, green, and blue in the color,
respectively. For example, `black = 0,0,0`, `red = 1.0,0,0`,
`white = 1.0,1.0,1.0`, and so on.
IMAGE	IMAGE_PNG
IMAGE_JPEG	IMAGE_SVG	IMAGE_BMP)n_az.pngz.jpegz.jpgz.svgz.bmpc                       \ rS rSrSrSrg)ImageFileError7   z:Error if an image file is improperly formatted or missing. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       +lib/googlecloudsdk/command_lib/dlp/hooks.pyr   r   7   s    Br#   r   c                       \ rS rSrSrSrg)RedactColorError;   z;Error if a redact color is improperly formatted or missing.r   Nr   r   r#   r$   r&   r&   ;   s    Cr#   r&   c                       \ rS rSrSrSrg)BigQueryTableNameError?   z7Error if a BigQuery table name is improperly formatted.r   Nr   r   r#   r$   r)   r)   ?   s    ?r#   r)   c                 V    [         R                  " [        [        5      n[	        X5      $ )z3Get API message object for given message type name.)r   GetMessagesModule_DLP_API_DLP_API_VERSIONgetattr)msg_type_namemsgs     r$   _GetMessageClassr2   D   s     x)9:#		$$r#   c                 6    U (       d  gU =(       a	    U [         ;   $ )z#Validate image file name extension.T)VALID_IMAGE_EXTENSIONS)	extensions    r$   _ValidateExtensionr6   J   s    			<	%;;<r#   c                 J    [        U 5      nUS:  d  US:  a  [        S5      eU$ )z;Convert color value(color) to a float or raise value error.g      ?g        zInvalid Color.)float
ValueError)colorjs     r$   _ConvertColorValuer<   R   s)    El!WC
%
&&	
(r#   c                    U R                  S5      n[        U5      S:X  a   U Vs/ s H  n[        U5      PM     sn$ [	        SR                  U [        5      5      es  snf ! [         a     [	        SR                  U [        5      5      ef = f)zFValidates that values has proper format and returns parsed components.,   zInvalid Color Value(s) [{}]. {}z0You must specify exactly 3 color values [{}]. {})splitlenr<   r9   r&   format_COLOR_SPEC_ERROR_SUFFIX)valuevaluesxs      r$   _ValidateAndParseColorsrG   [   s    ;;s&[AK-34V #V44
    &u.F GI I 5 K ""(&0H"IK KKs   A" AA" A" "*Bc                 z    U R                  S5      n[        U5      S:w  a  [        SR                  U 5      5      eU$ )CValidate BigQuery table name format and returned parsed components..r?   zInvalid BigQuery table name [{}]. BigQuery tables are uniquely identified by their project_id, dataset_id, and table_id in the format `<project_id>.<dataset_id>.<table_id>`.r@   rA   r)   rB   
table_name
name_partss     r$   _ValidateAndParseInputTableNamerO   j   sD    $*_
 	99?
9KM M
 
r#   c                     U R                  S5      n[        U5      S:w  a)  [        U5      S:w  a  [        SR                  U 5      5      eU$ )rI   rJ   r?      zInvalid BigQuery output table name [{}]. BigQuery tables are uniquely identified by their project_id, dataset_id, and or table_id in the format `<project_id>.<dataset_id>.<table_id>` or `<project_id>.<dataset_id>.rK   rL   s     r$    _ValidateAndParseOutputTableNamerR   v   sQ     $*_c*o2
 	& '-fZ&8	: : 
r#   c                 $    [        S5      nU" U S9$ )z=Return GooglePrivacyDlpV2InfoType message for a parsed value.GooglePrivacyDlpV2InfoTypenamer2   )rD   infotypes     r$   InfoTyperY      s    :;(	u	r#   c                 $    [        S5      nU" U S9$ )z-Create a GooglePrivacyDlpV2FieldId for value.GooglePrivacyDlpV2FieldIdrU   rW   )rD   field_ids     r$   PrivacyFieldr]      s    9:(	u	r#   c                 h    [        U 5      u  pn[        S5      n[        S5      nU" X!US9nU" US9nU$ )a  Convert BigQuery table name into GooglePrivacyDlpV2BigQueryOptions.

Creates BigQuery input options for a job trigger.

Args:
  table_name: str, BigQuery table name to create input options from in the
    form `<project_id>.<dataset_id>.<table_id>`.

Returns:
  GooglePrivacyDlpV2BigQueryOptions, input options for job trigger.

Raises:
  BigQueryTableNameError if table_name is improperly formatted.
!GooglePrivacyDlpV2BigQueryOptionsGooglePrivacyDlpV2BigQueryTable	datasetId	projectIdtableIdtableReference)rO   r2   )rM   
project_iddata_set_idtable_idbig_query_optionsbig_query_tabletableoptionss           r$   BigQueryInputOptionsrn      sM     'F'#*8&'JK$%FG/
8E%U3'	.r#   c                 B    [        S5      n[        S5      nU" U" U S9S9$ )z-Return CloudStorageOptions for given GCS url.%GooglePrivacyDlpV2CloudStorageOptionsGooglePrivacyDlpV2FileSet)url)fileSetrW   )rr   cloud_storage_optionsfile_sets      r$   GcsInputOptionsrv      s+    *-/9:(	xC'8	99r#   c                 ,   [        S5      n[        S5      n[        S5      n[        R                  R                  R                  R                  SS9nU R                  S5      n[        U5      S:X  a  Uu  pgU" US9nU" XdS	9n	OU" U S9nU" US
9n	U" XS9$ )a_  Convert Datastore arg value into GooglePrivacyDlpV2DatastoreOptions.

Creates Datastore input options for a job trigger from datastore table name.

Args:
  table_name: str, Datastore table name to create options from in the form
  `namespace:example-kind` or simply `example-kind`.

Returns:
  GooglePrivacyDlpV2Action, output action for job trigger.
"GooglePrivacyDlpV2DatastoreOptions GooglePrivacyDlpV2KindExpressionGooglePrivacyDlpV2PartitionIdTrequired:rQ   rU   )namespaceIdrc   )rc   )kindpartitionId)r2   r   VALUEScoreprojectGetr@   rA   )
rM   data_store_optionsr   partition_idr   
split_name	namespacerl   kind_exp	partitions
             r$   DatastoreInputOptionsr      s     ((LM	<	=$!"AB,""**...='$*_!IHFI$Hw/I		AAr#   c                 B    [        S5      n[        S5      nU" U" U S9S9$ )z1Return PubSub DlpV2Action for given PubSub topic.GooglePrivacyDlpV2Action!GooglePrivacyDlpV2PublishToPubSub)topic)pubSubrW   )r   
action_msgpubsub_actions      r$   PubSubTopicActionr      s(     :;*"#FG-	=u5	66r#   c                     [        U 5      nUS   nUS   nSn[        U5      S:X  a  US   n[        S5      n[        S5      n[        S5      n[        S	5      nU" X2US
9n	U" U" U" U	S9S9S9$ )a  Convert BigQuery formatted table name into GooglePrivacyDlpV2Action.

Creates a BigQuery output action for a job trigger.

Args:
  table_name: str, BigQuery table name to create action from in the form
    `<project_id>.<dataset_id>.<table_id>` or `<project_id>.<dataset_id>`.

Returns:
  GooglePrivacyDlpV2Action, output action for job trigger.

Raises:
  BigQueryTableNameError if table_name is improperly formatted.
r       r?   rQ   r   GooglePrivacyDlpV2SaveFindings%GooglePrivacyDlpV2OutputStorageConfigr`   ra   )rl   )outputConfig)saveFindings)rR   rA   r2   )
rM   rN   rg   rh   ri   r   save_findings_configoutput_configrk   rl   s
             r$   BigQueryTableActionr      s     0
;*!}*1+(_!}H :;*)*JK"#JK-$%FG/
8E% 
'$513
4 4r#   c                 H    [         R                  " U [         R                  S9$ )N)tzinfo)r   FormatDateTimeUTCrD   s    r$   DlpTimeStampr      s    			eEII	66r#   c                     A UR                   =(       d1    [        R                  R                  R                   R	                  SS9n[
        R                  R                  USS9nUR                  5       Ul	        U$ )z%Set parent value for a DlpXXXRequest.Tr{   zdlp.projects)
collection)
r   r   r   r   r   r   REGISTRYParseRelativeNameparent)refargsrequestr   project_refs        r$   SetRequestParentr      sc    	<<L:,,1199==t=L&""((N(K+++-'.	.r#   c                 8    A A[        S5      nU" 5       Ul        U$ )z6Set cancel message on DlpProjectsDlpJobsCancelRequest.%GooglePrivacyDlpV2CancelDlpJobRequest)r2   %googlePrivacyDlpV2CancelDlpJobRequest)r   r   r   cancel_requests       r$   SetCancelRequestHookr     s#    	
#$KL.2@2B'/	.r#   c                     A UR                   R                  R                  R                  R                  nUR
                  (       a"  U(       a  UR
                  UR                  l        U$ )z1Update partitionId.projectId on DatastoreOptions.))googlePrivacyDlpV2CreateJobTriggerRequest
jobTrigger
inspectJobstorageConfigdatastoreOptionsr   r   rc   )r   r   r   r   s       r$   UpdateDataStoreOptionsr     sN    	77BBMM}%%  
\\(/3||"",	.r#   c                     A UR                   R                  R                  R                  nU(       aA  UR                  (       a0  [        S5      nUR                   Vs/ s H  oT" US9PM
     snUl        U$ s  snf )z<Update bigQueryOptions.identifyingFields with parsed fields.r[   rU   )%googlePrivacyDlpV2CreateDlpJobRequestr   r   bigQueryOptionsidentifying_fieldsr2   identifyingFields)r   r   r   rj   r\   fields         r$   UpdateIdentifyingFieldsr     sq    	33>>LL  422 ;<H*.*A*A+*Ae*A+' 
.+s   A6c                    A UR                   (       al  / nUR                    HD  nUR                  S5      (       a  UR                  S5      S-   nOUS-  nUR                  U5        MF     SR	                  U5      Ul        U$ )z<Set orderBy attribute on message from common --sort-by flag.~z descz ascr>   )sort_by
startswithlstripappendjoinorderBy)r   r   r   order_by_fieldsr   s        r$   SetOrderByFromSortByr   )  sq    		\\O			#		S!G+U#  hh/GO	.r#   c                     U R                   $ )zGExtracts and returns BigQueryTable from parsed BigQueryOptions message.re   r   s    r$   ExtractBqTableFromInputConfigr   :  s    			r#   c                 B    [        S5      n[        S5      nU" U" U S9S9$ )N$GooglePrivacyDlpV2ReplaceValueConfigGooglePrivacyDlpV2Value)stringValue)newValuerW   )rD   replace_configvalue_holders      r$   GetReplaceTextTransformr   ?  s(    #$JK.!";<,	%!@	AAr#   c                 (    A [        S5      nU" 5       $ )N+GooglePrivacyDlpV2ReplaceWithInfoTypeConfigrW   )rD   infotype_configs     r$   GetInfoTypeTransformr   E  s    $35/		r#   c                 (    A [        S5      nU" 5       $ )NGooglePrivacyDlpV2RedactConfigrW   )rD   redact_configs     r$   GetRedactTransformr   L  s    "#CD-	r#   c                    [         R                  R                  U 5      S   R                  5       nU=(       d    Sn[	        S5      n[         R                  R                  U 5      (       aU  [        U5      (       aE  [        R                  " [        U   UR                  5      nU" [        R                  " U 5      US9nU$ [        SR                  U 5      5      e)a  Builds a GooglePrivacyDlpV2ByteContentItem message from a path.

Will attempt to set message.type from file extension (if present).

Args:
  path: the path arg given to the command.

Raises:
  ImageFileError: if the image path does not exist and does not have a valid
  extension.

Returns:
  GooglePrivacyDlpV2ByteContentItem: an message containing image data for
  the API on the image to analyze.
r   !GooglePrivacyDlpV2ByteContentItem)datatypezThe image path [{}] does not exist or has an invalid extension. Must be one of [jpg, jpeg, png, bmp or svg]. Please double-check your input and try again.)ospathsplitextlowerr2   isfiler6   r	   ChoiceToEnumr4   TypeValueValuesEnumr   ReadBinaryFileContentsr   rB   )r   r5   
image_itemenum_valimages        r$   GetImageFromFiler   R  s      ggt$R(..0) 5) CD*WW^^D0;;%%&<Y&G&0&D&DFHE88>XNE 
,	 	88>tF Fr#   c                 B    [        S5      n[        U 5      u  p#nU" X$US9$ )a  Convert color_string into GooglePrivacyDlpV2Color.

Creates a GooglePrivacyDlpV2Color message from input string to use for image
redaction.

Args:
  color_string: str, string representing red, green and blue color saturation
    percentages as float values between 0.0 and 1.0. For example, `black =
    0,0,0`, `red = 1.0,0,0`, `white = 1.0,1.0,1.0` etc.

Returns:
  GooglePrivacyDlpV2Color, color message.

Raises:
  RedactColorError if color_string is improperly formatted.
GooglePrivacyDlpV2Color)redbluegreen)r2   rG   )color_string	color_msgr   r   r   s        r$   GetRedactColorFromStringr   q  s+    " 89),\:#d	sU	33r#   c                 L    SR                  [        R                  " U 5      5      $ )z;Return API required format for duration specified by value.z{}s)rB   six	text_typer   s    r$   GetJobScheduleDurationStringr     s    	cmmE*	++r#   c                  \    Sn [         R                  " SS[        R                  " 5       U S9/$ )z.Capture identifying fields for BigQuery table.zComma separated list of references to field names uniquely identifying rows within the BigQuery table. Nested fields in the format `person.birthdate.year` are allowed.z--identifying-fieldsIDENTIFYING_FIELDS)metavarr   help)r   Argumentr   ArgList)	help_texts    r$   GetIdentifyingFieldsArgr    s7    A) mm
 &""$	
 r#   c                 .   UR                   (       d  g[        R                  " UR                   5       nUR                  U 5        SSS5        [        R
                  R                  SR                  UR                   5      5        g! , (       d  f       NG= f)z@Helper function for writing redacted contents to an output file.Nz+The redacted contents can be viewed in [{}])output_filer   BinaryFileWriterwriter   statusPrintrB   )rD   parsed_argsoutfiles      r$   *_PossiblyWriteRedactedResponseToOutputFiler    sg    		 	 
k556'MM% 7**@GG   76s   B
Bc                 F    [        U R                  R                  U5        U $ )zHWrite the contents of the redacted text file to parsed_args.output_file.)r  itemrD   responser	  s     r$   -PossiblyWriteRedactedTextResponseToOutputFiler    s    ,X]]-@-@+N	/r#   c                 2    [        U R                  U5        U $ )z4Write the redacted image to parsed_args.output_file.)r  redactedImager  s     r$   .PossiblyWriteRedactedImageResponseToOutputFiler    s    ,X-C-C-8:	/r#   c                  .    [         R                  " SSS9/$ )z&Add --output-file to a redact command.z--output-filez/Path to the file to write redacted contents to.)r   )r   r   r   r#   r$   AddOutputFileFlagr    s!     mm
@B
 r#   )=r!   
__future__r   r   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r   $googlecloudsdk.command_lib.util.apisr	   googlecloudsdk.corer
   r   r   r   googlecloudsdk.core.utilr   r   r   r-   r.   rC   r4   Errorr   r&   r)   r2   r6   r<   rG   rO   rR   rY   r]   rn   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r   r#   r$   <module>r     sA   5 &  ' 	 , / ( : * # * ) * * 
    CZ%% CDz'' D@Z-- @
%=I	2:B674D7
	"
B>4,, r#   