
                         $   d Z ddlmZ ddlmZ ddlmZ ddl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mZ ddlZdZd Zd Z ddZ!ddZ"ddZ#d Z$d Z%d Z&d Z'd Z(d Z)y)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                 T    | D cg c]  }t        j                  |       c}S c c}w )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 cg c]  }|j                    c}S c c}w )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                 R    t        j                  | g|t        j                         y)zPrints the given resource.)	resourcesprint_formatoutN)r   Printr   r   )resourcer   s     r   PrettyPrintr"   A   s    

''    c                 .    t        j                  d|       S Ngenomics)	core_apisGetClientInstanceversions    r   GetGenomicsClientr+   I       		$	$Z	99r#   c                 .    t        j                  d|       S r%   )r'   GetMessagesModuler)   s    r   GetGenomicsMessagesr/   M   r,   r#   c                  j    t         j                  j                  j                  j	                  d      S )NT)required)r   VALUEScoreprojectGet r#   r   GetProjectIdr7   Q   s(    						'	'	+	+T	+	::r#   c                 ,    | j                  t              S )N)
startswith
GCS_PREFIX)paths    r   	IsGcsPathr<   U   s    		$$r#   c           
         t        |       rt        j                  d      }|j                          t	        |       \  }}t        j                  dd      }|j                  ||      }	 t        j                  j                  |j                  d      }|j                  j                  ||       ~|j                  } t+        j,                  |       }
|
s$t!        j"                  dj%                  |             	 t/        j0                  |t3        j4                  |
            }|S # t        j                  $ r=}	t!        j"                  d	j%                  | t'        j(                  |	                  d
}	~	ww xY w# t6        t8        t2        j:                  f$ rj 	 t/        j<                  ||
      }Y |S # t6        t>        f$ r=}	t!        j"                  dj%                  | t'        j(                  |	                  d
}	~	ww xY ww xY w)a  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'-KFC 229dC";;c < #K'""++BGGt+Dhnnx8

 77D ""4('	

4
4!!$') )
9&&w		'0BCF 
-1 (( '66
9
@
@CMM!$&' '' nd&9&9	: 99%%gw7f 
- $ 9  66cmmA&679 999sC   *A	D& ;)E9 &E698E11E69HF11G= 8G88G==Hc                     g }| |S t        t        j                  |             D ]  \  }}|j                   |||               |S )N)keyvalue)sortedrS   	iteritemsappend)argdictr\   rd   kr   s        r   !ArgDictToAdditionalPropertiesListrn      sG    &_M S]]7+,da
MM'aq)* -	-r#   c                     | t        t              d j                  d      j                  dd      }t        |      dk7  r$t	        j
                  dj                  |             |S )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           
      z    | sydj                  dg| D cg c]  }dj                  ||g       c}z         S c c}w )aD  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      sL     
	?#:KL:K388VUO,:KLM 
N NLs   8
)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#   