
    !                        d 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 G d de	j.                        Z G d de	j.                        Zd Zd Zd Zd Zy)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                   ,    e Zd ZdZefdZd Zd Zd Zy)BqJobPollerzPoller for managing Bq Jobs.c                 .    || _         || _        || _        y)a  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__zBqJobPoller.__init__'   s     )D"D.D    c                     |j                   j                  dk(  rJ|j                   j                  r3t        j                  |j                   j                  j
                        yy)
Overrides.DONETF)statusstateerrorResultr   OperationErrormessage)r   jobs     r   IsDonezBqJobPoller.IsDone:   sH    
zz6!			##CJJ$:$:$B$BCCr   c                     | j                   j                  d      }| j                   j                   ||j                         |j	                         j                                     S )zzOverrides.

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

    Returns:
      fetched job message.
    Get)jobId	projectId)r   GetRequestTyper$   NameParent)r   job_refrequest_types      r   PollzBqJobPoller.PollB   sS     ##2259L7<<>W^^5E5J5J5LM r   c                    | j                   j                  d      }|j                  j                  }|dk(  rJ|j                  j                  j
                  } ||j                  |j                  |j                        }n|dk(  rJ|j                  j                  j
                  } ||j                  |j                  |j                        }nz|dk(  rs| j                   j                  d      } ||j                  j                  | j                  |j                  j                        }| j                   j                  |      S |S | j                   j                  |      S )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   	GetResultz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   N)	__name__
__module____qualname____doc__DEFAULT_MAX_QUERY_RESULTSr   r"   r,   r>    r   r   r   r   $   s    $ 2	/&%,r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)BqMigrationWorkflowPollerz1Poller for managing BigQuery Migration Workflows.c                     || _         y)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   rH   s     r   r   z"BqMigrationWorkflowPoller.__init__{   s     /Dr   c                 2    t        |j                        dk(  S )r   	COMPLETED)strr   )r   migration_workflows     r   r"   z BqMigrationWorkflowPoller.IsDone   s    !''(K77r   c                     | j                   j                  d      } ||j                               }| j                   j                  |      }|S )zOverrides.

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

    Returns:
      fetched migration workflow message.
    r$   name)rH   r'   RelativeNamer$   )r   migration_workflow_refr+   r=   ress        r   r,   zBqMigrationWorkflowPoller.Poll   sI     ))88?L 6 C C EFG

 
 
$
$W
-CJr   c                     | j                   j                  d      } ||j                        }| j                   j                  |      S )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$   rN   )rH   r'   rO   r$   )r   rL   r+   r=   s       r   r>   z#BqMigrationWorkflowPoller.GetResult   sB     ))88?L 2 7 78G!!%%g..r   N)r?   r@   rA   rB   r   r"   r,   r>   rD   r   r   rF   rF   x   s    9
/8/r   rF   c           	         	 t        j                  |       }t        |t              s$t        j                  dj                  |             	 t        j                  ||      }|S # t         j                  $ r=}t        j                  dj                  | t        j                  |                  d}~wt         j                  $ r=}t        j                  dj                  | t        j                  |                  d}~ww xY w# t        $ r=}t        j                  dj                  | t        j                  |                  d}~ww xY w)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   GetResourceFromFilere      s-   y1 
%t	,

%
%CJJ	
 	((0H 
/; 
		 

%
%*11)S]]1=MN  
		 

%
%299s}}Q'	
 $ 
 

%
%GNNs}}Q'	
 s;   A$ D $D78B//D8C==D	E8EEc                 j    ~ |j                   r%d|j                  j                  j                  _        |S )z*Process the overwrite flag on tables copy.WRITE_TRUNCATE)	overwriter!   r5   r7   writeDisposition)refargsr=   s      r   ProcessTableCopyOverwriterl      s)    		^^6FGKK""3	.r   c                 |   ~ |j                   j                  j                         }|j                   j                  j                         }t	        j
                  |d|j                         j                                t	        j
                  |d|j                         t	        j
                  |d|j                                t	        j
                  |d|j                         j                                t	        j
                  |d|j                         t	        j
                  |d|j                                |S )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&   )rj   rk   r=   
source_refdestination_refs        r   ProcessTableCopyConfigurationru      s   	}}##))+*MM--335/9##%
 9
 7
 4 
 4
 ;Z__=N 
.r   c                  ,   t         j                  j                  d      } t        j                  ddddddid| j                               t        j                  dd	dddd	id| j                               g}d
gdgd}t        j                  ||      gS )z?Get Table resource args (source, destination) for copy command.zbq.tablezto copy fromro   TtableF)verbrO   requiredprefixesattribute_overrides
positionalresource_dataz
to copy torq   --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   )rB   
__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   rY   rC   OperationPollerr   rF   re   rl   ru   r   rD   r   r   <module>r      sx    ? &  % ' % . . : : > D $ 
 ! Q,&(( Q,h,/ 6 6 ,/^!H!JBr   