B
    øoh\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	 y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 d Q R X |r$tdƒ‚ttdƒs6tdƒ‚yt ¡ }tt |¡ƒ W n0 tj	k
r€ } ztd| ƒ‚W d 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   ú=lib/python3.7/site-packages/keyring/backends/SecretService.pyÚpriority   s    
zKeyring.priorityc          
   C   s†   t  ¡ }y(t| dƒr$t  || j¡}n
t  |¡}W n0 tjk
r` } ztd| ƒ‚W dd}~X Y nX | 	¡ r‚| 
¡  | 	¡ r‚tdƒ‚|S )z§If 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œ¡}x:|D ]2}t|dƒr4| ¡  | ¡ rDtdƒ‚| ¡  d¡S W 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œ¡}x|D ]
}| ¡ S W tdƒ‚dS )z8Delete the stored password (only the first one)
        )r"   r#   zNo such password!N)r!   r$   Údeleter   )r   r#   r"   r    r&   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   
