U
    ۦ\v                     @   s   d dl Z ddlmZ ddlmZ ddlmZmZmZm	Z	 zd dl
Z
d dlmZ W n& ek
rd   Y n ek
rv   Y nX e eZG dd deZdS )    N   )
properties)KeyringBackend)	InitErrorPasswordDeleteErrorExceptionRaisedContextKeyringLockedc                   @   sF   e Zd ZdZdZejedd Zdd Z	dd Z
d	d
 Zdd ZdS )KeyringzSecret Service KeyringzPython keyring libraryc              
   C   s   t  }tj W 5 Q R X |r$tdttds6tdzt }tt| W n0 tj	k
r } ztd| W 5 d }~X Y nX dS )NzSecretStorage requiredget_default_collectionz#SecretStorage 1.0 or newer requiredz&Unable to initialize SecretService: %s   )
r   secretstorage__name__RuntimeErrorhasattr	dbus_initlistZget_all_collections
exceptionsSecretStorageException)clsexcbuse r   @/usr/lib/python3/dist-packages/keyring/backends/SecretService.pypriority   s    
zKeyring.priorityc              
   C   s   t  }z(t| dr$t || j}n
t |}W n0 tjk
r` } ztd| W 5 d}~X Y nX |	 r|
  |	 rtd|S )zIf self.preferred_collection contains a D-Bus path,
        the collection at that address is returned. Otherwise,
        the default collection is returned.
        preferred_collectionz$Failed to create the collection: %s.Nz Failed to unlock the collection!)r   r   r   
Collectionr   r
   r   r   r   	is_lockedunlockr   )selfr   
collectionr   r   r   r   get_preferred_collection)   s    
 z Keyring.get_preferred_collectionc                 C   sX   |   }|||d}|D ]6}t|dr2|  | rBtd| d  S dS )z5Get password of the username for the service
        usernameservicer   zFailed to unlock the item!zutf-8N)r!   search_itemsr   r   r   r   Z
get_secretdecoder   r$   r#   r    itemsitemr   r   r   get_password=   s    
zKeyring.get_passwordc                 C   s8   |   }| j||d}d||f }|j|||dd dS )z5Set password for the username of the service
        )Zapplicationr$   r#   zPassword for '%s' on '%s'T)replaceN)r!   appidZcreate_item)r   r$   r#   Zpasswordr    Z
attributesZlabelr   r   r   set_passwordJ   s    zKeyring.set_passwordc                 C   s8   |   }|||d}|D ]}|   S tddS )z8Delete the stored password (only the first one)
        r"   zNo such password!N)r!   r%   deleter   r'   r   r   r   delete_passwordV   s    zKeyring.delete_passwordN)r   
__module____qualname____doc__r,   r   ZClassPropertyclassmethodr   r!   r*   r-   r/   r   r   r   r   r	      s   r	   )Zloggingutilr   Zbackendr   errorsr   r   r   r   r   Zsecretstorage.exceptionsr   ImportErrorAttributeErrorZ	getLoggerr   logr	   r   r   r   r   <module>   s   
