
    eV                         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	 ddl
mZmZmZmZ 	 d dlZd dlmZ  ej(                  e      Z G d	 d
ej.                  e      Zy# e$ r Y 0e$ r Y 6w xY w)    )closingN   )backend)
properties)KeyringBackend)SimpleCredential)	InitErrorPasswordDeleteErrorExceptionRaisedContextKeyringLockedc                   b    e Zd ZdZdZej                  defd       Zd Z	d Z
d Zd Zd	 Zd
 Zy)KeyringzSecret Service KeyringzPython keyring libraryreturnc                    t               5 }t        j                   d d d        rt        d      t        j                  dk  rt        d      	 t        t        j                               5 }t        j                  |      st        d      	 d d d        y# 1 sw Y   }xY w# 1 sw Y   yxY w# t        j                  $ r}t        d|z        d }~ww xY 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es       @/usr/lib/python3/dist-packages/keyring/backends/SecretService.pypriorityzKeyring.priority    s    #% 	#""	#788**V3DEE	M0023 z$??
K&4  L 	# 	#  00 	MG!KLL	MsA   BB0 -!B$B0 B!$B-)B0 -B0 0CCCc                 |   t        j                         }	 t        | d      r!t        j                  || j                        }nt        j
                  |      }|j                         r+|j                          |j                         rt        d      |S # t        j                  $ r}t        d|z        d}~ww xY w)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   s       r   get_preferred_collectionz Keyring.get_preferred_collection3   s    
 %%'	Ht34*55c4;T;TU
*AA#F
 !##%#$FGG 00 	HBQFGG	Hs   AB B;(B66B;c                 r    t        |d      r|j                          |j                         rt        d      y )Nr'   zFailed to unlock the item!)r#   r'   r&   r   )r(   items     r   r'   zKeyring.unlockF   s0    4"KKM>> <==     c                 >   | j                         }t        |j                        5  |j                  | j	                  ||            }|D ]<  }| j                  |       |j                         j                  d      c cddd       S  	 ddd       y# 1 sw Y   yxY w)z,Get password of the username for the serviceutf-8N)r+   r   r   search_items_queryr'   
get_secretdecoder(   serviceusernamer*   itemsr-   s         r   get_passwordzKeyring.get_passwordL   s    224
Z**+ 	9++DKK,JKE 9D!(//88		9 	99	9 	9 	9s   ABBBc                    | j                         }| j                  ||| j                        }dj                  ||      }t	        |j
                        5  |j                  |||d       ddd       y# 1 sw Y   yxY w)z,Set password for the username of the service)applicationzPassword for '{}' on '{}'T)replaceN)r+   r2   appidformatr   r   create_item)r(   r6   r7   passwordr*   
attributeslabels          r   set_passwordzKeyring.set_passwordU   sx    224
[[(

[K
+228WEZ**+ 	N""5*h"M	N 	N 	Ns   A55A>c                 &   | j                         }t        |j                        5  |j                  | j	                  ||            }|D ]  }|j                         c cddd       S  	 ddd       t        d      # 1 sw Y   t        d      xY w)z/Delete the stored password (only the first one)NzNo such password!)r+   r   r   r1   r2   deleter
   r5   s         r   delete_passwordzKeyring.delete_password]   s    224
Z**+ 	%++DKK,JKE %{{}$	% 	%%	% ""566		% ""566s   8A=(A==Bc                    | j                   | j                     }| j                  ||      }| j                         }t	        |j
                        5  |j                  |      }|D ]h  }| j                  |       |j                         j                  |d         }t        ||j                         j                  d            c cddd       S  	 ddd       y# 1 sw Y   yxY w)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.
        r7   r0   N)schemesschemer2   r+   r   r   r1   r'   get_attributesgetr   r3   r4   )r(   r6   r7   rI   queryr*   r8   r-   s           r   get_credentialzKeyring.get_credentialf   s     dkk*GX.224
Z**+ 	U++E2E UD!..044VJ5GH'$//2C2J2J72STT	U 	UU	U 	U 	Us   A4CCC#N)r   
__module____qualname____doc__r=   r   classpropertyfloatr    r+   r'   r9   rC   rF   rM    r.   r   r   r      sI     $E  $&>9N7Ur.   r   )
contextlibr   logging r   _compatr   r   credentialsr   errorsr	   r
   r   r   r   secretstorage.exceptionsr   ImportErrorAttributeError	getLoggerr   logSchemeSelectabler   rS   r.   r   <module>r`      sw         $ * 	1 g!\Ug&& \U  	 		s   
A# #A1*A10A1