o
    }¨Êh0  ã                   @   sš  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 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& ddl'm(Z(m)Z)m*Z* g d¢Z+G dd„ dƒZ,dd„ Z-dd„ Z.G dd„ dƒZ/G dd„ de/ƒZ0G dd„ de!ƒZ1dd „ Z2d,d#ejfd$d%„Z3	'd-d#e0fd(d)„Z4e5d*krËe4ƒ Z6e7d+e6j8ƒ dS dS ).z'Synchronous IO wrappers around jeepney
é    N)Údeque)Ú
ECONNRESET)Úcount)ÚDefaultSelectorÚ
EVENT_READ)ÚOptional)Úwarn)Ú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                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú_Futurec                 C   s
   d | _ d S ©N©Ú_result©Úself© r#   úE/var/www/html/env/lib/python3.10/site-packages/jeepney/io/blocking.pyÚ__init__    ó   
z_Future.__init__c                 C   s
   t | jƒS r   )Úboolr    r!   r#   r#   r$   Údone#   r&   z_Future.donec                 C   ó   d|f| _ d S ©NFr   )r"   Ú	exceptionr#   r#   r$   Úset_exception&   ó   z_Future.set_exceptionc                 C   r)   )NTr   )r"   Úresultr#   r#   r$   Ú
set_result)   r-   z_Future.set_resultc                 C   s   | j \}}|r	|S |‚r   r   )r"   ÚsuccessÚvaluer#   r#   r$   r.   ,   s   
z_Future.resultN)Ú__name__Ú
__module__Ú__qualname__r%   r(   r,   r/   r.   r#   r#   r#   r$   r      s    r   c                 C   s   | d ur
t  ¡ |  S d S r   )ÚtimeÚ	monotonic©Útimeoutr#   r#   r$   Útimeout_to_deadline3   s   r9   c                 C   s   | d urt | t ¡  dƒS d S )Ng        )Úmaxr5   r6   )Údeadliner#   r#   r$   Údeadline_to_timeout8   s   r<   c                   @   sx   e Zd ZdZddejfdd„Zdd„ Zdd	„ Zd
ede	e
ej ffdd„Zdd„ Zdd„ Zddd„Zdd„ Zdd„ ZdS )ÚDBusConnectionBasez8Connection machinery shared by this module and threadingFÚsockc                 C   sB   || _ || _tƒ | _tdd�| _tƒ | _| j |t	¡| _
d | _d S )Nr   )Ústart)r>   Ú
enable_fdsr	   Úparserr   Úoutgoing_serialr   ÚselectorÚregisterr   Ú
select_keyÚunique_name)r"   r>   r@   r#   r#   r$   r%   @   s   
zDBusConnectionBase.__init__c                 C   s   | S r   r#   r!   r#   r#   r$   Ú	__enter__I   s   zDBusConnectionBase.__enter__c                 C   s   |   ¡  dS r*   )Úclose)r"   Úexc_typeÚexc_valÚexc_tbr#   r#   r$   Ú__exit__L   s   zDBusConnectionBase.__exit__ÚmessageÚreturnc                 C   s<   |d u r	t | jƒ}| jrt d¡nd }|j||d�}||fS )NÚi)ÚserialÚfds)ÚnextrB   r@   ÚarrayÚ	serialise)r"   rM   rP   rQ   Údatar#   r#   r$   Ú
_serialiseP   s
   
zDBusConnectionBase._serialisec                 C   sD   | j  |gtjtj|fg¡}|t|ƒk r | j  ||d … ¡ d S d S r   )r>   ÚsendmsgÚsocketÚ
SOL_SOCKETÚ
SCM_RIGHTSÚlenÚsendall)r"   rU   rQ   Ú
bytes_sentr#   r#   r$   Ú_send_with_fdsW   s   ÿÿz!DBusConnectionBase._send_with_fdsc                 C   s>   	 | j  ¡ }|d ur|S | jt|ƒd�\}}| j j||d� q)NTr7   )rQ   )rA   Úget_next_messageÚ_read_some_datar<   Úadd_data)r"   r;   ÚmsgÚbrQ   r#   r#   r$   Ú_receive`   s   
úzDBusConnectionBase._receiveNc                 C   sN   | j  |¡D ]\}}|| jkr$| jr|  ¡   S t| j d¡ƒg f  S qt‚)Ni   )	rC   ÚselectrE   r@   Ú_read_with_fdsÚunwrap_readr>   ÚrecvÚTimeoutError)r"   r8   ÚkeyÚevr#   r#   r$   r`   i   s   
üz"DBusConnectionBase._read_some_datac                 C   sT   | j  ¡ }| j |tƒ ¡\}}}}|ttddƒ@ r!|  ¡  tdƒ‚t	|ƒt
 |¡fS )NÚ
MSG_CTRUNCr   z&Unable to receive all file descriptors)rA   Úbytes_desiredr>   Úrecvmsgr   ÚgetattrrX   rH   ÚRuntimeErrorrg   r   Úfrom_ancdata)r"   ÚnbytesrU   ÚancdataÚflagsÚ_r#   r#   r$   rf   s   s   
z!DBusConnectionBase._read_with_fdsc                 C   s   | j  ¡  | j ¡  dS )zClose the connectionN)rC   rH   r>   r!   r#   r#   r$   rH   {   s   
zDBusConnectionBase.close©Fr   )r2   r3   r4   Ú__doc__rX   r%   rG   rL   r
   Úbytesr   rS   rV   r^   rd   r`   rf   rH   r#   r#   r#   r$   r=   >   s    		
	
r=   c                       s¦   e Zd Zddejf‡ fdd„Zedd„ ƒZddefd	d
„ZeZ	ddœdefdd„Z
ddœdd„Zdddœdd„Zdddœdee fdd„Zddœdefdd„Z‡  ZS )r   Fr>   c                    sD   t ƒ  ||¡ ttƒ| _tƒ | _tt| ƒ| _	| j	 
¡ }|d | _d S )Nr   )Úsuperr%   r   r   Ú_routerr   Ú_filtersr   r   Ú	bus_proxyÚHellorF   )r"   r>   r@   Úhello_reply©Ú	__class__r#   r$   r%   ‚   s   

zDBusConnection.__init__c                 C   s   t ddd� | jS )Nz@conn.router is deprecated, see the docs for APIs to use instead.é   ©Ú
stacklevel)r   rz   r!   r#   r#   r$   ÚrouterŽ   s   ÿzDBusConnection.routerNrM   c                 C   s4   |   ||¡\}}|r|  ||¡ dS | j |¡ dS )z.Serialise and send a :class:`~.Message` objectN)rV   r^   r>   r\   )r"   rM   rP   rU   rQ   r#   r#   r$   Úsend”   s   zDBusConnection.sendr7   rN   c                C   s   |   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.
        )rd   r9   )r"   r8   r#   r#   r$   Úreceivež   s   zDBusConnection.receivec                C   s:   | j |d�}| j |¡ | j |¡D ]}|j |¡ qdS )z\Receive one message and apply filters

        See :meth:`filter`. Returns nothing.
        r7   N)r†   rz   Úincomingr{   ÚmatchesÚqueueÚappend)r"   r8   rb   Úfilterr#   r#   r$   Úrecv_messages§   s
   ÿzDBusConnection.recv_messages)r8   Úunwrapc          	      C   sª   t |ƒ t|ƒ}|du rd}ntddd� t| jƒ}| j||d� 	 | jt|ƒd�}|jj	 
tjd	¡}||kr?|r=t|ƒS |S | j |¡ | j |¡D ]}|j |¡ qKq")
z¤Send a message, wait for the reply and return it

        Filters are applied to other messages received before the reply -
        see :meth:`add_filter`.
        NFzePassing unwrap= to .send_and_get_reply() is deprecated and will break in a future version of Jeepney.r�   r‚   )rP   Tr7   éÿÿÿÿ)r   r9   r   rR   rB   Úsend_messager†   r<   ÚheaderÚfieldsÚgetr   Úreply_serialr   rz   r‡   r{   rˆ   r‰   rŠ   )	r"   rM   r8   r�   r;   rP   Úmsg_inÚreply_tor‹   r#   r#   r$   Úsend_and_get_reply±   s(   ÿ
õz!DBusConnection.send_and_get_replyr   )r‰   Úbufsizer‰   c                C   s    |du r	t |d�}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—   r#   r#   r$   r‹   Ï   s   
zDBusConnection.filterc                C   s8   t |ƒ}t|ƒdkr| jt|ƒd� t|ƒdks
| ¡ 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   r7   )r9   r[   rŒ   r<   Úpopleft)r"   r‰   r8   r;   r#   r#   r$   Úrecv_until_filteredà   s
   ÿz"DBusConnection.recv_until_filteredrv   r   )r2   r3   r4   rX   r%   Úpropertyr„   r
   r…   r�   r†   rŒ   r–   r   r   r‹   r›   Ú__classcell__r#   r#   r   r$   r   �   s    
	
r   c                       s6   e Zd ZdZddœ‡ fdd„
Zdd„ Zdd	„ Z‡  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
    Nr7   c                   s   t ƒ  |¡ || _|| _d S r   )ry   r%   Ú_connectionÚ_timeout)r"   ÚmsggenÚ
connectionr8   r   r#   r$   r%     s   
zProxy.__init__c                 C   s4   | j d u rdnd| j › �}d| j› d| j› |› d�S )NÚ z
, timeout=zProxy(z, ú))rŸ   Ú_msggenrž   )r"   Úextrar#   r#   r$   Ú__repr__  s   zProxy.__repr__c                    s   t  ˆ ¡‡ ‡fdd„ƒ}|S )Nc                     sB   |  dˆj¡}ˆ | i |¤Ž}|jjtju sJ ‚tˆjj||d�ƒS )NrŸ   r7   )	ÚpoprŸ   r�   Úmessage_typer   Úmethod_callr   rž   r–   )ÚargsÚkwargsr8   rb   ©Úmake_msgr"   r#   r$   Úinner  s   ÿz!Proxy._method_call.<locals>.inner)Ú	functoolsÚwraps)r"   r­   r®   r#   r¬   r$   Ú_method_call  s   zProxy._method_call)r2   r3   r4   rw   r%   r¦   r±   r�   r#   r#   r   r$   r   ö   s
    r   c                 C   s   | s
t tt t¡ƒ‚| S )z´Raise 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)rc   r#   r#   r$   rg     s   rg   Fç       @rN   c              
      sÈ   t j t jd�‰t|ƒ‰ ‡ ‡fdd„}z)|ˆj| ƒ t|d�}|D ]}|ˆj|ƒ | t|ˆjdƒƒ¡ q |ˆjt	ƒ W n! t j
yU } zˆ ¡  td|› d�ƒ|‚d}~w   ˆ ¡  ‚ ˆ d¡ ˆS )	z=Create a socket and authenticate ready to send D-Bus messages)Úfamilyc                    s   ˆ  tˆ ƒ¡ | |Ž S r   )Ú
settimeoutr<   )Úmethrª   ©r;   r>   r#   r$   Úwith_sock_deadline1  s   z'prep_socket.<locals>.with_sock_deadline)r@   i   zDid not authenticate in z secondsN)rX   ÚAF_UNIXr9   Úconnectr   r\   Úfeedrg   rh   r   r8   rH   ri   r·   )Úaddrr@   r8   rº   ÚauthrÚreq_dataÚer#   r¹   r$   Úprep_socket)  s&   
€
rÂ   ÚSESSIONç      ð?c                 C   s$   t | ƒ}t|||d�}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.
    r7   )r   rÂ   r   )Úbusr@   Úauth_timeoutÚbus_addrr>   Úconnr#   r#   r$   r   G  s   
r   Ú__main__zUnique name:)Frµ   )rÃ   FrÄ   )9rw   rS   Úcollectionsr   Úerrnor   r¯   Ú	itertoolsr   r³   Ú	selectorsr   r   rX   r5   Útypingr   Úwarningsr   Ú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   r9   r<   r=   r   r   rg   rÂ   r   r2   rÈ   ÚprintrF   r#   r#   r#   r$   Ú<module>   sJ    Cu(ÿ
þþ