o
    ABiYe                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlZd dl	m
Z
 d dlmZmZ d dlmZ d dlmZmZmZ ejZeeZdZi d	d d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+iZd,Ze
je
je
je
je
je
j e
j!e
j"fZ#e
jfZ$e%d-d.d/gZ&G d0d1 d1e'Z(G d2d3 d3eZ)G d4d5 d5e'Z*G d6d7 d7e'Z+d8d9 Z,d:d; Z-d<d= Z.d>d? Z/G d@dA dAe'Z0dS )B    )annotationsN)Enum)
exceptions)BackgroundConsumerResumableBidiRpc)_helpers)ListenRequestTargetTargetChangeiyP  OK	CANCELLED   UNKNOWN   INVALID_ARGUMENT   DEADLINE_EXCEEDED   	NOT_FOUND   ALREADY_EXISTS   PERMISSION_DENIED   UNAUTHENTICATED   RESOURCE_EXHAUSTED   FAILED_PRECONDITION	   ABORTED
   OUT_OF_RANGE   UNIMPLEMENTED   INTERNAL   UNAVAILABLE   	DATA_LOSS   
DO_NOT_USEzThread-OnRpcTerminatedDocTreeEntryvalueindexc                   @  sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )WatchDocTreec                 C  s   i | _ d| _d S )Nr   )_dict_indexself r6   _/var/www/html/Trade-python/venv/lib/python3.10/site-packages/google/cloud/firestore_v1/watch.py__init__O   s   
zWatchDocTree.__init__c                 C  s   t | j S N)listr2   keysr4   r6   r6   r7   r;   S   s   zWatchDocTree.keysc                 C  s"   t  }| j |_| j|_|} | S r9   )r1   r2   copyr3   )r5   wdtr6   r6   r7   _copyV   s
   zWatchDocTree._copyc                 C  s,   |   } t|| j| j|< |  jd7  _| S )Nr   )r>   r.   r3   r2   )r5   keyr/   r6   r6   r7   insert]   s   zWatchDocTree.insertc                 C  s
   | j | S r9   r2   r5   r?   r6   r6   r7   findc      
zWatchDocTree.findc                 C  s   |   } | j|= | S r9   )r>   r2   rB   r6   r6   r7   removef   s   zWatchDocTree.removec                 c  s    | j D ]}|V  qd S r9   rA   r5   kr6   r6   r7   __iter__k   s   
zWatchDocTree.__iter__c                 C  s
   t | jS r9   )lenr2   r4   r6   r6   r7   __len__o   rD   zWatchDocTree.__len__c                 C  s
   || j v S r9   rA   rF   r6   r6   r7   __contains__r   rD   zWatchDocTree.__contains__N)__name__
__module____qualname__r8   r;   r>   r@   rC   rE   rH   rJ   rK   r6   r6   r6   r7   r1   K   s    r1   c                   @  s   e Zd ZdZdZdZdS )
ChangeTyper   r   r   N)rL   rM   rN   ADDEDREMOVEDMODIFIEDr6   r6   r6   r7   rO   v   s    rO   c                   @     e Zd Zdd ZdS )DocumentChangec                 C  s   || _ || _|| _|| _dS )zDocumentChange

        Args:
            type (ChangeType):
            document (document.DocumentSnapshot):
            old_index (int):
            new_index (int):
        N)typedocument	old_index	new_index)r5   rU   rV   rW   rX   r6   r6   r7   r8   }   s   

zDocumentChange.__init__NrL   rM   rN   r8   r6   r6   r6   r7   rT   |       rT   c                   @  rS   )WatchResultc                 C  s   || _ || _|| _d S r9   )snapshotnamechange_type)r5   r\   r]   r^   r6   r6   r7   r8      s   
zWatchResult.__init__NrY   r6   r6   r6   r7   r[      rZ   r[   c                 C  s   t | tjrt| S | S )z(Wraps a gRPC exception class, if needed.)
isinstancegrpcRpcErrorr   from_grpc_error)	exceptionr6   r6   r7   _maybe_wrap_exception   s   
rd   c                 C  s   | |ksJ ddS )Nz+Document watches only support one document.r   r6   )doc1doc2r6   r6   r7   document_watch_comparator   s   rg   c                 C     t | }t|tS r9   )rd   r_   _RECOVERABLE_STREAM_EXCEPTIONSrc   wrappedr6   r6   r7   _should_recover      
rl   c                 C  rh   r9   )rd   r_   _TERMINATING_STREAM_EXCEPTIONSrj   r6   r6   r7   _should_terminate   rm   ro   c                
   @  s   e Zd Zdd Zdd Zedd Zedd Zd	d
 Zdd Z	e
dd Zd.ddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZejeejeejeejeejeiZd d! Zd"d# Zd$d% Zed&d' Zd(d) Z d*d+ Z!d,d- Z"dS )/Watchc                 C  sz   || _ || _|| _|| _|| _|| _|j| _t	 | _
d| _| |j d| _t | _i | _i | _d| _d| _|   dS )a  
        Args:
            firestore:
            target:
            comparator:
            snapshot_callback: Callback method to process snapshots.
                Args:
                    docs (List(DocumentSnapshot)): A callback that returns the
                        ordered list of documents stored in this snapshot.
                    changes (List(str)): A callback that returns the list of
                        changed documents since the last snapshot delivered for
                        this watch.
                    read_time (string): The ISO 8601 time at which this
                        snapshot was obtained.

            document_snapshot_cls: factory for instances of DocumentSnapshot
        FN)_document_reference
_firestore_targets_comparator_document_snapshot_cls_snapshot_callback_firestore_api_api	threadingLock_closing_closed_set_documents_pfx_database_stringresume_tokenr1   doc_treedoc_map
change_mapcurrent
has_pushed_init_stream)r5   document_reference	firestoretarget
comparatorsnapshot_callbackdocument_snapshot_clsr6   r6   r7   r8      s"   
zWatch.__init__c                 C  sP   | j }t| jjjtt|| jjd| _	| j	
| j t| j	| j| _| j  d S )N)	start_rpcshould_recovershould_terminateinitial_requestmetadata)_get_rpc_requestr   rx   
_transportlistenrl   ro   rr   _rpc_metadata_rpcadd_done_callback_on_rpc_doner   on_snapshot	_consumerstart)r5   rpc_requestr6   r6   r7   r      s   zWatch._init_streamc                 C  s"   | ||j d|jgitdt||S )a  
        Creates a watch snapshot listener for a document. snapshot_callback
        receives a DocumentChange object, but may also start to get
        targetChange and such soon

        Args:
            document_ref: Reference to Document
            snapshot_callback: callback to be called on snapshot
            document_snapshot_cls: class to make snapshots with
            reference_class_instance: class make references

        	documents)r   	target_id)_client_document_pathWATCH_TARGET_IDrg   )clsdocument_refr   r   r6   r6   r7   for_document   s   
zWatch.for_documentc                 C  s>   |j  \}}tj|| d}| ||j|jtd|j||S )N)parentstructured_query)queryr   )	_parent_parent_infor	   QueryTarget_to_protobufr   _pbr   rt   )r   r   r   r   parent_path_query_targetr6   r6   r7   	for_query  s   
zWatch.for_queryc                 C  s8   | j d ur| j | jd< n| jdd  t| jj| jdS )Nr   )database
add_target)r   rs   popr   rr   r~   r4   r6   r6   r7   r   +  s   

zWatch._get_rpc_requestc                 C  s   | d| _ t| j | _d S )Nz/documents/)_documents_pfxrI   _documents_pfx_len)r5   database_stringr6   r6   r7   r}   5  s   zWatch._set_documents_pfxc                 C  s   | j duo| j jS )zbool: True if this manager is actively streaming.

        Note that ``False`` does not indicate this is complete shut down,
        just that it stopped getting new messages.
        N)r   	is_activer4   r6   r6   r7   r   9  s   zWatch.is_activeNc                 C  s   | j I | jr	 W d   dS | jr$| jr td | j  d| j_d| _d| _| j	r:| j	
  d| j	_g | j	_d| _	d| _td W d   n1 sOw   Y  |rhtd|  t|trd|t|dS )a  Stop consuming messages and shutdown all helper threads.

        This method is idempotent. Additional calls will have no effect.

        Args:
            reason (Any): The reason to close this. If None, this is considered
                an "intentional" shutdown.
        NzStopping consumer.TzFinished stopping manager.zreason for closing: %s)r{   r|   r   r   _LOGGERdebugstop_on_responserv   r   close_initial_request
_callbacksr_   	ExceptionRuntimeError)r5   reasonr6   r6   r7   r   B  s2   	



zWatch.closec                 C  s:   t d t|}tjt| jd|id}d|_|  dS )a
  Triggered whenever the underlying RPC terminates without recovery.

        This is typically triggered from one of two threads: the background
        consumer thread (when calling ``recv()`` produces a non-recoverable
        error) or the grpc management thread (when cancelling the RPC).

        This method is *non-blocking*. It will start another thread to deal
        with shutting everything down. This is to prevent blocking in the
        background consumer and preventing it from being ``joined()``.
        z.RPC termination has signaled manager shutdown.r   )r]   r   kwargsTN)	r   inford   ry   Thread_RPC_ERROR_THREAD_NAMEr   daemonr   )r5   futurethreadr6   r6   r7   r   g  s   
zWatch._on_rpc_donec                 C  s   |    d S r9   )r   r4   r6   r6   r7   unsubscribez  s   zWatch.unsubscribec                 C  sR   t d |jd u pt|jdk}|r#|jr%| jr'| |j|j d S d S d S d S )Nz%on_snapshot: target change: NO_CHANGEr   )r   r   
target_idsrI   	read_timer   pushr   )r5   target_changeno_target_idsr6   r6   r7   $_on_snapshot_target_change_no_change}  s   
z*Watch._on_snapshot_target_change_no_changec                 C  s,   t d |jd }|tkrtd| d S )Nzon_snapshot: target change: ADDr   z&Unexpected target ID %s sent by server)r   r   r   r   r   )r5   r   r   r6   r6   r7   _on_snapshot_target_change_add  s
   

z$Watch._on_snapshot_target_change_addc                 C  sJ   t d |jjr|jj}|jj}nd}d}d||f }t|t||)Nz"on_snapshot: target change: REMOVEr'   zinternal errorzError %s:  %s)r   r   causecodemessager   r   from_grpc_status)r5   r   r   r   error_messager6   r6   r7   !_on_snapshot_target_change_remove  s   


z'Watch._on_snapshot_target_change_removec                 C  s   t d |   d S )Nz!on_snapshot: target change: RESET)r   r   _reset_docsr5   r   r6   r6   r7    _on_snapshot_target_change_reset  s   
z&Watch._on_snapshot_target_change_resetc                 C  s   t d d| _d S )Nz#on_snapshot: target change: CURRENTT)r   r   r   r   r6   r6   r7   "_on_snapshot_target_change_current  s   

z(Watch._on_snapshot_target_change_currentc                 C  s   | | jr|| jd  }|S r9   )
startswithr   r   )r5   document_namer6   r6   r7   _strip_document_pfx  s   zWatch._strip_document_pfxc              
   C  sl  | j  rdS |du r|   dS |j}|d}|dkri|jj}td|  | j	
|}|du rJd| }td|  | jt|d dS z	|| |j W dS  tyh } z	td|   d}~ww |d	krtd
 t|jjv }t|jjv }	|jj}
|rtd t|
j| j}| |
j}| j|}| j||dd|
j|
jd}|| j|
j< dS |	rtd tj| j|
j< dS dS |dkrtd |jj}tj| j|< dS |dkrtd |j j}tj| j|< dS |dkr"td |j!j"| # kr td t$j%t&| jd}|'  |(  | )  | *  dS dS td d| }| jt|d dS )aS  Process a response from the bi-directional gRPC stream.

        Collect changes and push the changes in a batch to the customer
        when we receive 'current' from the listen response.

        Args:
            proto(`google.cloud.firestore_v1.types.ListenResponse`):
                Callback method that receives a object to
        Nresponse_typer   zon_snapshot: target change: zUnknown target change type: zon_snapshot: )r   zmeth(proto) exc: document_changezon_snapshot: document changez%on_snapshot: document change: CHANGEDT)	referencedataexistsr   create_timeupdate_timez%on_snapshot: document change: REMOVEDdocument_deletez$on_snapshot: document change: DELETEdocument_removez$on_snapshot: document change: REMOVEfilterzon_snapshot: filter updatez%Filter mismatch -- restarting stream.)r]   r   zUNKNOWN TYPE. UHOHzUnknown listen response type: )+r{   lockedr   r   
WhichOneofr   target_change_typer   r   _target_changetype_dispatchgetr   
ValueErrorr   r   r   r   removed_target_idsrV   r   decode_dictfieldsrr   r   r]   ru   r   r   r   rO   rQ   r   r   r   count_current_sizery   r   r   r   joinr   r   )r5   protopbwhichr   methr   exc2changedremovedrV   r   r   r   r\   r]   r   r6   r6   r7   r     s   













zWatch.on_snapshotc                 C  s   |  | j| j|\}}}| | j| j|||\}}}| jr!t|r9t| j	}	t
| |	d}
| |
|| d| _|| _|| _| j  || _dS )zInvoke the callback with a new snapshot

        Build the sntapshot from the current set of changes.

        Clear the current changes on completion.
        r?   TN)_extract_changesr   r   _compute_snapshotr   r   rI   	functools
cmp_to_keyrt   sortedr;   rv   clearr   )r5   r   next_resume_tokendeletesaddsupdatesupdated_treeupdated_mapappliedChangesr?   r;   r6   r6   r7   r   '  s   




z
Watch.pushc                 C  s   g }g }g }|  D ]0\}}|tjkr|| v r|| q
|| v r.|d ur(||_|| q
|d ur5||_|| q
|||fS r9   )itemsrO   rQ   appendr   )r   changesr   r  r  r  r]   r/   r6   r6   r7   r   D  s    


zWatch._extract_changesc                   s  |}|}t |t |ksJ ddd dd   fdd}g }	t| j}
t|}|D ]}|||\}}}|	| q-t||
d}td	 |D ]}td
  |||\}}}|	| qKt||
d}|D ]}||||\}}}|d ur}|	| qit |t |ksJ d|||	fS )NzJThe document tree and document map should have the same number of entries.c                 S  sP   | |v sJ d| | }||}|j}||}|| = ttj||d||fS )z
            Applies a document delete to the document tree and document map.
            Returns the corresponding DocumentChange event.
            z!Document to delete does not existr-   )r   rC   r0   rE   rT   rO   rQ   )r]   r  r  old_documentexistingrW   r6   r6   r7   
delete_docd  s   


z+Watch._compute_snapshot.<locals>.delete_docc                 S  sN   | j j}||vsJ d|| d}|| j}| ||< ttj| d|||fS )z
            Applies a document add to the document tree and the document map.
            Returns the corresponding DocumentChange event.
            zDocument to add already existsNr-   )r   r   r@   rC   r0   rT   rO   rP   )new_documentr  r  r]   rX   r6   r6   r7   add_docv  s   z(Watch._compute_snapshot.<locals>.add_docc                   sv   | j j}||v sJ d||}|j| jkr6|||\}}} | ||\}}}ttj| |j|j||fS d||fS )z
            Applies a document modification to the document tree and the
            document map.
            Returns the DocumentChange event for successful modifications.
            z!Document to modify does not existN)	r   r   r   r   rT   rO   rR   rW   rX   )r  r  r  r]   r  remove_change
add_changer  r  r6   r7   
modify_doc  s(   



z+Watch._compute_snapshot.<locals>.modify_docr   zwalk over add_changeszin add_changeszQThe update document tree and document map should have the same number of entries.)rI   r   r   rt   r   r	  r   r   )r5   r   r   delete_changesadd_changesupdate_changesr  r  r  r  r?   r]   changer\   r6   r  r7   r   Y  sH   !






zWatch._compute_snapshotc                 C  s2   |  | j| jd\}}}t| jt| t| S )zsReturn the current count of all documents.

        Count includes the changes from the current changeMap.
        N)r   r   r   rI   )r5   r  r  r   r6   r6   r7   r     s   zWatch._current_sizec                 C  sH   t d | j  d| _| j D ]}|jj}t	j
| j|< qd| _dS )zG
        Helper to clear the docs on RESET or filter mismatch.
        zresetting documentsNF)r   r   r   r   r   r   r;   r   r   rO   rQ   r   )r5   r\   r]   r6   r6   r7   r     s   


zWatch._reset_docsr9   )#rL   rM   rN   r8   r   classmethodr   r   r   r}   propertyr   r   r   r   r   r   r   r   r   TargetChangeType	NO_CHANGEADDREMOVERESETCURRENTr   r   r   r   staticmethodr   r   r   r   r6   r6   r6   r7   rp      sB    >




%q
qrp   )1
__future__r   collectionsr   loggingry   enumr   r`   google.api_corer   google.api_core.bidir   r   google.cloud.firestore_v1r   )google.cloud.firestore_v1.types.firestorer   r	   r
   r  	getLoggerrL   r   r   GRPC_STATUS_CODEr   Aborted	CancelledUnknownDeadlineExceededResourceExhaustedInternalServerErrorServiceUnavailableUnauthenticatedri   rn   
namedtupler.   objectr1   rO   rT   r[   rd   rg   rl   ro   rp   r6   r6   r6   r7   <module>   s   
	

+