U
    [e                     @   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edddZddedddZed	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)   T/var/www/html/services/stratfitenv/lib/python3.8/site-packages/jeepney/io/tornado.py__init__   s    zDBusConnection.__init__Nserialmessagec                   s.   |d krt | j}| j||I d H  d S N)nextr&   r#   writeZ	serialiser(   r/   r-   r)   r)   r*   send!   s    
zDBusConnection.send)returnc                    s>   | j  }|d k	r|S | jjdddI d H }| j | q d S )Ni   Tpartial)r%   Zget_next_messager#   
read_bytesZadd_data)r(   msgbr)   r)   r*   receive'   s
    
zDBusConnection.receivec                 C   s   | j   d S r0   )r#   closer(   r)   r)   r*   r<   0   s    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sx|	|j
dddI d H  |jrFt|jqF|tI d H  t|}t|*}ttt| dI d H }|d |_W 5 Q R X |S )N)family    i   Tr6   
   r   )r   r	   socketAF_UNIXconnectr2   r   r   authenticatedfeedr8   errorr   r   r"   
DBusRouterr   Proxyr   ZHellor'   )busZbus_addrr#   Zauth_parserconnrouterZ
reply_bodyr)   r)   r*   open_dbus_connection4   s    
rP   c                   @   s   e Zd ZedddZddddZdd	 Zdd
dee dddZ	dd Z
dd Zdd Zedd ZedddZedddZdd ZdS )rK   )rN   c                 C   s<   || _ t | _t | _t | _t 	| j
 tt| _d S r0   )rN   r   _repliesr   _filtersr
   _stop_receivingr   currentZadd_callback	_receiverr   r   rO   )r(   rN   r)   r)   r*   r+   L   s    zDBusRouter.__init__Nr,   c                   s   | j j||dI d H  d S )Nr,   )rN   r4   r3   r)   r)   r*   r4   V   s    zDBusRouter.sendc              
      sl   t | | j rtdt| jj}| j|t	 ,}| j
||dI d H  |I d H W  5 Q R  S Q R X d S )NzThis DBusRouter has stoppedr,   )r   rS   is_setr   r1   rN   r&   rQ   Zcatchr   r4   )r(   r/   r-   Z	reply_futr)   r)   r*   send_and_get_replyY   s    
zDBusRouter.send_and_get_replyr   )queuebufsize)rX   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   rR   r   )r(   rulerX   rY   r)   r)   r*   filterd   s    zDBusRouter.filterc                 C   s   | j   d S r0   )rS   setr=   r)   r)   r*   stopr   s    zDBusRouter.stopc                 C   s   | S r0   r)   r=   r)   r)   r*   	__enter__u   s    zDBusRouter.__enter__c                 C   s   |    dS )NF)r]   r(   exc_typeexc_valexc_tbr)   r)   r*   __exit__x   s    zDBusRouter.__exit__c                 C   s   | j jS r0   )rN   r'   r=   r)   r)   r*   r'   ~   s    zDBusRouter.unique_namer.   c                    sD   |j jtjkr0|j jtj@ s0t| |I d H S | 	|I d H  d S r0   )
headermessage_typer   Zmethod_returnflagsr   Zno_reply_expectedr   rW   r4   )r(   r/   r)   r)   r*   send_message   s    zDBusRouter.send_message)r9   c              	   C   sL   | j |rdS | j|D ]*}z|j| W q tk
rD   Y qX qdS )zHandle one received messageN)rQ   dispatchrR   matchesrX   
put_nowaitr   )r(   r9   r[   r)   r)   r*   	_dispatch   s    zDBusRouter._dispatchc                    sn   zVt| j | j gD ]6}|I dH }|dkr: W "dS | | | j	
| qqW 5 d| _ | j  X dS )z'Receiver loop - runs in a separate taskFN)
is_runningrQ   Zdrop_allr   rN   r;   rS   waitrk   rO   incoming)r(   coror9   r)   r)   r*   rU      s    


zDBusRouter._receiver)r>   r?   r@   r"   r+   r4   rW   r   r   r[   r]   r^   rc   propertyr'   r   rg   rk   rU   r)   r)   r)   r*   rK   K   s   

rK   c                       s2   e Zd Zed fddZdd Zdd Z  ZS )rL   )rO   c                    s   t  | || _d S r0   )superr+   _router)r(   ZmsggenrO   	__class__r)   r*   r+      s    zProxy.__init__c                 C   s   d | j| jS )NzProxy({}, {}))formatZ_msggenrr   r=   r)   r)   r*   __repr__   s    zProxy.__repr__c                    s    fdd}|S )Nc                     s2    | |}|j jtjksttj|I d H S r0   )rd   re   r   method_callAssertionErrorr   rr   rW   )argskwargsr9   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)r>   r?   r@   rK   r+   rv   r~   __classcell__r)   r)   rs   r*   rL      s   rL   c                   @   s.   e Zd ZdZdZd	ddZdd Zdd ZdS )
_RouterContextNrA   c                 C   s
   || _ d S r0   rM   )r(   rM   r)   r)   r*   r+      s    z_RouterContext.__init__c                    s$   t | jI d H | _t| j| _| jS r0   )rP   rM   rN   rK   rO   r=   r)   r)   r*   
__aenter__   s    z_RouterContext.__aenter__c                    s   | j   | j  d S r0   )rO   r]   rN   r<   r_   r)   r)   r*   	__aexit__   s    
z_RouterContext.__aexit__)rA   )r>   r?   r@   rN   rO   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   )rA   )rA   )3asyncior   r   r   	itertoolsr   rE   typingr   warningsr   Ztornado.ioloopr   Ztornado.iostreamr	   Ztornado.locksr
   Ztornado.queuesr   r   Zjeepney.authr   r   r   r   Zjeepney.busr   Zjeepney.low_levelr   r   r   r   Zjeepney.wrappersr   r   Zjeepney.routingr   Zjeepney.bus_messagesr   commonr   r   r   r   r   r"   rP   rK   rL   r   r   r)   r)   r)   r*   <module>   s0   
^