U
    K^                     @   s:  d Z dZddlZddlZddlZddlZddlm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mZ ddlmZmZmZ ddlmZmZmZ dd	lmZ dd
lmZ e dZ!G dd de"Z#G dd de"Z$dd Z%dd Z&dd Z'G dd de(Z)e)de"fi Z*e" Z+G dd de*Z,G dd de,Z-dS ))BusNameObjectFallbackObjectmethodsignalZrestructuredtext    N)Sequence)INTROSPECTABLE_IFACE
ObjectPath
SessionBus	SignatureStructvalidate_bus_namevalidate_object_path)r   r   )DBusExceptionNameExistsExceptionUnknownMethodException)ErrorMessageMethodReturnMessageMethodCallMessage)
LOCAL_PATH)is_py2zdbus.servicec                   @   s(   e Zd ZdZdd Zdd Zer$eZdS )_VariantSignaturezA fake method signature which, when iterated, yields an endless stream
    of 'v' characters representing variants (handy with zip()).

    It has no string representation.
    c                 C   s   | S )zReturn self. selfr   r   ./usr/lib/python3/dist-packages/dbus/service.py__iter__:   s    z_VariantSignature.__iter__c                 C   s   dS )zReturn 'v' whenever called.vr   r   r   r   r   __next__>   s    z_VariantSignature.__next__N)__name__
__module____qualname____doc__r   r   r   nextr   r   r   r   r   4   s
   r   c                   @   sF   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	e	Z
dS )r   aa  A base class for exporting your own Named Services across the Bus.

    When instantiated, objects of this class attempt to claim the given
    well-known name on the given bus for the current process. The name is
    released when the BusName object becomes unreferenced.

    If a well-known name is requested multiple times, multiple references
    to the same BusName object will be returned.

    :Caveats:

        - Assumes that named services are only ever requested using this class -
          if you request names from the bus directly, confusion may occur.
        - Does not handle queueing.
    NFc           
      C   s   t |ddd |dkr4ddl}|jdtdd t }||jkrH|j| S |rRtjpTd|r^tjp`dB |rltj	pndB }|
||}|tjkrn<|tjkrn0|tjkrt|n|tjkrntd	||f t| }	||	_||	_|	|j|< |	S )
aS  Constructor, which may either return an existing cached object
        or a new object.

        :Parameters:
            `name` : str
                The well-known name to be advertised
            `bus` : dbus.Bus
                A Bus on which this service will be advertised.

                Omitting this parameter or setting it to None has been
                deprecated since version 0.82.1. For backwards compatibility,
                if this is done, the global shared connection to the session
                bus will be used.

            `allow_replacement` : bool
                If True, other processes trying to claim the same well-known
                name will take precedence over this one.
            `replace_existing` : bool
                If True, this process can take over the well-known name
                from other processes already holding it.
            `do_not_queue` : bool
                If True, this service will not be placed in the queue of
                services waiting for the requested name if another service
                already holds it.
        TF)Zallow_well_knownZallow_uniqueNr   zKOmitting the "bus" parameter to dbus.service.BusName.__init__ is deprecated   )
stacklevelz3requesting bus name %s returned unexpected value %s)r   warningswarnDeprecationWarningr
   Z
_bus_names_dbus_bindingsZNAME_FLAG_ALLOW_REPLACEMENTZNAME_FLAG_REPLACE_EXISTINGZNAME_FLAG_DO_NOT_QUEUEZrequest_nameZ REQUEST_NAME_REPLY_PRIMARY_OWNERZREQUEST_NAME_REPLY_IN_QUEUEZREQUEST_NAME_REPLY_EXISTSr   Z REQUEST_NAME_REPLY_ALREADY_OWNERRuntimeErrorobject__new___bus_name)
clsnameZbusZallow_replacementZreplace_existingZdo_not_queuer&   Z
name_flagsretvalbus_namer   r   r   r,   V   s>     








zBusName.__new__c                 O   s   d S Nr   )r   argskeywordsr   r   r   __init__   s    zBusName.__init__c                 C   s   | j | j d S r3   )r-   Zrelease_namer.   r   r   r   r   __del__   s    zBusName.__del__c                 C   s   | j S )zGet the Bus this Service is on)r-   r   r   r   r   get_bus   s    zBusName.get_busc                 C   s   | j S )zGet the name of this service)r.   r   r   r   r   get_name   s    zBusName.get_namec                 C   s   d| j | jt| f S )Nz&<dbus.service.BusName %s on %r at %#x>)r.   r-   idr   r   r   r   __repr__   s    zBusName.__repr__)NFFF)r   r    r!   r"   r,   r6   r7   r8   r9   r;   __str__r   r   r   r   r   F   s   
Or   c                 C   sZ  d}d}d}|r| j jD ]}|sx||jkrxd|j| jkrtd|j| jkrt|j| j|krx|}|j| }d} qqxn|}|r||jkrd|j| jkrd|j| jkr|j| j|kr|j| }d} qqnR| j jD ]H}|s||jkr|}|r||jkrd|j| jkr|j| }d} qq|r2|j| |fS |rJtd||f ntd| dS )zWalks the Python MRO of the given class to find the method to invoke.

    Returns two methods, the one to call, and the one it inherits from which
    defines its D-Bus interface name, signature, and attributes.
    NF_dbus_is_method_dbus_interfaceTz(%s is not a valid method of interface %sz%s is not a valid method)	__class____mro____dict__r>   r   )r   method_nameZdbus_interfaceparent_methodZcandidate_classZ
successfulr/   r   r   r   _method_lookup   sP    


rD   c                 G   s   t |}z|j|d|i W n tk
r } ztt  |d krz||d }W n6 tk
r } ztd||j|  W 5 d }~X Y nX td|||j|  W 5 d }~X Y nX |	 s| 
| d S )N	signaturez
 (guessed)z2Unable to guess signature for arguments %r: %s: %sz8Unable to append %r to message with signature %s: %s: %s)r   append	ExceptionloggingZbasicConfigZguess_signature_loggererrorr?   get_no_replysend_message)
connectionmessagerB   rE   r1   replyer   r   r   _method_reply_return   s0         rQ   c           	      C   s   t |dd }|d k	rn0t |dddkr4d|jj }nd|j|jjf }t \}}}t|trn|jsn|	 }n2||krd
t|||}nd
t|j|}t|||}| s| | d S )NZ_dbus_error_namer     )rR   __main__zorg.freedesktop.DBus.Python.%sz!org.freedesktop.DBus.Python.%s.%s)getattrr?   r   r    sysexc_info
isinstancer   Zinclude_tracebackZget_dbus_messagejoin	tracebackformat_exceptionformat_exception_onlyr   rK   rL   )	rM   rN   	exceptionr0   ZetZevZetbcontentsrO   r   r   r   _method_reply_error  s"    
r^   c                       s,   e Zd Z fddZdd Zdd Z  ZS )InterfaceTypec                    s   t | di }|| _i  }|| jd | < |D ]L}|jd |j }t |ddr,||  D ]\}}	||i }
|
|	 qXq,| D ](}t |ddr||ji }	||	|j< qt	t
| ||| d S )N_dbus_class_table.Fr>   )rT   r`   r    r   items
setdefaultupdatevaluesr>   superr_   r6   )r/   r0   basesZdctZclass_tableZinterface_tablebZ	base_nameZ	interfaceZmethod_tableZour_method_tablefuncr?   r   r   r6   0  s    zInterfaceType.__init__c                 C   s   |j }|jrtt|j}nt }|jr4t|j}ng }d|j }t||D ]}|d| 7 }qL|D ]}|d| 7 }qb|d7 }|S )Nz    <method name="%s">
z2      <arg direction="in"  type="%s" name="%s" />
z(      <arg direction="out" type="%s" />
z    </method>
)
_dbus_argsZ_dbus_in_signaturetupler   r   _dbus_out_signaturer   zip)r/   ri   r4   Zin_sigZout_sigreflection_datapairtyper   r   r   _reflect_on_methodJ  s    
z InterfaceType._reflect_on_methodc                 C   sT   |j }|jrtt|j}nt }d|j }t||D ]}|d|  }q6|d }|S )Nz    <signal name="%s">
z"      <arg type="%s" name="%s" />
z    </signal>
)rk   Z_dbus_signaturerl   r   r   r   rn   )r/   ri   r4   Zsigro   rp   r   r   r   _reflect_on_signalg  s    
z InterfaceType._reflect_on_signal)r   r    r!   r6   rr   rs   __classcell__r   r   rj   r   r_   /  s   r_   	Interfacec                   @   s   e Zd ZdZdZdZdddZedd Zedd	 Z	ed
d Z
dd ZdddZdd Zdd Zeeddddddd Zdd ZeZdS )r   a  A base class for exporting your own Objects across the Bus.

    Just inherit from Object and mark exported methods with the
    @\ `dbus.service.method` or @\ `dbus.service.signal` decorator.

    Example::

        class Example(dbus.service.object):
            def __init__(self, object_path):
                dbus.service.Object.__init__(self, dbus.SessionBus(), path)
                self._last_input = None

            @dbus.service.method(interface='com.example.Sample',
                                 in_signature='v', out_signature='s')
            def StringifyVariant(self, var):
                self.LastInputChanged(var)      # emits the signal
                return str(var)

            @dbus.service.signal(interface='com.example.Sample',
                                 signature='v')
            def LastInputChanged(self, var):
                # run just before the signal is actually emitted
                # just put "pass" if nothing should happen
                self._last_input = var

            @dbus.service.method(interface='com.example.Sample',
                                 in_signature='', out_signature='v')
            def GetLastInput(self):
                return self._last_input
    FNc                 C   s   |dk	rt | t|tr(|}| }n|dkr@|dk	r@| }d| _d| _g | _t | _	d| _
|| _|dkr|dk	rtd|dk	r|dk	r| || dS )a  Constructor. Either conn or bus_name is required; object_path
        is also required.

        :Parameters:
            `conn` : dbus.connection.Connection or None
                The connection on which to export this object.

                If None, use the Bus associated with the given ``bus_name``.
                If there is no ``bus_name`` either, the object is not
                initially available on any Connection.

                For backwards compatibility, if an instance of
                dbus.service.BusName is passed as the first parameter,
                this is equivalent to passing its associated Bus as
                ``conn``, and passing the BusName itself as ``bus_name``.

            `object_path` : str or None
                A D-Bus object path at which to make this Object available
                immediately. If this is not None, a `conn` or `bus_name` must
                also be provided.

            `bus_name` : dbus.service.BusName or None
                Represents a well-known name claimed by this process. A
                reference to the BusName object will be held by this
                Object, preventing the name from being released during this
                Object's lifetime (unless it's released manually).
        NFz<If object_path is given, either conn or bus_name is required)r   rW   r   r8   _object_path_connection
_locations	threadingZLock_locations_lock	_fallbackr.   	TypeErroradd_to_connection)r   connobject_pathr2   r   r   r   r6     s$    


zObject.__init__c                 C   s:   | j tkrtd|  n| j dkr0td|  n| j S dS )zThe object-path at which this object is available.
        Access raises AttributeError if there is no object path, or more than
        one object path.

        Changed in 0.82.0: AttributeError can be raised.
        zEObject %r has more than one object path: use Object.locations insteadNz Object %r has no object path yet)rv   _MANYAttributeErrorr   r   r   r   __dbus_object_path__  s    

zObject.__dbus_object_path__c                 C   s:   | j tkrtd|  n| j dkr0td|  n| j S dS )zThe Connection on which this object is available.
        Access raises AttributeError if there is no Connection, or more than
        one Connection.

        Changed in 0.82.0: AttributeError can be raised.
        zFObject %r is on more than one Connection: use Object.locations insteadNzObject %r has no Connection yet)rw   r   r   r   r   r   r   rM     s    

zObject.connectionc                 C   s
   t | jS )ae  An iterable over tuples representing locations at which this
        object is available.

        Each tuple has at least two items, but may have more in future
        versions of dbus-python, so do not rely on their exact length.
        The first two items are the dbus.connection.Connection and the object
        path.

        :Since: 0.82.0
        )iterrx   r   r   r   r   	locations  s    zObject.locationsc                 C   s   |t krtdt  | j  z| jdk	rL| j|k	rL| jsLtd| | jf | jdk	rx| jsx| j|krxtd| | jf |	|| j
| j| j | jdkr|| _n| j|k	rt| _| jdkr|| _n| j|krt| _| j||| jf W 5 | j  X dS )aN  Make this object accessible via the given D-Bus connection and
        object path.

        :Parameters:
            `connection` : dbus.connection.Connection
                Export the object on this connection. If the class attribute
                SUPPORTS_MULTIPLE_CONNECTIONS is False (default), this object
                can only be made available on one connection; if the class
                attribute is set True by a subclass, the object can be made
                available on more than one connection.

            `path` : dbus.ObjectPath or other str
                Place the object at this object path. If the class attribute
                SUPPORTS_MULTIPLE_OBJECT_PATHS is False (default), this object
                can only be made available at one object path; if the class
                attribute is set True by a subclass, the object can be made
                available with more than one object path.

        :Raises ValueError: if the object's class attributes do not allow the
            object to be exported in the desired way.
        :Since: 0.82.0
        z3Objects may not be exported on the reserved path %sNz'%r is already exported on connection %rz(%r is already exported at object path %s)r   
ValueErrorrz   acquirereleaserw   SUPPORTS_MULTIPLE_CONNECTIONSrv   SUPPORTS_MULTIPLE_OBJECT_PATHSZ_register_object_path_message_cb_unregister_cbr{   r   rx   rF   )r   rM   pathr   r   r   r}     sH    







zObject.add_to_connectionc              	   C   s  | j    z| jdks"| jdkr.td|  |dk	s>|dk	rg }| jD ]6}|dks`|d |krH|dkst|d |krH|| qHn| j}g | _|std| ||f |D ]^}z|d |d  W n tk
r   Y nX | jrz| j	| W q t
k
r   Y qX qW 5 | j   X dS )a  Make this object inaccessible via the given D-Bus connection
        and object path. If no connection or path is specified,
        the object ceases to be accessible via any connection or path.

        :Parameters:
            `connection` : dbus.connection.Connection or None
                Only remove the object from this Connection. If None,
                remove from all Connections on which it's exported.
            `path` : dbus.ObjectPath or other str, or None
                Only remove the object from this object path. If None,
                remove from all object paths.
        :Raises LookupError:
            if the object was not exported on the requested connection
            or path, or (if both are None) was not exported at all.
        :Since: 0.81.1
        Nz%r is not exportedr      z1%r is not exported at a location matching (%r,%r))rz   r   r   rv   rw   LookupErrorrx   rF   Z_unregister_object_pathremover   )r   rM   r   Zdroppedlocationr   r   r   remove_from_connectionT  s<    


zObject.remove_from_connectionc                 C   s   t d| | d S )Nz5Unregistering exported object %r from some path on %r)rI   info)r   rM   r   r   r   r     s     zObject._unregister_cbc              
      s  t tsd S z`  }t| |\}}jf |j}i }|jd k	rZt|jnd |j	r|j	\}}	 fdd||<  fdd||	< |j
r ||j
< |jr ||j< |jr^ }
|
}| jD ]r}|d  kr|
|d kr d} qL|d dkrq|
|d d r|
t|d d  }t|t|k r|}qt|}|||j< |jrt ||j< |jr||j< |jr ||j< || f||}|j	rW d S d k	r*t}t|dkr|d krd}ntd n4t|dkr
|f}nt |trntd	f n0|d kr:d}n t |trTt |tsTn|f}t f|  W n0 tk
r } zt | W 5 d }~X Y nX d S )
Nc                     s   t  f|  S r3   )rQ   )r1   rM   rN   rB   rE   r   r   <lambda>      z$Object._message_cb.<locals>.<lambda>c                    s   t  | S r3   )r^   )r\   )rM   rN   r   r   r     r   r   r   /r   z8%s has an empty output signature but did not return NonezK%s has multiple output values in signature %s but did not return a sequence)rW   r   Z
get_memberZget_interfacerD   Zget_args_listZ_dbus_get_args_optionsrm   r   Z_dbus_async_callbacksZ_dbus_sender_keywordZ
get_senderZ_dbus_path_keywordZget_pathZ_dbus_rel_path_keywordrx   
startswithlenr	   Z_dbus_destination_keywordZget_destinationZ_dbus_message_keywordZ_dbus_connection_keywordrl   r|   r   r   rQ   rG   r^   )r   rM   rN   Zinterface_nameZcandidate_methodrC   r4   r5   Zreturn_callbackZerror_callbackr   Zrel_pathZexpsuffixr1   Zsignature_tupler\   r   r   r   r     s    









zObject._message_cbrR   sr   rM   )Zin_signatureZout_signatureZpath_keywordZconnection_keywordc                 C   s   t j}|d| 7 }| j| jjd | jj  }| D ]d\}}|d| 7 }| D ]>}t|ddrr|| j	|7 }qPt|ddrP|| j
|7 }qP|d7 }q4||D ]}|d| 7 }q|d	7 }|S )
ziReturn a string of XML encoding this object's supported interfaces,
        methods and signals.
        z<node name="%s">
ra   z  <interface name="%s">
r=   FZ_dbus_is_signalz  </interface>
z  <node name="%s"/>
z</node>
)r)   Z)DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODEr`   r?   r    r   rb   re   rT   rr   rs   Zlist_exported_child_objects)r   r   rM   ro   Z
interfacesr0   Zfuncsri   r   r   r   
Introspect  s    
zObject.Introspectc                 C   s>   d}| j tk	r"| j d k	r"d| j  }d| jj| jj|t| f S )NrR   z at %sz<%s.%s%s at %#x>)rv   r   r?   r    r   r:   )r   wherer   r   r   r;     s    

 zObject.__repr__)NNN)NN)r   r    r!   r"   r   r   r6   propertyr   rM   r   r}   r   r   r   r   r   r   r;   r<   r   r   r   r   r     s*   #
=


;
1n 
r   c                       s&   e Zd ZdZdZd fdd	Z  ZS )r   zaAn object that implements an entire subtree of the object-path
    tree.

    :Since: 0.82.0
    TNc                    sP   t t|   d| _|dkr.|dk	rLtdn|dkr@tdn| || dS )aH  Constructor.

        Note that the superclass' ``bus_name`` __init__ argument is not
        supported here.

        :Parameters:
            `conn` : dbus.connection.Connection or None
                The connection on which to export this object. If this is not
                None, an `object_path` must also be provided.

                If None, the object is not initially available on any
                Connection.

            `object_path` : str or None
                A D-Bus object path at which to make this Object available
                immediately. If this is not None, a `conn` must also be
                provided.

                This object will implements all object-paths in the subtree
                starting at this object-path, except where a more specific
                object has been added.
        TNz)If object_path is given, conn is requiredz)If conn is given, object_path is required)rf   r   r6   r{   r|   r}   )r   r~   r   rj   r   r   r6   '  s    

zFallbackObject.__init__)NN)r   r    r!   r"   r   r6   rt   r   r   rj   r   r     s   r   ).__all__Z__docformat__rU   rH   ry   rY   collectionsr   r)   Zdbusr   r	   r
   r   r   r   r   Zdbus.decoratorsr   r   Zdbus.exceptionsr   r   r   Zdbus.lowlevelr   r   r   Zdbus.proxiesr   Zdbus._compatr   Z	getLoggerrI   r+   r   r   rD   rQ   r^   rq   r_   ru   r   r   r   r   r   r   r   <module>   s6   $
uBM   