
    >a.                        S r SSKrSSKJr  SSKJr  SSKrSSKJr  SSK	r	SSK
JrJr  SSKrSSKrSSKJr  SSKJrJrJrJr  SS	KJrJr  SS
KJr  SSKJrJr  SSKJrJ r   SSK!J"r"  SSK#J$r$  SSK%J&r&J'r'J(r(  / SQr) " S S5      r*S r+S r, " S S5      r- " S S\-5      r. " S S\5      r/S r0S"S\R                  4S jjr1 S#S\.4S jjr2\3S :X  a  \2" 5       r4\5" S!\4Rl                  5        gg)$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                   2    \ rS rSrS rS rS rS rS rSr	g)	_Future   c                     S U l         g N_resultselfs    3lib/python3.13/site-packages/jeepney/io/blocking.py__init___Future.__init__   s	        c                 ,    [        U R                  5      $ r!   )boolr#   r$   s    r&   done_Future.done"   s    DLL!!r)   c                     SU4U l         g NFr"   )r%   	exceptions     r&   set_exception_Future.set_exception%   s    y)r)   c                     SU4U l         g )NTr"   )r%   results     r&   
set_result_Future.set_result(   s    f~r)   c                 4    U R                   u  pU(       a  U$ Uer!   r"   )r%   successvalues      r&   r4   _Future.result+   s    Lr)   r"   N)
__name__
__module____qualname____firstlineno__r'   r,   r1   r5   r4   __static_attributes__ r)   r&   r   r      s    "*&r)   r   c                 :    U b  [         R                  " 5       U -   $ g r!   )time	monotonictimeouts    r&   timeout_to_deadlinerF   2   s    ~~'))r)   c                 N    U b"  [        U [        R                  " 5       -
  S5      $ g )Ng        )maxrB   rC   )deadlines    r&   deadline_to_timeoutrJ   7   s$    8dnn..33r)   c                       \ rS rSrSrSS\R
                  4S jjrS rS rS\	S\
\\R                     44S	 jrS
 rS rSS jrS rS rSrg)DBusConnectionBase=   z8Connection machinery shared by this module and threadingsockc                     Xl         X l        [        5       U l        [	        SS9U l        [        5       U l        U R                  R                  U[        5      U l
        S U l        g )Nr   )start)rN   
enable_fdsr	   parserr   outgoing_serialr   selectorregisterr   
select_keyunique_name)r%   rN   rQ   s      r&   r'   DBusConnectionBase.__init__?   sK    	$h$1~')--00zBr)   c                     U $ r!   r@   r$   s    r&   	__enter__DBusConnectionBase.__enter__H   s    r)   c                 $    U R                  5         gr/   )close)r%   exc_typeexc_valexc_tbs       r&   __exit__DBusConnectionBase.__exit__K   s    

r)   messagereturnc                     Uc  [        U R                  5      nU R                  (       a  [        R                  " S5      OS nUR	                  X#S9nXC4$ )Ni)serialfds)nextrS   rQ   array	serialise)r%   rc   rg   rh   datas        r&   
_serialiseDBusConnectionBase._serialiseO   sH    >$../F"&//ekk#t   8yr)   c                     U R                   R                  U/[        R                  [        R                  U4/5      nU[        U5      :  a  U R                   R                  XS  5        g g r!   )rN   sendmsgsocket
SOL_SOCKET
SCM_RIGHTSlensendall)r%   rl   rh   
bytes_sents       r&   _send_with_fds!DBusConnectionBase._send_with_fdsV   s]    YY&&Ff''):):C@A


 D	!IId;/0 "r)   c                      U R                   R                  5       nUb  U$ U R                  [        U5      S9u  p4U R                   R	                  X4S9  MT  )NrD   )rh   )rR   get_next_message_read_some_datarJ   add_data)r%   rI   msgbrh   s        r&   _receiveDBusConnectionBase._receive_   sV    ++..0C
))2Eh2O)PFAKK   , r)   Nc                 
   U R                   R                  U5       H_  u  p#X R                  :X  d  M  U R                  (       a  U R	                  5       s  $ [        U R                  R                  S5      5      / 4s  $    [        e)Ni   )	rT   selectrV   rQ   _read_with_fdsunwrap_readrN   recvTimeoutError)r%   rE   keyevs       r&   r{   "DBusConnectionBase._read_some_datah   sb    }}++G4GCoo%??..00&tyy~~d';<b@@ 5 r)   c                 .   U R                   R                  5       nU R                  R                  U[	        5       5      u  p#pEU[        [        SS5      -  (       a  U R                  5         [        S5      e[        U5      [        R                  " U5      4$ )N
MSG_CTRUNCr   z&Unable to receive all file descriptors)rR   bytes_desiredrN   recvmsgr   getattrrq   r]   RuntimeErrorr   r   from_ancdata)r%   nbytesrl   ancdataflags_s         r&   r   !DBusConnectionBase._read_with_fdsr   sr    **,"&))"3"3FLN"Ku76<33JJLGHH4 ."="=g"FFFr)   c                 l    U R                   R                  5         U R                  R                  5         g)zClose the connectionN)rT   r]   rN   r$   s    r&   r]   DBusConnectionBase.closez   s     		r)   )rQ   rS   rR   rV   rT   rN   rW   Fr!   )r;   r<   r=   r>   __doc__rq   r'   rZ   ra   r
   bytesr   rj   rm   rw   r   r{   r   r]   r?   r@   r)   r&   rL   rL   =   sZ    B V]]  ' uhu{{>S6T 1-Gr)   rL   c                      ^  \ rS rSrSS\R                  4U 4S jjjrSS\4S jjr\rSS.S\4S	 jjr	SS.S
 jr
SSS.S jrSSS.S\\   4S jjrSS.S\4S jjrSrU =r$ )r      rN   c                    > [         TU ]  X5        SU l        [        [        5      U l        [        5       U l        [        [        U 5      U l
        U R                  R                  5       nUS   U l        g )NFr   )superr'   _unwrap_replyr   r   routerr   _filtersr   r   	bus_proxyHellorW   )r%   rN   rQ   hello_reply	__class__s       r&   r'   DBusConnection.__init__   s]    *" Wo&( {D1nn**,&q>r)   Nrc   c                     U R                  X5      u  p4U(       a  U R                  X45        gU R                  R                  U5        g)z.Serialise and send a :class:`~.Message` objectN)rm   rw   rN   ru   )r%   rc   rg   rl   rh   s        r&   sendDBusConnection.send   s5    OOG4	*IId#r)   rD   rd   c                6    U R                  [        U5      5      $ )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.
)r   rF   )r%   rE   s     r&   receiveDBusConnection.receive   s     }}09::r)   c                    U R                  US9nU R                  R                  U5        U R                  R	                  U5       H  nUR
                  R                  U5        M      g)zLReceive one message and apply filters

See :meth:`filter`. Returns nothing.
rD   N)r   r   incomingr   matchesqueueappend)r%   rE   r}   filters       r&   recv_messagesDBusConnection.recv_messages   sQ    
 ll7l+S!mm++C0FLL$ 1r)   rE   unwrapc                   [        U5        [        U5      nUc  U R                  n[        U R                  5      nU R                  XS9   U R                  [        U5      S9nUR                  R                  R                  [        R                  S5      nXu:X  a  U(       a  [        U5      $ U$ U R                  R                  U5        U R                   R#                  U5       H  nUR$                  R'                  U5        M      M  )zSend a message, wait for the reply and return it

Filters are applied to other messages received before the reply -
see :meth:`add_filter`.
)rg   rD   )r   rF   r   ri   rS   send_messager   rJ   headerfieldsgetr   reply_serialr   r   r   r   r   r   r   )	r%   rc   rE   r   rI   rg   msg_inreply_tor   s	            r&   send_and_get_reply!DBusConnection.send_and_get_reply   s     	 &w/>''Fd**+'1\\*=h*G\HF}}++//0I0I2NH!%f-- KK  (--//7##F+ 8 r)   r   )r   bufsizer   c                F    Uc	  [        US9n[        U R                  X5      $ )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
)maxlen)r   r   r   )r%   ruler   r   s       r&   r   DBusConnection.filter   s$     =)EDMM477r)   c                    [        U5      n[        U5      S:X  a)  U R                  [        U5      S9  [        U5      S:X  a  M)  UR	                  5       $ )a
  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   rD   )rF   rt   r   rJ   popleft)r%   r   rE   rI   s       r&   recv_until_filtered"DBusConnection.recv_until_filtered   sI      'w/%jAo':8'DE %jAo}}r)   )r   r   r   r   rW   r   r!   )r;   r<   r=   r>   rq   r'   r
   r   r   r   r   r   r   r   r   r   r?   __classcell__r   s   @r&   r   r      s}    *V]] * *$G $ L!% ;' ; (, % 6:$ ,6 7;A 8Xe_ 8" 59 W  r)   r   c                   @   ^  \ rS rSrSrSS.U 4S jjrS rS rSrU =r	$ )	r      a6  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
NrD   c                <   > [         TU ]  U5        X l        X0l        g r!   )r   r'   _connection_timeout)r%   msggen
connectionrE   r   s       r&   r'   Proxy.__init__   s     %r)   c                 |    U R                   c  SOSU R                    3nSU R                   SU R                   U S3$ )N z
, timeout=zProxy(z, ))r   _msggenr   )r%   extras     r&   __repr__Proxy.__repr__  sA    }},Z3O~R(8(8'9%BBr)   c                 J   ^ ^ [         R                  " T5      UU 4S j5       nU$ )Nc                     > UR                  STR                  5      nT" U 0 UD6nUR                  R                  [        R
                  L d   eTR                  R                  X2SS9$ )Nr   Tr   )popr   r   message_typer   method_callr   r   )argskwargsrE   r}   make_msgr%   s       r&   inner!Proxy._method_call.<locals>.inner	  sg    jjT]];GD+F+C::**k.E.EEEE##66T 7  r)   )	functoolswraps)r%   r   r   s   `` r&   _method_callProxy._method_call  s%    		"	 
#	 r)   )r   r   )
r;   r<   r=   r>   r   r'   r   r   r?   r   r   s   @r&   r   r      s'    " 7;    
C
 
r)   r   c                 d    U (       d(  [        [        [        R                  " [        5      5      eU $ )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)r~   s    r&   r   r     s"     ":r{{:/FGGHr)   rd   c           	      $  ^^ [         R                   " [         R                  S9m[        U5      mUU4S jn U" TR                  U 5        [	        US9nU HA  nU" TR
                  U5        UR                  [        U" TR                  S5      5      5        MC     U" TR
                  [        5        TR                  S5        T$ ! [         R                   a%  nTR                  5         [        SU S35      UeSnAf  TR                  5         e = f)z=Create a socket and authenticate ready to send D-Bus messages)familyc                 B   > TR                  [        T5      5        U " U6 $ r!   )
settimeoutrJ   )methr   rI   rN   s     r&   with_sock_deadline'prep_socket.<locals>.with_sock_deadline(  s    +H56T{r)   )rQ   i   zDid not authenticate in z secondsN)rq   AF_UNIXrF   connectr   ru   feedr   r   r   rE   r]   r   r   )	addrrQ   rE   r   authrreq_dataerI   rN   s	          @@r&   prep_socketr      s    ==/D
 #7+H4<<.4Ht||X6JJ{#5dii#FGH  	4<</ 	OODK >> P

5gYhGHaO

s   A:C D C99Dc                 F    [        U 5      n[        X1US9n[        XA5      nU$ )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.
rD   )r   r   r   )busrQ   auth_timeoutbus_addrrN   conns         r&   r   r   >  s(     s|Hx\BD$+DKr)   __main__zUnique name:)Fg       @)SESSIONFg      ?)7r   rj   collectionsr   errnor   r   	itertoolsr   r   	selectorsr   r   rq   rB   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   rF   rJ   rL   r   r   r   r   r   r;   r  printrW   r@   r)   r&   <module>r     s         	 1    > > -  4 2 " , A A (
@ @Fj' jZ%I %P > 79* z!D	.$**+ r)   