o
    ÞñÐaÜ  ã                   @   sÄ   d dl 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lm	Z	m
Z
mZ ddlmZ zd dlZd dlmZ W n eyC   Y n	 eyK   Y nw d	d
„ ZG dd„ deƒZG dd„ deƒZdS )é    Né   )ÚKeyringBackend)ÚSimpleCredential)ÚPasswordDeleteError)ÚPasswordSetErrorÚ	InitErrorÚKeyringLocked)Ú
properties)ÚDBusGMainLoopc                  C   sB   t ttf} t | ¡� tjd W  d  ƒ S 1 sw   Y  dS )z/
    Safely infer an app id from sys.argv.
    r   N)ÚAttributeErrorÚ
IndexErrorÚ	TypeErrorÚ
contextlibÚsuppressÚsysÚargv)Úallowed© r   ú:/usr/lib/python3/dist-packages/keyring/backends/kwallet.pyÚ_id_from_argv   s   
$ÿr   c                       s|   e Zd ZdZeƒ p
dZdZdZdZe	j
edd„ ƒƒZ‡ fdd	„Zd
d„ Zdd„ Zdd„ Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚDBusKeyringz!
    KDE KWallet 5 via D-Bus
    zPython keyring libraryNzorg.kde.kwalletd5z/modules/kwalletd5c              
   C   sŒ   dt ƒ vr	tdƒ‚z	tjtƒ d�}W n tjy% } zt| ¡ ƒ‚d }~ww | | j¡s7| j| 	¡ v s7tdƒ‚dt
 dd¡ d¡v rDd	S d
S )NÚdbuszpython-dbus not installed©ÚmainloopzCThe KWallet daemon is neither running nor activatable through D-BusÚKDEÚXDG_CURRENT_DESKTOPÚ ú:gffffff@gš™™™™™@)ÚglobalsÚRuntimeErrorr   Ú
SessionBusr
   ÚDBusExceptionÚget_dbus_messageÚname_has_ownerÚbus_nameÚlist_activatable_namesÚosÚgetenvÚsplit)ÚclsÚbusÚexcr   r   r   Úpriority(   s"   
€ÿ
ÿÿzDBusKeyring.priorityc                    s   t ƒ j|i |¤Ž d| _d S )Néÿÿÿÿ)ÚsuperÚ__init__Úhandle)ÚselfÚargÚkw©Ú	__class__r   r   r/   <   s   
zDBusKeyring.__init__c           	      C   sÐ   d}g }| j  | j|| j¡rd| j  | j|d| j¡}| ¡ D ]-}|d }|d }| dd¡\}}| j  | j|||| j¡}|dkrJ| j  | j||| j¡ q| j  | j|d| j¡}|sf| j  	| j|| j¡ d S d S d S )NÚPythonz*@*r   é   ú@Ú*)
ÚifaceÚ	hasFolderr0   ÚappidÚreadPasswordListÚitemsÚrsplitÚwritePasswordÚremoveEntryÚremoveFolder)	r1   ÚserviceÚ
old_folderÚ
entry_listÚentryÚkeyÚpasswordÚusernameÚretr   r   r   Ú_migrate@   s.   ÿÿ€ÿìzDBusKeyring._migratec              
   C   s®   | j dkr| j | j ¡rdS tjtƒ d�}d}z| | j| j¡}t 	|d¡| _| j 
| j ¡ || j¡| _ W n tjyH } ztd| ƒ‚d }~ww | j dk rPdS |  |¡ dS )Nr   Tr   zorg.kde.KWalletzFailed to open keyring: %s.F)r0   r:   ÚisOpenr   r    r
   Ú
get_objectr$   Úobject_pathÚ	InterfaceÚopenÚnetworkWalletr<   r!   r   rK   )r1   rC   r*   ÚwIdÚ
remote_objÚer   r   r   Ú	connectedY   s"   
€ÿ

zDBusKeyring.connectedc                 C   sJ   |   |¡s	tdƒ‚| j | j||| j¡sdS | j | j||| j¡}t|ƒS )z,Get password of the username for the serviceúFailed to unlock the keyring!N)rU   r   r:   ÚhasEntryr0   r<   ÚreadPasswordÚstr©r1   rC   rI   rH   r   r   r   Úget_passwordl   s   
zDBusKeyring.get_passwordc                    sp   |durt ƒ  ||¡S |  |¡stdƒ‚| j | j|| j¡D ]}| j | j||| j¡}t	t
|ƒt
|ƒƒ  S dS )a  Gets the first username and password for a service.
        Returns a Credential instance

        The username can be omitted, but if there is one, it will forward to
        get_password.
        Otherwise, it will return the first username and password combo that it finds.
        NrV   )r.   Úget_credentialrU   r   r:   Ú	entryListr0   r<   rX   r   rY   rZ   r4   r   r   r\   v   s   
ÿüzDBusKeyring.get_credentialc                 C   s.   |   |¡s	tdƒ‚| j | j|||| j¡ dS )z,Set password for the username of the serviceúCancelled by userN)rU   r   r:   r@   r0   r<   rZ   r   r   r   Úset_password‹   s   
zDBusKeyring.set_passwordc                 C   sJ   |   |¡s	tdƒ‚| j | j||| j¡stdƒ‚| j | j||| j¡ dS )z4Delete the password for the username of the service.r^   zPassword not foundN)rU   r   r:   rW   r0   r<   rA   )r1   rC   rI   r   r   r   Údelete_password’   s
   
zDBusKeyring.delete_password)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r<   Úwalletr$   rN   r	   ÚClassPropertyÚclassmethodr,   r/   rK   rU   r[   r\   r_   r`   Ú__classcell__r   r   r4   r   r      s     

r   c                       s2   e Zd ZdZdZdZeje‡ fdd„ƒƒZ	‡  Z
S )ÚDBusKeyringKWallet4z!
    KDE KWallet 4 via D-Bus
    zorg.kde.kwalletdz/modules/kwalletdc                    s   t ƒ jd S )Nr7   )r.   r,   )r)   r4   r   r   r,   ¤   s   zDBusKeyringKWallet4.priority)ra   rb   rc   rd   r$   rN   r	   rf   rg   r,   rh   r   r   r4   r   ri   œ   s    ri   )r   r&   r   Úbackendr   Úcredentialsr   Úerrorsr   r   r   r   Úutilr	   r   Údbus.mainloop.glibr
   ÚImportErrorr   r   r   ri   r   r   r   r   Ú<module>   s&    þ	~