
    .Ph.                        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$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 Z.d%dej        fdZ/	 d&de,fd"Z0e1d#k    r e0            Z2 e3d$e2j4                   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)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    S/var/www/html/test/jupyter/venv/lib/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$   r/   z_Future.result*   s     	Lr&   N)__name__
__module____qualname__r%   r)   r-   r0   r/    r&   r$   r   r      s_          " " "* * *& & &    r&   r   c                 6    | t          j                    | z   S d S r   )time	monotonictimeouts    r$   timeout_to_deadliner=   1   s     ~'))4r&   c                 R    | $t          | t          j                    z
  d          S d S )Ng        )maxr9   r:   )deadlines    r$   deadline_to_timeoutrA   6   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)rD   
enable_fdsr	   parserr   outgoing_serialr   selectorregisterr   
select_keyunique_name)r#   rD   rG   s      r$   r%   zDBusConnectionBase.__init__>   s_    	$hh$1~~~'))-00zBBr&   c                     | S r   r7   r"   s    r$   	__enter__zDBusConnectionBase.__enter__G   s    r&   c                 .    |                                   dS r+   )close)r#   exc_typeexc_valexc_tbs       r$   __exit__zDBusConnectionBase.__exit__J   s    

ur&   messagereturnc                     |t          | j                  }| j        rt          j        d          nd }|                    ||          }||fS )Ni)serialfds)nextrI   rG   array	serialise)r#   rV   rZ   r[   datas        r$   
_serialisezDBusConnectionBase._serialiseN   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   )rD   sendmsgsocket
SOL_SOCKET
SCM_RIGHTSlensendall)r#   r_   r[   
bytes_sents       r$   _send_with_fdsz!DBusConnectionBase._send_with_fdsU   sp    Y&&Ff'):C@A
 


 D		!!Id:;;/00000 "!r&   c                     	 | j                                         }||S |                     t          |                    \  }}| j                             ||           `)NTr;   )r[   )rH   get_next_message_read_some_datarA   add_data)r#   r@   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   )	rJ   selectrL   rG   _read_with_fdsunwrap_readrD   recvTimeoutError)r#   r<   keyevs       r$   rl   z"DBusConnectionBase._read_some_datag   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)rH   bytes_desiredrD   recvmsgr   getattrrc   rQ   RuntimeErrorrt   r   from_ancdata)r#   nbytesr_   ancdataflags_s         r$   rs   z!DBusConnectionBase._read_with_fdsq   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)rJ   rQ   rD   r"   s    r$   rQ   zDBusConnectionBase.closey   s.    	r&   Fr   )r4   r5   r6   __doc__rc   r%   rO   rU   r
   bytesr   r]   r`   ri   rp   rl   rs   rQ   r7   r&   r$   rC   rC   <   s        BB   V]            ' uhu{>S6T    1 1 1- - -   G G G    r&   rC   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Z
ddddee         fdZdddefdZ xZS )r   FrD   c                     t                                          ||           t                      | _        t	          t
          |           | _        | j                                        }|d         | _        d S )Nr   )	superr%   r   _filtersr   r   	bus_proxyHellorM   )r#   rD   rG   hello_reply	__class__s       r$   r%   zDBusConnection.__init__   sd    z*** '(( {D11n**,,&q>r&   NrV   c                     |                      ||          \  }}|r|                     ||           dS | j                            |           dS )z.Serialise and send a :class:`~.Message` objectN)r`   ri   rD   rg   )r#   rV   rZ   r_   r[   s        r$   sendzDBusConnection.send   sY    OOGV44	c 	$c*****Id#####r&   r;   rW   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.
        )rp   r=   )r#   r<   s     r$   receivezDBusConnection.receive   s     }}099:::r&   c                    |                      |          }| j                            |          D ]}|j                            |           dS )z\Receive one message and apply filters

        See :meth:`filter`. Returns nothing.
        r;   N)r   r   matchesqueueappend)r#   r<   rn   filters       r$   recv_messageszDBusConnection.recv_messages   sY    
 ll7l++m++C00 	% 	%FL$$$$	% 	%r&   c                   t          |           t          |          }t          | j                  }|                     ||           	 |                     t          |                    }|j        j        	                    t          j        d          }||k    r|S | 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`.
        )rZ   Tr;   )r   r=   r\   rI   send_messager   rA   headerfieldsgetr   reply_serialr   r   r   r   )r#   rV   r<   r@   rZ   msg_inreply_tor   s           r$   send_and_get_replyz!DBusConnection.send_and_get_reply   s     	   &w//d*++'&111	,\\*=h*G*G\HHF}+//0I2NNH6!! -//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=   rf   r   rA   popleft)r#   r   r<   r@   s       r$   recv_until_filteredz"DBusConnection.recv_until_filtered   s_      'w//%jjAoo':8'D'DEEE %jjAoo}}r&   r   r   )r4   r5   r6   rc   r%   r
   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r$   r   r      s0       	* 	*V] 	* 	* 	* 	* 	* 	*$ $G $ $ $ $ L!% ; ; ;' ; ; ; ; (, % % % % % 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 t          j                            ||                    S )Nr   r;   )	popr   r   message_typer   method_callr   r   r   )argskwargsr<   rn   make_msgr#   s       r$   innerz!Proxy._method_call.<locals>.inner   sw    jjT];;G(D+F++C:*k.EEEEEd.AAW B     r&   )	functoolswraps)r#   r   r   s   `` r$   _method_callzProxy._method_call   s>    		"	"	 	 	 	 	 
#	"	 r&   )r4   r5   r6   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)ro   s    r$   rt   rt     s,      H":r{:/F/FGGGHr&   F       @rW   c           	         t          j         t           j                  t          |          fd}	  |j        |            t	          |d          }t          t           d          r=                    dgt           j        t           j        t          d          fg           n
                    d           |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   )
settimeoutrA   )methr   r@   rD   s     r$   with_sock_deadlinez'prep_socket.<locals>.with_sock_deadline  s*    +H55666tT{r&   F)rG   inc_null_byte	SCM_CREDS    i   i   zDid not authenticate in z secondsN)rc   AF_UNIXr=   connectr   hasattrrb   rd   r   r   r   rg   feedrt   ru   r   r<   rQ   rv   r   )	addrrG   r<   r   authrreq_dataer@   rD   s	          @@r$   prep_socketr     s   =///D
 #7++H     4<...5III6;'' 	LL%F$5v7Gs#T"UVVVV IIe 	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   C&D E0.(EE0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   )busrG   auth_timeoutbus_addrrD   conns         r$   r   r   :  s7     s||Hx\BBBD$
++DKr&   __main__zUnique name:)Fr   )r   Fr   )5r   r]   collectionsr   errnor   r   	itertoolsr   r   	selectorsr   r   rc   r9   typingr   jeepneyr	   r
   r   r   jeepney.authr   r   jeepney.busr   jeepney.fdsr   r   jeepney.wrappersr   r   jeepney.bus_messagesr   commonr   r   r   __all__r   r=   rA   rC   r   r   rt   r   r   r4   r   printrM   r7   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a a a a a' a a aH% % % % %I % % %P  ! ! ! ! ! !J 79    * z!!D	E.$*+++++ r&   