
    k7i                         S SK r S SKrS SKJr  S SKJr  S SKJrJr  S SK	J
r
  S SK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JrJrJr   " S S5      rSS jr " S S5      r " S S5      r " S S\5      rg)    N)count)Optional)AuthenticatorBEGIN)get_bus)MessageMessageTypeParser)	ProxyBase
unwrap_msg)message_bus   )MessageFiltersFilterHandleReplyMatcherRouterClosedcheck_replyablec                       \ rS rSrSrS\R                  S\R                  4S jrSS.S\	4S	 jjr
S
\	4S jrS rS rS rSrg)DBusConnection   a<  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                     Xl         X l        [        5       U l        [	        SS9U l        S U l        [        R                  " 5       U l	        g )Nr   )start)
r   r   r
   parserr   outgoing_serialunique_nameasyncioLock	send_lock)selfr   r   s      L/home/james-whalen/.local/lib/python3.13/site-packages/jeepney/io/asyncio.py__init__DBusConnection.__init__   s6    h$1~     Nserialmessagec                ^  #    U R                    ISh  vN   Uc  [        U R                  5      nU R                  R	                  UR                  U5      5        U R                  R                  5       I Sh  vN   SSS5      ISh  vN   g Ny N N	! , ISh  vN  (       d  f       g= f7f)z.Serialise and send a :class:`~.Message` objectN)r    nextr   r   write	serialisedrainr!   r(   r'   s      r"   sendDBusConnection.send    so     >>>~d223KKg//78++##%%%	 ">> &	 ">>>sW   B-BB-A B7B8B<B-BB-BB-B*BB*&B-returnc                    #     U R                   R                  5       nUb  U$ U R                  R                  S5      I Sh  vN nU(       d  [        eU R                   R                  U5        Ml   N.7f)z5Return the next available message from the connectionNi   )r   get_next_messager   readEOFErroradd_data)r!   msgbs      r"   receiveDBusConnection.receive(   s_     ++..0C
kk&&t,,AKK  # 
 -s   ?A2A0/A2c                    #    U R                   R                  5         U R                   R                  5       I Sh  vN   g N7f)zClose the D-Bus connectionN)r   closewait_closedr!   s    r"   r<   DBusConnection.close4   s+     kk%%'''s   8AA Ac                    #    U $ 7fN r>   s    r"   
__aenter__DBusConnection.__aenter__9   
        c                 @   #    U R                  5       I S h  vN   g  N7frA   )r<   r!   exc_typeexc_valexc_tbs       r"   	__aexit__DBusConnection.__aexit__<   s     jjls   )r   r   r   r    r   r   )__name__
__module____qualname____firstlineno____doc__r   StreamReaderStreamWriterr#   r   r/   r9   r<   rC   rL   __static_attributes__rB   r%   r"   r   r      sR    (w33 (W=Q=Q ( 6: &' &
$w 
$(
r%   r   c                   #    [        U 5      n[        R                  " U5      I Sh  vN u  p#[        5       nU Hh  nUR	                  U5        UR                  5       I Sh  vN   UR                  S5      I Sh  vN nU(       d  [        S5      eUR                  U5        Mj     UR	                  [        5        UR                  5       I Sh  vN   [        X#5      n[        U5       ISh  vN n[        R                  " [        [        U5      R                  5       S5      I Sh  vN n	U	S   Ul        SSS5      ISh  vN   U$  GN" N N N Nd N+ N! , ISh  vN  (       d  f       U$ = f7f)z@Open a plain D-Bus connection

:return: :class:`DBusConnection`
Ni   z#Socket closed before authentication
   r   )r   r   open_unix_connectionr   r+   r-   r4   r5   feedr   r   
DBusRouterwait_forProxyr   Hellor   )
busbus_addrr   r   authrreq_datar8   connrouter
reply_bodys
             r"   open_dbus_connectionre   @   s$    
 s|H"77AANF OEXlln++d##@AA

1  LL
,,. &)D $6"++E+v,F,L,L,NPRSS
%a=   K1 B 	#   S   Ks   %E3E	9E3!E"E3:E;AE3EE3-E.E317E(E)E7E3EE3E3E3E3E3EE3E0E!E0+E3c                       \ rS rSrSrSrSrSrS\4S jr	\
S 5       rSS.S jrS	\4S
 jrSSS.S\\R"                     4S jjrS rS rS\4S jrS rSrg)rZ   `   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.
Nrb   c                     Xl         [        5       U l        [        5       U l        [
        R                  " U R                  5       5      U l        g rA   )	_connr   _repliesr   _filtersr   create_task	_receiver	_rcv_task)r!   rb   s     r"   r#   DBusRouter.__init__j   s3    
$&( ,,T^^-=>r%   c                 .    U R                   R                  $ rA   )ri   r   r>   s    r"   r   DBusRouter.unique_namep   s    zz%%%r%   r&   c                R   #    U R                   R                  XS9I Sh  vN   g N7f)z&Send a message, don't wait for a replyr&   N)ri   r/   r.   s      r"   r/   DBusRouter.sendt   s     jjoogo555s   '%'r1   c                   #    [        U5        U R                  R                  5       (       a  [        S5      e[	        U R
                  R                  5      nU R                  R                  U[        R                  " 5       5       nU R                  XS9I Sh  vN   UI Sh  vN sSSS5        $  N N! , (       d  f       g= f7f)ztSend 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   rn   doner   r*   ri   r   rj   catchr   Futurer/   )r!   r(   r'   	reply_futs       r"   send_and_get_replyDBusRouter.send_and_get_replyx   s     
 	 >>  <==djj001]]  )9:i))G)333#O ;:3# ;:sB   BCB5B1	B5#B3$B5'
C1B53B55
C?Cr   )queuebufsizer{   c                h    [        U R                  X=(       d    [        R                  " U5      5      $ )a:  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   rk   r   Queue)r!   ruler{   r|   s       r"   filterDBusRouter.filter   s#     DMM41P'--:PQQr%   c                    #    U $ 7frA   rB   r>   s    r"   rC   DBusRouter.__aenter__   rE   rF   c                 \  #    U R                   R                  5       (       a  U R                   R                  5         gU R                   R                  5         [        R
                  " [        R                  5         U R                   I S h  vN   S S S 5        g N! , (       d  f       g= f7f)NF)rn   ru   resultcancel
contextlibsuppressr   CancelledErrorrH   s       r"   rL   DBusRouter.__aexit__   sx     >>  NN!!#
  NN!!#$$W%;%;<nn$$ = % =<s0   A9B,;BBB	B,B
B)%B,r7   c                 
   U R                   R                  U5      (       a  g[        U R                  R	                  U5      5       H  n UR
                  R                  U5        M!     g! [        R                   a     M;  f = f)zHandle one received messageN)	rj   dispatchlistrk   matchesr{   
put_nowaitr   	QueueFull)r!   r7   r   s      r"   	_dispatchDBusRouter._dispatch   si    ==!!#&&4==0056F'', 7 $$ s   
A**BBc                    #      U R                   R                  5       I Sh  vN nU R                  U5        M5   N! U R                  R	                  5         f = f7f)z'Receiver loop - runs in a separate taskN)ri   r9   r   rj   drop_all)r!   r7   s     r"   rm   DBusRouter._receiver   sI     	% JJ..00s# 0 MM""$s   A< :< AA)ri   rk   rn   rj   )rN   rO   rP   rQ   rR   _nursery_mgr_send_cancel_scope_rcv_cancel_scoper   r#   propertyr   r/   r   ry   r   r   r~   r   rC   rL   r   rm   rU   rB   r%   r"   rZ   rZ   `   s    
 L?^ ? & & -1 6%7 % ?CA RXgmm%< R	W 	%r%   rZ   c                   6    \ rS rSrSrSrSrSS jrS rS r	Sr
g)	open_dbus_router   zOpen a D-Bus 'router' to send and receive messages

Use as an async context manager::

    async with open_dbus_router() as router:
        ...
Nc                     Xl         g rA   )r^   )r!   r^   s     r"   r#   open_dbus_router.__init__   s    r%   c                    #    [        U R                  5      I S h  vN U l        [        U R                  5      U l        U R                  R                  5       I S h  vN $  NE N7frA   )re   r^   rb   rZ   req_ctxrC   r>   s    r"   rC   open_dbus_router.__aenter__   sF     .txx88	!$)),\\,,... 9.s"   A%A!A A%A#A%#A%c                    #    U R                   R                  XU5      I S h  vN   U R                  R                  5       I S h  vN   g  N' N7frA   )r   rL   rb   r<   rH   s       r"   rL   open_dbus_router.__aexit__   s<     ll$$X???iioo 	@s!    AA
!AAAA)r^   rb   r   SESSION)rN   rO   rP   rQ   rR   rb   r   r#   rC   rL   rU   rB   r%   r"   r   r      s"     DG/
 r%   r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )r\      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                 0   > [         TU ]  U5        X l        g rA   )superr#   _router)r!   msggenrc   	__class__s      r"   r#   Proxy.__init__   s     r%   c                 N    SR                  U R                  U R                  5      $ )NzProxy({}, {}))format_msggenr   r>   s    r"   __repr__Proxy.__repr__   s    %%dllDLLAAr%   c                    ^ ^ UU 4S jnU$ )Nc                     >#    T" U 0 UD6nUR                   R                  [        R                  L d   eTR                  R                  U5      I S h  vN n[        U5      $  N7frA   )headermessage_typer	   method_callr   ry   r   )argskwargsr7   replymake_msgr!   s       r"   inner!Proxy._method_call.<locals>.inner   sZ     D+F+C::**k.E.EEEE,,99#>>Ee$$ ?s   AA%A#A%rB   )r!   r   r   s   `` r"   _method_callProxy._method_call   s    	% r%   )r   )
rN   rO   rP   rQ   rR   r#   r   r   rU   __classcell__)r   s   @r"   r\   r\      s    	B r%   r\   r   )r   r   	itertoolsr   typingr   jeepney.authr   r   jeepney.busr   jeepneyr   r	   r
   jeepney.wrappersr   r   jeepney.bus_messagesr   commonr   r   r   r   r   r   re   rZ   r   r\   rB   r%   r"   <module>r      sa        -  0 0 2 , 
- -`@V% V%p   0I r%   