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 )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   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     
