509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
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 )NZ_slip_polkit_auth_required)Ú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                s6   ‡‡‡fdd„‰ | dk	r"t ˆ | ƒ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, ...):
                ...c                s    y
| ||ŽS  t jk
rš } zr|jƒ }|jtƒs2‚ |ttƒd … }ˆ d k	rTˆ |d� ˆd k	r‚yˆ|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_proxyNc                s
   t ˆ | ƒS )N)r   )r   )r#   r   r   Údecorate”   s    zenable_proxy.<locals>.decorate)r   )r   r"   r!   r    r$   r   )r#   r    r!   r"   r   r   @   s
    6
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   r)   ¡   s    
zNotAuthorizedException.__init__)r   r   r   Ú__doc__r'   r)   Ú__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_namer-   Ú_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)r-   r/   r   Z	SystemBusZadd_signal_receiverr4   r.   r0   )r*   r   r   r   Ú_bus½   s    
zPolKit._busc             C   s   t js| jjƒ t _t jS )N)r-   Ú_PolKit__bus_namer5   Zget_unique_name)r*   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)
r-   r1   r   Ú	Interfacer5   Ú
get_objectr.   Ú
_dbus_pathÚ_dbus_interfacer   )r*   r   r   r   Ú
_interfaceÎ   s    

zPolKit._interfacec             C   s
   t | jƒS )N)Úboolr<   )r*   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)r5   r9   r   r8   ZGetConnectionUnixUser)r*   Ú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-namer3   r   Ú )r>   r<   ÚCheckAuthorizationr7   )r*   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)r*   r   r   Ú<lambda>û   s    z4PolKit.AreAuthorizationsObtainable.<locals>.<lambda>)r>   Ú
isinstanceÚtupleÚlistÚsetr   )r*   ÚauthorizationsZ
obtainabler   )r*   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   )ÚargsrB   rC   rD   )Úreply_handlerr   r   Úreply_cb  s    
z7PolKit.IsSystemBusNameAuthorizedAsync.<locals>.reply_cbzsystem-bus-namer3   r@   )rO   Úerror_handlerZtimeout)r>   Ú!_PolKit__dbus_system_bus_name_uidr<   rA   r   )	r*   r?   r   rO   rQ   Ú	challengerD   ÚflagsrP   r   )rO   r   r      s    

z%PolKit.IsSystemBusNameAuthorizedAsync)r   r   r   r+   r.   r:   r;   r1   r/   r6   r0   Úclassmethodr4   Úpropertyr5   r7   r<   r>   rR   rE   r   r   r   r   r   r   r-   ¨   s"   
r-   c             C   s
   t j| ƒS )N)Ú__polkitr   )rM   r   r   r   r     s    Tc             C   s   t j| |||||ƒS )N)rW   r   )r?   r   rO   rQ   rS   rD   r   r   r   r     s    
)r+   Z
__future__r   Úcollectionsr   r   Ú	functoolsr   Z	constantsr   Ú__all__r   r   Úobjectr	   r   r   r
   r-   rW   r   r   r   r   r   r   Ú<module>   s(   Wo