
    k7i.                        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$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 r.S!S\R                  4S jjr/ S"S\,4S jjr0\1S:X  a  \0" 5       r2\3" S \2Rh                  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)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    M/home/james-whalen/.local/lib/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%   r3   _Future.result*   s    Lr(   r!   N)
__name__
__module____qualname____firstlineno__r&   r+   r0   r4   r3   __static_attributes__ r(   r%   r   r      s    "*&r(   r   c                 :    U b  [         R                  " 5       U -   $ g r    )time	monotonictimeouts    r%   timeout_to_deadlinerE   1   s    ~~'))r(   c                 N    U b"  [        U [        R                  " 5       -
  S5      $ g )Ng        )maxrA   rB   )deadlines    r%   deadline_to_timeoutrI   6   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)rM   
enable_fdsr	   parserr   outgoing_serialr   selectorregisterr   
select_keyunique_name)r$   rM   rP   s      r%   r&   DBusConnectionBase.__init__>   sK    	$h$1~')--00zBr(   c                     U $ r    r?   r#   s    r%   	__enter__DBusConnectionBase.__enter__G   s    r(   c                 $    U R                  5         gr.   )close)r$   exc_typeexc_valexc_tbs       r%   __exit__DBusConnectionBase.__exit__J   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)nextrR   rP   array	serialise)r$   rb   rf   rg   datas        r%   
_serialiseDBusConnectionBase._serialiseN   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    )rM   sendmsgsocket
SOL_SOCKET
SCM_RIGHTSlensendall)r$   rk   rg   
bytes_sents       r%   _send_with_fds!DBusConnectionBase._send_with_fdsU   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  )NrC   )rg   )rQ   get_next_message_read_some_datarI   add_data)r$   rH   msgbrg   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   )	rS   selectrU   rP   _read_with_fdsunwrap_readrM   recvTimeoutError)r$   rD   keyevs       r%   rz   "DBusConnectionBase._read_some_datag   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)rQ   bytes_desiredrM   recvmsgr   getattrrp   r\   RuntimeErrorr   r   from_ancdata)r$   nbytesrk   ancdataflags_s         r%   r   !DBusConnectionBase._read_with_fdsq   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)rS   r\   rM   r#   s    r%   r\   DBusConnectionBase.closey   s     		r(   )rP   rR   rQ   rU   rS   rM   rV   Fr    )r:   r;   r<   r=   __doc__rp   r&   rY   r`   r
   bytesr   ri   rl   rv   r~   rz   r   r\   r>   r?   r(   r%   rK   rK   <   sZ    B V]]  ' uhu{{>S6T 1-Gr(   rK   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 jrSSS.S\\   4S jjrSS.S\4S jjrSrU =r$ )r      rM   c                    > [         TU ]  X5        [        5       U l        [	        [
        U 5      U l        U R                  R                  5       nUS   U l        g )Nr   )	superr&   r   _filtersr   r   	bus_proxyHellorV   )r$   rM   rP   hello_reply	__class__s       r%   r&   DBusConnection.__init__   sJ    * '( {D1nn**,&q>r(   Nrb   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)rl   rv   rM   rt   )r$   rb   rf   rk   rg   s        r%   sendDBusConnection.send   s5    OOG4	*IId#r(   rC   rc   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~   rE   )r$   rD   s     r%   receiveDBusConnection.receive   s     }}09::r(   c                    U R                  US9n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.
rC   N)r   r   matchesqueueappend)r$   rD   r|   filters       r%   recv_messagesDBusConnection.recv_messages   s?    
 ll7l+mm++C0FLL$ 1r(   c                   [        U5        [        U5      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Xd:X  a  U$ 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`.
)rf   rC   )r   rE   rh   rR   send_messager   rI   headerfieldsgetr   reply_serialr   r   r   r   )r$   rb   rD   rH   rf   msg_inreply_tor   s           r%   send_and_get_reply!DBusConnection.send_and_get_reply   s     	 &w/d**+'1\\*=h*G\HF}}++//0I0I2NH! --//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   rC   )rE   rs   r   rI   popleft)r$   r   rD   rH   s       r%   recv_until_filtered"DBusConnection.recv_until_filtered   sI      'w/%jAo':8'DE %jAo}}r(   )r   r   rV   r   r    )r:   r;   r<   r=   rp   r&   r
   r   r   r   r   r   r   r   r   r   r>   __classcell__r   s   @r%   r   r      s{    	*V]] 	* 	*$G $ L!% ;' ; (, % 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
NrC   c                <   > [         TU ]  U5        X l        X0l        g r    )r   r&   _connection_timeout)r$   msggen
connectionrD   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95      $ )Nr   rC   )	popr   r   message_typer   method_callr   r   r   )argskwargsrD   r|   make_msgr$   s       r%   inner!Proxy._method_call.<locals>.inner   sm    jjT]];GD+F+C::**k.E.EEEEd..AA B   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(   rc   c           	        ^^ [         R                   " [         R                  S9m[        U5      mUU4S jn U" TR                  U 5        [	        USS9n[        [         S5      (       a=  TR                  S/[         R                  [         R                  [        S5      4/5        OTR                  S5        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    )
settimeoutrI   )methr   rH   rM   s     r%   with_sock_deadline'prep_socket.<locals>.with_sock_deadline  s    +H56T{r(   F)rP   inc_null_byte	SCM_CREDS    i   i   zDid not authenticate in z secondsN)rp   AF_UNIXrE   connectr   hasattrro   rq   r   r   r   rt   feedr   r   r   rD   r\   r   r   )	addrrP   rD   r   authrreq_dataerH   rM   s	          @@r%   prep_socketr     s%   ==/D
 #7+H4<<.5I6;''LL%F$5$5v7G7Gs#T"UV IIeHt||X6JJ{#5dii#FGH  	4<</ 	OODK >> P

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

s   CD) )E3= EE3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.
rC   )r   r   r   )busrP   auth_timeoutbus_addrrM   conns         r%   r   r   :  s(     s|Hx\BD$+DKr(   __main__zUnique name:)Fg       @)SESSIONFg      ?)5r   ri   collectionsr   errnor   r   	itertoolsr   r   	selectorsr   r   rp   rA   typingr   jeepneyr	   r
   r   r   jeepney.authr   r   jeepney.busr   jeepney.fdsr   r   jeepney.wrappersr   r   jeepney.bus_messagesr   commonr   r   r   __all__r   rE   rI   rK   r   r   r   r   r   r:   r   printrV   r?   r(   r%   <module>r     s         	 1    > > -  4 2 , A A (
@ @Fa' aH%I %P! !J 79* z!D	.$**+ r(   