o
    Uh
                     @   s|   d dl mZ d dlmZ d dlmZmZmZmZ G dd dZ	G dd dZ
G dd	 d	ZG d
d deZdefddZdS )    )contextmanager)count)HeaderFieldsMessageMessageFlagMessageTypec                   @   s   e Zd Zdd Zdd ZdS )MessageFiltersc                 C   s   i | _ t | _d S N)filtersr   
filter_idsself r   e/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/jeepney/io/common.py__init__   s   zMessageFilters.__init__c                 c   s(    | j  D ]}|j|r|V  qd S r	   )r
   valuesrulematches)r   messagehandler   r   r   r      s   zMessageFilters.matchesN)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   s2   e Zd ZdefddZdd Zdd Zdd	 Zd
S )FilterHandler
   c                 C   s0   || _ t|j| _|| _|| _| | j j| j< d S r	   )_filtersnextr   
_filter_idr   queuer
   )r   r
   r   r   r   r   r   r      s
   zFilterHandle.__init__c                 C   s   | j j| j= d S r	   )r   r
   r   r   r   r   r   close   s   zFilterHandle.closec                 C   s   | j S r	   )r   r   r   r   r   	__enter__   s   zFilterHandle.__enter__c                 C   s   |    dS )NF)r   )r   exc_typeexc_valexc_tbr   r   r   __exit__    s   zFilterHandle.__exit__N)r   r   r   r   r   r   r   r#   r   r   r   r   r      s
    r   c                   @   s8   e Zd Zdd Zedd Zdd Zddefd	d
ZdS )ReplyMatcherc                 C   s
   i | _ d S r	   _futuresr   r   r   r   r   &   s   
zReplyMatcher.__init__c                 c   s,    || j |< z
|V  W | j |= dS | j |= w )z>Context manager to capture a reply for the given serial numberNr%   )r   serialfuturer   r   r   catch)   s
   
zReplyMatcher.catchc                 C   s4   |j jtjd}|| jv r| j| | dS dS )zDispatch an incoming message which may be a reply

        Returns True if a task was waiting for it, otherwise False.
        TF)headerfieldsgetr   reply_serialr&   
set_result)r   msg
rep_serialr   r   r   dispatch3   s
   
zReplyMatcher.dispatchNexcc                 C   s:   |du rt d}| ji }| _| D ]}|| qdS )z4Throw an error in any task still waiting for a replyNz(D-Bus router closed before reply arrived)RouterClosedr&   r   set_exception)r   r3   futuresfutr   r   r   drop_all?   s   zReplyMatcher.drop_allr	   )	r   r   r   r   r   r)   r2   	Exceptionr8   r   r   r   r   r$   %   s    
	r$   c                   @   s   e Zd ZdZdS )r4   a  Raised in tasks waiting for a reply when the router is closed

    This will also be raised if the receiver task crashes, so tasks are not
    stuck waiting for a reply that can never come. The router object will not
    be usable after this is raised.
    N)r   r   r   __doc__r   r   r   r   r4   H   s    r4   r0   c                 C   s<   | j jtjkrtd| j j dtj| j j@ rtddS )z4Raise an error if we wouldn't expect a reply for msgz,Only method call messages have replies (not )z/This message has the no_reply_expected flag setN)	r+   message_typer   method_call	TypeErrorr   no_reply_expectedflags
ValueError)r0   r   r   r   check_replyableR   s   
rB   N)
contextlibr   	itertoolsr   jeepneyr   r   r   r   r   r   r$   r9   r4   rB   r   r   r   r   <module>   s    #
