
    3                         S r SSK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K
Jr  SSKJr   " S	 S
\R                  5      rSS jrS rS rSS jrS rSS jrSS jrg)a6  Futures for long-running operations returned from Google Cloud APIs.

These futures can be used to synchronously wait for the result of a
long-running operation using :meth:`Operation.result`:


.. code-block:: python

    operation = my_api_client.long_running_method()
    result = operation.result()

Or asynchronously using callbacks and :meth:`Operation.add_done_callback`:

.. code-block:: python

    operation = my_api_client.long_running_method()

    def my_callback(future):
        result = future.result()

    operation.add_done_callback(my_callback)

    N)
exceptions)protobuf_helpers)polling)operations_pb2)json_format)code_pb2c                      ^  \ rS rSrSrS\R                  4U 4S jjr\S 5       r	\S 5       r
\S 5       rS rSS	 jrSS
 jrS rS rSrU =r$ )	Operation2   a  A Future for interacting with a Google API Long-Running Operation.

Args:
    operation (google.longrunning.operations_pb2.Operation): The
        initial operation.
    refresh (Callable[[], ~.api_core.operation.Operation]): A callable that
        returns the latest state of the operation.
    cancel (Callable[[], None]): A callable that tries to cancel
        the operation.
    result_type (func:`type`): The protobuf type for the operation's
        result.
    metadata_type (func:`type`): The protobuf type for the operation's
        metadata.
    polling (google.api_core.retry.Retry): The configuration used for polling.
        This parameter controls how often :meth:`done` is polled. If the
        ``timeout`` argument is specified in the :meth:`result` method, it will
        override the ``polling.timeout`` property.
    retry (google.api_core.retry.Retry): DEPRECATED: use ``polling`` instead.
        If specified it will override ``polling`` parameter to maintain
        backward compatibility.
Nc                    > [         [        U ]
  " SSU0UD6  Xl        X l        X0l        X@l        XPl        [        R                  " 5       U l
        U R                  5         g )Nr    )superr
   __init__
_operation_refresh_cancel_result_type_metadata_type	threadingLock_completion_lock_set_result_from_operation)	self	operationrefreshcancelresult_typemetadata_typer   kwargs	__class__s	           ,lib/third_party/google/api_core/operation.pyr   Operation.__init__I   sR     	i'BB6B#'+ ) 0'')    c                     U R                   $ )zAgoogle.longrunning.Operation: The current long-running operation.)r   r   s    r!   r   Operation.operation]   s     r#   c                     U R                   R                  S5      (       d  g[        R                  " U R                  U R                   R
                  5      $ )z8google.protobuf.Message: the current operation metadata.metadataN)r   HasFieldr   from_any_pbr   r(   r%   s    r!   r(   Operation.metadatab   sD     ''
33++!9!9
 	
r#   c                 @    [         R                  R                  U5      $ )zDeserialize a ``google.longrunning.Operation`` protocol buffer.

Args:
    payload (bytes): A serialized operation protocol buffer.

Returns:
    ~.operations_pb2.Operation: An Operation protobuf object.
)r   r
   
FromString)r   payloads     r!   deserializeOperation.deserializel   s     ''227;;r#   c                     U R                      U R                  R                  (       a  U R                  (       a
   SSS5        gU R                  R	                  S5      (       aG  [
        R                  " U R                  U R                  R                  5      nU R                  U5        OU R                  R	                  S5      (       a  [        R                  " U R                  R                  R                  U R                  R                  R                  U R                  R                  4U R                  S9nU R                  U5        O'[        R                   " S5      nU R                  U5        SSS5        g! , (       d  f       g= f)zASet the result or exception from the operation if it is complete.Nresponseerror)status_codemessageerrorsr2   zLUnexpected state: Long-running operation had neither response nor error set.)r   r   done_result_setr)   r   r*   r   r2   
set_resultr   from_grpc_statusr3   coder5   set_exceptionGoogleAPICallError)r   r2   	exceptions      r!   r   $Operation._set_result_from_operationx   s   
 ""
 ??''4+;+; #" ''
33+77%%t'?'? )))'22&77 $ 5 5 : : OO1199 OO113!__		 ""9-&99.	 ""9-5 #""s   .E?D2E??
Fc                     U R                   R                  (       d<  U(       a  U R                  US9OU R                  5       U l         U R                  5         gg)zRefresh the operation and update the result if needed.

Args:
    retry (google.api_core.retry.Retry): (Optional) How to retry the RPC.
)retryN)r   r7   r   r   r   rA   s     r!   _refresh_and_updateOperation._refresh_and_update   s>     ##<Admm%m8t}}DO++- $r#   c                 P    U R                  U5        U R                  R                  $ )zChecks to see if the operation is complete.

Args:
    retry (google.api_core.retry.Retry): (Optional) How to retry the RPC.

Returns:
    bool: True if the operation is complete, False otherwise.
)rC   r   r7   rB   s     r!   r7   Operation.done   s"     	  '###r#   c                 P    U R                  5       (       a  gU R                  5         g)zAttempt to cancel the operation.

Returns:
    bool: True if the cancel RPC was made, False if the operation is
        already complete.
FT)r7   r   r%   s    r!   r   Operation.cancel   s     99;;r#   c                     U R                  5         U R                  R                  S5      =(       a1    U R                  R                  R                  [
        R                  :H  $ )z$True if the operation was cancelled.r3   )rC   r   r)   r3   r;   r   	CANCELLEDr%   s    r!   	cancelledOperation.cancelled   sI      "OO$$W- A%%**h.@.@@	
r#   )r   r   r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   DEFAULT_POLLINGr   propertyr   r(   classmethodr/   r   rC   r7   r   rK   __static_attributes____classcell__)r    s   @r!   r
   r
   2   sv    8 ''*(   
 
 	< 	<.B
.
$
 
r#   r
   c                     SR                  U5      nUb  U" U 5      n U " SUS9n[        R                  " U[        R                  " 5       5      $ )a  Refresh an operation using a JSON/HTTP client.

Args:
    api_request (Callable): A callable used to make an API request. This
        should generally be
        :meth:`google.cloud._http.Connection.api_request`.
    operation_name (str): The name of the operation.
    retry (google.api_core.retry.Retry): (Optional) retry policy

Returns:
    google.longrunning.operations_pb2.Operation: The operation.
zoperations/{}GETmethodpath)formatr   	ParseDictr   r
   )api_requestoperation_namerA   r\   api_responses        r!   _refresh_httprb      sL     !!.1DK(e$7L  ~/G/G/IJJr#   c                 4    SR                  U5      nU " SUS9  g)a
  Cancel an operation using a JSON/HTTP client.

Args:
    api_request (Callable): A callable used to make an API request. This
        should generally be
        :meth:`google.cloud._http.Connection.api_request`.
    operation_name (str): The name of the operation.
zoperations/{}:cancelPOSTrZ   N)r]   )r_   r`   r\   s      r!   _cancel_httpre      s     "((8DvD)r#   c                    [         R                  " U [        R                  " 5       5      n[        R
                  " [        XR                  5      n[        R
                  " [        XR                  5      n[        XEXb40 UD6$ )a  Create an operation future using a HTTP/JSON client.

This interacts with the long-running operations `service`_ (specific
to a given API) via `HTTP/JSON`_.

.. _HTTP/JSON: https://cloud.google.com/speech/reference/rest/            v1beta1/operations#Operation

Args:
    operation (dict): Operation as a dictionary.
    api_request (Callable): A callable used to make an API request. This
        should generally be
        :meth:`google.cloud._http.Connection.api_request`.
    result_type (:func:`type`): The protobuf result type.
    kwargs: Keyword args passed into the :class:`Operation` constructor.

Returns:
    ~.api_core.operation.Operation: The operation future to track the given
        operation.
)	r   r^   r   r
   	functoolspartialrb   namere   )r   r_   r   r   operation_protor   r   s          r!   from_http_jsonrk      sa    * "++I~7O7O7QRO{<P<PQG|[:N:NOF_vMfMMr#   c                 h    [         R                  " US9nU R                  nUb  U" U5      nU" U5      $ )a[  Refresh an operation using a gRPC client.

Args:
    operations_stub (google.longrunning.operations_pb2.OperationsStub):
        The gRPC operations stub.
    operation_name (str): The name of the operation.
    retry (google.api_core.retry.Retry): (Optional) retry policy

Returns:
    google.longrunning.operations_pb2.Operation: The operation.
ri   )r   GetOperationRequestGetOperation)operations_stubr`   rA   
request_pbrpcs        r!   _refresh_grpcrs     s8      33HJ

&
&CCjz?r#   c                 N    [         R                  " US9nU R                  U5        g)zCancel an operation using a gRPC client.

Args:
    operations_stub (google.longrunning.operations_pb2.OperationsStub):
        The gRPC operations stub.
    operation_name (str): The name of the operation.
rm   N)r   CancelOperationRequestCancelOperation)rp   r`   rq   s      r!   _cancel_grpcrw     s"      66NKJ##J/r#   c                     [         R                  " [        UU R                  US9n[         R                  " [        UU R                  US9n[        XXb40 UD6$ )aH  Create an operation future using a gRPC client.

This interacts with the long-running operations `service`_ (specific
to a given API) via gRPC.

.. _service: https://github.com/googleapis/googleapis/blob/                 050400df0fdb16f63b63e9dee53819044bffc857/                 google/longrunning/operations.proto#L38

Args:
    operation (google.longrunning.operations_pb2.Operation): The operation.
    operations_stub (google.longrunning.operations_pb2.OperationsStub):
        The operations stub.
    result_type (:func:`type`): The protobuf result type.
    grpc_metadata (Optional[List[Tuple[str, str]]]): Additional metadata to pass
        to the rpc.
    kwargs: Keyword args passed into the :class:`Operation` constructor.

Returns:
    ~.api_core.operation.Operation: The operation future to track the given
        operation.
r(   )rg   rh   rs   ri   rw   r
   )r   rp   r   grpc_metadatar   r   r   s          r!   	from_grpcr{   &  sZ    . 	G 	F YGGGr#   c                     [         R                  " UR                  U R                  US9n[         R                  " UR                  U R                  US9n[        XXb40 UD6$ )aT  Create an operation future from a gapic client.

This interacts with the long-running operations `service`_ (specific
to a given API) via a gapic client.

.. _service: https://github.com/googleapis/googleapis/blob/                 050400df0fdb16f63b63e9dee53819044bffc857/                 google/longrunning/operations.proto#L38

Args:
    operation (google.longrunning.operations_pb2.Operation): The operation.
    operations_client (google.api_core.operations_v1.OperationsClient):
        The operations client.
    result_type (:func:`type`): The protobuf result type.
    grpc_metadata (Optional[List[Tuple[str, str]]]): Additional metadata to pass
        to the rpc.
    kwargs: Keyword args passed into the :class:`Operation` constructor.

Returns:
    ~.api_core.operation.Operation: The operation future to track the given
        operation.
ry   )rg   rh   get_operationri   cancel_operationr
   )r   operations_clientr   rz   r   r   r   s          r!   
from_gapicr   L  s`    . ''G
 **F
 YGGGr#   rM   )rR   rg   r   google.api_corer   r   google.api_core.futurer   google.longrunningr   cloudsdk.google.protobufr   
google.rpcr   PollingFuturer
   rb   re   rk   rs   rw   r{   r   r   r#   r!   <module>r      sa   0   & , * - 0 R
%% R
jK,
*N6*	0#HL!Hr#   