o
    ˷e                     @   s>  U d Z ddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZ eed< eed< eed< eed< eed	< eed
< eed< eed< eed< eed< dZ	 dZeed< dZeed< edZeed< edZeed< dZeed< dZeed
< 	 dZeed< 	 dZeed< 	 dZeed	< dZeed< G dd deZG dd deZdS )zEHelpers for applying Google Cloud Firestore changes in a transaction.    )retry)types)Any	CoroutineNoReturnOptionalUnion_CANT_BEGIN_CANT_COMMIT_CANT_RETRY_READ_ONLY_CANT_ROLLBACK_EXCEED_ATTEMPTS_TEMPLATE_INITIAL_SLEEP
_MAX_SLEEP_MISSING_ID_TEMPLATE_MULTIPLIER_WRITE_READ_ONLY   z@The transaction has already begun. Current transaction ID: {!r}.z:The transaction has no transaction ID, so it cannot be {}.zrolled back	committedz8Cannot perform write operation in read-only transaction.g      ?g      >@g       @z.Failed to commit transaction in {:d} attempts.z,Only read-write transactions can be retried.c                	   @   s   e Zd ZdZedfdddZdefddZd	ee	df de
ejj fd
dZedd Zedd ZdddZd defddZdefddZdeeeeeef f fddZ		d!dedejdedefddZ		d!dejdedefddZdS )"BaseTransactiona  Accumulate read-and-write operations to be sent in a transaction.

    Args:
        max_attempts (Optional[int]): The maximum number of attempts for
            the transaction (i.e. allowing retries). Defaults to
            :attr:`~google.cloud.firestore_v1.transaction.MAX_ATTEMPTS`.
        read_only (Optional[bool]): Flag indicating if the transaction
            should be read-only or should allow writes. Defaults to
            :data:`False`.
    FreturnNc                 C   s   || _ || _d | _d S N)_max_attempts
_read_only_id)selfmax_attempts	read_only r   a/var/www/ideatree/venv/lib/python3.10/site-packages/google/cloud/firestore_v1/base_transaction.py__init__?   s   
zBaseTransaction.__init__c                 C      t r   NotImplementedError)r   	write_pbsr   r   r   _add_write_pbsD      zBaseTransaction._add_write_pbsretry_idc                 C   sH   |dur| j rtttjtjj|ddS | j r"tjtj dS dS )a  Convert the current object to protobuf.

        The ``retry_id`` value is used when retrying a transaction that
        failed (e.g. due to contention). It is intended to be the "first"
        transaction that failed (i.e. if multiple retries are needed).

        Args:
            retry_id (Union[bytes, NoneType]): Transaction ID of a transaction
                to be retried.

        Returns:
            Optional[google.cloud.firestore_v1.types.TransactionOptions]:
            The protobuf ``TransactionOptions`` if ``read_only==True`` or if
            there is a transaction ID to be retried, else :data:`None`.

        Raises:
            ValueError: If ``retry_id`` is not :data:`None` but the
                transaction is read-only.
        N)retry_transaction)
read_write)r   )r   
ValueErrorr   r   TransactionOptions	ReadWriteReadOnlyr   r'   r   r   r   _options_protobufG   s   z!BaseTransaction._options_protobufc                 C   s
   | j duS )zDetermine if this transaction has already begun.

        Returns:
            bool: Indicates if the transaction has started.
        Nr   r   r   r   r   in_progressm   s   
zBaseTransaction.in_progressc                 C   s   | j S )zGet the current transaction ID.

        Returns:
            Optional[bytes]: The transaction ID (or :data:`None` if the
            current transaction is not in progress).
        r0   r1   r   r   r   idv   s   zBaseTransaction.idc                 C   s   g | _ d| _dS )zClean up the instance after :meth:`_rollback`` or :meth:`_commit``.

        This intended to occur on success or failure of the associated RPCs.
        N)
_write_pbsr   r1   r   r   r   	_clean_up   s   
zBaseTransaction._clean_upc                 C   r!   r   r"   r.   r   r   r   _begin   r&   zBaseTransaction._beginc                 C   r!   r   r"   r1   r   r   r   	_rollback   r&   zBaseTransaction._rollbackc                 C   r!   r   r"   r1   r   r   r   _commit   r&   zBaseTransaction._commit
referencesr   timeoutc                 C   r!   r   r"   )r   r9   r   r:   r   r   r   get_all      zBaseTransaction.get_allc                 C   r!   r   r"   )r   ref_or_queryr   r:   r   r   r   get   r<   zBaseTransaction.getr   Nr   )NN)__name__
__module____qualname____doc__MAX_ATTEMPTSr    r   r%   r   bytesr   r   commonr+   r/   propertyr2   r3   r5   r6   r7   listr   r   r8   retriesRetryfloatr;   r>   r   r   r   r   r   3   sJ    


&


	 
r   c                   @   sH   e Zd ZdZdddZdddZdefdd	Zdefd
dZdd Z	dS )_BaseTransactionalaY  Provide a callable object to use as a transactional decorater.

    This is surfaced via
    :func:`~google.cloud.firestore_v1.transaction.transactional`.

    Args:
        to_wrap (Callable[[:class:`~google.cloud.firestore_v1.transaction.Transaction`, ...], Any]):
            A callable that should be run (and retried) in a transaction.
    r   Nc                 C   s   || _ d | _	 d | _d S r   )to_wrap
current_idr'   )r   rM   r   r   r   r       s
   z_BaseTransactional.__init__c                 C   s   d| _ d| _dS )zUnset the transaction IDs.N)rN   r'   r1   r   r   r   _reset   s   
z_BaseTransactional._resetc                 O   r!   r   r"   r   transactionargskwargsr   r   r   _pre_commit   r&   z_BaseTransactional._pre_commitc                 C   r!   r   r"   )r   rQ   r   r   r   _maybe_commit   r&   z _BaseTransactional._maybe_commitc                 O   r!   r   r"   rP   r   r   r   __call__   r&   z_BaseTransactional.__call__r?   )
r@   rA   rB   rC   r    rO   r   rT   rU   rV   r   r   r   r   rL      s    


rL   N)rC   google.api_corer   rI   google.cloud.firestore_v1r   typingr   r   r   r   r   str__annotations__rK   rD   r	   r   formatr   r
   r   r   r   r   r   r   objectr   rL   r   r   r   r   <module>   s>   o