
    ^M)                     L    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 Zd	 Zy)
z%Utils for using deferreds with D-Bus.z%Sebastian Heinlein <devel@glatzor.de>)dbus_deferred_method	deferable    wrapsN   )deferDeferredDeferredExceptionc                        f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                       t        j                  j                  i         t                fd       }d|_        |S )Nc                      fd}|j                  d      |j                  d      t        g| i |}|j                  |       |j                  fd       y )Nc                  *    | dk(  r         y  |   y )NN )cb_argsdbus_callbacks    -/usr/lib/python3/dist-packages/defer/utils.pyignore_none_callbackzVdbus_deferred_method.<locals>.decorator.<locals>.wrapper.<locals>.ignore_none_callback5   s     g%!O!7+    _dbus_callback_dbus_errbackc                 (     | j                         S r   )value)errordbus_errbacks    r   <lambda>zJdbus_deferred_method.<locals>.decorator.<locals>.wrapper.<locals>.<lambda>@   s    |EKK/H r   )popr   add_callbackadd_errback)argskwargsr   deferredr   r   functions       @@r   wrapperz8dbus_deferred_method.<locals>.decorator.<locals>.wrapper3   sY    , #JJ'78M!::o6LX777H!!"67  !HIr   )r   r   )dbusservicemethodr   _dbus_async_callbacks)r#   r$   r    r!   s   ` r   	decoratorz'dbus_deferred_method.<locals>.decorator1   sK    74<<&&77A	x	J 
	J  )L%r   r   )r    r!   r)   s   `` r   r   r   %   s    * r   c                 .     t                f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                      d 	 |j                  d      }|r,t               j                  |d<   fd|d	<    | i | S  | i |S # t        $ r5 t        j                         }	 |d   d   dk(  }n# t        $ r d}Y nw xY wY sw xY w)
Nc                 |    t        | t              r|j                  |        y |j                  t        |              y r   )
isinstancer
   errback)r   r"   s     r   on_errorz/deferable.<locals>._deferable.<locals>.on_errorR   s/    %!23  '  !25!9:r   r         _inline_callbacksFreply_handlerc                      |       S r   r   )errr"   r/   s    r   r   z/deferable.<locals>._deferable.<locals>.<lambda>h   s    (32I r   error_handler)r   KeyErrorinspectstack
IndexErrorr	   callback)r    r!   to_deferr9   r"   r/   funcs       @@r   
_deferablezdeferable.<locals>._deferableP   s    	;
	!zz'*H zH&.&7&7F?#&IF?#$!&!OT$V$$  	! MMOE! 8A;*== ! !	!s5   A B-A98B9BBBBBr   )r=   r>   s   ` r   r   r   H   s#     4[% %6 r   )__doc__
__author____all__	functoolsr   r8   r%    r   r	   r
   r   r   r   r   r   <module>rD      s0    ,, 6

/    0 0!F$r   