o
    t3b`                     @   s   d dl Z zd dlmZ W n ey   d dlmZ Y nw d dlZd dlmZ d dlm	Z	m
Z
mZmZ dZe dZd dlmZmZmZmZmZ d dlmZ G d	d
 d
ZG dd dZG dd deZG dd deZdS )    N)RLock)process_introspection_data)DBusExceptionIntrospectionParserExceptionMissingErrorHandlerExceptionMissingReplyHandlerExceptionZrestructuredtextzdbus.proxies)BUS_DAEMON_IFACEBUS_DAEMON_NAMEBUS_DAEMON_PATHINTROSPECTABLE_IFACE
LOCAL_PATH)is_py2c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	_DeferredMethodzXA proxy method which will only get called once we have its
    introspection reply.
    c                 C   s   || _ |j| _|| _|| _d S N)_proxy_method_method_name_append_block)selfproxy_methodappendblock r   ./usr/lib/python3/dist-packages/dbus/proxies.py__init__8   s   
z_DeferredMethod.__init__c                 O   s@   d|v s
| ddr| | j|| d S |   | j|i |S )Nreply_handlerignore_replyF)getr   r   r   r   argskeywordsr   r   r   __call__?   s   
z_DeferredMethod.__call__c                 O   s   |  | j|| d S r   )r   r   r   r   r   r   
call_asyncJ   s   z_DeferredMethod.call_asyncN__name__
__module____qualname____doc__r   r"   r#   r   r   r   r   r   4   s
    r   c                   @   r   )	_ProxyMethodzA proxy method.

    Typically a member of a ProxyObject. Calls to the
    method produce messages that travel over the Bus and are routed
    to a specific named Service.
    c                 C   sX   |t kr
tdt  || _|| _|| _|| _t| || _|d ur't	| || _
d S )Nz1Methods may not be called on the reserved path %s)r   r   _proxy_connection_named_service_object_path_dbus_bindingsZvalidate_member_namer   Zvalidate_interface_name_dbus_interface)r   proxyZ
connectionbus_nameobject_pathZmethod_nameZifacer   r   r   r   U   s   


z_ProxyMethod.__init__c           	   	   O   s  | dd }| dd }| dd}| dd }|d us |d ur4|d u r't |d u r.t |r4td| d| j}|d u rV|d u rG| j}n|d | j }| jj|d }|s\|d urr| j	j
| j| j|| j||||fi | d S | j	j| j| j|| j||fi |S )	Nr   error_handlerr   F	signaturez6ignore_reply and reply_handler cannot be used togetherdbus_interface.)popr   r   	TypeErrorr/   r   r*   _introspect_method_mapr   r+   r#   r,   r-   Zcall_blocking)	r   r    r!   r   r3   r   r4   r5   keyr   r   r   r"   i   sL   



z_ProxyMethod.__call__c              	   O   s   | dd }| dd }| dd }| d| j}|d u r2|r'|d | j }n| j}| jj|d }| jj| j| j	|| j||||fi | d S )Nr   r3   r4   r5   r6   )
r7   r/   r   r*   r9   r   r+   r#   r,   r-   )r   r    r!   r   r3   r4   r5   r:   r   r   r   r#      s(   

z_ProxyMethod.call_asyncNr$   r   r   r   r   r)   N   s
    ,r)   c                   @   s   e Zd ZdZeZeZdZdZ	dZ
		d%dd	Zed
d dddZedd dddZedd 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!d"Zd#d$ ZeZdS )'ProxyObjectzA proxy to the remote Object.

    A ProxyObject is provided by the Bus. ProxyObjects
    have member functions, and can be called like normal Python objects.
    r         NTFc           
      K   s8  | dd}|dur!|durtd|}ddlm} |dtdd | d	d}	|	durB|dur3td
|	}ddlm} |dtdd |rOtdd|  |rU|  || _|durat	
| | | _| _t	| || _|sw||| _d| _g | _i | _t | _|r| jtkr| j| _dS | j| _|  | _dS )a  Initialize the proxy object.

        :Parameters:
            `conn` : `dbus.connection.Connection`
                The bus or connection on which to find this object.
                The keyword argument `bus` is a deprecated alias for this.
            `bus_name` : str
                A bus name for the application owning the object, to be used
                as the destination for method calls and the sender for
                signal matches. The keyword argument ``named_service`` is a
                deprecated alias for this.
            `object_path` : str
                The object path at which the application exports the 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 true (default is false) and the `bus_name` is a
                well-known name, follow ownership changes for that name
        busNz%conn and bus cannot both be specifiedr   )warnz`Passing the bus parameter to ProxyObject by name is deprecated: please use positional parametersr=   )
stacklevelnamed_servicez3bus_name and named_service cannot both be specifiedzjPassing the named_service parameter to ProxyObject by name is deprecated: please use positional parametersz>ProxyObject.__init__ does not take these keyword arguments: %sz, )r7   r8   warningsr?   DeprecationWarningjoinkeysZ_require_main_loop_busr.   Zvalidate_bus_namer,   _requested_bus_nameZvalidate_object_path__dbus_object_path__Zactivate_name_owner_pending_introspect_pending_introspect_queuer9   r   _introspect_lockr    INTROSPECT_STATE_DONT_INTROSPECT_introspect_state'INTROSPECT_STATE_INTROSPECT_IN_PROGRESS_Introspect)
r   Zconnr1   r2   Z
introspectZfollow_name_owner_changeskwargsr>   r?   rA   r   r   r   r      sP   

zProxyObject.__init__c                 C      | j S r   )r,   r   r   r   r   <lambda>      zProxyObject.<lambda>a  The bus name to which this proxy is bound. (Read-only,
            may change.)

            If the proxy was instantiated using a unique name, this property
            is that unique name.

            If the proxy was instantiated with a well-known name and with
            ``follow_name_owner_changes`` set false (the default), this
            property is the unique name of the connection that owned that
            well-known name when the proxy was instantiated, which might
            not actually own the requested well-known name any more.

            If the proxy was instantiated with a well-known name and with
            ``follow_name_owner_changes`` set true, this property is that
            well-known name.
            c                 C   rQ   r   )rG   rR   r   r   r   rS      rT   z[The bus name which was requested when this proxy was
            instantiated.
            c                 C   rQ   r   )rH   rR   r   r   r   rS   &  rT   zThe object-path of this proxy.c                 K   s"   | j j|f||| j| jd|S )ax  Arrange for the given function to be called when the given signal
        is received.

        :Parameters:
            `signal_name` : str
                The name of the signal
            `handler_function` : callable
                A function to be called when the signal is emitted by
                the remote object. Its positional arguments will be the
                arguments of the signal; optionally, it may be given
                keyword arguments as described below.
            `dbus_interface` : str
                Optional interface with which to qualify the signal name.
                If None (the default) the handler will be called whenever a
                signal of the given member name is received, whatever
                its interface.
        :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).
        )signal_namer5   r1   path)rF   Zadd_signal_receiverr,   rH   r   rU   Zhandler_functionr5   r!   r   r   r   connect_to_signal2  s   
<zProxyObject.connect_to_signalc              	   C   s>   i }t rd|d< | jj| j| jtddd| j| jfddi|S )NTZutf8_stringsZ
Introspect r   Zrequire_main_loopF)r   rF   r#   r,   rH   r   _introspect_reply_handler_introspect_error_handler)r   rP   r   r   r   rO   u  s   
zProxyObject._Introspectc                 C   s*   | j D ]\}}}||i | qg | _ d S r   )rJ   )r   r   r    r!   r   r   r   _introspect_execute_queue  s   
z%ProxyObject._introspect_execute_queuec              
   C   s   | j   z9zt|| _W n ty+ } z| | W Y d }~W | j   d S d }~ww | j| _d | _	| 
  W | j   d S | j   w r   )rK   acquirer   r9   r   r[   release INTROSPECT_STATE_INTROSPECT_DONErM   rI   r\   )r   dataer   r   r   rZ     s   


z%ProxyObject._introspect_reply_handlerc                 C   sp   t   td| j| j|jj|jj| | j	
  ztd | j| _d | _|   W | j	  d S | j	  w )Nz$Introspect error on %s:%s: %s.%s: %sz'Executing introspect queue due to error)loggingZbasicConfig_loggererrorr,   rH   	__class__r&   r%   rK   r]   debugrL   rM   rI   r\   r^   )r   rd   r   r   r   r[     s   


z%ProxyObject._introspect_error_handlerc                 C   sL   | j   z| jd ur| j  W | j   d S W | j   d S | j   w r   )rK   r]   rI   r   r^   rR   r   r   r   _introspect_block  s   

zProxyObject._introspect_blockc                 C   sf   | j   z'| j| jkr| j|||f n||i | W | j   d S W | j   d S | j   w r   )rK   r]   rM   rN   rJ   r   r^   )r   callbackr    rP   r   r   r   _introspect_add_to_queue  s   
z$ProxyObject._introspect_add_to_queuec                 C   s&   | dr|drt|| |S N__)
startswithendswithAttributeErrorget_dbus_methodr   memberr   r   r   __getattr__  s   
zProxyObject.__getattr__c                 C   s<   |  | | j| j| j||}| j| jkr| || j| j}|S )a,  Return a proxy method representing the given D-Bus method. The
        returned proxy method can be called in the usual way. For instance, ::

            proxy.get_dbus_method("Foo", dbus_interface='com.example.Bar')(123)

        is equivalent to::

            proxy.Foo(123, dbus_interface='com.example.Bar')

        or even::

            getattr(proxy, "Foo")(123, dbus_interface='com.example.Bar')

        However, using `get_dbus_method` is the only way to call D-Bus
        methods with certain awkward names - if the author of a service
        implements a method called ``connect_to_signal`` or even
        ``__getattr__``, you'll need to use `get_dbus_method` to call them.

        For services which follow the D-Bus convention of CamelCaseMethodNames
        this won't be a problem.
        )	ProxyMethodClassrF   r,   rH   rM   rN   DeferredMethodClassri   rg   )r   rq   r5   retr   r   r   ro     s   
	
zProxyObject.get_dbus_methodc                 C   s   d| j | j| jt| f S )Nz&<ProxyObject wrapping %s %s %s at %#x>)rF   r,   rH   idrR   r   r   r   __repr__  s   zProxyObject.__repr__)NNNTFr   )r%   r&   r'   r(   r)   rs   r   rt   rL   rN   r_   r   propertyr1   requested_bus_namer2   rX   rO   r\   rZ   r[   rg   ri   rr   ro   rw   __str__r   r   r   r   r;      s@    
S
C

&r;   c                   @   s   e Zd ZdZdd Zedd dddZeZedd ddd	Zed
d dddZ	edd dddZ
edd dddZ	dddZdd ZdddZdd ZeZdS )	InterfacezAn interface into a remote object.

    An Interface can be used to wrap ProxyObjects
    so that calls can be routed to their correct
    D-Bus interface.
    c                 C   s$   t |tr
|j| _n|| _|| _dS )a:  Construct a proxy for the given interface on the given object.

        :Parameters:
            `object` : `dbus.proxies.ProxyObject` or `dbus.Interface`
                The remote object or another of its interfaces
            `dbus_interface` : str
                An interface the `object` implements
        N)
isinstancer{   proxy_object_objr/   )r   objectr5   r   r   r   r     s   
	

zInterface.__init__c                 C      | j jS r   )r~   r2   rR   r   r   r   rS         zInterface.<lambda>Nz.The D-Bus object path of the underlying objectc                 C   r   r   )r~   r1   rR   r   r   r   rS     r   z:The bus name to which the underlying proxy object is boundc                 C   r   r   )r~   ry   rR   r   r   r   rS     r   zGThe bus name which was requested when the underlying object was createdc                 C   rQ   r   )r~   rR   r   r   r   rS     rT   zThe underlying proxy objectc                 C   rQ   r   )r/   rR   r   r   r   rS     rT   zThe D-Bus interface representedc                 K   s"   |s| j }| jj|||fi |S )aa  Arrange for a function to be called when the given signal is
        emitted.

        The parameters and keyword arguments are the same as for
        `dbus.proxies.ProxyObject.connect_to_signal`, except that if
        `dbus_interface` is None (the default), the D-Bus interface that
        was passed to the `Interface` constructor is used.
        )r/   r~   rX   rW   r   r   r   rX     s   

zInterface.connect_to_signalc                 C   s,   | dr|drt|| j|| jS rj   )rl   rm   rn   r~   ro   r/   rp   r   r   r   rr   $  s   zInterface.__getattr__c                 C   s   |du r| j }| j||S )a&  Return a proxy method representing the given D-Bus method.

        This is the same as `dbus.proxies.ProxyObject.get_dbus_method`
        except that if `dbus_interface` is None (the default),
        the D-Bus interface that was passed to the `Interface` constructor
        is used.
        N)r/   r~   ro   )r   rq   r5   r   r   r   ro   *  s   zInterface.get_dbus_methodc                 C   s   d| j | jt| f S )Nz%<Interface %r implementing %r at %#x>)r~   r/   rv   rR   r   r   r   rw   6  s   zInterface.__repr__r   )r%   r&   r'   r(   r   rx   r2   rH   r1   ry   r}   r5   rX   rr   ro   rw   rz   r   r   r   r   r{     s4    

r{   )rb   Z	threadingr   ImportErrorZdummy_threadingr.   Zdbus._expat_introspect_parserr   Zdbus.exceptionsr   r   r   r   Z__docformat__Z	getLoggerrc   r   r	   r
   r   r   Zdbus._compatr   r   r)   r   r;   r{   r   r   r   r   <module>   s&   
`  B