
iZc           @   s   d  Z  d d l Z d d l m Z d d l m Z d d d d d	 d
 g Z d   Z d Z d e f d     YZ	 d e	 d d d  Z d e j f d     YZ d e f d     YZ e   Z d   Z e i  d  Z d S(   sm   This module contains convenience decorators and functions for using
PolicyKit with dbus services and clients.iN(   t	   decorator(   t   method_call_no_timeoutt   require_autht   enable_proxyt   AUTHFAIL_DONTCATCHt   NotAuthorizedExceptiont   AreAuthorizationsObtainablet   IsSystemBusNameAuthorizedAsyncc            s     f d   } | S(   s   Decorator for DBus service methods.

    Specify that a user needs a specific PolicyKit authorization `polkit_auth´
    to execute it.c            s)   t  |  d  s t  t |  d    |  S(   Nt   _dbus_is_methodt   _slip_polkit_auth_required(   t   hasattrt   AssertionErrort   setattr(   t   method(   t   polkit_auth(    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   require_auth_decorator)   s    (    (   R   R   (    (   R   s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR   #   s    sB   org.fedoraproject.slip.dbus.service.PolKit.NotAuthorizedException.c           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR   4   s   c            s   |  d k s t |   s t   d t f k sB  d k sB t   d k s` t   s` t   d k s t  t  s t     f d     |  d k	 r t   |   S  f d   } | Sd S(   s  Decorator for DBus proxy methods.

    Let's you (optionally) specify either a result value or an exception type
    and a callback which is returned, thrown or called respectively if a
    PolicyKit authorization doesn't exist or can't be obtained in the DBus
    mechanism, i.e. an appropriate DBus exception is thrown.

    An exception constructor may and a callback must accept an `action_id´
    parameter which will be set to the id of the PolicyKit action for which
    authorization could not be obtained.

    Examples:

    1) Return `False´ in the event of an authorization problem, and call
    `error_handler´:

        def error_handler(action_id=None):
            print "Authorization problem:", action_id

        class MyProxy(object):
            @polkit.enable_proxy(authfail_result=False,
                                 authfail_callback=error_handler)
            def some_method(self, ...):
                ...

    2) Throw a `MyAuthError´ instance in the event of an authorization problem:

        class MyAuthError(Exception):
            def __init__(self, *args, **kwargs):
                action_id = kwargs.pop("action_id")
                super(MyAuthError, self).__init__(*args, **kwargs)
                self.action_id = action_id

        class MyProxy(object):
            @polkit.enable_proxy(authfail_exception=MyAuthError)
            def some_method(self, ...):
                ...c            s   y |  | |   SWn t  j k
 r } | j   } | j t  sG   n  | t t  }   d  k	 rs   d |  n   d  k	 r y  d |  } Wn    } n X|  n   t k r   n   SXd  S(   Nt	   action_id(   t   dbust   DBusExceptiont   get_dbus_namet
   startswitht   AUTH_EXC_PREFIXt   lent   NoneR   (   t   funct   pt   kt   et   exc_nameR   t   af_exc(   t   authfail_callbackt   authfail_exceptiont   authfail_result(    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   _enable_proxyd   s$    	c            s   t    |   S(   N(   R    (   R   (   R#   (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   decorate   s    N(   R   t   callableR   R   t
   issubclasst	   ExceptionR    (   R   R"   R!   R    R$   (    (   R#   R    R!   R"   s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR   7   s    '$!c           B   s   e  Z d  Z d Z d   Z RS(   sq   Exception which a DBus service method throws if an authorization
    required for executing it can't be obtained.sA   org.fedoraproject.slip.dbus.service.PolKit.NotAuthorizedExceptionc         O   s4   |  j  j d | |  _ t t |   j | |   d  S(   Nt   .(   t	   __class__t   _dbus_error_namet   superR   t   __init__(   t   selfR   R   R   (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR,      s    (   R   R   t   __doc__R*   R,   (    (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR      s   t   PolKitc           B   s   e  Z d  Z d Z d Z d Z d Z d Z d Z	 d Z
 e d    Z e d    Z e d    Z e d    Z e d    Z d	   Z d
   Z d   Z e i  d  Z RS(   s"   Convenience wrapper around polkit.s   org.freedesktop.PolicyKit1s%   /org/freedesktop/PolicyKit1/Authoritys$   org.freedesktop.PolicyKit1.Authorityc         C   sM   | |  j  k rI t j rI t j j t j  d  t _ d  t _ d  t _ n  d  S(   N(   t
   _dbus_nameR/   t   _PolKit__bust   remove_signal_receivert   _PolKit__signal_receiverR   t   _PolKit__interface(   t   clst   namet	   old_ownert	   new_owner(    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   _on_name_owner_changed   s
    		c         C   sI   t  j sB t j   t  _ t  j j d |  j d d d d  t  _ n  t  j S(   Nt   handler_functiont   signal_namet   NameOwnerChangedt   dbus_interfaces   org.freedesktop.DBus(   R/   R1   R   t	   SystemBust   add_signal_receiverR9   R3   (   R-   (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   _bus   s    		c         C   s%   t  j s |  j j   t  _ n  t  j S(   N(   R/   t   _PolKit__bus_nameR@   t   get_unique_name(   R-   (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt	   _bus_name   s    	c         C   s[   t  j sT y1 t j |  j j |  j |  j  |  j  t  _ WqT t j	 k
 rP qT Xn  t  j S(   N(
   R/   R4   R   t	   InterfaceR@   t
   get_objectR0   t
   _dbus_patht   _dbus_interfaceR   (   R-   (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt
   _interface   s    	c         C   s   t  |  j  S(   N(   t   boolRH   (   R-   (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   _polkit_present   s    c         C   sN   |  j  j d d  } t j | d  } y | j |  } Wn d  } n X| S(   Ns   org.freedesktop.DBuss   /org/freedesktop/DBus(   R@   RE   R   RD   t   GetConnectionUnixUserR   (   R-   t   system_bus_namet
   bus_objectt   bus_interfacet   uid(    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   __dbus_system_bus_name_uid   s    	
c         C   sN   |  j  s t S|  j j d i |  j d 6f | i  d d  \ } } } | pM | S(   Ns   system-bus-nameR6   i    t    (   RJ   t   TrueRH   t   CheckAuthorizationRC   (   R-   t   authorizationt   is_authorizedt   is_challenget   details(    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   __authorization_is_obtainable   s    		c            sG     j  s t St | t  r( | g } n  t   f d   | t  } | S(   Nc            s   |  o   j  |  S(   N(   t$   _PolKit__authorization_is_obtainable(   t   xt   y(   R-   (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   <lambda>   s    (   RJ   RR   t
   isinstancet
   basestringt   reduce(   R-   t   authorizationst
   obtainable(    (   R-   s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR      s    	c   	         s   |  j  s.   | d  k p* |  j |  d k  Sd } | rG | d O} n    f d   } |  j j d i | d 6f | | | d d | d | d	 t d  S(
   Ni    i   c            s   |  \ } } }   |  d  S(   N(    (   t   argsRU   RV   RW   (   t   reply_handler(    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   reply_cb   s    s   system-bus-nameR6   RQ   Rc   t   error_handlert   timeout(   RJ   R   t!   _PolKit__dbus_system_bus_name_uidRH   RS   R   (	   R-   RL   R   Rc   Re   t	   challengeRW   t   flagsRd   (    (   Rc   s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR      s    	N(   R   R   R.   R0   RF   RG   R   R4   R1   RA   R3   t   classmethodR9   t   propertyR@   RC   RH   RJ   Rg   RY   R   RR   R   (    (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR/      s"   
	
		c         C   s   t  j |   S(   N(   t   __polkitR   (   R`   (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR     s    c         C   s   t  j |  | | | | |  S(   N(   Rl   R   (   RL   R   Rc   Re   Rh   RW   (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyR   
  s    (   R.   R   R    t	   constantsR   t   __all__R   R   t   objectR   R   R   R   R   R/   Rl   R   RR   R   (    (    (    s4   /usr/lib/python2.7/site-packages/slip/dbus/polkit.pyt   <module>   s   			On		