o
    •ºé`Ê  ã                   @   sR  d dl mZmZmZ d dlm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mZ d d
lmZmZmZmZ d dlmZ d dlmZmZmZmZ d dl m!Z!m"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
ddd� G dd„ dƒZ-d"dd„Z.G dd„ dƒZ/G dd„ de!ƒZ0G dd„ dƒZ1d"d d!„Z2dS )#é    )Úas_completedÚFutureÚwait_for)ÚcountN)ÚOptional)Úwarn)ÚIOLoop)ÚIOStream)ÚEvent)ÚQueueÚ	QueueFull)Ú
SASLParserÚmake_auth_externalÚBEGINÚAuthenticationError)Úget_bus)ÚParserÚMessageTypeÚMessageÚMessageFlag)Ú	ProxyBaseÚ
unwrap_msg)ÚRouter)Úmessage_busé   )ÚMessageFiltersÚFilterHandleÚReplyMatcherÚRouterClosedÚcheck_replyablezsjeepney.io.tornado is deprecated. Tornado is now built on top of asyncio, so please use jeepney.io.asyncio instead.é   )Ú
stacklevelc                   @   sD   e Zd Zdefdd„Zddœdefdd„Zd	efd
d„Zdd„ ZdS )ÚDBusConnectionÚstreamc                 C   s$   || _ tƒ | _tdd�| _d | _d S )Nr   )Ústart)r#   r   Úparserr   Úoutgoing_serialÚunique_name)Úselfr#   © r)   ú4/usr/lib/python3/dist-packages/jeepney/io/tornado.pyÚ__init__   s   
zDBusConnection.__init__N©ÚserialÚmessagec                Ã   s0   �|d u r
t | jƒ}| j | |¡¡I d H  d S ©N)Únextr&   r#   ÚwriteÚ	serialise©r(   r.   r-   r)   r)   r*   Úsend!   s   €
zDBusConnection.sendÚreturnc                 Ã   s>   �	 | j  ¡ }|d ur|S | jjddd�I d H }| j  |¡ q)NTi   ©Úpartial)r%   Úget_next_messager#   Ú
read_bytesÚadd_data)r(   ÚmsgÚbr)   r)   r*   Úreceive'   s   €
úzDBusConnection.receivec                 C   ó   | j  ¡  d S r/   )r#   Úclose©r(   r)   r)   r*   r?   0   ó   zDBusConnection.close)	Ú__name__Ú
__module__Ú__qualname__r	   r+   r   r4   r=   r?   r)   r)   r)   r*   r"      s
    	r"   ÚSESSIONc                 Ã   sè   �t | ƒ}ttjtjd�ƒ}| |¡I d H  | dtƒ  ¡I d H  tƒ }|js?| 	|j
ddd�I d H ¡ |jr<t|jƒ‚|jr'| t¡I d H  t|ƒ}t|ƒ�}ttt|ƒ ¡ dƒI d H }|d |_W d   ƒ |S 1 smw   Y  |S )N)Úfamilyó    i   Tr6   é
   r   )r   r	   ÚsocketÚAF_UNIXÚconnectr1   r   r   ÚauthenticatedÚfeedr9   Úerrorr   r   r"   Ú
DBusRouterr   ÚProxyr   ÚHellor'   )ÚbusÚbus_addrr#   Úauth_parserÚconnÚrouterÚ
reply_bodyr)   r)   r*   Úopen_dbus_connection4   s(   €
ý

þürX   c                   @   s’   e Zd Zdefdd„Zddœdd„Zdd	„ Zdd
dœdee fdd„Z	dd„ Z
dd„ Zdd„ Zedd„ ƒZdefdd„Zdefdd„Zdd„ ZdS )rO   rU   c                 C   s<   || _ tƒ | _tƒ | _tƒ | _t ¡  	| j
¡ ttƒ| _d S r/   )rU   r   Ú_repliesr   Ú_filtersr
   Ú_stop_receivingr   ÚcurrentÚadd_callbackÚ	_receiverr   r   rV   )r(   rU   r)   r)   r*   r+   L   s   zDBusRouter.__init__Nr,   c                Ã   s   �| j j||d�I d H  d S )Nr,   )rU   r4   r3   r)   r)   r*   r4   V   s   €zDBusRouter.sendc                 Ã   sz   �t |ƒ | j ¡ rtdƒ‚t| jjƒ}| j |t	ƒ ¡�}| j
||d�I d H  |I d H W  d   ƒ S 1 s6w   Y  d S )NzThis DBusRouter has stoppedr,   )r   r[   Úis_setr   r0   rU   r&   rY   Úcatchr   r4   )r(   r.   r-   Ú	reply_futr)   r)   r*   Úsend_and_get_replyY   s   €
$þzDBusRouter.send_and_get_replyr   )ÚqueueÚbufsizerc   c                C   s   t | j||p	t|ƒƒS )aŒ  Create a filter for incoming messages

        Usage::

            with router.filter(rule) as queue:
                matching_msg = await queue.get()

        :param jeepney.MatchRule rule: Catch messages matching this rule
        :param tornado.queues.Queue queue: Matched messages will be added to this
        :param int bufsize: If no queue is passed in, create one with this size
        )r   rZ   r   )r(   Úrulerc   rd   r)   r)   r*   Úfilterd   s   zDBusRouter.filterc                 C   r>   r/   )r[   Úsetr@   r)   r)   r*   Ústopr   rA   zDBusRouter.stopc                 C   s   | S r/   r)   r@   r)   r)   r*   Ú	__enter__u   s   zDBusRouter.__enter__c                 C   s   |   ¡  dS )NF)rh   ©r(   Úexc_typeÚexc_valÚexc_tbr)   r)   r*   Ú__exit__x   s   zDBusRouter.__exit__c                 C   s   | j jS r/   )rU   r'   r@   r)   r)   r*   r'   ~   s   zDBusRouter.unique_namer.   c                 Ã   sF   �|j jtjkr|j jtj@ st|  |¡I d H ƒS |  	|¡I d H  d S r/   )
ÚheaderÚmessage_typer   Úmethod_returnÚflagsr   Úno_reply_expectedr   rb   r4   )r(   r.   r)   r)   r*   Úsend_message‚   s   €ÿzDBusRouter.send_messager;   c              	   C   sH   | j  |¡rdS | j |¡D ]}z|j |¡ W q ty!   Y qw dS )zHandle one received messageN)rY   ÚdispatchrZ   Úmatchesrc   Ú
put_nowaitr   )r(   r;   rf   r)   r)   r*   Ú	_dispatchŽ   s   ÿýzDBusRouter._dispatchc                 Ã   sx   �z1	 t | j ¡ | j ¡ gƒD ]"}|I dH }|du r& W d| _| j ¡  dS |  |¡ | j	 
|¡ qqd| _| j ¡  w )z'Receiver loop - runs in a separate taskTNF)r   rU   r=   r[   ÚwaitÚ
is_runningrY   Údrop_allrx   rV   Úincoming)r(   Úcoror;   r)   r)   r*   r^   ™   s   €

ûúzDBusRouter._receiver)rB   rC   rD   r"   r+   r4   rb   r   r   rf   rh   ri   rn   Úpropertyr'   r   rt   rx   r^   r)   r)   r)   r*   rO   K   s    

rO   c                       s2   e Zd Zdef‡ fdd„Zdd„ Zdd„ Z‡  ZS )rP   rV   c                    s   t ƒ  |¡ || _d S r/   )Úsuperr+   Ú_router)r(   ÚmsggenrV   ©Ú	__class__r)   r*   r+   ª   s   
zProxy.__init__c                 C   s   d  | j| j¡S )NzProxy({}, {}))ÚformatÚ_msggenr€   r@   r)   r)   r*   Ú__repr__®   s   zProxy.__repr__c                    s   ‡ ‡fdd„}|S )Nc                  Ÿ   s8   �ˆ | i |¤Ž}|j jtju sJ ‚tˆj |¡I d H ƒS r/   )ro   rp   r   Úmethod_callr   r€   rb   )ÚargsÚkwargsr;   ©Úmake_msgr(   r)   r*   Úinner²   s   €z!Proxy._method_call.<locals>.innerr)   )r(   r‹   rŒ   r)   rŠ   r*   Ú_method_call±   s   zProxy._method_call)rB   rC   rD   rO   r+   r†   r�   Ú__classcell__r)   r)   r‚   r*   rP   ©   s    rP   c                   @   s.   e Zd ZdZdZd	dd„Zdd„ Zdd„ ZdS )
Ú_RouterContextNrE   c                 C   s
   || _ d S r/   ©rR   )r(   rR   r)   r)   r*   r+   ¾   s   
z_RouterContext.__init__c                 Ã   s&   �t | jƒI d H | _t| jƒ| _| jS r/   )rX   rR   rU   rO   rV   r@   r)   r)   r*   Ú
__aenter__Á   s   €z_RouterContext.__aenter__c                 Ã   s   �| j  ¡  | j ¡  d S r/   )rV   rh   rU   r?   rj   r)   r)   r*   Ú	__aexit__Æ   s   €
z_RouterContext.__aexit__©rE   )rB   rC   rD   rU   rV   r+   r‘   r’   r)   r)   r)   r*   r�   º   s    
r�   c                 C   s   t | ƒS )a§  Open a D-Bus 'router' to send and receive messages.

    Use as an async context manager::

        async with open_dbus_router() as req:
            ...

    :param str bus: 'SESSION' or 'SYSTEM' or a supported address.
    :return: :class:`DBusRouter`

    This is a shortcut for::

        conn = await open_dbus_connection()
        async with conn:
            async with conn.router() as req:
                ...
    )r�   r�   r)   r)   r*   Úopen_dbus_routerË   s   r”   r“   )3Úasyncior   r   r   Ú	itertoolsr   rI   Útypingr   Úwarningsr   Útornado.ioloopr   Útornado.iostreamr	   Útornado.locksr
   Útornado.queuesr   r   Újeepney.authr   r   r   r   Újeepney.busr   Újeepney.low_levelr   r   r   r   Újeepney.wrappersr   r   Újeepney.routingr   Újeepney.bus_messagesr   Úcommonr   r   r   r   r   r"   rX   rO   rP   r�   r”   r)   r)   r)   r*   Ú<module>   s2    ÿ
^