
    3                        S 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Jr  SSKJr  SSKJr  SSKrSrS rS rSS jrSS jrSS jrS r S r!S r"S r#S r$S r%g)z,Common helper methods for Genomics commands.    N)DecodeError)encoding)
exceptions)extra_types)transfer)apis)log)
properties)yaml)resource_printer)fileszgs://c                 V    U  Vs/ s H  n[         R                  " US9PM     sn$ s  snf )zConverts a list of strings to the API JsonValue equivalent.

Args:
  values: the string values to be converted
Returns:
  An equivalent list of JsonValue strings
string_value)r   	JsonValuevaluesvs     4lib/googlecloudsdk/api_lib/genomics/genomics_util.pyInfoValuesToAPIr   (   s(     :@	@A+

Q
/	@@	@s   &c                 D    U  Vs/ s H  oR                   PM     sn$ s  snf )zConverts a list of strings to an API JsonValue equivalent.

Args:
  values: the list of JsonValue strings to be converted
Returns:
  An equivalent list of strings
r   r   s     r   InfoValuesFromAPIr   3   s     #)	)&Q..&	))	)s   c                 N    [         R                  " U /U[        R                  S9  g)zPrints the given resource.)	resourcesprint_formatoutN)r   Printr	   r   )resourcer   s     r   PrettyPrintr   >   s    

''    c                 0    [         R                  " SU 5      $ Ngenomics)	core_apisGetClientInstanceversions    r   GetGenomicsClientr(   F       		$	$Z	99r    c                 0    [         R                  " SU 5      $ r"   )r$   GetMessagesModuler&   s    r   GetGenomicsMessagesr,   J   r)   r    c                  d    [         R                  R                  R                  R	                  SS9$ )NT)required)r
   VALUEScoreprojectGet r    r   GetProjectIdr4   N   s(    						'	'	+	+T	+	::r    c                 ,    U R                  [        5      $ )N)
startswith
GCS_PREFIX)paths    r   	IsGcsPathr9   R   s    		$$r    c           
         [        U 5      (       a  [        R                  " SS9nUR                  5         [	        U 5      u  pE[
        R                  " SS5      nUR                  XES9n [        R                  R                  UR                  SS9nUR                  R                  XxS9  AUR                  n [*        R,                  " U 5      n
U
(       d%  [         R"                  " SR%                  U 5      5      e [.        R0                  " U[2        R4                  " U
5      5      nU$ ! [        R                   a?  n	[         R"                  " S	R%                  U [&        R(                  " U	5      5      5      eS
n	A	ff = f! [6        [8        [2        R:                  4 am     [.        R<                  " X5      n U$ ! [6        [>        4 a?  n	[         R"                  " SR%                  U [&        R(                  " U	5      5      5      eS
n	A	ff = ff = f)aq  Reads a YAML or JSON object of type message from path (local or GCS).

Args:
  path: A local or GCS path to an object specification in YAML or JSON format.
  message: The message type to be parsed from the file.
  client: The storage_v1 client to use.

Returns:
  Object of type message, if successful.
Raises:
  files.Error, genomics_exceptions.GenomicsInputFileError
F)deletestoragev1)bucketobjectT)	overwrite)downloadz-Unable to read remote file [{0}] due to [{1}]NzEmpty file [{0}]zGPipeline file [{0}] is not properly formatted YAML or JSON due to [{1}]) r9   tempfileNamedTemporaryFileclose_SplitBucketAndObjectr$   r+   StorageObjectsGetRequestr   DownloadFromFilenameobjectsr2   apitools_exceptions	HttpErrorgenomics_exceptionsGenomicsInputFileErrorformatsix	text_typer   ReadFileContentsr   PyValueToMessager   load
ValueErrorAttributeErrorYAMLParseErrorJsonToMessager   )r8   messageclienttfr>   objstorage_messagesget_requestrA   ein_textresults               r   GetFileAsMessagerb   V   s    t__		$	$E	2BHHJ'-KF 229dC";; < #K'""++BGGt+Dhnn8

 77D ""4('	

4
4!!$') )
9&&w		'0BCF 
-1 (( '66
9
@
@CMM!$&' '' nd&9&9	: 99%%g7f 
- $ 9  66cmmA&679 999sC   )AD' :+E= 'E:;:E55E:=H	F66H:H  HH	c                     / nU c  U$ [        [        R                  " U 5      5       H  u  p4UR                  U" X4S95        M     U$ )N)keyvalue)sortedrP   	iteritemsappend)argdictrY   ra   kr   s        r   !ArgDictToAdditionalPropertiesListrk      sC    &_M S]]7+,da
MM'a)* -	-r    c                     U [        [        5      S R                  S5      R                  SS5      n[        U5      S:w  a%  [        R
                  " SR                  U 5      5      eU$ )zHSplit a GCS path into bucket & object tokens, or raise BadFileException.N/      z.[{0}] is not a valid Google Cloud Storage path)lenr7   stripsplitcalliope_exceptionsBadFileExceptionrO   )gcs_pathtokenss     r   rE   rE      s_    C
O$%++C066sA>&[A

.
.8??IK K	-r    c           
          U (       d  gSR                  S/U  Vs/ s H  nSR                  X/5      PM     sn-   5      $ s  snf )a6  Returns the comma separated list of field names referenced by the command.

Args:
  referenced_fields: A list of field names referenced by the format and filter
    expressions.
  prefix: The referenced field name resource prefix.

Returns:
  The comma separated list of field names referenced by the command.
N,nextPageToken.)join)referenced_fieldsprefixfields      r   GetQueryFieldsr      sJ     
	?#:KL:K388VO,:KLM 
N NLs   ?
)json)r=   )&__doc__rB   #apitools.base.protorpclite.messagesr   apitools.base.pyr   r   rK   r   r   googlecloudsdk.api_lib.genomicsrM   googlecloudsdk.api_lib.utilr   r$   googlecloudsdk.calliopers   googlecloudsdk.corer	   r
   r   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   rP   r7   r   r   r   r(   r,   r4   r9   rb   rk   rE   r   r3   r    r   <module>r      sx     3  ; % > ( % M 9 E # * $ 9 * 

A*::;%2jNr    