U
    º±K^žl  ã                   @   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ršd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   s   d S ©N© )ÚargsÚkwargsr   r   ú1/usr/lib/python3/dist-packages/dbus/connection.pyÚ_noop3   s    r   c                   @   s¦   e Zd Zddddddddd	d
dddddddg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   sf  |d k	rt |ƒ |d k	r t|ƒ |d k	r0t|ƒ |d k	r@t|ƒ d | _t |¡| _|| _|| _	|| _
|| _|| _|| _trŠ| dd¡| _nd|krštdƒ‚|| _|| _|	| _|| _|
| _|| _|| _|| _|sÖd | _nŒi | _|D ]€}| d¡sútd| ƒ‚zt|dd … ƒ}W n" tk
r0   td| ƒ‚Y nX |dk sF|dkrRtd	| ƒ‚|| | j|< qàd 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   ZkwargÚindexr   r   r   Ú__init__C   s^    
ÿÿÿzSignalMatch.__init__c                 C   s   t t| ƒƒS ©z-SignalMatch objects are compared by identity.)ÚhashÚid©r9   r   r   r   Ú__hash__~   s    zSignalMatch.__hash__c                 C   s   | |kS rC   r   ©r9   Úotherr   r   r   Ú__eq__‚   s    zSignalMatch.__eq__c                 C   s   | |k	S rC   r   rH   r   r   r   Ú__ne__†   s    zSignalMatch.__ne__c                 C   s   | j S r   )r   rF   r   r   r   Ú<lambda>Š   ó    zSignalMatch.<lambda>c                 C   sº   | j d kr´dg}| jd k	r*| d| j ¡ | jd k	rD| d| j ¡ | jd k	r^| d| j ¡ | jd k	rx| d| j ¡ | jd k	r¨| j ¡ D ]\}}| d||f ¡ qŒd |¡| _ | 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)r9   ZrulerA   Ú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__rE   r"   r$   rF   r   r   r   Ú__repr__Ÿ   s    ÿzSignalMatch.__repr__c                 C   s
   || _ d S r   )r   )r9   Únew_namer   r   r   Úset_sender_name_owner£   s    z!SignalMatch.set_sender_name_ownerc                 K   s\   |d | j fkrdS || jkr dS || jkr.dS || jkr<dS || jkrJdS || jkrXdS dS )NFT)r    r   r   r   r   r!   )r9   r;   r<   r=   r>   r?   r   r   r   r   Úmatches_removal_spec¦   s    




z SignalMatch.matches_removal_specc                 C   sî  d }| j d | ¡ fkrdS | jd k	r’tdd}tr6tnt}trFd|d< |jf |Ž}| j 	¡ D ]4\}}|t
|ƒksŠt|| |ƒrŠ|| |kr\ dS q\| jd | ¡ fkr¨dS | jd | ¡ fkr¾dS | jd | ¡ fkrÔdS zòtoÞ| j}|d ksô|rô| jst| jd}tr| j|d< |jf |Ž}i }| jd k	r:| ¡ || j< | jd k	rT| ¡ || j< | jd k	rn| ¡ || j< | jd k	rˆ| ¡ || j< | jd k	r¢| ¡ || j< | jd k	r¸||| j< | j||Ž W n"   t ¡  tjddd Y nX dS )NFT©r@   r-   z&Exception in handler for D-Bus signal:é   ©Úexc_info) r   Z
get_senderr+   Údictr   r   r   r   Úget_args_listrP   ÚlenÚ
isinstancer   Ú
get_memberr   Úget_interfacer   Úget_pathr,   r#   r)   r%   Zget_destinationr*   r(   r&   r'   r    ÚloggingÚbasicConfigÚ_loggerÚerror)r9   Úmessager   r   Zarg_typerA   rR   r-   r   r   r   Úmaybe_handle_message¶   s^    

ÿ
þ


z SignalMatch.maybe_handle_messagec                 C   s4   |   ¡ }|d k	r0|j| | j| j| j| jf| jŽ d S r   )r$   Úremove_signal_receiverr   r   r   r   r!   )r9   r:   r   r   r   Úremoveð   s    
 þýzSignalMatch.remove)FNNNNNN)Ú__name__Ú
__module__Ú__qualname__Z_slotsr   rO   ÚtupleÚ	__slots__rB   rG   rJ   rK   Úpropertyr;   rS   rU   rW   rX   ri   rk   r   r   r   r   r   7   sH           û
          ü
;: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                    sJ   t t| ƒj||Ž t| dƒsFd| _g | _i | _t ¡ | _	|  
| jj¡ d S )NÚ_dbus_Connection_initializedrZ   )Úsuperr   rB   Úhasattrrr   Ú"_Connection__call_on_disconnectionÚ!_signal_recipients_by_object_pathÚ	threadingZLockÚ_signals_lockZadd_message_filterrT   Ú_signal_func)r9   r   r   ©rT   r   r   rB     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   )r9   Úbus_namer   r   r   Úactivate_name_owner  s    zConnection.activate_name_ownerNTc                 K   sn   |  dd¡}|dk	rB|dk	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_serviceNú3bus_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)r4   r5   Úwarningsr€   ÚDeprecationWarningrQ   ÚkeysÚProxyObjectClass)r9   r{   r<   r„   r   r}   r€   r   r   r   Ú
get_object&  s"     þÿ
ÿzConnection.get_objectc                 K   s°   |   ¡  | dd¡}|dk	rJ|dk	r,tdƒ‚|}ddlm} |dtdd t| |||||f|Ž}	| j ¡  z4| j
 |i ¡}
|
 |i ¡}| |g ¡}| |	¡ W 5 | j 	¡  X |	S )	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_loopr4   r5   r…   r€   r†   r   rx   ÚacquireÚreleaserv   Ú
setdefaultrO   )r9   Zhandler_functionÚsignal_namer=   r{   ÚpathÚkeywordsr}   r€   ÚmatchÚby_interfaceÚ	by_memberÚmatchesr   r   r   Úadd_signal_receiverK  s8    G þ
 ÿÿ
 ÿzConnection.add_signal_receiverc                 c   s¶   |d k	rd |f}nd}|d k	r(d |f}nd}|d k	r>d |f}nd}|D ]j}| j  |¡}|d kr`qF|D ]J}| |d ¡}|d kr~qd|D ]*}| |d ¡}	|	d krœq‚|	D ]
}
|
V  q q‚qdqFd S )Nr   )rv   Úget)r9   rŽ   r=   r>   Z	path_keysZinterface_keysZmember_keysr‘   r’   r“   Úmr   r   r   Ú_iter_easy_matches¯  s.    


zConnection._iter_easy_matchesc                 K   sB  |  dd ¡}|d k	rB|d k	r$tdƒ‚|}ddlm} |dtdd g }	g }
| j ¡  zÆ| j 	|d ¡}|d krtW ¢¬d S | 	|d ¡}|d krW ¢d S | 	|d ¡}|d kr¬W ¢td S |D ]8}||ksÒ|j
|||||f|ŽrÞ|
 |¡ q°|	 |¡ q°|	rø|	||< n ||= |s||= |s| j|= W 5 | j ¡  X |
D ]}|  |¡ q,d S )Nr}   r~   r   r   zuPassing the named_service parameter to remove_signal_receiver by name is deprecated: please use positional parametersr   r‚   )r4   r5   r…   r€   r†   rx   rŠ   r‹   rv   r•   rX   rO   Ú_clean_up_signal_match)r9   Zhandler_or_matchr   r=   r{   rŽ   r   r}   r€   ÚnewZ	deletionsr‘   r’   r“   r   r   r   r   rj   Ì  s`     ý
ÿüûÿ
z!Connection.remove_signal_receiverc                 C   s   d S r   r   )r9   r   r   r   r   r˜     s    z!Connection._clean_up_signal_matchc              	   C   s¤   t |tƒstS | ¡ }| ¡ }| ¡ }|  |||¡D ]}| |¡ q4|tkr |t	kr |dkr | j
D ]<}z|| ƒ W qb tk
rœ   t ¡  tjddd Y qbX qbt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:rZ   r[   )r`   r   r   rb   rc   ra   r—   ri   r   r   ru   Ú	Exceptionrd   re   rf   rg   )r9   rh   r=   rŽ   r   r   Úcbr   r   r   ry     s.    
ÿÿþ
ÿzConnection._signal_funcç      ð¿Fc              
      s  |t krtdt  ƒ‚|tkr(tdt ƒ‚t|
d‰trH| dd¡ˆd< nd|krXtdƒ‚t||||d}z|j|d|iŽ W n@ t	k
r¼ } z"t
 ¡  t d	|||j|¡ ‚ W 5 d
}~X Y nX ˆd
krÜˆ d
krÜ|  |¡ d
S ˆd
krèt‰ˆ d
krôt‰ ‡ ‡‡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 %sú6Methods may not be called on the reserved interface %srY   r-   Fr.   ©ZdestinationrŽ   Z	interfaceÚmethodÚ	signatureú<Unable to set arguments %r according to signature %r: %s: %sNc                    sV   t | tƒrˆ| jf ˆŽŽ  n6t | tƒrBˆ t|  ¡ d|  ¡ iŽƒ nˆ td|  ƒƒ d S )NÚnamez%Unexpected type for reply message: %r)r`   r   r^   r   r
   Zget_error_namer5   )rh   ©Ú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•   r5   r   rO   rš   rd   re   rf   rg   rT   Zsend_messager   Zsend_message_with_reply)r9   r{   r<   r=   r    r¡   r   r§   r¥   Útimeoutr@   r©   r   rh   Úer¨   r   r¤   r   Ú
call_async)  sR    ÿÿ
ý   þ
	þzConnection.call_asyncc	              
   K   s  |t krtdt  ƒ‚|tkr(tdt ƒ‚t|d}
trH|	 dd¡|
d< nd|	krXtdƒ‚t||||d}z|j|d|iŽ W n@ t	k
r¼ } z"t
 ¡  t d	|||j|¡ ‚ W 5 d
}~X Y nX |  ||¡}|jf |
Ž}t|ƒdkræd
S t|ƒdkrú|d S t|ƒS d
S )zECall the given method, synchronously.
        :Since: 0.81.0
        r   rž   rY   r-   Fr.   rŸ   r¡   r¢   Nr   rZ   )r   r
   r   r]   r   r•   r5   r   rO   rš   rd   re   rf   rg   rT   Z!send_message_with_reply_and_blockr^   r_   ro   )r9   r{   r<   r=   r    r¡   r   rª   r@   r   r¦   rh   r«   Zreply_messageZ	args_listr   r   r   Úcall_blockingi  sN    ÿÿ
ý   þ ÿzConnection.call_blockingc                 C   s   | j  |¡ dS )z©Arrange for `callable` to be called with one argument (this
        Connection object) when the Connection becomes
        disconnected.

        :Since: 0.83.0
        N)ru   rO   )r9   Úcallabler   r   r   Úcall_on_disconnection–  s    z Connection.call_on_disconnection)NNT)NNNN)NNNN)rœ   FT)rœ   F)rl   rm   rn   Ú__doc__r   rˆ   rB   r|   r‰   r”   r—   rj   r˜   ry   r¬   r­   r¯   Ú__classcell__r   r   rz   r   r   ú   s4   
&    ü
d    ü
;     ý
A  þ
-r   )"Ú__all__Z__docformat__rd   rw   r2   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	getLoggerrf   r   Úobjectr   r   r   r   r   Ú<module>   s"   $
 D