
                            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dZddZ ddZ!d Z"d Z#d Z$d Z%d Z&y)z1Common helper methods for Life Sciences commands.    )absolute_import)division)unicode_literalsN)DecodeError)encoding)
exceptions)extra_types)transfer)apis)log)
properties)yaml)resource_printer)fileszgs://c                 R    t        j                  | g|t        j                         y)zPrints the given resource.)	resourcesprint_formatoutN)r   Printr   r   )resourcer   s     <lib/googlecloudsdk/api_lib/lifesciences/lifesciences_util.pyPrettyPrintr   +   s    

''    c                 .    t        j                  d|       S Nlifesciences)	core_apisGetClientInstanceversions    r   GetLifeSciencesClientr!   3       		$	$^W	==r   c                 .    t        j                  d|       S r   )r   GetMessagesModuler   s    r   GetLifeSciencesMessagesr%   7   r"   r   c                  j    t         j                  j                  j                  j	                  d      S )NT)required)r   VALUEScoreprojectGet r   r   GetProjectIdr-   ;   s(    						'	'	+	+T	+	::r   c                 ,    | j                  t              S )N)
startswith
GCS_PREFIX)paths    r   	IsGcsPathr2   ?   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, lifesciences_exceptions.LifeSciencesInputFileError
  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}]) r2   tempfileNamedTemporaryFileclose_SplitBucketAndObjectr   r$   StorageObjectsGetRequestr
   DownloadFromFilenameobjectsr+   apitools_exceptions	HttpErrorlifesciences_exceptionsLifeSciencesInputFileErrorformatsix	text_typer   ReadFileContentsr   PyValueToMessager   load
ValueErrorAttributeErrorYAMLParseErrorJsonToMessager   )r1   messageclienttfr7   objstorage_messagesget_requestr:   ein_textresults               r   GetFileAsMessager[   C   s    t_		$	$E	2BHHJ'-KFC 229dC";;c < #K'""++BGGt+Dhnnx8

 77D ""4('	
!
<
<!!$') )
9&&w		'0BCF 
-1 (( '#>>
9
@
@CMM!$&' '' nd&9&9	: 99%%gw7f 
- $ 9 $>>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)sortedrI   	iteritemsappend)argdictrR   rZ   kvs        r   !ArgDictToAdditionalPropertiesListre   x   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)lenr0   stripsplitcalliope_exceptionsBadFileExceptionrH   )gcs_pathtokenss     r   r>   r>      s_    C
O$%++C066sA>&[A

.
.8??IK K	-r   )json)v2beta)'__doc__
__future__r   r   r   r;   #apitools.base.protorpclite.messagesr   apitools.base.pyr   r   rD   r	   r
   #googlecloudsdk.api_lib.lifesciencesrF   googlecloudsdk.api_lib.utilr   r   googlecloudsdk.callioperm   googlecloudsdk.corer   r   r   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   rI   r0   r   r!   r%   r-   r2   r[   re   r>   r,   r   r   <module>r}      sn     8 &  '  ; % > ( % U 9 E # * $ 9 * 

>>;%2jr   