
    !                         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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.    )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.IsDone6   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.Poll>   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destinationTabler/   r0   r%   loadjobReferencer$   r   r3   r#   )r   r   r*   job_typeresult_tablerequests         r   	GetResultBqJobPoller.GetResultL   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+   r>   __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)	BqMigrationWorkflowPollert   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   rM   s     r   r   "BqMigrationWorkflowPoller.__init__w   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)rM   r&   RelativeNamer#   )r   migration_workflow_refr*   r=   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#   rU   )rM   r&   rV   r#   )r   rR   r*   r=   s       r   r>   #BqMigrationWorkflowPoller.GetResult   sB     ))88?L 2 7 78G!!%%g..r   rL   N)
r@   rA   rB   rC   rD   r   r    r+   r>   rF   rG   r   r   rI   rI   t   s    9
/8/r   rI   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   GetResourceFromFilern      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   r5   r7   writeDisposition)refargsr=   s      r   ProcessTableCopyOverwriteru      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%   )rs   rt   r=   
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 fromrx   TtableF)verbrV   requiredprefixesattribute_overrides
positionalresource_dataz
to copy torz   --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   )rD   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
   rb   rE   OperationPollerr   rI   rn   ru   r~   r   rG   r   r   <module>r      sn    ? & . . : : > D $ 
 ! Q,&(( Q,h,/ 6 6 ,/^!H!JBr   