
                             S r SSKrSSKrSSKrSSKJr  SSKrSSKrSSKJ	r
   SS\S\\   S\4S jjrSS	 jr " S
 S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)z!BQ CLI library for wait printers.    N)Optional)utilssprevious_tokenreturnc                     Ub%  [         R                  R                  SSU-  -   5        [         R                  R                  SU -   5        [         R                  R                  5         [	        U 5      $ )a  Print string over the current terminal line, and stay on that line.

The full width of any previous output (by the token) will be wiped clean.
If multiple callers call this at the same time, it would be bad.

Args:
  s: string to print.  May not contain newlines.
  previous_token: token returned from previous call, or None on first call.

Returns:
  a token to pass into your next call to this function.
 )sysstderrwriteflushlen)r   r   s     #platform/bq/clients/wait_printer.py_overwrite_current_liner      sU    ( JJTS>123**4!8**	Q-    c                    SnSnSnSnUcH   U R                  5       u  pAU(       a)  ['        S
[)        UR+                  5       S-  5      -  U5      nUc  MH  ['        SU5        [,        R.                  R1                  S5        U$ ! [        R                  R                   a  n[        R
                  " SUR                  R                  5        UR                  R                  5        H  u  pg[        R                  " SXg5        M     UR                  R                  S;   aQ  SU-  nUS-  nUS:  a  e [        S	UR                  R                  U4-  5        [        R                  " U5         SnAGNP[        R                  " U5         SnAGNlSnAf[        R                   ["        4 a!  n[        R$                  " U5         SnAGNSnAff = f)a@  Run an apiclient request with a resumable upload, showing progress.

Args:
  request: an apiclient request having a media_body that is a
    MediaFileUpload(resumable=True).

Returns:
  The result of executing the request, if it succeeds.

Raises:
  BigQueryError: on a non-retriable error or too many retriable errors.
Nr   z+HTTP Error %d during resumable media uploadz  %s: %s)i  i  i           zError %d, retry #%dzUploaded %d%%... d   zUpload complete.
)
next_chunkgoogleapiclienterrors	HttpErrorloggingerrorrespstatusitemsinfoprinttimesleepbq_client_utilsRaiseErrorFromHttpErrorhttplib2HttpLib2ErrorIOErrorRaiseErrorFromNonHttpErrorr   intprogressr   r   r   )	requestresultretriable_errorsoutput_tokenr    ekeyvalue	sleep_secs	            r   execute_in_chunks_with_progressr6   ,   sy    &,&4))+nf( ,
FOO$5$; <
<ll/ 	4 ,l;**4	-3 !!++ 3mm
7 *#Z, '	
/	)''	Aa
#qvv}}6F&GGH

9 	//22""G, 400334s*   B G	C
F/FG	(GG	c                   <    \ rS rSrSrS\S\S\SS4S jrSS	 jrS
r	g)WaitPrinter\   z2Base class that defines the WaitPrinter interface.job_id	wait_timer    r   Nc                     [        S5      e)zPrints status for the current job we are waiting on.

Args:
  job_id: the identifier for this job.
  wait_time: the number of seconds we have been waiting so far.
  status: the status of the job we are waiting for.
zSubclass must implement PrintNotImplementedErrorselfr:   r;   r    s       r   r#   WaitPrinter.print_   s     =
>>r   c                     [        S5      e)zwWaiting is done and no more Print calls will be made.

This function should handle the case of Print not being called.
zSubclass must implement Doner=   r@   s    r   doneWaitPrinter.donei   s    
 <
==r    r   N)
__name__
__module____qualname____firstlineno____doc__strfloatr#   rD   __static_attributes__rF   r   r   r8   r8   \   s+    :?# ?% ? ? ?>r   r8   c                   &    \ rS rSrSrSrSS jrSrg)WaitPrinterHelperq   z7A Done implementation that prints based off a property.FNc                 f    U R                   (       a   [        R                  R                  S5        g g )Nr   )print_on_doner   r   r   rC   s    r   rD   WaitPrinterHelper.donev   s"    	jjt r   rF   rG   )rH   rI   rJ   rK   rL   rT   rD   rO   rF   r   r   rQ   rQ   q   s    ?-r   rQ   c                   .    \ rS rSrSrS\S\S\4S jrSrg)	QuietWaitPrinter{   z"A WaitPrinter that prints nothing.unused_job_idunused_wait_timeunused_statusc                     g NrF   )r@   rY   rZ   r[   s       r   r#   QuietWaitPrinter.print~   s     	r   rF   N)	rH   rI   rJ   rK   rL   rM   rN   r#   rO   rF   r   r   rW   rW   {   s"    *		27	HK	r   rW   c                   8    \ rS rSrSrS rS\S\S\SS4S	 jrS
r	g)VerboseWaitPrinter   z'A WaitPrinter that prints every update.c                     S U l         g r]   )r1   rC   s    r   __init__VerboseWaitPrinter.__init__   s
    Dr   r:   r;   r    r   Nc                 R    SU l         [        SXU4-  U R                  5      U l        g )NTz,Waiting on %s ... (%ds) Current status: %-7s)rT   r   r1   r?   s       r   r#   VerboseWaitPrinter.print   s0    D/6f
%	&Dr   )r1   rT   )
rH   rI   rJ   rK   rL   rc   rM   rN   r#   rO   rF   r   r   r`   r`      s+    /# %   r   r`   c                   D   ^  \ rS rSrSrSrS\S\S\SS4U 4S jjrS	r	U =r
$ )
TransitionWaitPrinter   z5A WaitPrinter that only prints status change updates.Nr:   r;   r    r   c                 Z   > X0R                   :w  a  X0l         [        [        U ]  XU5        g g r]   )_previous_statussuperrh   r#   )r@   r:   r;   r    	__class__s       r   r#   TransitionWaitPrinter.print   s,    &&&$!4.v&I 'r   )rk   )rH   rI   rJ   rK   rL   rk   rM   rN   r#   rO   __classcell__)rm   s   @r   rh   rh      s8    =J# J% J J J Jr   rh   r]   rG   )rL   r   r   r$   typingr   r   r(   clientsr   r&   rM   r,   r   r6   r8   rQ   rW   r`   rh   rF   r   r   <module>rr      s    '  
     , -1
$SM:-`> >* 	( 	* J. Jr   