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&    	~