U
    K^(F                     @   s   d Z d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mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# d	eeef Z$d
Z%e&dZ'G dd de(Z)G dd deZ*dS ))BusConnectionZreStructuredText    N)BUS_DAEMON_IFACEBUS_DAEMON_NAMEBUS_DAEMON_PATHBUS_SESSIONBUS_STARTER
BUS_SYSTEM DBUS_START_REPLY_ALREADY_RUNNINGDBUS_START_REPLY_SUCCESSNAME_FLAG_ALLOW_REPLACEMENTNAME_FLAG_DO_NOT_QUEUENAME_FLAG_REPLACE_EXISTINGRELEASE_NAME_REPLY_NON_EXISTENTRELEASE_NAME_REPLY_NOT_OWNERRELEASE_NAME_REPLY_RELEASED REQUEST_NAME_REPLY_ALREADY_OWNERREQUEST_NAME_REPLY_EXISTSREQUEST_NAME_REPLY_IN_QUEUE REQUEST_NAME_REPLY_PRIMARY_OWNERvalidate_bus_namevalidate_error_namevalidate_interface_namevalidate_member_namevalidate_object_path)
Connection)DBusException)HANDLER_RESULT_NOT_YET_HANDLED)is_py2zWtype='signal',sender='%s',interface='%s',member='NameOwnerChanged',path='%s',arg0='%%s'z)org.freedesktop.DBus.Error.NameHasNoOwnerzdbus.busc                   @   s    e Zd ZdZdd Zdd ZdS )NameOwnerWatch)_match_pending_callc              	      sn   t   fdd} fdd}|j|dttt d| _i }trJd|d< |jtttd	d
 f|f|| _d S )Nc                    s    | d S N )ZownedZ	old_owner	new_owner)callbackr"   */usr/lib/python3/dist-packages/dbus/bus.py	signal_cbB   s    z*NameOwnerWatch.__init__.<locals>.signal_cbc                    s:   |   tkrd n t  tjd | j| d fd d S )N zGetNameOwner(%s) failed:)exc_info)get_dbus_name_NAME_HAS_NO_OWNERloggingZbasicConfig_loggerdebug	__class__)ebus_namer$   r"   r%   error_cbE   s    

z)NameOwnerWatch.__init__.<locals>.error_cbZNameOwnerChanged)Zarg0Tutf8_stringsGetNameOwners)	r   add_signal_receiverr   r   r   r   r   
call_asyncr    )selfZbus_connr1   r$   r&   r2   keywordsr"   r0   r%   __init__?   s0      zNameOwnerWatch.__init__c                 C   s8   | j d k	r| j   | jd k	r(| j  d | _ d | _d S r!   )r   remover    cancel)r8   r"   r"   r%   r<   ^   s    



zNameOwnerWatch.cancelN)__name__
__module____qualname__	__slots__r:   r<   r"   r"   r"   r%   r   <   s   r   c                       s   e Zd ZdZeZeZeZ	e
ZeZedfddZd* fdd	Zdd Zd	d
 Zd+ddZdd Zd,ddZd-ddZdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Z  Z S ).r   zxA connection to a D-Bus daemon that implements the
    ``org.freedesktop.DBus`` pseudo-service.

    :Since: 0.81.0
    Nc                 C   s"   | j ||d}t |_i |_|S )N)mainloop)Z_new_for_busweakrefWeakValueDictionaryZ
_bus_names_signal_sender_matches)clsZaddress_or_typerA   Zbusr"   r"   r%   __new__{   s    
zBusConnection.__new__c                    s   | dd }|d k	rB|d k	r$td|}ddlm} |dtdd tt| j|||||f| |d k	r|tkr|d d d	kr fd
d}	n j	}	| 
||	}
|
| j < | t   S )Nnamed_service3bus_name and named_service cannot both be specifiedr   warnzrPassing the named_service parameter to add_signal_receiver by name is deprecated: please use positional parameters   
stacklevel   :c                    s   | dkr    d S )Nr'   )r;   )r#   matchr"   r%   r$      s    z3BusConnection.add_signal_receiver.<locals>.callback)pop	TypeErrorwarningsrJ   DeprecationWarningsuperr   r6   r   Zset_sender_name_ownerwatch_name_ownerrD   add_match_stringstr)r8   Zhandler_functionZsignal_nameZdbus_interfacer1   pathr9   rG   rJ   r$   watchr.   rP   r%   r6      s6     
   
z!BusConnection.add_signal_receiverc                 C   s0   |  t| | j|d }|d k	r,|  d S r!   ) remove_match_string_non_blockingrY   rD   rR   r<   )r8   rQ   r[   r"   r"   r%   _clean_up_signal_match   s    z$BusConnection._clean_up_signal_matchc              
   C   s   |d k	rx|d d dkrx|t krxz| |W S  tk
rt } z*| tkrL | | | | W Y S d }~X Y q|X n|S d S )NrN   rO   )r   get_name_ownerr   r)   r*   start_service_by_name)r8   r1   r/   r"   r"   r%   activate_name_owner   s    
"z!BusConnection.activate_name_ownerTFc                 K   s|   |r|    |dd}|dk	rN|dk	r0tdddlm} |dtdd |}|rhtd	d
|  | j| ||||dS )aJ  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
            `follow_name_owner_changes` : bool
                If the object path is a well-known name and this parameter
                is false (default), resolve the well-known name to the unique
                name of its current owner and bind to that instead; if the
                ownership of the well-known name changes in future,
                keep communicating with the original owner.
                This is necessary if the D-Bus API used is stateful.

                If the object path is a well-known name and this parameter
                is true, whenever the well-known name changes ownership in
                future, bind to the new owner, if any.

                If the given object path is a unique name, this parameter
                has no effect.

        :Returns: a `dbus.proxies.ProxyObject`
        :Raises `DBusException`: if resolving the well-known name to a
            unique name fails
        rG   NrH   r   rI   ziPassing the named_service parameter to get_object by name is deprecated: please use positional parametersrK   rL   z4get_object does not take these keyword arguments: %sz, )
introspectfollow_name_owner_changes)	Z_require_main_looprR   rS   rT   rJ   rU   joinkeysZProxyObjectClass)r8   r1   Zobject_pathrb   rc   kwargsrG   rJ   r"   r"   r%   
get_object   s(    $ 
zBusConnection.get_objectc                 C   s   t | | tttdd|fS )zGet the numeric uid of the process owning the given bus name.

        :Parameters:
            `bus_name` : str
                A bus name, either unique or well-known
        :Returns: a `dbus.UInt32`
        :Since: 0.80.0
        ZGetConnectionUnixUserr5   r   call_blockingr   r   r   r8   r1   r"   r"   r%   get_unix_user   s    	  zBusConnection.get_unix_userr   c              
   C   s$   t | d| tttdd||ffS )a?  Start a service which will implement the given bus name on this Bus.

        :Parameters:
            `bus_name` : str
                The well-known bus name to be activated.
            `flags` : dbus.UInt32
                Flags to pass to StartServiceByName (currently none are
                defined)

        :Returns: A tuple of 2 elements. The first is always True, the
            second is either START_REPLY_SUCCESS or
            START_REPLY_ALREADY_RUNNING.

        :Raises `DBusException`: if the service could not be started.
        :Since: 0.80.0
        TZStartServiceByNamesurh   )r8   r1   flagsr"   r"   r%   r`     s    
 z#BusConnection.start_service_by_namec              	   C   s$   t |dd | tttdd||fS )a  Request a bus name.

        :Parameters:
            `name` : str
                The well-known name to be requested
            `flags` : dbus.UInt32
                A bitwise-OR of 0 or more of the flags
                `NAME_FLAG_ALLOW_REPLACEMENT`,
                `NAME_FLAG_REPLACE_EXISTING`
                and `NAME_FLAG_DO_NOT_QUEUE`
        :Returns: `REQUEST_NAME_REPLY_PRIMARY_OWNER`,
            `REQUEST_NAME_REPLY_IN_QUEUE`,
            `REQUEST_NAME_REPLY_EXISTS` or
            `REQUEST_NAME_REPLY_ALREADY_OWNER`
        :Raises `DBusException`: if the bus daemon cannot be contacted or
            returns an error.
        FZallow_uniqueZRequestNamerl   rh   )r8   namerm   r"   r"   r%   request_name  s      zBusConnection.request_namec                 C   s"   t |dd | tttdd|fS )as  Release a bus name.

        :Parameters:
            `name` : str
                The well-known name to be released
        :Returns: `RELEASE_NAME_REPLY_RELEASED`,
            `RELEASE_NAME_REPLY_NON_EXISTENT`
            or `RELEASE_NAME_REPLY_NOT_OWNER`
        :Raises `DBusException`: if the bus daemon cannot be contacted or
            returns an error.
        Frn   ZReleaseNamer5   rh   )r8   ro   r"   r"   r%   release_name3  s      zBusConnection.release_namec                 C   s(   i }t rd|d< | jtttdddf|S )zReturn a list of all currently-owned names on the bus.

        :Returns: a dbus.Array of dbus.UTF8String
        :Since: 0.81.0
        Tr3   Z	ListNamesr'   r"   r   ri   r   r   r   r8   r9   r"   r"   r%   
list_namesD  s      zBusConnection.list_namesc                 C   s(   i }t rd|d< | jtttdddf|S )zReturn a list of all names that can be activated on the bus.

        :Returns: a dbus.Array of dbus.UTF8String
        :Since: 0.81.0
        Tr3   ZListActivatableNamesr'   r"   rr   rs   r"   r"   r%   list_activatable_namesQ  s      z$BusConnection.list_activatable_namesc                 C   s6   i }t rd|d< t|dd | jtttdd|ff|S )zReturn the unique connection name of the primary owner of the
        given name.

        :Raises `DBusException`: if the `bus_name` has no owner
        :Since: 0.81.0
        Tr3   Frn   r4   r5   )r   r   ri   r   r   r   )r8   r1   r9   r"   r"   r%   r_   ^  s      zBusConnection.get_name_ownerc                 C   s   t | ||S )a  Watch the unique connection name of the primary owner of the
        given name.

        `callback` will be called with one argument, which is either the
        unique connection name, or the empty string (meaning the name is
        not owned).

        :Since: 0.81.0
        )r   )r8   r1   r$   r"   r"   r%   rW   m  s    
zBusConnection.watch_name_ownerc              	   C   s   t | tttdd|fS )zReturn True iff the given bus name has an owner on this bus.

        :Parameters:
            `bus_name` : str
                The bus name to look up
        :Returns: a `bool`
        ZNameHasOwnerr5   )boolri   r   r   r   rj   r"   r"   r%   name_has_ownery  s    
  zBusConnection.name_has_ownerc                 C   s   |  tttdd|f dS )  Arrange for this application to receive messages on the bus that
        match the given rule. This version will block.

        :Parameters:
            `rule` : str
                The match rule
        :Raises `DBusException`: on error.
        :Since: 0.80.0
        AddMatchr5   Nri   r   r   r   r8   Zruler"   r"   r%   rX     s    
   zBusConnection.add_match_stringc              
   C   s   |  tttdd|fdd dS )=  Arrange for this application to receive messages on the bus that
        match the given rule. This version will not block, but any errors
        will be ignored.


        :Parameters:
            `rule` : str
                The match rule
        :Raises `DBusException`: on error.
        :Since: 0.80.0
        ry   r5   Nr7   r   r   r   r{   r"   r"   r%   add_match_string_non_blocking  s        z+BusConnection.add_match_string_non_blockingc                 C   s   |  tttdd|f dS )rx   RemoveMatchr5   Nrz   r{   r"   r"   r%   remove_match_string  s    
   z!BusConnection.remove_match_stringc              
   C   s   |  tttdd|fdd dS )r|   r   r5   Nr}   r{   r"   r"   r%   r]     s        z.BusConnection.remove_match_string_non_blocking)NNNN)TF)r   )r   )!r=   r>   r?   __doc__r   ZTYPE_SESSIONr   ZTYPE_SYSTEMr   ZTYPE_STARTERr
   ZSTART_REPLY_SUCCESSr	   ZSTART_REPLY_ALREADY_RUNNINGrF   r6   r^   ra   rg   rk   r`   rp   rq   rt   ru   r_   rW   rw   rX   r~   r   r]   __classcell__r"   r"   r\   r%   r   g   s<           
9

r   )+__all__Z__docformat__r+   rB   Z_dbus_bindingsr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zdbus.connectionr   Zdbus.exceptionsr   Zdbus.lowlevelr   Zdbus._compatr   Z_NAME_OWNER_CHANGE_MATCHr*   Z	getLoggerr,   objectr   r   r"   r"   r"   r%   <module>   s"   d

+