
    .Ph                        d dl Z d dlZd dlm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 d dlmZmZ d dlmZ d	d
lmZmZmZmZmZ  G d d          ZddZ G d d          Z G d d          Z G d de          ZdS )    N)count)Optional)AuthenticatorBEGIN)get_bus)MessageMessageTypeParser)	ProxyBase
unwrap_msg)message_bus   )MessageFiltersFilterHandleReplyMatcherRouterClosedcheck_replyablec                   f    e Zd ZdZdej        dej        fdZdddefdZ	d	efd
Z
d Zd Zd ZdS )DBusConnectionaP  A plain D-Bus connection with no matching of replies.

    This doesn't run any separate tasks: sending and receiving are done in
    the task that calls those methods. It's suitable for implementing servers:
    several worker tasks can receive requests and send replies.
    For a typical client pattern, see :class:`DBusRouter`.
    readerwriterc                     || _         || _        t                      | _        t	          d          | _        d | _        t          j                    | _	        d S )Nr   )start)
r   r   r
   parserr   outgoing_serialunique_nameasyncioLock	send_lock)selfr   r   s      R/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jeepney/io/asyncio.py__init__zDBusConnection.__init__   sD    hh$1~~~     Nserialmessagec                2  K   | j         4 d{V  |t          | j                  }| j                            |                    |                     | j                                         d{V  ddd          d{V  dS # 1 d{V swxY w Y   dS )z.Serialise and send a :class:`~.Message` objectN)r   nextr   r   write	serialisedrainr    r&   r%   s      r!   sendzDBusConnection.send    s+     > 	& 	& 	& 	& 	& 	& 	& 	&~d233Kg//77888+##%%%%%%%%%		& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s   A#B
BBreturnc                    K   	 | j                                         }||S | j                            d           d{V }|st          | j                             |           a)z5Return the next available message from the connectionTNi   )r   get_next_messager   readEOFErroradd_data)r    msgbs      r!   receivezDBusConnection.receive(   sw      	$+..00C
k&&t,,,,,,,,A K  ###	$r#   c                 z   K   | j                                          | j                                          d{V  dS )zClose the D-Bus connectionN)r   closewait_closedr    s    r!   r8   zDBusConnection.close4   sF      k%%'''''''''''r#   c                 
   K   | S N r:   s    r!   
__aenter__zDBusConnection.__aenter__9         r#   c                 >   K   |                                   d {V  d S r<   )r8   r    exc_typeexc_valexc_tbs       r!   	__aexit__zDBusConnection.__aexit__<   s,      jjllr#   )__name__
__module____qualname____doc__r   StreamReaderStreamWriterr"   r   r-   r6   r8   r>   rE   r=   r#   r!   r   r      s         (w3 (W=Q ( ( ( ( 6: & & &' & & & &
$w 
$ 
$ 
$ 
$( ( (
      r#   r   SESSIONc                   K   t          |           }t          j        |           d{V \  }}t                      }|D ]r}|                    |           |                                 d{V  |                    d           d{V }|st          d          |                    |           s|                    t                     |                                 d{V  t          ||          }t          |          4 d{V }t          j        t          t          |                                          d           d{V }	|	d         |_        ddd          d{V  n# 1 d{V swxY w Y   |S )zHOpen a plain D-Bus connection

    :return: :class:`DBusConnection`
    Ni   z#Socket closed before authentication
   r   )r   r   open_unix_connectionr   r)   r+   r1   r2   feedr   r   
DBusRouterwait_forProxyr   Hellor   )
busbus_addrr   r   authrreq_datar5   connrouter
reply_bodys
             r!   open_dbus_connectionr\   @   s     
 s||H"7AAAAAAAANFF OOE  Xllnn++d######## 	B@AAA

1
LL
,,.. &&))D $ ) ) ) ) ) ) )6"+E+v,F,F,L,L,N,NPRSSSSSSSS
%a=) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) Ks   AE++
E58E5c                       e Zd ZdZdZdZdZdefdZe	d             Z
dddZdefd	Zdd
ddeej                 fdZd Zd ZdefdZd ZdS )rQ   zA 'client' D-Bus connection which can wait for a specific reply.

    This runs a background receiver task, and makes it possible to send a
    request and wait for the relevant reply.
    NrY   c                     || _         t                      | _        t                      | _        t          j        |                                           | _        d S r<   )	_connr   _repliesr   _filtersr   create_task	_receiver	_rcv_task)r    rY   s     r!   r"   zDBusRouter.__init__j   sA    
$&(( ,T^^-=-=>>r#   c                     | j         j        S r<   )r_   r   r:   s    r!   r   zDBusRouter.unique_namep   s    z%%r#   r$   c                N   K   | j                             ||           d{V  dS )z&Send a message, don't wait for a replyr$   N)r_   r-   r,   s      r!   r-   zDBusRouter.sendt   s6      joogfo55555555555r#   r.   c                 |  K   t          |           | j                                        rt          d          t	          | j        j                  }| j                            |t          j
                              5 }|                     ||           d{V  | d{V cddd           S # 1 swxY w Y   dS )zSend a method call message and wait for the reply

        Returns the reply message (method return or error message type).
        zThis DBusRouter has stoppedr$   N)r   rd   doner   r(   r_   r   r`   catchr   Futurer-   )r    r&   r%   	reply_futs       r!   send_and_get_replyzDBusRouter.send_and_get_replyx   s     
 	   >   	><===dj011]  )9)9:: 	%i))GF)333333333#OOOOOO	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   ?%B11B58B5r   )queuebufsizerm   c                V    t          | j        ||pt          j        |                    S )ar  Create a filter for incoming messages

        Usage::

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

        :param MatchRule rule: Catch messages matching this rule
        :param asyncio.Queue queue: Send matching messages here
        :param int bufsize: If no queue is passed in, create one with this size
        )r   ra   r   Queue)r    rulerm   rn   s       r!   filterzDBusRouter.filter   s'     DM41P'-:P:PQQQr#   c                 
   K   | S r<   r=   r:   s    r!   r>   zDBusRouter.__aenter__   r?   r#   c                 (  K   | j                                         r| j                                          n\| j                                          t	          j        t          j                  5  | j          d {V  d d d            n# 1 swxY w Y   dS )NF)rd   rh   resultcancel
contextlibsuppressr   CancelledErrorrA   s       r!   rE   zDBusRouter.__aexit__   s      >   	%N!!####N!!###$W%;<< % %n$$$$$$$% % % % % % % % % % % % % % %us   -BBBr4   c                     | j                             |          rdS t          | j                            |                    D ]2}	 |j                            |           # t          j        $ r Y /w xY wdS )zHandle one received messageN)	r`   dispatchlistra   matchesrm   
put_nowaitr   	QueueFull)r    r4   rr   s      r!   	_dispatchzDBusRouter._dispatch   s    =!!#&& 	F4=005566 	 	F'',,,,$   	 	s   A""A43A4c                    K   	 	 | j                                          d{V }|                     |           5# | j                                         w xY w)z'Receiver loop - runs in a separate taskTN)r_   r6   r   r`   drop_all)r    r4   s     r!   rc   zDBusRouter._receiver   si      	%$ J..00000000s###$
 M""$$$$s	   6: A)rF   rG   rH   rI   _nursery_mgr_send_cancel_scope_rcv_cancel_scoper   r"   propertyr   r-   r   rl   r   r   rp   rr   r>   rE   r   rc   r=   r#   r!   rQ   rQ   `   s#        
 L?^ ? ? ? ? & & X& -1 6 6 6 6 6%7 % % % % ?CA R R RXgm%< R R R R    	W 	 	 	 	% % % % %r#   rQ   c                   .    e Zd ZdZdZdZddZd Zd ZdS )open_dbus_routerzOpen a D-Bus 'router' to send and receive messages

    Use as an async context manager::

        async with open_dbus_router() as router:
            ...
    NrL   c                     || _         d S r<   )rU   )r    rU   s     r!   r"   zopen_dbus_router.__init__   s    r#   c                    K   t          | j                   d {V | _        t          | j                  | _        | j                                         d {V S r<   )r\   rU   rY   rQ   req_ctxr>   r:   s    r!   r>   zopen_dbus_router.__aenter__   s`      .tx88888888	!$),,\,,.........r#   c                    K   | j                             |||           d {V  | j                                         d {V  d S r<   )r   rE   rY   r8   rA   s       r!   rE   zopen_dbus_router.__aexit__   s\      l$$Xw?????????ioor#   rL   )	rF   rG   rH   rI   rY   r   r"   r>   rE   r=   r#   r!   r   r      s\          DG   / / /
         r#   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )rS   a  An asyncio proxy for calling D-Bus methods

    You can call methods on the proxy object, such as ``await bus_proxy.Hello()``
    to make a method call over D-Bus and wait for a reply. It will either
    return a tuple of returned data, or raise :exc:`.DBusErrorResponse`.
    The methods available are defined by the message generator you wrap.

    :param msggen: A message generator object.
    :param ~asyncio.DBusRouter router: Router to send and receive messages.
    c                 X    t                                          |           || _        d S r<   )superr"   _router)r    msggenrZ   	__class__s      r!   r"   zProxy.__init__   s&       r#   c                 B    d                     | j        | j                  S )NzProxy({}, {}))format_msggenr   r:   s    r!   __repr__zProxy.__repr__   s    %%dlDLAAAr#   c                       fd}|S )Nc                     K    | i |}|j         j        t          j        u sJ j                            |           d {V }t          |          S r<   )headermessage_typer	   method_callr   rl   r   )argskwargsr4   replymake_msgr    s       r!   innerz!Proxy._method_call.<locals>.inner   si      (D+F++C:*k.EEEEE,99#>>>>>>>>Ee$$$r#   r=   )r    r   r   s   `` r!   _method_callzProxy._method_call   s)    	% 	% 	% 	% 	% 	% r#   )rF   rG   rH   rI   r"   r   r   __classcell__)r   s   @r!   rS   rS      sc        	 	    B B B      r#   rS   r   )r   rw   	itertoolsr   typingr   jeepney.authr   r   jeepney.busr   jeepneyr   r	   r
   jeepney.wrappersr   r   jeepney.bus_messagesr   commonr   r   r   r   r   r   r\   rQ   r   rS   r=   r#   r!   <module>r      s                    - - - - - - - -       0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 , , , , , ,             
- - - - - - - -`   @V% V% V% V% V% V% V% V%p               0    I     r#   