
    .                         S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
Jr  SS	KJr   " S
 S\	R                  5      rg)zAbstract waiter utility for api_lib.util.waiter.py.

Abstract waiter utility class for api_lib.util.waiter.WaitFor.
This class is the base class for poller that need to stream output and poll on
dataproc's operation.
    )absolute_import)division)unicode_literalsN)storage_helpers)waiter)log)console_attrc                       \ rS rSrSrS r\R                  S 5       r\R                  S 5       r	\R                  S 5       r
\R                  S 5       rS rS	 rS
 rS rSrg)AbstractOperationStreamerPoller$   a  Base abstract poller class for dataproc operation.

Base abstract poller class for dataproc operation. The class is designed to
stream remote output from GCS.

Pass TrackerUpdateFunction to waiter.WaitFor's tracker_update_func parameter
to stream remote output.
c                 ,    SU l         SU l        Xl        g)zTPoller for batch workload.

Args:
  dataproc: A api_lib.dataproc.Dataproc instance.
N)saved_stream_uridriver_log_streamerdataproc)selfr   s     Plib/googlecloudsdk/api_lib/dataproc/poller/abstract_operation_streamer_poller.py__init__(AbstractOperationStreamerPoller.__init__.   s     !D#DM    c                     g)a\  Determines if the poll result is done.

Determines if the poll result is done. This is a null implementation that
simply returns False. Sub class should override this and provide concrete
checking logic.

Overrides.

Args:
  poll_result: Poll result returned from Poll function.

Returns:
  True if the remote resource is done, or False otherwise.
F r   poll_results     r   IsDone&AbstractOperationStreamerPoller.IsDone8   s      r   c                     g)ad  Fetches remote resource.

Fetches remote resource. This is a null implementation that simply returns
None. Sub class should override this and provide concrete fetching logic.

Overrides.

Args:
  ref: Resource reference. The same argument passed to waiter.WaitFor.

Returns:
  None. Sub class should override this and return the actual fetched
  resource.
Nr   )r   refs     r   Poll$AbstractOperationStreamerPoller.PollJ         r   c                     g)a  Gets output uri from poll result.

Gets output uri from poll result. This is a null implementation that
returns None. Sub class should override this and return actual output uri
for output streamer, or returns None if something goes wrong and there is
no output uri in the poll result.

Args:
  poll_result: Poll result returned by Poll.

Returns:
  None. Sub class should override this and returns actual output uri, or
  None when something goes wrong.
Nr   r   s     r   _GetOutputUri-AbstractOperationStreamerPoller._GetOutputUri\   r    r   c                     g)aa  Returns operation result to caller.

This function is called after GetResult streams remote output.
This is a null implementation that simply returns None. Sub class should
override this and provide actual _GetResult logic.

Args:
  poll_result: Poll result returned from Poll.

Returns:
  None. Sub class should override this and return actual result.
Nr   r   s     r   
_GetResult*AbstractOperationStreamerPoller._GetResultn   s     r   c                 J    U R                  SUS5        U R                  U5      $ )zReturns result for remote resource.

This function first stream remote output to user, then returns operation
result by calling _GetResult.

Overrides.

Args:
  poll_result: Poll result returned by Poll.

Returns:
  Wahtever returned from _GetResult.
N)TrackerUpdateFunctionr%   r   s     r   	GetResult)AbstractOperationStreamerPoller.GetResult~   s%    " 	t[$7??;''r   c                 F    U R                  U5        U R                  5         g)a  Custom tracker function which gets called after every tick.

This gets called whenever progress tracker gets a tick. However we want to
stream remote output to users instead of showing a progress tracker.

Args:
  tracker: Progress tracker instance. Not being used.
  poll_result: Result from Poll function.
  status: Status argument that is supposed to pass to the progress tracker
  instance. Not being used here as well.
N)_CheckStreamer_StreamOutput)r   trackerr   statuss       r   r(   5AbstractOperationStreamerPoller.TrackerUpdateFunction   s     	$r   c                     U R                   (       aF  U R                   R                  (       a*  U R                   R                  [        R                  5        g g g )N)r   openReadIntoWritabler   err)r   s    r   r-   -AbstractOperationStreamerPoller._StreamOutput   s:    D$<$<$A$A
//8 %Br   c                 H   S nU R                  U5      nU(       d  gU R                  (       aB  U R                  U:w  a2  SU l        SU l        U" 5         [        R                  " S5        U" 5         U R                  (       d"  X0l        [
        R                  " U5      U l        gg)a   Checks if need to init a new output streamer.

Checks if need to init a new output streamer.
Remote may fail; switch to new output uri.
Invalidate the streamer instance and init a new one if necessary.

Args:
  poll_result: Poll result returned from Poll.
c                      [         R                  " 5       n [        R                  R	                  SU R                  5       S   -  5        g )N=r   )r	   GetConsoleAttrr   r4   PrintGetTermSize)attrs    r   _PrintEqualsLineAccrossScreenUAbstractOperationStreamerPoller._CheckStreamer.<locals>._PrintEqualsLineAccrossScreen   s3    ((*d	ggmmC$**,Q//0r   Nz/Attempt failed. Streaming new attempt's output.)r"   r   r   r   warningr   StorageObjectSeriesStream)r   r   r=   uris       r   r,   .AbstractOperationStreamerPoller._CheckStreamer   s    1
 

[
)C  !6!6#!=!%d"d#%	kkCD#% ##!!0!J!J3!Od $r   )r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   abcabstractmethodr   r   r"   r%   r)   r(   r-   r,   __static_attributes__r   r   r   r   r   $   s      "  "  "  ((9"Pr   r   )rG   
__future__r   r   r   rH   googlecloudsdk.api_lib.dataprocr   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   googlecloudsdk.core.consoler	   OperationPollerr   r   r   r   <module>rQ      s:     '  ' 
 ; . # 4cPf&<&< cPr   