
    *
                         d 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  G d
 dej                        Zy)z The BigQuery CLI cancel command.    )Optional)flagsN)
client_job)utils)bigquery_command)bq_cached_client)bq_id_utilsc                   \     e Zd ZdZdZdedej                  f fdZd	dede	e
   fdZ xZS )
Cancelz5Attempt to cancel the specified job if it is running.zcancel [--nosync] [<job_id>]namefvc                 P    t         t        |   ||       | j                  |       y )N)superr   __init___ProcessCommandRc)selfr   r   	__class__s      &platform/bq/frontend/command_cancel.pyr   zCancel.__init__   s"    	&$ r*2    job_idreturnc           	         t         j                  j                         }t        t	        j
                  ||t        j                  j                              }t        j                  ||d   |d         }t        j                  |t        j                  j                  j                   di |d   d       |d   }|d	   d
k(  r.d|v rd|d   v r|d   d   dk(  rt#        d       yt#        d       yt#        d       y)a  Request a cancel and waits for the job to be cancelled.

    Requests a cancel and then either:
    a) waits until the job is done if the sync flag is set [default], or
    b) returns immediately if the sync flag is not set.
    Not all job types support a cancel, an error is returned if it cannot be
    cancelled. Even for jobs that support a cancel, success is not guaranteed,
    the job may have completed by the time the cancel request is noticed, or
    the job may be in a stage where it cannot be cancelled.

    Examples:
      bq cancel job_id  # Requests a cancel and waits until the job is done.
      bq --nosync cancel job_id  # Requests a cancel and returns immediately.

    Arguments:
      job_id: Job ID to cancel.
    )id_fallbacks
identifierdefault_locationjobIdlocation)bqclientr   r   jobReferenceshow)custom_formatstatusstateDONEerrorResultreasonstoppedz$Job has been cancelled successfully.z+Job completed before it could be cancelled.zJob cancel has been requested.r    )r   ClientGetdictbq_client_utilsGetJobReferencebq_flagsLOCATIONvaluer   	CancelJobfrontend_utilsPrintObjectInfor	   ApiClientHelperJobReferenceCreateprint)r   r   clientjob_reference_dictjobr"   s         r   RunWithArgszCancel.RunWithArgs   s   & $$((*F''%..44	
 

!'*#J/C
 ""##0077N#n:MN
 ]Fg& 
6
!&//]#H-:45
  	;<  ,-r   ) )__name__
__module____qualname____doc__usagestrr   
FlagValuesr   r   intr;   __classcell__)r   s   @r   r   r      s;    =
,%3 E$4$4 1 1Xc] 1r   r   )r@   typingr   abslr   r.   clientsr   r   r,   frontendr   r   r2   r	   BigqueryCmdr   r(   r   r   <module>rK      s6    &     , % % , :)) :r   