
    >a.                        d Z ddlZddlmZ ddlmZ ddlZddlmZ ddl	Z	ddl
mZmZ ddlZddl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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( g dZ) G d d          Z*d Z+d Z, G d d          Z- G d de-          Z. G d de          Z/d Z0d&dej        fd Z1	 d'de.fd#Z2e3d$k    r e2            Z4 e5d%e4j6                   dS dS )(z'Synchronous IO wrappers around jeepney
    N)deque)
ECONNRESET)count)DefaultSelector
EVENT_READ)Optional)ParserMessageMessageTypeHeaderFields)AuthenticatorBEGIN)get_bus)FileDescriptorfds_buf_size)	ProxyBase
unwrap_msg)Router)message_bus   )MessageFiltersFilterHandlecheck_replyable)open_dbus_connectionDBusConnectionProxyc                   ,    e Zd Zd Zd Zd Zd Zd ZdS )_Futurec                     d | _         d S N_resultselfs    3lib/python3.11/site-packages/jeepney/io/blocking.py__init__z_Future.__init__   s        c                 *    t          | j                  S r    )boolr"   r#   s    r%   donez_Future.done"   s    DL!!!r'   c                     d|f| _         d S NFr!   )r$   	exceptions     r%   set_exceptionz_Future.set_exception%   s    y)r'   c                     d|f| _         d S )NTr!   )r$   results     r%   
set_resultz_Future.set_result(   s    f~r'   c                 "    | j         \  }}|r|S |r    r!   )r$   successvalues      r%   r0   z_Future.result+   s     	Lr'   N)__name__
__module____qualname__r&   r*   r.   r1   r0    r'   r%   r   r      s_          " " "* * *& & &    r'   r   c                 6    | t          j                    | z   S d S r    )time	monotonictimeouts    r%   timeout_to_deadliner>   2   s     ~'))4r'   c                 R    | $t          | t          j                    z
  d          S d S )Ng        )maxr:   r;   )deadlines    r%   deadline_to_timeoutrB   7   s*    8dn...3334r'   c                       e Zd ZdZddej        fdZd Zd Zdede	e
ej                 ffd	Zd
 Zd ZddZd Zd ZdS )DBusConnectionBasez8Connection machinery shared by this module and threadingFsockc                     || _         || _        t                      | _        t	          d          | _        t                      | _        | j                            |t                    | _
        d | _        d S )Nr   )start)rE   
enable_fdsr	   parserr   outgoing_serialr   selectorregisterr   
select_keyunique_name)r$   rE   rH   s      r%   r&   zDBusConnectionBase.__init__?   s_    	$hh$1~~~'))-00zBBr'   c                     | S r    r8   r#   s    r%   	__enter__zDBusConnectionBase.__enter__H   s    r'   c                 .    |                                   dS r,   )close)r$   exc_typeexc_valexc_tbs       r%   __exit__zDBusConnectionBase.__exit__K   s    

ur'   messagereturnc                     |t          | j                  }| j        rt          j        d          nd }|                    ||          }||fS )Ni)serialfds)nextrJ   rH   array	serialise)r$   rW   r[   r\   datas        r%   
_serialisezDBusConnectionBase._serialiseO   sT    >$.//F"&/;ek#t  C 88Syr'   c                     | j                             |gt          j        t          j        |fg          }|t          |          k     r$| j                             ||d                     d S d S r    )rE   sendmsgsocket
SOL_SOCKET
SCM_RIGHTSlensendall)r$   r`   r\   
bytes_sents       r%   _send_with_fdsz!DBusConnectionBase._send_with_fdsV   sp    Y&&Ff'):C@A
 


 D		!!Id:;;/00000 "!r'   c                     	 | j                                         }||S |                     t          |                    \  }}| j                             ||           `)NTr<   )r\   )rI   get_next_message_read_some_datarB   add_data)r$   rA   msgbr\   s        r%   _receivezDBusConnectionBase._receive_   sg    	-+..00C
))2Eh2O2O)PPFAsK   ,,,	-r'   Nc                     | j                             |          D ]X\  }}|| j        k    rH| j        r|                                 c S t          | j                            d                    g fc S Yt          )Ni   )	rK   selectrM   rH   _read_with_fdsunwrap_readrE   recvTimeoutError)r$   r=   keyevs       r%   rm   z"DBusConnectionBase._read_some_datah   s    }++G44 	A 	AGCdo%%? A..00000&ty~~d';';<<b@@@@	 & r'   c                 J   | j                                         }| j                            |t	                                \  }}}}|t          t          dd          z  r#|                                  t          d          t          |          t          j        |          fS )N
MSG_CTRUNCr   z&Unable to receive all file descriptors)rI   bytes_desiredrE   recvmsgr   getattrrd   rR   RuntimeErrorru   r   from_ancdata)r$   nbytesr`   ancdataflags_s         r%   rt   z!DBusConnectionBase._read_with_fdsr   s    **,,"&)"3"3FLNN"K"Kgua76<333 	IJJLLLGHHH4  ."=g"F"FFFr'   c                 j    | j                                          | j                                         dS )zClose the connectionN)rK   rR   rE   r#   s    r%   rR   zDBusConnectionBase.closez   s.    	r'   Fr    )r5   r6   r7   __doc__rd   r&   rP   rV   r
   bytesr   r^   ra   rj   rq   rm   rt   rR   r8   r'   r%   rD   rD   =   s        BB   V]            ' uhu{>S6T    1 1 1- - -   G G G    r'   rD   c                        e Zd Zddej        f fdZddefdZeZdddefd	Zddd
Z	ddddZ
ddddee         fdZdddefdZ xZS )r   FrE   c                 2   t                                          ||           d| _        t          t                    | _        t                      | _        t          t          |           | _
        | j
                                        }|d         | _        d S )NFr   )superr&   _unwrap_replyr   r   routerr   _filtersr   r   	bus_proxyHellorN   )r$   rE   rH   hello_reply	__class__s       r%   r&   zDBusConnection.__init__   sy    z***" Woo&(( {D11n**,,&q>r'   NrW   c                     |                      ||          \  }}|r|                     ||           dS | j                            |           dS )z.Serialise and send a :class:`~.Message` objectN)ra   rj   rE   rh   )r$   rW   r[   r`   r\   s        r%   sendzDBusConnection.send   sY    OOGV44	c 	$c*****Id#####r'   r<   rX   c                F    |                      t          |                    S )a+  Return the next available message from the connection

        If the data is ready, this will return immediately, even if timeout<=0.
        Otherwise, it will wait for up to timeout seconds, or indefinitely if
        timeout is None. If no message comes in time, it raises TimeoutError.
        )rq   r>   )r$   r=   s     r%   receivezDBusConnection.receive   s     }}099:::r'   c                    |                      |          }| j                            |           | j                            |          D ]}|j                            |           dS )z\Receive one message and apply filters

        See :meth:`filter`. Returns nothing.
        r<   N)r   r   incomingr   matchesqueueappend)r$   r=   ro   filters       r%   recv_messageszDBusConnection.recv_messages   so    
 ll7l++S!!!m++C00 	% 	%FL$$$$	% 	%r'   r=   unwrapc                   t          |           t          |          }|| j        }t          | j                  }|                     ||           	 |                     t          |                    }|j        j	        
                    t          j        d          }||k    r|rt          |          S |S | j                            |           | j                            |          D ]}|j                            |           )zSend a message, wait for the reply and return it

        Filters are applied to other messages received before the reply -
        see :meth:`add_filter`.
        N)r[   Tr<   )r   r>   r   r]   rJ   send_messager   rB   headerfieldsgetr   reply_serialr   r   r   r   r   r   r   )	r$   rW   r=   r   rA   r[   msg_inreply_tor   s	            r%   send_and_get_replyz!DBusConnection.send_and_get_reply   s    	   &w//>'Fd*++'&111	,\\*=h*G*G\HHF}+//0I2NNH6!! .%f--- K  (((-//77 , ,##F++++	,r'   r   )r   bufsizer   c                R    |t          |          }t          | j        ||          S )a  Create a filter for incoming messages

        Usage::

            with conn.filter(rule) as matches:
                # matches is a deque containing matched messages
                matching_msg = conn.recv_until_filtered(matches)

        :param jeepney.MatchRule rule: Catch messages matching this rule
        :param collections.deque queue: Matched messages will be added to this
        :param int bufsize: If no deque is passed in, create one with this size
        N)maxlen)r   r   r   )r$   ruler   r   s       r%   r   zDBusConnection.filter   s-     =)))EDM4777r'   c                    t          |          }t          |          dk    r6|                     t          |                     t          |          dk    6|                                S )aZ  Process incoming messages until one is filtered into queue

        Pops the message from queue and returns it, or raises TimeoutError if
        the optional timeout expires. Without a timeout, this is equivalent to::

            while len(queue) == 0:
                conn.recv_messages()
            return queue.popleft()

        In the other I/O modules, there is no need for this, because messages
        are placed in queues by a separate task.

        :param collections.deque queue: A deque connected by :meth:`filter`
        :param float timeout: Maximum time to wait in seconds
        r   r<   )r>   rg   r   rB   popleft)r$   r   r=   rA   s       r%   recv_until_filteredz"DBusConnection.recv_until_filtered   s_      'w//%jjAoo':8'D'DEEE %jjAoo}}r'   r   r    )r5   r6   r7   rd   r&   r
   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r%   r   r      s2       * *V] * * * * * *$ $G $ $ $ $ L!% ; ; ;' ; ; ; ; (, % % % % % 6:$ , , , , ,6 7;A 8 8 8Xe_ 8 8 8 8" 59   W        r'   r   c                   4     e Zd ZdZdd fd
Zd Zd Z xZS )r   aj  A blocking proxy for calling D-Bus methods

    You can call methods on the proxy object, such as ``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.

    You can set a time limit on a call by passing ``_timeout=`` in the method
    call, or set a default when creating the proxy. The ``_timeout`` argument
    is not passed to the message generator.
    All timeouts are in seconds, and :exc:`TimeoutErrror` is raised if it
    expires before a reply arrives.

    :param msggen: A message generator object
    :param ~blocking.DBusConnection connection: Connection to send and receive messages
    :param float timeout: Default seconds to wait for a reply, or None for no limit
    Nr<   c                f    t                                          |           || _        || _        d S r    )r   r&   _connection_timeout)r$   msggen
connectionr=   r   s       r%   r&   zProxy.__init__   s.       %r'   c                 R    | j         dn	d| j          }d| j         d| j         | dS )N z
, timeout=zProxy(z, ))r   _msggenr   )r$   extras     r%   __repr__zProxy.__repr__  s@    },3O3O3OBBB(8B%BBBBr'   c                 J     t          j                   fd            }|S )Nc                      |                     dj                  } | i |}|j        j        t          j        u sJ j                            ||d          S )Nr   Tr   )popr   r   message_typer   method_callr   r   )argskwargsr=   ro   make_msgr$   s       r%   innerz!Proxy._method_call.<locals>.inner	  sl    jjT];;G(D+F++C:*k.EEEEE#66WT 7   r'   )	functoolswraps)r$   r   r   s   `` r%   _method_callzProxy._method_call  s>    		"	"	 	 	 	 	 
#	"	 r'   )r5   r6   r7   r   r&   r   r   r   r   s   @r%   r   r      sr         " 7;              
C C C
 
 
 
 
 
 
r'   r   c                 b    | s,t          t          t          j        t                              | S )zRaise ConnectionResetError from an empty read.

    Sometimes the socket raises an error itself, sometimes it gives no data.
    I haven't worked out when it behaves each way.
    )ConnectionResetErrorr   osstrerror)rp   s    r%   ru   ru     s,      H":r{:/F/FGGGHr'   F       @rX   c           	      D   t          j         t           j                  t          |          fd}	  |j        |            t	          |          }|D ]D} |j        |           |                    t           |j        d                               E |j        t                     nT# t           j
        $ r-}                                 t          d| d          |d}~w                                   xY w                    d           S )z=Create a socket and authenticate ready to send D-Bus messages)familyc                 R                         t                                | | S r    )
settimeoutrB   )methr   rA   rE   s     r%   with_sock_deadlinez'prep_socket.<locals>.with_sock_deadline(  s*    +H55666tT{r'   )rH   i   zDid not authenticate in z secondsN)rd   AF_UNIXr>   connectr   rh   feedru   rv   r   r=   rR   rw   r   )	addrrH   r=   r   authrreq_dataerA   rE   s	          @@r%   prep_socketr      sN   =///D
 #7++H     4<...444 	I 	IHt|X666JJ{#5#5di#F#FGGHHHH4<////> P P P

GgGGGHHaO

OODKs   A>B7 7D(C..DSESSION      ?c                 h    t          |           }t          |||          }t          ||          }|S )a  Connect to a D-Bus message bus

    Pass ``enable_fds=True`` to allow sending & receiving file descriptors.
    An error will be raised if the bus does not allow this. For simplicity,
    it's advisable to leave this disabled unless you need it.

    D-Bus has an authentication step before sending or receiving messages.
    This takes < 1 ms in normal operation, but there is a timeout so that client
    code won't get stuck if the server doesn't reply. *auth_timeout* configures
    this timeout in seconds.
    r<   )r   r   r   )busrH   auth_timeoutbus_addrrE   conns         r%   r   r   >  s7     s||Hx\BBBD$
++DKr'   __main__zUnique name:)Fr   )r   Fr   )7r   r^   collectionsr   errnor   r   	itertoolsr   r   	selectorsr   r   rd   r:   typingr   jeepneyr	   r
   r   r   jeepney.authr   r   jeepney.busr   jeepney.fdsr   r   jeepney.wrappersr   r   jeepney.routingr   jeepney.bus_messagesr   commonr   r   r   __all__r   r>   rB   rD   r   r   ru   r   r   r5   r   printrN   r8   r'   r%   <module>r      s                           				 1 1 1 1 1 1 1 1         > > > > > > > > > > > > - - - - - - - -       4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 " " " " " " , , , , , , A A A A A A A A A A         (  
  @ @ @ @ @ @ @ @Fj j j j j' j j jZ% % % % %I % % %P       > 79    * z!!D	E.$*+++++ r'   