
    !                        S 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 S\	R.                  5      r " S S\	R.                  5      rS rS rS rS rg)z8General BQ surface command utilites for python commands.    )absolute_import)division)print_function)unicode_literals)encoding)waiter)
exceptions)	arg_utils)	yaml_data)resource_args)concept_parsers)yamlNi  c                   6    \ rS rSrSr\4S jrS rS rS r	Sr
g)	BqJobPoller$   zPoller for managing Bq Jobs.c                 (    X l         Xl        X0l        g)am  Sets up poller for BigQuery Jobs.

Args:
  job_service: apitools.base.py.base_api.BaseApiService, api service for
    retrieving information about ongoing job.
  result_service: apitools.base.py.base_api.BaseApiService, api service for
    retrieving created result of initiated operation.
  max_query_results: maximum number of records to return from a query job.
N)result_servicejob_servicemax_query_results)selfr   r   r   s       2lib/googlecloudsdk/command_lib/bq/command_utils.py__init__BqJobPoller.__init__'   s     )".    c                     UR                   R                  S:X  aP  UR                   R                  (       a4  [        R                  " UR                   R                  R
                  5      egg)
Overrides.DONETF)statusstateerrorResultr   OperationErrormessage)r   jobs     r   IsDoneBqJobPoller.IsDone:   sH    
zz6!			##CJJ$:$:$B$BCCr   c                     U R                   R                  S5      nU R                   R                  U" UR                  5       UR	                  5       R                  5       S95      $ )zfOverrides.

Args:
  job_ref: googlecloudsdk.core.resources.Resource.

Returns:
  fetched job message.
Get)jobId	projectId)r   GetRequestTyper'   NameParent)r   job_refrequest_types      r   PollBqJobPoller.PollB   sS     ##2259L7<<>W^^5E5J5J5LM r   c                    U R                   R                  S5      nUR                  R                  nUS:X  aG  UR                  R                  R
                  nU" UR                  UR                  UR                  S9nOUS:X  aG  UR                  R                  R
                  nU" UR                  UR                  UR                  S9nOxUS:X  ap  U R                   R                  S5      nU" UR                  R                  U R                  UR                  R                  S9nU R                   R                  U5      $ U$ U R                   R                  U5      $ )zOverrides to get the response from the completed job by job type.

Args:
  job: api_name_messages.Job.

Returns:
  the 'response' field of the job.
r'   COPY)	datasetIdtableIdr)   LOADQUERYGetQueryResults)r(   
maxResultsr)   )r   r*   configurationjobTypecopydestinationTabler3   r4   r)   loadjobReferencer(   r   r7   r'   )r   r#   r.   job_typeresult_tablerequests         r   	GetResultBqJobPoller.GetResultP   s<    &&55e<L  ((H6&&++<<l **&& **g
 
V	&&++<<l **&& **g
 
W	((778IJl  &&++$$..g
   0099j""7++r   )r   r   r   N)__name__
__module____qualname____firstlineno____doc__DEFAULT_MAX_QUERY_RESULTSr   r$   r/   rB   __static_attributes__ r   r   r   r   $   s    $ 2	/&%,r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	BqMigrationWorkflowPollerx   z1Poller for managing BigQuery Migration Workflows.c                     Xl         g)zSets up poller for generic long running processes.

Args:
  migration_service: apitools.base.py.base_api.BaseApiService, api service
    for retrieving information about migration workflows.
Nmigration_service)r   rQ   s     r   r   "BqMigrationWorkflowPoller.__init__{   s
     /r   c                 2    [        UR                  5      S:H  $ )r   	COMPLETED)strr   )r   migration_workflows     r   r$    BqMigrationWorkflowPoller.IsDone   s    !''(K77r   c                     U R                   R                  S5      nU" UR                  5       S9nU R                   R                  U5      nU$ )zOverrides.

Args:
  migration_workflow_ref: googlecloudsdk.core.resources.Resource.

Returns:
  fetched migration workflow message.
r'   name)rQ   r*   RelativeNamer'   )r   migration_workflow_refr.   rA   ress        r   r/   BqMigrationWorkflowPoller.Poll   sI     ))88?L 6 C C EFG

 
 
$
$W
-CJr   c                     U R                   R                  S5      nU" UR                  S9nU R                   R                  U5      $ )zOverrides to get the response from the completed job by job type.

Args:
  migration_workflow: api_name_messages.MigrationWorkflow.

Returns:
  the 'response' field of the Operation.
r'   rY   )rQ   r*   rZ   r'   )r   rV   r.   rA   s       r   rB   #BqMigrationWorkflowPoller.GetResult   sB     ))88?L 2 7 78G!!%%g..r   rP   N)
rD   rE   rF   rG   rH   r   r$   r/   rB   rJ   rK   r   r   rM   rM   x   s    9
/8/r   rM   c           	          [         R                  " U 5      n[        U[        5      (       d%  [        R                  " SR                  U 5      5      e [        R                  " X5      nU$ ! [         R                   a?  n[        R                  " SR                  U [        R                  " U5      5      5      eSnAf[         R                   a?  n[        R                  " SR                  U [        R                  " U5      5      5      eSnAff = f! [         a?  n[        R                  " SR                  U [        R                  " U5      5      5      eSnAff = f)z7Returns the resource message and update fields in file.z File [{0}] cannot be parsed. {1}Nz(File [{0}] cannot be opened or read. {1}z9File [{0}] is not a properly formatted YAML or JSON file.z=File [{0}] is not a properly formatted YAML or JSON file. {1})r   	load_pathYAMLParseErrorr	   BadFileExceptionformatsix	text_typeFileLoadError
isinstancedictr   PyValueToMessageAttributeError)	file_pathresource_message_typeresource_to_parseeresources        r   GetResourceFromFilerr      s-   y1 
%t	,	,

%
%CJJ	
 	((H 
/; 
		 

%
%*11)S]]1=MN  
		 

%
%299s}}Q'	
 $ 
 

%
%GNNs}}Q'	
 s;   A+ D +D?:B99D:D

D
E:EEc                 t    A UR                   (       a%  SUR                  R                  R                  l        U$ )z*Process the overwrite flag on tables copy.WRITE_TRUNCATE)	overwriter#   r9   r;   writeDisposition)refargsrA   s      r   ProcessTableCopyOverwritery      s)    		^^6FGKK""3	.r   c                    A UR                   R                  R                  5       nUR                   R                  R                  5       n[        R
                  " USUR                  5       R                  5       5        [        R
                  " USUR                  5        [        R
                  " USUR                  5       5        [        R
                  " USUR                  5       R                  5       5        [        R
                  " USUR                  5        [        R
                  " USUR                  5       5        U$ )z;Build JobConfigurationTableCopy from request resource args.z1job.configuration.copy.destinationTable.datasetIdz1job.configuration.copy.destinationTable.projectIdz/job.configuration.copy.destinationTable.tableIdz,job.configuration.copy.sourceTable.datasetIdz,job.configuration.copy.sourceTable.projectIdz*job.configuration.copy.sourceTable.tableId)	CONCEPTSsourceParsedestinationr
   SetFieldInMessager,   r+   r)   )rw   rx   rA   
source_refdestination_refs        r   ProcessTableCopyConfigurationr      s   	}}##))+*MM--335/9##%
 9
 7
 4 
 4
 ;Z__=N 
.r   c                  $   [         R                  R                  S5      n [        R                  " SSSSSS0SU R                  5       S9[        R                  " SS	SSSS	0SU R                  5       S9/nS
/S/S.n[        R                  " X5      /$ )z?Get Table resource args (source, destination) for copy command.zbq.tablezto copy fromr|   TtableF)verbrZ   requiredprefixesattribute_overrides
positionalresource_dataz
to copy tor~   --destination.dataset--source.dataset)r   r   )r   ResourceYAMLDataFromPathr   GetResourcePresentationSpecGetDatar   ConceptParser)table_spec_data	arg_specsfallthroughss      r   GetTableCopyResourceArgsr      s    ..77
C///&1'//1 //&6'//1)* 33 23, 
'
'	
@	AAr   )rH   
__future__r   r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper	   $googlecloudsdk.command_lib.util.apisr
   r   $googlecloudsdk.command_lib.util.argsr   (googlecloudsdk.command_lib.util.conceptsr   googlecloudsdk.corer   rf   rI   OperationPollerr   rM   rr   ry   r   r   rK   r   r   <module>r      sx    ? &  % ' % . . : : > D $ 
 ! Q,&(( Q,h,/ 6 6 ,/^!H!JBr   