U
    ^žìM)  ã                   @   sP   d Z dZdZddlmZ ddlZddlZddlmZm	Z	m
Z
 dd	„ Zd
d„ ZdS )z%Utils for using deferreds with D-Bus.z%Sebastian Heinlein <devel@glatzor.de>)Údbus_deferred_methodÚ	deferableé    ©ÚwrapsNé   )ÚdeferÚDeferredÚDeferredExceptionc                     s   ‡ ‡fdd„}|S )a¦  Export the decorated method on the D-Bus and handle a maybe
    returned Deferred.

    This decorator can be applied to methods in the same way as the
    @dbus.service.method method, but it correctly handles the case where
    the method returns a Deferred.

    This decorator was kindly taken from James Henstridge blog post and
    adopted:
    http://blogs.gnome.org/jamesh/2009/07/06/watching-iview-with-rygel/
    c                    s0   t jjˆˆŽˆ ƒ‰ tˆ ƒ‡ fdd„ƒ}d|_|S )Nc                     sP   ‡ fdd„}|  d¡‰ |  d¡‰tˆf| ž|Ž}| |¡ | ‡fdd„¡ d S )Nc                     s   | dkrˆ ƒ  nˆ | Ž  d S )N©N© )Zcb_args)Údbus_callbackr   ú-/usr/lib/python3/dist-packages/defer/utils.pyÚignore_none_callback5   s    zVdbus_deferred_method.<locals>.decorator.<locals>.wrapper.<locals>.ignore_none_callbackÚ_dbus_callbackÚ_dbus_errbackc                    s
   ˆ | j ƒS r
   )Úvalue)Úerror)Údbus_errbackr   r   Ú<lambda>@   ó    zJdbus_deferred_method.<locals>.decorator.<locals>.wrapper.<locals>.<lambda>)Úpopr   Zadd_callbackZadd_errback)ÚargsÚkwargsr   Údeferred©Úfunction)r   r   r   Úwrapper3   s    


z8dbus_deferred_method.<locals>.decorator.<locals>.wrapper)r   r   )ÚdbusÚserviceÚmethodr   Z_dbus_async_callbacks)r   r   ©r   r   r   r   Ú	decorator1   s
    z'dbus_deferred_method.<locals>.decoratorr   )r   r   r!   r   r    r   r   %   s    r   c                    s   t ˆ ƒ‡ fdd„ƒ}|S )a:  Add a defer attribute to the decorated function and return a Deferred
    object. The callback of the Deferred will be passed as reply_handler
    argument and the errback as the error_handler argument to the decorated
    function.

    This decorator allows to easily make use of Deferreds in a DBus client.
    c                     s    dd„ ‰z|  d¡}W nJ tk
r`   t ¡ }z|d d dk}W n tk
rZ   d}Y nX Y nX |r–tƒ ‰ ˆ j|d< ‡ ‡fd	d
„|d< ˆ| |Ž ˆ S ˆ| |ŽS )Nc                 S   s(   t | tƒr| | ¡ n| t| ƒ¡ d S r
   )Ú
isinstancer	   Zerrback)r   r   r   r   r   Úon_errorR   s    
z/deferable.<locals>._deferable.<locals>.on_errorr   é   é   Z_inline_callbacksFZreply_handlerc                    s
   ˆ| ˆ ƒS r
   r   )Úerr©r   r#   r   r   r   h   r   z/deferable.<locals>._deferable.<locals>.<lambda>Zerror_handler)r   ÚKeyErrorÚinspectÚstackÚ
IndexErrorr   Úcallback)r   r   Zto_deferr*   ©Úfuncr'   r   Ú
_deferableP   s     

zdeferable.<locals>._deferabler   )r.   r/   r   r-   r   r   H   s    r   )Ú__doc__Ú
__author__Ú__all__Ú	functoolsr   r)   r   Ú r   r   r	   r   r   r   r   r   r   Ú<module>   s   #