o
    •ºé`ˆ
  ã                   @   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   ú3/usr/lib/python3/dist-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    #
