o
    t3bl                     @   s   d Z dZddlZddlZddlZddlmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZmZmZmZmZ ddlmZ ddlmZmZ erMdd	lmZ ndd
lmZ edZdd ZG dd de Z!G dd deZdS ))
ConnectionSignalMatchZreStructuredText    N)r   LOCAL_IFACE
LOCAL_PATHvalidate_bus_namevalidate_interface_namevalidate_member_namevalidate_object_path)DBusException)ErrorMessageHANDLER_RESULT_NOT_YET_HANDLEDMethodCallMessageMethodReturnMessageSignalMessage)ProxyObject)is_py2is_py3)String)
UTF8Stringzdbus.connectionc                  O      d S N )argskwargsr   r   1/usr/lib/python3/dist-packages/dbus/connection.py_noop3   s   r   c                   @   s   e Zd Zg dZered eeZ				dddZdd Z	d	d
 Z
dd Zedd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )r   )_sender_name_owner_member
_interface_sender_path_handler_args_match_rule_byte_arrays_conn_weakref_destination_keyword_interface_keyword_message_keyword_member_keyword_sender_keyword_path_keyword_int_args_match_utf8_stringsFNc              	   K   s\  |d urt | |d urt| |d urt| |d ur t| d | _t|| _|| _|| _	|| _
|| _|| _|| _trE|dd| _nd|v rMtd|| _|| _|	| _|| _|
| _|| _|| _|| _|sld | _d S i | _|D ]:}|ds~td| z
t|dd  }W n ty   td| w |dk s|dkrtd	| || | j|< qqd S )
Nutf8_stringsF*unexpected keyword argument 'utf8_strings'argz(SignalMatch: unknown keyword argument %s   r   ?   z9SignalMatch: arg match index must be in range(64), not %d)r   r   r   r	   r#   weakrefrefr%   r   r   r   r    r!   r   r   popr-   	TypeErrorr$   r*   r+   r)   r'   r(   r&   r"   r,   
startswithint
ValueError)selfconnsenderobject_pathdbus_interfacememberhandlerbyte_arraysZsender_keywordZpath_keywordZinterface_keywordZmember_keywordZmessage_keywordZdestination_keywordr   kwargindexr   r   r   __init__C   sb   

zSignalMatch.__init__c                 C   s   t t| S z-SignalMatch objects are compared by identity.)hashidr:   r   r   r   __hash__~   s   zSignalMatch.__hash__c                 C   s   | |u S rE   r   r:   otherr   r   r   __eq__      zSignalMatch.__eq__c                 C   s   | |uS rE   r   rJ   r   r   r   __ne__   rM   zSignalMatch.__ne__c                 C   s   | j S r   )r   rH   r   r   r   <lambda>   s    zSignalMatch.<lambda>c                 C   s   | j d u rZdg}| jd ur|d| j  | jd ur"|d| j  | jd ur/|d| j  | jd ur<|d| j  | jd urT| j D ]\}}|d||f  qFd|| _ | j S )Nztype='signal'zsender='%s'z	path='%s'zinterface='%s'zmember='%s'z
arg%d='%s',)	r#   r   appendr    r   r   r,   itemsjoin)r:   ZrulerC   valuer   r   r   __str__   s   





zSignalMatch.__str__c                 C   s   d| j t| | j|  f S )Nz<%s at %x "%s" on conn %r>)	__class__rG   r#   r%   rH   r   r   r   __repr__   s   zSignalMatch.__repr__c                 C   s
   || _ d S r   )r   )r:   new_namer   r   r   set_sender_name_owner   s   
z!SignalMatch.set_sender_name_ownerc                 K   s\   |d | j fvr	dS || jkrdS || jkrdS || jkrdS || jkr%dS || jkr,dS dS )NFT)r!   r   r    r   r   r"   )r:   r<   r=   r>   r?   r@   r   r   r   r   matches_removal_spec   s   




z SignalMatch.matches_removal_specc                 C   s  d }| j d | fvrdS | jd urKtdd}trtnt}tr#d|d< |jdi |}| j	 D ]\}}|t
|ksGt|| |rG|| |krJ dS q0| jd | fvrVdS | jd | fvradS | jd | fvrldS zvtoq| j}|d u s{|r{| jst| jd}tr| j|d< |jdi |}i }| jd ur| || j< | jd ur| || j< | jd ur| || j< | jd ur| || j< | jd ur| || j< | jd ur||| j< | j|i | W dS    t  tjddd Y dS )	NFTrA   r.   z&Exception in handler for D-Bus signal:   exc_infor   ) r   Z
get_senderr,   dictr   r   r   r   get_args_listrR   len
isinstancer   
get_memberr   get_interfacer    get_pathr-   r$   r*   r&   Zget_destinationr+   r)   r'   r(   r!   loggingbasicConfig_loggererror)r:   messager   r   Zarg_typerC   rT   r.   r   r   r   maybe_handle_message   sb   











z SignalMatch.maybe_handle_messagec                 C   s<   |   }|d ur|j| | j| j| j| jfi | j d S d S r   )r%   remove_signal_receiverr   r   r   r    r"   )r:   r;   r   r   r   remove   s   

zSignalMatch.remove)FNNNNNN)__name__
__module____qualname__Z_slotsr   rQ   tuple	__slots__rD   rI   rL   rN   propertyr<   rU   rW   rY   rZ   rk   rm   r   r   r   r   r   7   s(    

;:r   c                       s   e Zd ZdZeZ fddZdd Zddd	Z				dd
dZ	dd Z
				dddZdd Zdd Z		dddZ		dddZdd Z  ZS ) r   zzA connection to another application. In this base class there is
    assumed to be no bus daemon.

    :Since: 0.81.0
    c                    sV   t t| j|i | t| ds)d| _g | _i | _	 t | _		 | 
| jj d S d S )N_dbus_Connection_initializedr\   )superr   rD   hasattrrt   "_Connection__call_on_disconnection!_signal_recipients_by_object_path	threadingZLock_signals_lockZadd_message_filterrV   _signal_func)r:   r   r   rV   r   r   rD     s   

zConnection.__init__c                 C   s   |S )a  Return the unique name for the given bus name, activating it
        if necessary and possible.

        If the name is already unique or this connection is not to a
        bus daemon, just return it.

        :Returns: a bus name. If the given `bus_name` exists, the returned
            name identifies its current owner; otherwise the returned name
            does not exist.
        :Raises DBusException: if the implementation has failed
            to activate the given bus name.
        :Since: 0.81.0
        r   )r:   bus_namer   r   r   activate_name_owner  s   zConnection.activate_name_ownerNTc                 K   sn   | dd}|dur!|durtdddlm} |dtdd |}|r.td	d
|  | j| |||dS )a  Return a local proxy for the given remote object.

        Method calls on the proxy are translated into method calls on the
        remote object.

        :Parameters:
            `bus_name` : str
                A bus name (either the unique name or a well-known name)
                of the application owning the object. The keyword argument
                named_service is a deprecated alias for this.
            `object_path` : str
                The object path of the desired object
            `introspect` : bool
                If true (default), attempt to introspect the remote
                object to find out supported methods and their signatures

        :Returns: a `dbus.proxies.ProxyObject`
        named_serviceN3bus_name and named_service cannot both be specifiedr   warnziPassing the named_service parameter to get_object by name is deprecated: please use positional parameters   
stacklevelz4get_object does not take these keyword arguments: %sz, )
introspect)r5   r6   warningsr   DeprecationWarningrS   keysProxyObjectClass)r:   r}   r=   r   r   r   r   r   r   r   
get_object&  s    
zConnection.get_objectc                 K   s   |    |dd}|dur%|durtd|}ddlm} |dtdd t| |||||fi |}	| j  z | j	
|i }
|

|i }|
|g }||	 W | j  |	S | j  w )	a  Arrange for the given function to be called when a signal matching
        the parameters is received.

        :Parameters:
            `handler_function` : callable
                The function to be called. Its positional arguments will
                be the arguments of the signal. By default it will receive
                no keyword arguments, but see the description of
                the optional keyword arguments below.
            `signal_name` : str
                The signal name; None (the default) matches all names
            `dbus_interface` : str
                The D-Bus interface name with which to qualify the signal;
                None (the default) matches all interface names
            `bus_name` : str
                A bus name for the sender, which will be resolved to a
                unique name if it is not already; None (the default) matches
                any sender.
            `path` : str
                The object path of the object which must have emitted the
                signal; None (the default) matches any object path
        :Keywords:
            `utf8_strings` : bool
                If True, the handler function will receive any string
                arguments as dbus.UTF8String objects (a subclass of str
                guaranteed to be UTF-8). If False (default) it will receive
                any string arguments as dbus.String objects (a subclass of
                unicode).
            `byte_arrays` : bool
                If True, the handler function will receive any byte-array
                arguments as dbus.ByteArray objects (a subclass of str).
                If False (default) it will receive any byte-array
                arguments as a dbus.Array of dbus.Byte (subclasses of:
                a list of ints).
            `sender_keyword` : str
                If not None (the default), the handler function will receive
                the unique name of the sending endpoint as a keyword
                argument with this name.
            `destination_keyword` : str
                If not None (the default), the handler function will receive
                the bus name of the destination (or None if the signal is a
                broadcast, as is usual) as a keyword argument with this name.
            `interface_keyword` : str
                If not None (the default), the handler function will receive
                the signal interface as a keyword argument with this name.
            `member_keyword` : str
                If not None (the default), the handler function will receive
                the signal name as a keyword argument with this name.
            `path_keyword` : str
                If not None (the default), the handler function will receive
                the object-path of the sending object as a keyword argument
                with this name.
            `message_keyword` : str
                If not None (the default), the handler function will receive
                the `dbus.lowlevel.SignalMessage` as a keyword argument with
                this name.
            `arg...` : unicode or UTF-8 str
                If there are additional keyword parameters of the form
                ``arg``\ *n*, match only signals where the *n*\ th argument
                is the value given for that keyword parameter. As of this
                time only string arguments can be matched (in particular,
                object paths and signatures can't).
            `named_service` : str
                A deprecated alias for `bus_name`.
        r   Nr   r   r   zrPassing the named_service parameter to add_signal_receiver by name is deprecated: please use positional parametersr   r   )Z_require_main_loopr5   r6   r   r   r   r   rz   acquirerx   
setdefaultrQ   release)r:   Zhandler_functionsignal_namer>   r}   pathkeywordsr   r   matchby_interface	by_membermatchesr   r   r   add_signal_receiverK  s4   G


zConnection.add_signal_receiverc                 c   s    |d ur
d |f}nd}|d urd |f}nd}|d ur d |f}nd}|D ]5}| j |}|d u r1q$|D ]%}||d }|d u r@q3|D ]}||d }	|	d u rOqB|	D ]}
|
V  qQqBq3q$d S )Nr   )rx   get)r:   r   r>   r?   Z	path_keysZinterface_keysZmember_keysr   r   r   mr   r   r   _iter_easy_matches  s8   


zConnection._iter_easy_matchesc                 K   sb  | dd }|d ur!|d urtd|}ddlm} |dtdd g }	g }
| j  zt| j|d }|d u r>W | j	  d S ||d }|d u rPW | j	  d S ||d }|d u rbW | j	  d S |D ]}||u sw|j
|||||fi |r}|
| qd|	| qd|	r|	||< n||= |s||= |s| j|= W | j	  n| j	  w |
D ]}| | qd S )Nr   r   r   r   zuPassing the named_service parameter to remove_signal_receiver by name is deprecated: please use positional parametersr   r   )r5   r6   r   r   r   rz   r   rx   r   r   rZ   rQ   _clean_up_signal_match)r:   Zhandler_or_matchr   r>   r}   r   r   r   r   newZ	deletionsr   r   r   r   r   r   r   rl     sh   

z!Connection.remove_signal_receiverc                 C   r   r   r   )r:   r   r   r   r   r     s   z!Connection._clean_up_signal_matchc              	   C   s   t |tstS | }| }| }| |||D ]}|| q|tkrN|t	krN|dkrN| j
D ]}z||  W q1 tyM   t  tjddd Y q1w tS )zvD-Bus filter function. Handle signals by dispatching to Python
        callbacks kept in the match-rule tree.
        ZDisconnectedz-Exception in handler for Disconnected signal:r\   r]   )rb   r   r   rd   re   rc   r   rk   r   r   rw   	Exceptionrf   rg   rh   ri   )r:   rj   r>   r   r   r   cbr   r   r   r{     s0   


zConnection._signal_func      Fc              
      s  |t kr
tdt  |tkrtdt t|
dtr$|ddd< nd|v r,tdt||||d}z
|j|d|i W n t	yY } zt
  td	|||j|  d
}~ww d
u ri d
u ri| | d
S d
u rot d
u rut  fdd}| j|||	|dS )aS  Call the given method, asynchronously.

        If the reply_handler is None, successful replies will be ignored.
        If the error_handler is None, failures will be ignored. If both
        are None, the implementation may request that no reply is sent.

        :Returns: The dbus.lowlevel.PendingCall.
        :Since: 0.81.0
        1Methods may not be called on the reserved path %s6Methods may not be called on the reserved interface %sr[   r.   Fr/   Zdestinationr   Z	interfacemethod	signature<Unable to set arguments %r according to signature %r: %s: %sNc                    s^   t | tr| jdi   d S t | tr% t|  d|  i d S  td|   d S )Nnamez%Unexpected type for reply message: %rr   )rb   r   r`   r   r
   Zget_error_namer6   )rj   error_handlerget_args_optsreply_handlerr   r   msg_reply_handler\  s   

z0Connection.call_async.<locals>.msg_reply_handler)require_main_loop)r   r
   r   r_   r   r   r6   r   rQ   r   rf   rg   rh   ri   rV   Zsend_messager   Zsend_message_with_reply)r:   r}   r=   r>   r   r   r   r   r   timeoutrA   r   r   rj   er   r   r   r   
call_async)  sP   


	zConnection.call_asyncc	              
   K   s   |t kr
tdt  |tkrtdt t|d}
tr$|	dd|
d< nd|	v r,tdt||||d}z
|j|d|i W n t	yY } zt
  td	|||j|  d
}~ww | ||}|jdi |
}t|dkrpd
S t|dkrz|d S t|S )zECall the given method, synchronously.
        :Since: 0.81.0
        r   r   r[   r.   Fr/   r   r   r   Nr   r\   r   )r   r
   r   r_   r   r   r6   r   rQ   r   rf   rg   rh   ri   rV   Z!send_message_with_reply_and_blockr`   ra   rq   )r:   r}   r=   r>   r   r   r   r   rA   r   r   rj   r   Zreply_messageZ	args_listr   r   r   call_blockingi  sJ   

zConnection.call_blockingc                 C   s   | j | dS )zArrange for `callable` to be called with one argument (this
        Connection object) when the Connection becomes
        disconnected.

        :Since: 0.83.0
        N)rw   rQ   )r:   callabler   r   r   call_on_disconnection  s   z Connection.call_on_disconnection)NNT)NNNN)r   FT)r   F)rn   ro   rp   __doc__r   r   rD   r~   r   r   r   rl   r   r{   r   r   r   __classcell__r   r   r|   r   r      s4    
&
d
; 
A
-r   )"__all__Z__docformat__rf   ry   r3   Z_dbus_bindingsr   Z_Connectionr   r   r   r   r   r	   Zdbus.exceptionsr
   Zdbus.lowlevelr   r   r   r   r   Zdbus.proxiesr   Zdbus._compatr   r   r   r   Z	getLoggerrh   r   objectr   r   r   r   r   <module>   s$   $
 D