???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3
uAcI$                 @   s   d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlm	Z	 dd	d
dddgZ
dd ZdZG dd
 d
eZdeddfdd	ZG dd dejZG dd deZe Zdd Zdi fddZdS )zmThis module contains convenience decorators and functions for using
PolicyKit with dbus services and clients.    )absolute_importN)	decorator)reduce   )method_call_no_timeoutrequire_authenable_proxyAUTHFAIL_DONTCATCHNotAuthorizedExceptionAreAuthorizationsObtainableIsSystemBusNameAuthorizedAsyncc                s    fdd}|S )u   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 )NZ_dbus_is_methodZ_slip_polkit_auth_required)hasattrAssertionErrorsetattr)method)polkit_auth /usr/lib/python3.6/polkit.pyrequire_auth_decorator/   s    z,require_auth.<locals>.require_auth_decoratorr   )r   r   r   )r   r   r   )   s    zBorg.fedoraproject.slip.dbus.service.PolKit.NotAuthorizedException.c               @   s   e Zd ZdS )r	   N)__name__
__module____qualname__r   r   r   r   r	   <   s   c                s   | dkst | tjstdtfks0dks0tdksHt tjsHtdks^tts^tfdd | dk	rt | S  fdd}|S dS )u  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, ...):
                ...Nc                s   y
| ||S  t jk
r } zr|j }|jts2 |ttd  } d k	rT |d d k	ry|d}W n    }Y nX |tkr S d }~X nX d S )N)	action_id)dbusDBusExceptionZget_dbus_name
startswithAUTH_EXC_PREFIXlenr	   )funcpkeZexc_namer   Zaf_exc)authfail_callbackauthfail_exceptionauthfail_resultr   r   _enable_proxyv   s$    


z#enable_proxy.<locals>._enable_proxyc                s
   t  | S )N)r   )r   )r%   r   r   decorate   s    zenable_proxy.<locals>.decorate)
isinstancecollectionsCallabler   r	   
issubclass	Exceptionr   )r   r$   r#   r"   r&   r   )r%   r"   r#   r$   r   r   @   s    *
c                   s$   e Zd ZdZdZ fddZ  ZS )r
   zqException which a DBus service method throws if an authorization
    required for executing it can't be obtained.zAorg.fedoraproject.slip.dbus.service.PolKit.NotAuthorizedExceptionc                s(   | j jd | | _tt| j|| d S )N.)	__class___dbus_error_namesuperr
   __init__)selfr   r   r    )r-   r   r   r0      s    
zNotAuthorizedException.__init__)r   r   r   __doc__r.   r0   __classcell__r   r   )r-   r   r
      s   c               @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
edd Zedd	 Zed
d Zedd Zedd Zdd Zdd Zdd Zdi fddZdS )PolKitz"Convenience wrapper around polkit.zorg.freedesktop.PolicyKit1z%/org/freedesktop/PolicyKit1/Authorityz$org.freedesktop.PolicyKit1.AuthorityNc             C   s4   || j kr0tjr0tjjtj d t_d t_d t_d S )N)
_dbus_namer4   _PolKit__busZremove_signal_receiver_PolKit__signal_receiver_PolKit__interface)clsnameZ	old_ownerZ	new_ownerr   r   r   _on_name_owner_changed   s
    zPolKit._on_name_owner_changedc             C   s0   t js*tj t _t jj| jdd| jdt _t jS )NZNameOwnerChangedzorg.freedesktop.DBus)Zhandler_functionZsignal_nameZdbus_interfaceZarg0)r4   r6   r   Z	SystemBusZadd_signal_receiverr;   r5   r7   )r1   r   r   r   _bus   s    
zPolKit._busc             C   s   t js| jj t _t jS )N)r4   _PolKit__bus_namer<   Zget_unique_name)r1   r   r   r   	_bus_name   s    zPolKit._bus_namec             C   sF   t js@y"tj| jj| j| j| jt _W n tj	k
r>   Y nX t jS )N)
r4   r8   r   	Interfacer<   
get_objectr5   
_dbus_path_dbus_interfacer   )r1   r   r   r   
_interface   s    

zPolKit._interfacec             C   s
   t | jS )N)boolrC   )r1   r   r   r   _polkit_present   s    zPolKit._polkit_presentc          	   C   s>   | j jdd}tj|d}y|j|}W n   d }Y nX |S )Nzorg.freedesktop.DBusz/org/freedesktop/DBus)r<   r@   r   r?   ZGetConnectionUnixUser)r1   system_bus_nameZ
bus_objectZbus_interfaceZuidr   r   r   Z__dbus_system_bus_name_uid   s    
z!PolKit.__dbus_system_bus_name_uidc             C   s6   | j s
dS | jjdd| jif|i dd\}}}|p4|S )NTzsystem-bus-namer:   r    )rE   rC   CheckAuthorizationr>   )r1   Zauthorizationis_authorizedis_challengedetailsr   r   r   Z__authorization_is_obtainable   s    z$PolKit.__authorization_is_obtainablec                s8    j s
dS t|tttfs |f}t fdd|d}|S )NTc                s   | o j |S )N)$_PolKit__authorization_is_obtainable)xy)r1   r   r   <lambda>   s    z4PolKit.AreAuthorizationsObtainable.<locals>.<lambda>)rE   r'   tuplelistsetr   )r1   authorizationsZ
obtainabler   )r1   r   r      s    
z"PolKit.AreAuthorizationsObtainableTc       	   
      sd   | j s  |d kp| j|dkS d}|r0|dO } fdd}| jjdd|if|||d||td d S )	Nr   r   c                s   | \}}} | d S )Nr   )argsrI   rJ   rK   )reply_handlerr   r   reply_cb  s    
z7PolKit.IsSystemBusNameAuthorizedAsync.<locals>.reply_cbzsystem-bus-namer:   rG   )rU   error_handlerZtimeout)rE   !_PolKit__dbus_system_bus_name_uidrC   rH   r   )	r1   rF   r   rU   rW   	challengerK   flagsrV   r   )rU   r   r      s    

z%PolKit.IsSystemBusNameAuthorizedAsync)r   r   r   r2   r5   rA   rB   r8   r6   r=   r7   classmethodr;   propertyr<   r>   rC   rE   rX   rL   r   r   r   r   r   r   r4      s"   
r4   c             C   s
   t j| S )N)__polkitr   )rS   r   r   r   r     s    Tc             C   s   t j| |||||S )N)r]   r   )rF   r   rU   rW   rY   rK   r   r   r   r     s    
)r2   Z
__future__r   r(   r   r   	functoolsr   Z	constantsr   __all__r   r   objectr	   r   r   r
   r4   r]   r   r   r   r   r   r   <module>   s(   Wo