
    .Ph:                        d dl Z d dlZd dlZd dl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 d dl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 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'  ej(        e)          Z*g dZ+ed             Z, G d de          Z-d!ddde-fdZ. G d de$          Z/ G d d          Z0 G d d          Z1 G d de          Z2ed!ddd             Z3dS )"    N)asynccontextmanagercontextmanager)count)Optional)ValueError)Channel)AuthenticatorBEGIN)get_bus)FileDescriptorfds_buf_size)ParserMessageTypeMessage)	ProxyBase
unwrap_msg)message_bus   )MessageFiltersFilterHandleReplyMatcherRouterClosedcheck_replyable)open_dbus_connectionopen_dbus_routerProxyc               #      K   	 d V  d S # t           $ ra} | j        t          j        t          j        hv rt	          j        d          d t	          j        d                    |                     | d } ~ ww xY w)Nzthis socket was already closedzsocket connection broken: {})OSErrorerrnoEBADFENOTSOCKtrioClosedResourceErrorBrokenResourceErrorformat)excs    O/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jeepney/io/trio.py)_translate_socket_errors_to_stream_errorsr)   ,   s      	   9en555*+KLLRVV*.55c:: s   
 
A5AA00A5c                   z    e Zd ZdZddZdddefdZdefd	Zdde	fdZ
defdZd Zd Zd Zed             ZdS )DBusConnectiona  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`.

    Implements trio's channel interface for Message objects.
    Fc                     || _         || _        t                      | _        t	          d          | _        d | _        t          j                    | _	        t          j                    | _
        d | _        d S )Nr   )start)socket
enable_fdsr   parserr   outgoing_serialunique_namer#   Lock	send_lock	recv_lock_leftover_to_send)selfr.   r/   s      r(   __init__zDBusConnection.__init__E   s[    $hh$1~~~!%    Nserialmessagec                :  K   | j         4 d{V  |t          | j                  }| j        rt	          j        d          nd}|                    ||          }|                     ||           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i)fds)r4   nextr1   r/   array	serialise
_send_data)r7   r<   r;   r?   datas        r(   sendzDBusConnection.sendO   s:     > 	- 	- 	- 	- 	- 	- 	- 	-~d233&*o?%+c"""4C$$V$55D//$,,,,,,,,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   A'B


BBrD   c                 :  K   | j         j        rt          j        d          t	                      5  | j        r |                     | j                   d {V  t          |          5 }|rE| j                             |gt          j         j	        t          j         j
        |fg           d {V }n | j                             |           d {V }|                     ||           d {V  d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nz!can't send data after sending EOF)r.   did_shutdown_SHUT_WRr#   r$   r)   r6   _send_remainder
memoryviewsendmsg
SOL_SOCKET
SCM_RIGHTSrE   )r7   rD   r?   sents       r(   rC   zDBusConnection._send_dataZ   s     ;+ 	P*+NOOO688 	7 	7% C**4+ABBBBBBBBBD!! 7T 8!%!4!4dV.0F? > " "      DD "&!1!1$!7!7777777D**46666666667 7 7 7 7 7 7 7 7 7 7 7 7 7 7	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s6   7D(BC8,D8C<	<D?C<	 DDDr   c                 :  K   	 |t          |          k     rZ||d          5 }| j                            |           d {V }d d d            n# 1 swxY w Y   ||z  }|t          |          k     Zd | _        d S # t          j        $ r ||d          | _         w xY wN)lenr.   rE   r6   r#   	Cancelled)r7   rD   already_sent	remainingrM   s        r(   rH   zDBusConnection._send_remainderm   s     	T**,--( =I!%!1!1)!<!<<<<<<<D= = = = = = = = = = = = = = =$ T** &*D"""~ 	 	 	 &*,--%8D"	s.   A: !AA: AA: A"A: : Breturnc                 F  K   | j         4 d{V  	 | j                                        }||cddd          d{V  S |                                  d{V \  }}|st	          j        d          | j                            ||           ~# 1 d{V swxY w Y   dS )z5Return the next available message from the connectionNTzSocket closed at the other end)r5   r0   get_next_message
_read_datar#   EndOfChanneladd_data)r7   msgbr?   s       r(   receivezDBusConnection.receive{   sK     > 	- 	- 	- 	- 	- 	- 	- 	-
-k2244?		- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-  $000000003 N+,LMMM$$Q,,,
-	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   BAB
BBc                 8  K   | j         r| j                                        }t                      5  | j                            |t                                 d {V \  }}}}d d d            n# 1 swxY w Y   |t          t          j        dd          z  r#| 	                                 t          d          |t          j        |          fS t                      5  | j                            d           d {V }d d d            n# 1 swxY w Y   |g fS )N
MSG_CTRUNCr   z&Unable to receive all file descriptorsi   )r/   r0   bytes_desiredr)   r.   recvmsgr   getattrr#   _closeRuntimeErrorr   from_ancdatarecv)r7   nbytesrD   ancdataflags_s         r(   rW   zDBusConnection._read_data   s     ? 	[..00F:<<  040C0CLNN1 1 + + + + + +'gua               wt{L!<<< M"#KLLL4W==== ;<< 4 4![--d333333334 4 4 4 4 4 4 4 4 4 4 4 4 4 48Os#   3A00A47A4 !DDDc                 F    | j                                          d | _        d S rO   )r.   closer6   r7   s    r(   rb   zDBusConnection._close   s$    !%r9   c                 2   K   |                                   dS )zClose the D-Bus connectionN)rb   rl   s    r(   aclosezDBusConnection.aclose   s      r9   c                \  K   t          j                    4 d{V }t          |           }|                    |           d{V  	 |W V  |                                 d{V  n# |                                 d{V  w xY w	 ddd          d{V  dS # 1 d{V swxY w Y   dS )aY  Temporarily wrap this connection as a :class:`DBusRouter`

        To be used like::

            async with conn.router() as req:
                reply = await req.send_and_get_reply(msg)

        While the router is running, you shouldn't use :meth:`receive`.
        Once the router is closed, you can use the plain connection again.
        N)r#   open_nursery
DBusRouterr-   rn   )r7   nurseryrouters      r(   rs   zDBusConnection.router   s_      $&& 	& 	& 	& 	& 	& 	& 	&'%%F,,w'''''''''&mmoo%%%%%%%%fmmoo%%%%%%%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s)   +BA(B(BB
B%(B%)F)r   )__name__
__module____qualname____doc__r8   r   rE   bytesrC   rI   rH   r\   rW   rb   rn   r   rs    r9   r(   r+   r+   ;   s         & & & & 6: - - -' - - - -7U 7 7 7 7& *    -w - - - -  "& & &
   & & & & &r9   r+   SESSIONFr/   rT   c                l  K   t          |           }t          j        |           d{V }t          |d          }t	          t
          d          rH|j                            dgt
          j        t
          j        t          d          fg           d{V  n|
                    d           d{V  |D ]J}|
                    |           d{V  |                    |                                 d{V            K|
                    t                     d{V  t          |j        |          }|                                4 d{V }|                    t#          j                               d{V }|j        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`
    NF)r/   inc_null_byte	SCM_CREDS    i   r{   r   )r   r#   open_unix_socketr
   hasattrr.   rJ   rK   r~   rx   send_allfeedreceive_somer   r+   rs   send_and_get_replyr   Hellobodyr2   )	busr/   bus_addrsockauthrreq_dataconnrs   replys	            r(   r   r      sx     
 s||H%)%:8%D%DDDDDDDD ZuEEEEv{## #k!!Gv(&*:E#JJGH
 
 	
 	
 	
 	
 	
 	
 	
 	

 mmE""""""""" . .mmH%%%%%%%%%

**,,,,,,,,----
--

$+*===D {{}} ) ) ) ) ) ) )//0A0C0CDDDDDDDD :a=) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) Ks   ?F##
F-0F-c                   L     e Zd Zdef fdZed             Zd Zd Zd Z	 xZ
S )TrioFilterHandlefiltersc                 \    t                                          |||           || _        d S rO   )superr8   send_channel)r7   r   rulesend_chnrecv_chn	__class__s        r(   r8   zTrioFilterHandle.__init__   s-    $111$r9   c                     | j         S rO   queuerl   s    r(   receive_channelz TrioFilterHandle.receive_channel   s
    zr9   c                 p   K   |                                   | j                                         d {V  d S rO   )rk   r   rn   rl   s    r(   rn   zTrioFilterHandle.aclose   s@      

&&(((((((((((r9   c                    K   | j         S rO   r   rl   s    r(   
__aenter__zTrioFilterHandle.__aenter__   s      zr9   c                 >   K   |                                   d {V  d S rO   )rn   )r7   exc_typeexc_valexc_tbs       r(   	__aexit__zTrioFilterHandle.__aexit__   s,      kkmmr9   )rt   ru   rv   r   r8   propertyr   rn   r   r   __classcell__r   s   @r(   r   r      s        % % % % % % %   X) ) )        r9   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )Futurez4A very simple Future for trio based on `trio.Event`.c                 D    d | _         t          j                    | _        d S rO   )_outcomer#   Event_eventrl   s    r(   r8   zFuture.__init__   s    jllr9   c                 `    t          |          | _        | j                                         d S rO   )r   r   r   set)r7   results     r(   
set_resultzFuture.set_result   s'    fr9   c                 `    t          |          | _        | j                                         d S rO   )r   r   r   r   )r7   r'   s     r(   set_exceptionzFuture.set_exception   s'    c

r9   c                 v   K   | j                                          d {V  | j                                        S rO   )r   waitr   unwraprl   s    r(   getz
Future.get   s@      k         }##%%%r9   N)rt   ru   rv   rw   r8   r   r   r   ry   r9   r(   r   r      sV        >># # #    & & & & &r9   r   c                       e 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ej        fdZd ZdefdZej        fdZdS )rq   zA client D-Bus connection which can wait for replies.

    This runs a separate receiver task and dispatches received messages.
    Nr   c                 `    || _         t                      | _        t                      | _        d S rO   )_connr   _repliesr   _filters)r7   r   s     r(   r8   zDBusRouter.__init__
  s%    
$&((r9   c                     | j         j        S rO   )r   r2   rl   s    r(   r2   zDBusRouter.unique_name  s    z%%r9   r:   c                N   K   | j                             ||           d{V  dS )z/Send a message, don't wait for a reply
        r:   N)r   rE   )r7   r<   r;   s      r(   rE   zDBusRouter.send  s8       joogfo55555555555r9   rT   c                 r  K   t          |           | j        t          d          t          | j        j                  }| j                            |t                                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).
        NzThis DBusRouter has stoppedr:   )r   _rcv_cancel_scoper   r@   r   r1   r   catchr   rE   r   )r7   r<   r;   	reply_futs       r(   r   zDBusRouter.send_and_get_reply  s     
 	   !)<===dj011]  22 	+i))GF)333333333#--//))))))	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   (7B,,B03B0r   )channelbufsizer   c                h    |t          j        |          \  }}nd}t          | j        |||          S )a  Create a filter for incoming messages

        Usage::

            async with router.filter(rule) as receive_channel:
                matching_msg = await receive_channel.receive()

            # OR:
            send_chan, recv_chan = trio.open_memory_channel(1)
            async with router.filter(rule, channel=send_chan):
                matching_msg = await recv_chan.receive()

        If the channel fills up,
        The sending end of the channel is closed when leaving the ``async with``
        block, whether or not it was passed in.

        :param jeepney.MatchRule rule: Catch messages matching this rule
        :param trio.MemorySendChannel channel: Send matching messages here
        :param int bufsize: If no channel is passed in, create one with this size
        N)r#   open_memory_channelr   r   )r7   r   r   r   recv_channels        r(   filterzDBusRouter.filter'  s;    * ?$($<W$E$E!G\\LtWlKKKr9   rr   c                    K   | j         t          d          |                    | j                   d {V | _         d S )Nz+DBusRouter receiver task is already running)r   rc   r-   	_receiver)r7   rr   s     r(   r-   zDBusRouter.startD  sL      !-LMMM'.}}T^'D'D!D!D!D!D!D!Dr9   c                    K   | j          | j                                          d| _         t          j        d           d{V  dS )z Stop the sender & receiver tasksNr   )r   cancelr#   sleeprl   s    r(   rn   zDBusRouter.acloseI  sV       !-"))+++%)D" jmmr9   rZ   c                     | j                             |          rdS | j                            |          D ]2}	 |j                            |           # t          j        $ r Y /w xY wdS )zHandle one received messageN)r   dispatchr   matchesr   send_nowaitr#   
WouldBlock)r7   rZ   r   s      r(   	_dispatchzDBusRouter._dispatchV  s    =!!#&& 	Fm++C00 	 	F#//4444?   	 	s   AA'&A'c           	         K   t          j                    5 }d| _        |                    |           	 	 | j                                         d{V }|                     |           5# d| _        | j                                         t          j	        d          5 }| j
        j                                        D ](}d|_        |j                                         d{V  )	 ddd           w # 1 swxY w Y   w xY w# 1 swxY w Y   dS )z'Receiver loop - runs in a separate taskTNF   )r#   CancelScope
is_runningstartedr   r\   r   r   drop_allmove_on_afterr   r   valuesshieldr   rn   )r7   task_statuscscoperZ   cleanup_scoper   s         r(   r   zDBusRouter._receivera  s      	;6"DO''';( $
 2 2 4 4444444CNN3'''( #(&&((( '** ;m"&-"7">">"@"@ ; ;/3,$188::::::::::;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;	; 	; 	; 	; 	; 	; 	; 	; 	; 	;sG   D6A**5D AC4	(D 4C88D ;C8<D  DD
D)rt   ru   rv   rw   _nursery_mgrr   r+   r8   r   r2   rE   r   r   r   r#   MemorySendChannelr   Nurseryr-   rn   r   TASK_STATUS_IGNOREDr   ry   r9   r(   rq   rq     s@         L)^ ) ) ) )
 & & X& -1 6 6 6 6 6
+7 + + + + IMVW L L Lx0F'G L L L L:E4< E E E E
	 	 		W 	 	 	 	 +/*B ; ; ; ; ; ;r9   rq   c                   (     e Zd ZdZ fdZd Z xZS )r   a  A trio 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 ~trio.DBusRouter router: Router to send and receive messages.
    c                     t                                          |           t          |t                    st	          d          || _        d S )Nz)Proxy can only be used with DBusRequester)r   r8   
isinstancerq   	TypeError_router)r7   msggenrs   r   s      r(   r8   zProxy.__init__  sH       &*-- 	IGHHHr9   c                       fd}|S )Nc                     K    | i |}|j         j        t          j        u sJ j                            |           d {V }t          |          S rO   )headermessage_typer   method_callr   r   r   )argskwargsrZ   r   make_msgr7   s       r(   innerz!Proxy._method_call.<locals>.inner  si      (D+F++C:*k.EEEEE,99#>>>>>>>>Ee$$$r9   ry   )r7   r   r   s   `` r(   _method_callzProxy._method_call  s)    	% 	% 	% 	% 	% 	% r9   )rt   ru   rv   rw   r8   r   r   r   s   @r(   r   r   w  sQ        	 	          r9   r   c                 K   t          | |           d{V }|4 d{V  |                                4 d{V }|W V  ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  dS # 1 d{V swxY w Y   dS )a  Open a D-Bus 'router' to send and receive messages.

    Use as an async context manager::

        async with open_dbus_router() as req:
            ...

    :param str bus: 'SESSION' or 'SYSTEM' or a supported address.
    :return: :class:`DBusRouter`

    This is a shortcut for::

        conn = await open_dbus_connection()
        async with conn:
            async with conn.router() as req:
                ...
    r{   N)r   rs   )r   r/   r   rtrs       r(   r   r     s     & &cjAAAAAAAAAD        ;;== 	 	 	 	 	 	 	CIIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	                             s4   A9AA9
A	A9"A	#A99
BB)rz   )4rA   r    loggingr.   
contextlibr   r   	itertoolsr   typingr   outcomer   r   r#   trio.abcr	   jeepney.authr
   r   jeepney.busr   jeepney.fdsr   r   jeepney.low_levelr   r   r   jeepney.wrappersr   r   jeepney.bus_messagesr   commonr   r   r   r   r   	getLoggerrt   log__all__r)   r+   r   r   r   rq   r   r   ry   r9   r(   <module>r     s       : : : : : : : :                                    - - - - - - - -       4 4 4 4 4 4 4 4 : : : : : : : : : : 2 2 2 2 2 2 2 2 , , , , , ,              g!!  & 
 
 
{& {& {& {& {&W {& {& {&|E   n    D    |   (& & & & & & & &&r; r; r; r; r; r; r; r;j    I   6        r9   