
                         Z   d 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mZ dd	lmZ ddlmZ ddlmZ ddlZ G d dej.                        Z G d dej.                        Z G d dej.                        Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$y)z,Utilities for Data Catalog entries commands.    )absolute_import)division)unicode_literals)messages)encoding)entries)util)
exceptions)	arg_utils)log)yamlNc                       e Zd ZdZy)InvalidSchemaErrorz*Error if a schema is improperly specified.N__name__
__module____qualname____doc__     ;lib/googlecloudsdk/command_lib/data_catalog/entries/util.pyr   r   !   s    2r   r   c                       e Zd ZdZy)InvalidSchemaFileErrorz1Error if a schema file is not valid JSON or YAML.Nr   r   r   r   r   r   %   s    9r   r   c                       e Zd ZdZy)UnderSpecifiedEntryErrorz;Error if an entry resource argument is not fully specified.Nr   r   r   r   r   r   )   s    Cr   r   c                 l    ~ |j                  d      r!|j                  j                  dd      |_        |S )a  Returns the update request with the corrected mask.

  The API expects a request with an update mask of 'schema', whereas the inline
  schema argument generates an update mask of 'schema.columns'. So if --schema
  was specified, we have to correct the update mask.

  Args:
    ref: The entry resource reference.
    args: The parsed args namespace.
    request: The update entry request.

  Returns:
    Request with corrected update mask.
  schemazschema.columns)IsSpecified
updateMaskreplacerefargsrequests      r   CorrectUpdateMaskr%   -   s6     
	h ++334DhOG	.r   c                 p    | j                  d      xs$ | j                  d      xs | j                  d      S )Nadd_file_patternsremove_file_patternsclear_file_patterns)r   )r#   s    r   _IsChangeFilePatternSpecifiedr*   B   s>    


.
/ 2


1
22


0
13r   c                    t        |      s|S ~ |j                  j                  j                         }t	        j
                         j                  |      j                  j                  xs g }|j                  d      rg }|j                  d      r)t        |j                        }|D cg c]	  }||vs| }}|j                  d      r||j                  z  }t        j                  |d|       |xj                  dz  c_        |S c c}w )zMerges user-provided GCS file patterns with existing patterns.

  Args:
    ref: The entry resource reference.
    args: The parsed args namespace.
    request: The update entry request.

  Returns:
    Request with merged GCS file pattern.
  r)   r(   r'   z>googleCloudDatacatalogV1beta1Entry.gcsFilesetSpec.filePatternsz,gcsFilesetSpec.filePatterns)r*   CONCEPTSentryParser   EntriesClientGetgcsFilesetSpecfilePatternsr   setr(   r'   r   SetFieldInMessager   )r"   r#   r$   	entry_reffile_patterns	to_removebs          r   MergeGcsFilePatternsr9   H   s     
't	,N	mm!!''))'')--302 	+,M	,-D--.I -D1)1CQMD	)*T+++MO 
66	. Es   +	D	5D	c                 D    ~ t        j                  |j                  |      S N)r   ParseResourceIntoLookupRequestresourcer!   s      r   r<   r<   k   s    			/	/w	GGr   c                    ~ |j                   j                  j                         }|j                  d      r|st	        d      |r|j                  d      s|s'|j                  d      st        j                  dd      |r|j                         |_        |S t        j                         }|j                  |j                        j                  |_        |S )a  Parses the entry into the request, performing a lookup first if necessary.

  Args:
    ref: None.
    args: The parsed args namespace.
    request: The update entry request.

  Returns:
    Request containing the parsed entry.
  Raises:
    UnderSpecifiedEntryError: if ENTRY was only partially specified.
    RequiredMutexGroupError: if both or neither ENTRY, --lookup-entry specified.
  r-   zYArgument [ENTRY : --entry-group=ENTRY_GROUP --location=LOCATION] was not fully specified.lookup_entryzJ([ENTRY : --entry-group=ENTRY_GROUP --location=LOCATION] | --lookup-entry))r,   r-   r.   r   r   concept_exceptionsRequiredMutexGroupErrorRelativeNamenamer   r/   Lookupr?   )r"   r#   r$   r5   clients        r   LookupAndParseEntryrF   p   s     
mm!!''))
 
gy
"	    T%%n5T--n=

4
4   ))+GL 
. ""$F==!2!2388GL	.r   c                     	 t        j                  |       }t        |      S # t         j                  $ r}t        dj	                  |            d }~ww xY w)NzError parsing schema file: [{}])r   loadYAMLParseErrorr   format_SchemaToMessage)schema_filer   es      r   ProcessSchemaFromFilerN      sT    NYY{#F 
&	!! 
		 N
 !B!I!I!!L
MMNs   " AAAc                 b   t        j                         }	 t        j                  d| i|j                        }t        |      }|r"dg}||z  }t        dj                  |            |S # t
        $ r t        d      t        j                  $ r}t        dj                  |            d}~ww xY w)zConverts the given schema dict to the corresponding schema message.

  Args:
    schema: dict, The schema that has been processed.

  Returns:
    googleCloudDatacatalogV1betaSchema
  Raises:
    InvalidSchemaError: If the schema is invalid.
  columnsztInvalid schema: expected list of column names along with their types, modes, descriptions, and/or nested subcolumns.zInvalid schema: [{}]Nz6Invalid schema, the following fields are unrecognized:
)api_utilGetMessagesModuler   DictToMessage#GoogleCloudDatacatalogV1beta1SchemaAttributeErrorr   	_messagesValidationErrorrJ   _GetUnrecognizedFieldPathsjoin)r   r   schema_messagerM   unrecognized_field_pathserror_msg_liness         r   rK   rK      s     '')(?++	FXIIKN 8GOPO//O
TYY7
88	 
 :
	9: : 
	"	" ? 3::1=
>>?s   "A) )&B.B))B.c                     t        j                  |       }g }|D ]X  \  }}dj                  d |D              }|j                  ddd      }|D ]#  }|j	                  dj                  ||             % Z t        |      S )z?Returns the field paths for unrecognized fields in the message..c              3   F   K   | ]  }t        j                  |        y wr;   )six	text_type).0rM   s     r   	<genexpr>z-_GetUnrecognizedFieldPaths.<locals>.<genexpr>   s     !M<Lq#--"2<Ls   !rP       z{}.{})r   UnrecognizedFieldIterrZ   r    appendrJ   sorted)messageerrorsr\   edges_to_messagefield_namesmessage_field_path
field_names          r   rY   rY      s    ))'2&'-#!M<L!MM+33Ir1E!
%%gnn5G5?'A B "	 (. 
(	))r   c                 t    t         j                  j                  dj                  |j                               y )NzStarred entry [{}].r   outPrintrJ   r-   _r#   s     r   LogStarSuccessrv      s"    ''--%,,TZZ89r   c                 t    t         j                  j                  dj                  |j                               y )NzUnstarred entry [{}].rq   rt   s     r   LogUnstarSuccessrx      s"    ''--'..tzz:;r   )%r   
__future__r   r   r   apitools.base.protorpcliter   rW   apitools.base.pyr   #googlecloudsdk.api_lib.data_catalogr   r	   rR   #googlecloudsdk.command_lib.conceptsr
   r@   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.corer   r   ra   Errorr   r   r   r%   r*   r9   r<   rF   rN   rK   rY   rv   rx   r   r   r   <module>r      s    3 &  ' < % 7 @ P : * # $ 
3)) 3:Z-- :Dz// D*3 FH
$N"D*:<r   