o
    Vhh                     @   s   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	 ddl
mZ dd	lmZmZmZ zd dlZd dlmZ W n eyG   Y n	 eyO   Y nw e eZG d
d dejeZdS )    N)closing)ExceptionTrap   )backend)KeyringBackend)
properties)SimpleCredential)	InitErrorKeyringLockedPasswordDeleteErrorc                   @   sX   e Zd ZdZdZejdef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 libraryreturnc              
   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:    )
r   secretstorage__name__RuntimeError__version_tuple__r   	dbus_initcheck_service_availability
exceptionsSecretStorageException)clsexc
connectione r   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-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|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: .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_collection4   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%   G   s
   
zKeyring.unlockc                 C   s|   |   }t|j+ || ||}|D ]}| | | d  W  d   S W d   dS 1 s7w   Y  dS )z,Get password of the username for the serviceutf-8N)r)   r   r   search_items_queryr%   
get_secretdecoder&   serviceusernamer(   itemsr*   r   r   r   get_passwordM   s   
"zKeyring.get_passwordc                 C   sn   |   }| j||| jd}d| d| d}t|j |j|||dd W d   dS 1 s0w   Y  dS )z,Set password for the username of the service)applicationzPassword for 'z' on ''T)replaceN)r)   r-   appidr   r   create_item)r&   r1   r2   passwordr(   
attributeslabelr   r   r   set_passwordV   s   "zKeyring.set_passwordc                 C   st   |   }t|j% || ||}|D ]}|   W  d   S W d   td1 s1w   Y  td)z/Delete the stored password (only the first one)NzNo such password!)r)   r   r   r,   r-   deleter   r0   r   r   r   delete_password^   s   

zKeyring.delete_passwordc                 C   s   | j | j }| ||}|  }t|j3 ||}|D ]"}| | | 	|d }t
|| d  W  d   S W d   dS 1 sKw   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.
        r2   r+   N)schemesschemer-   r)   r   r   r,   r%   get_attributesgetr   r.   r/   )r&   r1   r2   rA   queryr(   r3   r*   r   r   r   get_credentialg   s   

"zKeyring.get_credentialN)r   
__module____qualname____doc__r8   r   classpropertyfloatr   r)   r%   r4   r=   r?   rE   r   r   r   r   r      s    		r   )logging
contextlibr   jaraco.contextr    r   r   compatr   credentialsr   errorsr	   r
   r   r   secretstorage.exceptionsr   ImportErrorAttributeError	getLoggerr   logSchemeSelectabler   r   r   r   r   <module>   s$    
