
    j	i3                         d Z ddl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ddZd Zd ZddZd ZddZddZdS )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                        e Zd ZdZdej        f fd	Zed             Zed             Z	e
d             Zd ZddZdd	Zd
 Zd Z xZS )	OperationaH  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                      t          t          |           j        dd|i| || _        || _        || _        || _        || _        t          j	                    | _
        |                                  d S )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	           Q/srv/django_bis/venv311/lib/python3.11/site-packages/google/api_core/operation.pyr   zOperation.__init__I   sz     	(i'BBB6BBB#'+ ) 0 0'')))))    c                     | j         S )zAgoogle.longrunning.Operation: The current long-running operation.)r   r   s    r    r   zOperation.operation]   s     r!   c                     | j                             d          sdS t          j        | j        | j         j                  S )z8google.protobuf.Message: the current operation metadata.metadataN)r   HasFieldr   from_any_pbr   r%   r#   s    r    r%   zOperation.metadatab   sC     ''
33 	4+!9
 
 	
r!   c                 @    t           j                            |          S )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    deserializezOperation.deserializel   s     '227;;;r!   c                 n   | j         5  | j        j        r| j        r	 ddd           dS | j                            d          r:t          j        | j        | j        j                  }| 	                    |           n| j                            d          r\t          j        | j        j        j        | j        j        j        | j        j        f| j                  }|                     |           n)t          j        d          }|                     |           ddd           dS # 1 swxY w Y   dS )zASet the result or exception from the operation if it is complete.Nresponseerror)status_codemessageerrorsr-   zLUnexpected state: Long-running operation had neither response nor error set.)r   r   done_result_setr&   r   r'   r   r-   
set_resultr   from_grpc_statusr.   coder0   set_exceptionGoogleAPICallError)r   r-   	exceptions      r    r   z$Operation._set_result_from_operationx   s   
 " 	. 	.
 ?' 4+; 	. 	. 	. 	. 	. 	. 	. 	. ''
33 .+7%t'?  ))))))'22 .&7 $ 5 : O19 O13!_	  	 ""9----&9. 	 ""9---5	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s   D*C3D**D.1D.c                     | j         j        sG|r|                     |          n|                                 | _         |                                  dS dS )zRefresh the operation and update the result if needed.

        Args:
            retry (google.api_core.retry.Retry): (Optional) How to retry the RPC.
        )retryN)r   r2   r   r   r   r;   s     r    _refresh_and_updatezOperation._refresh_and_update   sZ     # 	.<AVdmm%m888t}}DO++-----	. 	.r!   c                 D    |                      |           | j        j        S )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.
        )r=   r   r2   r<   s     r    r2   zOperation.done   s#     	  '''##r!   c                 Z    |                                  rdS |                                  dS )zAttempt to cancel the operation.

        Returns:
            bool: True if the cancel RPC was made, False if the operation is
                already complete.
        FT)r2   r   r#   s    r    r   zOperation.cancel   s+     99;; 	5tr!   c                     |                                   | j                            d          o| j        j        j        t
          j        k    S )z$True if the operation was cancelled.r.   )r=   r   r&   r.   r6   r   	CANCELLEDr#   s    r    	cancelledzOperation.cancelled   sE      """O$$W-- A%*h.@@	
r!   N)__name__
__module____qualname____doc__r   DEFAULT_POLLINGr   propertyr   r%   classmethodr+   r   r=   r2   r   rB   __classcell__)r   s   @r    r
   r
   2   s         8 '* * * * * *(   X 
 
 X
 	< 	< [	<. . .B
. 
. 
. 
.
$ 
$ 
$ 
$  
 
 
 
 
 
 
r!   r
   c                     d                     |          }| ||           }  | d|          }t          j        |t          j                              S )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/{}NGETmethodpath)formatr   	ParseDictr   r
   )api_requestoperation_namer;   rP   api_responses        r    _refresh_httprV      s\     !!.11DeK((;e$777L ~/G/I/IJJJr!   c                 J    d                     |          } | d|           dS )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POSTrN   N)rQ   )rS   rT   rP   s      r    _cancel_httprY      s2     "((88DKvD))))))r!   c                     t          j        | t          j                              }t	          j        t          ||j                  }t	          j        t          ||j                  }t          ||||fi |S )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   rR   r   r
   	functoolspartialrV   namerY   )r   rS   r   r   operation_protor   r   s          r    from_http_jsonr_      si    * "+I~7O7Q7QRRO{O<PQQG|[/:NOOF_gv{MMfMMMr!   c                 j    t          j        |          }| j        }| ||          } ||          S )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.
    r]   )r   GetOperationRequestGetOperation)operations_stubrT   r;   
request_pbrpcs        r    _refresh_grpcrg     s@      3HHHJ

&CeCjj3z??r!   c                 Z    t          j        |          }|                     |           dS )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.
    ra   N)r   CancelOperationRequestCancelOperation)rd   rT   re   s      r    _cancel_grpcrk     s1      6NKKKJ##J/////r!   c                     t          j        t          || j        |          }t          j        t          || j        |          }t          | |||fi |S )a  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%   )r[   r\   rg   r]   rk   r
   )r   rd   r   grpc_metadatar   r   r   s          r    	from_grpcro   &  sn    . 	  G 	  F YGGGGGr!   c                     t          j        |j        | j        |          }t          j        |j        | j        |          }t          | |||fi |S )a  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.
    rm   )r[   r\   get_operationr]   cancel_operationr
   )r   operations_clientr   rn   r   r   r   s          r    
from_gapicrt   L  sn    . '  G
 *  F
 YGGGGGr!   rC   )rG   r[   r   google.api_corer   r   google.api_core.futurer   google.longrunningr   google.protobufr   
google.rpcr   PollingFuturer
   rV   rY   r_   rg   rk   ro   rt   r   r!   r    <module>r{      sd   0         & & & & & & , , , , , , * * * * * * - - - - - - ' ' ' ' ' '      R
 R
 R
 R
 R
% R
 R
 R
jK K K K,
* 
* 
*N N N6   *	0 	0 	0#H #H #H #HL!H !H !H !H !H !Hr!   