
                         *   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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.    )absolute_import)division)unicode_literalsN)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   6   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"   A   s    

''    c                 0    [         R                  " SU 5      $ Ngenomics)	core_apisGetClientInstanceversions    r   GetGenomicsClientr+   I       		$	$Z	99r#   c                 0    [         R                  " SU 5      $ r%   )r'   GetMessagesModuler)   s    r   GetGenomicsMessagesr/   M   r,   r#   c                  d    [         R                  R                  R                  R	                  SS9$ )NT)required)r   VALUEScoreprojectGet r#   r   GetProjectIdr7   Q   s(    						'	'	+	+T	+	::r#   c                 ,    U R                  [        5      $ )N)
startswith
GCS_PREFIX)paths    r   	IsGcsPathr<   U   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}]) r<   tempfileNamedTemporaryFileclose_SplitBucketAndObjectr'   r.   StorageObjectsGetRequestr
   DownloadFromFilenameobjectsr5   apitools_exceptions	HttpErrorgenomics_exceptionsGenomicsInputFileErrorformatsix	text_typer   ReadFileContentsr   PyValueToMessager   load
ValueErrorAttributeErrorYAMLParseErrorJsonToMessager   )r;   messageclienttfrA   objstorage_messagesget_requestrD   ein_textresults               r   GetFileAsMessagere   Y   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)sortedrS   	iteritemsappend)argdictr\   rd   kr   s        r   !ArgDictToAdditionalPropertiesListrn      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)lenr:   stripsplitcalliope_exceptionsBadFileExceptionrR   )gcs_pathtokenss     r   rH   rH      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__
__future__r   r   r   rE   #apitools.base.protorpclite.messagesr   apitools.base.pyr   r   rN   r	   r
   googlecloudsdk.api_lib.genomicsrP   googlecloudsdk.api_lib.utilr   r'   googlecloudsdk.callioperv   googlecloudsdk.corer   r   r   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   rS   r:   r   r   r"   r+   r/   r7   r<   re   rn   rH   r   r6   r#   r   <module>r      s     3 &  '  ; % > ( % M 9 E # * $ 9 * 

A*::;%2jNr#   