
    @iai                         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 dd	lmZ  ej8                  d
      Zd Z G d de      Z  G d de      Zy))
ConnectionSignalMatch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)Stringzdbus.connectionc                       y N )argskwargss     1/usr/lib/python3/dist-packages/dbus/connection.py_noopr   0   s        c                   |    e Zd Zg dZ ee      Z	 	 	 	 ddZd Zd Zd Z	 e
d       Zd Zd	 Zd
 Zd Zd Zd Zy)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_matchNc                    |t        |       |t        |       |t        |       |t        |       d | _        t        j                  |      | _        || _        || _	        || _
        || _        || _        || _        d|v rt        d      || _        || _        |	| _        || _        |
| _        || _        || _        || _        |sd | _        y i | _        |D ]Z  }|j1                  d      st        d|z        	 t3        |dd        }|dk  s|dkD  rt        d|z        ||   | j.                  |<   \ y # t4        $ r t        d|z        w xY w)	Nutf8_strings*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   	TypeErrorr'   r-   r.   r,   r*   r+   r)   r%   r/   
startswithint
ValueError)selfconnsenderobject_pathdbus_interfacememberhandlerbyte_arrayssender_keywordpath_keywordinterface_keywordmember_keywordmessage_keyworddestination_keywordr   kwargindexs                    r   __init__zSignalMatch.__init__>   s     (%#N3f%" -
$[[.( 
 #)V#HII'-)-"3 /$7!!#'D #%D  <''.#$N&+%, - --ab	NE 19
# %8:?%@ A A.4Um$$U+< " -#$N&+%, - --s   =D88Ec                 *    t        t        |             S z-SignalMatch objects are compared by identity.)hashidr<   s    r   __hash__zSignalMatch.__hash__w   s    BtH~r   c                 
    | |u S rN   r   r<   others     r   __eq__zSignalMatch.__eq__{   s    u}r   c                 
    | |uS rN   r   rT   s     r   __ne__zSignalMatch.__ne__   s    5  r   c                     | j                   S r   )r"   rQ   s    r   <lambda>zSignalMatch.<lambda>   s
    4<< r   c                 >   | j                   dg}| j                  |j                  d| j                  z         | j                  |j                  d| j                  z         | j                  |j                  d| j                  z         | j
                  |j                  d| j
                  z         | j                  8| j                  j                         D ]  \  }}|j                  d||fz          dj                  |      | _         | 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<   rulerK   values       r   __str__zSignalMatch.__str__   s    ::#$D||'MDLL89zz%K$**45*,t>?||'MDLL89##/$($8$8$>$>$@ ?LE5KKu~ =>? $DJzzr   c                 j    d| j                   t        |       | j                  | j                         fz  S )Nz<%s at %x "%s" on conn %r>)	__class__rP   r&   r(   rQ   s    r   __repr__zSignalMatch.__repr__   s2    ,>>2d8TZZ9K9K9MNO 	Pr   c                     || _         y r   )r   )r<   new_names     r   set_sender_name_ownerz!SignalMatch.set_sender_name_owner   s
    "*r   c                     |d | j                   fvry|| j                  k7  ry|| j                  k7  ry|| j                  k7  ry|| j                  k7  ry|| j
                  k7  ryy)NFT)r$   r"   r#   r!   r    r%   )r<   r>   r?   r@   rA   rB   r   s          r   matches_removal_specz SignalMatch.matches_removal_spec   sf    4//T\\!$**$T__,T\\!T%%%r   c                    d }| j                   d |j                         fvry| j                  kt        d      } |j                  di |}| j                  j                         D ]0  \  }}|t        |      k\  st        ||   t              r	||   |k7  s0 y | j                  d |j                         fvry| j                  d |j                         fvry| j                  d |j                         fvry	 || j                  s|j	                  | j                        }i }| j                   |j                         || j                   <   | j"                  |j%                         || j"                  <   | j&                  |j                         || j&                  <   | j(                  |j                         || j(                  <   | j*                  |j                         || j*                  <   | j,                  ||| j,                  <    | j.                  |i | y#  t1        j2                          t4        j7                  dd       Y yxY w)NFTrC   z&Exception in handler for D-Bus signal:   exc_infor   )r   
get_senderr/   dictget_args_listr^   len
isinstancer   r    
get_memberr!   get_interfacer#   get_pathr'   r-   r)   get_destinationr.   r,   r*   r+   r$   loggingbasicConfig_loggererror)r<   messager   r   rK   ra   s         r   maybe_handle_messagez SignalMatch.maybe_handle_message   s?    ""41C1C1E*FF+d+F(7((262D $ 4 4 : : < !uSY&%d5k6:E{e+ 	! <<g&8&8&:;;??4)>)>)@"AA::dG$4$4$677	P |4#4#4,,9J9J,KF##//6/A/A/Ct++,((44;4K4K4Mt001!!--4-=-=-?t))*##//6/A/A/Ct++,&&2292G2G2It../$$007t,,-DMM4*6* 	P!MMBQMOs   7D&H -Ic                     | j                         }|J |j                  | | j                  | j                  | j                  | j
                  fi | j                   y y r   )r(   remove_signal_receiverr    r!   r"   r#   r%   )r<   r=   s     r   removezSignalMatch.remove   sW    !!#'D''dll(,(,

< +/*:*:< r   )FNNNNNN)__name__
__module____qualname___slotstuple	__slots__rL   rR   rV   rX   propertyr>   rb   re   rh   rj   r~   r   r   r   r   r   r   4   se    EF fI /4378<;?	7<r! /0F&P+ 1f<r   r   c                        e Zd ZdZeZ fdZd ZddZ	 	 	 	 ddZ	d Z
	 	 	 	 ddZd Zd	 Z	 	 dd
Z	 	 ddZd Z x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                     t        t        | 
  |i | t        | d      sVd| _        g | _        i | _        	 t        j                         | _	        	 | j                  | j                  j                         y y )N_dbus_Connection_initializedrm   )superr   rL   hasattrr   "_Connection__call_on_disconnection!_signal_recipients_by_object_path	threadingLock_signals_lockadd_message_filterrd   _signal_func)r<   r   r   rd   s      r   rL   zConnection.__init__   sr    j$($9&9 t;<01D-+-D(57D2> "+!1D=##DNN$?$?@ =r   c                     |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_names     r   activate_name_ownerzConnection.activate_name_owner  s	     r   c                     |j                  dd      }|$|t        d      ddlm}  |dt        d       |}|r+t        d	d
j                  |j                               z        | j                  | |||      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)popr8   warningsr   DeprecationWarningr_   keysProxyObjectClass)r<   r   r?   r   r   r   r   s          r   
get_objectzConnection.get_object  s    ( 

?D9$# !/ 0 0% C#3 %H ,.2ii.FG H H $$T8[0: % < 	<r   c                    | j                          |j                  dd      }|$|t        d      |}ddlm}  |dt
        d       t        | |||||fi |}	| j                  j                          	 | j                  j                  |i       }
|
j                  |i       }|j                  |g       }|j                  |	       | j                  j                          |	S # | j                  j                          w xY 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   )_require_main_loopr   r8   r   r   r   r   r   acquirer   
setdefaultr]   release)r<   handler_functionsignal_namer@   r   pathkeywordsr   r   matchby_interface	by_membermatchess                r   add_signal_receiverzConnection.add_signal_receiver=  s   N 	! _d;$# !, - -$H% K#3 D(D.')9G=EG 	""$	)AALL"L$//CI**;;GNN5!&&( &&(s   5AC" "C>c              #     K   |d |f}nd}|d |f}nd}|d |f}nd}|D ]c  }| j                   j                  |      }|!|D ]>  }|j                  |d       }||D ]"  }|j                  |d       }	|	|	D ]  }
|
  $ @ e y w)Nr   )r   get)r<   r   r@   rA   	path_keysinterface_keysmember_keysr   r   r   ms              r   _iter_easy_matcheszConnection._iter_easy_matches  s     tII%"N3N$N.K!K 	 DAAEEdKL#"0 	 (,,^TB	$)  F'mmFD9G $   	 		 		 s   BBc                 P   |j                  dd       }|$|t        d      |}ddlm}  |dt        d       g }	g }
| j
                  j                          	 | j                  j                  |d       }|	 | j
                  j                          y |j                  |d       }|	 | j
                  j                          y |j                  |d       }|	 | j
                  j                          y |D ]@  }||u s |j                  |||||fi |r|
j                  |       0|	j                  |       B |	r|	||<   n||= |s||= |s| j                  |= | j
                  j                          |
D ]  }| j                  |        y # | j
                  j                          w xY w)Nr   r   r   r   zuPassing the named_service parameter to remove_signal_receiver by name is deprecated: please use positional parametersr   r   )r   r8   r   r   r   r   r   r   r   r   rj   r]   _clean_up_signal_match)r<   handler_or_matchr   r@   r   r   r   r   r   new	deletionsr   r   r   r   s                  r   r   z!Connection.remove_signal_receiver  s    !_d;$# !, - -$H% ) $3
 	""$!	)AAEEdFJLL#: &&(9 %((>I 4 &&(3  mmK6G. &&(+ ! 
&$-1u11(262@2=2B	>
 5=> $$U+JJu%
& ),	+&k* $^4' BB4H&&( 	/E''.	/ &&(s   F	 F	 F	 2A$F	 	F%c                      y r   r   )r<   r   s     r   r   z!Connection._clean_up_signal_match  s    r   c                    t        |t              st        S |j                         }|j	                         }|j                         }| j                  |||      D ]  }|j                  |        |t        k(  r(|t        k(  r|dk(  r| j                  D ]  }	  ||         t        S # t        $ r. t        j                          t        j                  dd       Y Jw xY w)zvD-Bus filter function. Handle signals by dispatching to Python
        callbacks kept in the match-rule tree.
        Disconnectedz-Exception in handler for Disconnected signal:rm   rn   )rt   r   r   rv   rw   ru   r   r~   r   r   r   	Exceptionry   rz   r{   r|   )r<   r}   r@   r   r   r   cbs          r   r   zConnection._signal_func  s    
 '=111 ..0!((*,,T>-8: 	0E&&w/	0 k)J>)22 //tH/ .- ! /'')MM #",- " //s   B''4CCc                    |t         k(  rt        dt         z        |t        k(  rt        dt        z        t        |
      d|v rt	        d      t        ||||      }	  |j                  |d|i | j                  |       y	t        t        fd
}| j                  |||	|      S # t        $ r=}t        j                          t        j                  d|||j                  |        d	}~ww xY w)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 %srl   r1   r2   destinationr   	interfacemethod	signature<Unable to set arguments %r according to signature %r: %s: %sNc                     t        | t              r  | j                  di   y t        | t              r. t	        | j                         d| j                         i       y  t        d| z               y )Nnamez%Unexpected type for reply message: %rr   )rt   r   rr   r   r   get_error_namer8   )r}   error_handlerget_args_optsreply_handlers    r   msg_reply_handlerz0Connection.call_async.<locals>.msg_reply_handlerL  s    '#674w44E}EFG\2m-4-B-B-DF1G1G1I F G i )68?)@ A Br   )require_main_loop)r   r   r   rq   r8   r   r]   r   ry   rz   r{   r|   rd   send_messager   send_message_with_reply)r<   r   r?   r@   r   r   r   r   r   timeoutrC   r   r   r}   er   r   s          ``       @r   
call_asynczConnection.call_async  s8    *$ !*,6!7 8 8[( !/1<!= > > 5V#HII#)4.<+13
	GNN6Y6  ]%:g& !M !M	B ++G5F(/:K , M 	M5  	!MM 1	1;;; 	s   %B9 9	C?8C::C?c	                    |t         k(  rt        dt         z        |t        k(  rt        dt        z        t        |      }
d|	v rt	        d      t        ||||      }	  |j                  |d|i | j                  ||      } |j                  di |
}t        |      d
k(  ry	t        |      dk(  r|d
   S t!        |      S # t        $ r=}t        j                          t        j                  d|||j                  |        d	}~ww xY w)zECall the given method, synchronously.
        :Since: 0.81.0
        r   r   rl   r1   r2   r   r   r   Nr   rm   r   )r   r   r   rq   r8   r   r]   r   ry   rz   r{   r|   rd   !send_message_with_reply_and_blockrr   rs   r   )r<   r   r?   r@   r   r   r   r   rC   r   r   r}   r   reply_message	args_lists                  r   call_blockingzConnection.call_blockingY  s2    *$ !*,6!7 8 8[( !/1<!= > > 5V#HII#)4.<+13
	GNN6Y6 >>W/M//@-@	y>Q^q Q<##!  	!MM 1	1;;; 	s   "C 	D8DDc                 :    | j                   j                  |       y)zArrange for `callable` to be called with one argument (this
        Connection object) when the Connection becomes
        disconnected.

        :Since: 0.83.0
        N)r   r]   )r<   callables     r   call_on_disconnectionz Connection.call_on_disconnection  s     	$$++H5r   )NNT)NNNN)      FT)r   F)r   r   r   __doc__r   r   rL   r   r   r   r   r   r   r   r   r   r   __classcell__)rd   s   @r   r   r      s|     #A& #<L /315+/'+	bH < ,0.2(,$(	9/v.@ .3%)<M~ 04"')$V6r   r   )!__all____docformat__ry   r   r6   _dbus_bindingsr   _Connectionr   r   r   r	   r
   r   dbus.exceptionsr   dbus.lowlevelr   r   r   r   r   dbus.proxiesr   dbus._compatr   r   r   	getLoggerr{   r   objectr   r   r   r   <module>r      s}   2 ("   I I I *( ( % ' ! '

-
.	u<& u<p_6 _6r   