o
    ÞñÐaƒ  ã                   @   sª   d dl m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mZ zd dlZd dlmZ W n ey=   Y n	 eyE   Y nw e e¡ZG dd	„ d	eƒZdS )
é    )ÚclosingNé   )Ú
properties)ÚKeyringBackend)ÚSimpleCredential)Ú	InitErrorÚPasswordDeleteErrorÚExceptionRaisedContextÚKeyringLockedc                   @   sV   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d„ Zdd„ ZdS )ÚKeyringzSecret Service KeyringzPython keyring libraryc              
   C   sº   t ƒ �}tj W d   ƒ n1 sw   Y  |rtdƒ‚tjdk r%tdƒ‚z$tt ¡ ƒ�}t |¡s6tdƒ‚W d   ƒ W dS 1 sBw   Y  W dS  tj	y\ } ztd| ƒ‚d }~ww )NzSecretStorage required)é   r   z#SecretStorage 3.2 or newer requiredzJThe Secret Service daemon is neither running nor activatable through D-Busz&Unable to initialize SecretService: %sé   )
r	   ÚsecretstorageÚ__name__ÚRuntimeErrorÚ__version_tuple__r   Ú	dbus_initÚcheck_service_availabilityÚ
exceptionsÚSecretStorageException)ÚclsÚexcÚ
connectionÚ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 n tjy+ } ztd| ƒ‚d}~ww | 	¡ 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   ÚhasattrÚ
Collectionr   Úget_default_collectionr   r   r   Ú	is_lockedÚunlockr
   )ÚselfÚbusÚ
collectionr   r   r   r   Úget_preferred_collection3   s   

€€ÿz Keyring.get_preferred_collectionc                 C   s&   t |dƒr	| ¡  | ¡ rtdƒ‚d S )Nr"   zFailed to unlock the item!)r   r"   r!   r
   )r#   Úitemr   r   r   r"   F   s
   
ÿzKeyring.unlockc                 C   sz   |   ¡ }t|jƒ�* | ||dœ¡}|D ]}|  |¡ | ¡  d¡  W  d  ƒ S W d  ƒ dS 1 s6w   Y  dS )z,Get password of the username for the service©ÚusernameÚserviceúutf-8N)r&   r   r   Úsearch_itemsr"   Ú
get_secretÚdecode©r#   r*   r)   r%   Úitemsr'   r   r   r   Úget_passwordL   s   
ü"þzKeyring.get_passwordc                 C   sd   |   ¡ }| j||dœ}d ||¡}t|jƒ� |j|||dd� W d  ƒ dS 1 s+w   Y  dS )z,Set password for the username of the service)Úapplicationr*   r)   zPassword for '{}' on '{}'T)ÚreplaceN)r&   ÚappidÚformatr   r   Úcreate_item)r#   r*   r)   Úpasswordr%   Ú
attributesÚlabelr   r   r   Úset_passwordU   s   ý"ÿzKeyring.set_passwordc                 C   sr   |   ¡ }t|jƒ�$ | ||dœ¡}|D ]}| ¡   W  d  ƒ S W d  ƒ tdƒ‚1 s0w   Y  tdƒ‚)z/Delete the stored password (only the first one)r(   NzNo such password!)r&   r   r   r,   Údeleter   r/   r   r   r   Údelete_passworda   s   
ý
þüzKeyring.delete_passwordc                 C   sœ   d|i}|r
||d< |   ¡ }t|jƒ�1 | |¡}|D ] }|  |¡ | ¡  d¡}t|| ¡  	d¡ƒ  W  d  ƒ S W d  ƒ dS 1 sGw   Y  dS )aY  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 use get_password
        and return a SimpleCredential containing  the username and password
        Otherwise, it will return the first username and password combo that it finds.
        r*   r)   r+   N)
r&   r   r   r,   r"   Úget_attributesÚgetr   r-   r.   )r#   r*   r)   Úqueryr%   r0   r'   r   r   r   Úget_credentialj   s   	

û"þzKeyring.get_credentialN)r   Ú
__module__Ú__qualname__Ú__doc__r4   r   ÚClassPropertyÚclassmethodr   r&   r"   r1   r:   r<   r@   r   r   r   r   r      s    		r   )Ú
contextlibr   ÚloggingÚutilr   Úbackendr   Úcredentialsr   Úerrorsr   r   r	   r
   r   Úsecretstorage.exceptionsr   ÚImportErrorÚAttributeErrorÚ	getLoggerr   Úlogr   r   r   r   r   Ú<module>   s     þ
