o
    Vhm#                     @  s"  U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZ eeZed
ZdZded< G dd dejZG dd dedZ G dd dZ!G dd de!Z"dddZ#ed ddZ$G dd dZ%dS )!z 
Keyring implementation support
    )annotationsN)ExceptionTrap)once   )credentialserrorsutil)
properties)metadatapriorityz.typing.Callable[[KeyringBackend], bool] | None_limitc                      s0   e Zd ZdZ fddZdd Zdd Z  ZS )KeyringBackendMetaz
    Specialized subclass behavior.

    Keeps a registry of all (non-abstract) types.

    Wraps set_password to validate the username.
    c                   s$   t  ||| |   |   d S N)super__init__	_register"_validate_username_in_set_password)clsnamebasesdict	__class__ c/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/keyring/backend.pyr   '   s   zKeyringBackendMeta.__init__c                 C  s0   t | ds	t | _| j}| js||  d S d S )N_classes)hasattrsetr   __abstractmethods__add)r   classesr   r   r   r   ,   s   
zKeyringBackendMeta._registerc                   s&   | j  t  fdd}|| _ dS )zM
        Wrap ``set_password`` such to validate the passed username.
        c                   s$   |  |  | ||g|R i |S r   )_validate_username)selfsystemusernameargskwargsorigr   r   wrapper9   s   
zFKeyringBackendMeta._validate_username_in_set_password.<locals>.wrapperN)set_password	functoolswraps)r   r)   r   r'   r   r   3   s   
z5KeyringBackendMeta._validate_username_in_set_password)__name__
__module____qualname____doc__r   r   r   __classcell__r   r   r   r   r      s
    r   c                   @  s   e Zd ZdZdd Zejd+ddZe j	Z	eje	dd	 Z
ed,ddZejd-ddZd-ddZejd.ddZd/ddZejd0ddZd1dd Zd2d"d#Zd3d$d%Zd4d(d)Zd*S )5KeyringBackendz]The abstract base class of the keyring, every backend must implement
    this interface.
    c                 C  s   |    d S r   )set_properties_from_envr"   r   r   r   r   F   s   zKeyringBackend.__init__returnfloatc                 C  s   t )a  
        Each backend class must supply a priority, a number (float or integer)
        indicating the priority of the backend relative to all other backends.
        The priority need not be static -- it may (and should) vary based
        attributes of the environment in which is runs (platform, available
        packages, etc.).

        A higher number indicates a higher priority. The priority should raise
        a RuntimeError with a message indicating the underlying cause if the
        backend is not suitable for the current environment.

        As a rule of thumb, a priority between zero but less than one is
        suitable, but a priority of one or greater is recommended.
        )NotImplementedErrorr4   r   r   r   r   I   s   zKeyringBackend.priorityc                 C  s
   | j  d S r   )r   r   r   r   r   viable^   s   
zKeyringBackend.viabler   type[KeyringBackend]filter[type[KeyringBackend]]c                 C  s   t td| jS )z6
        Return all subclasses deemed viable.
        r9   )filteroperator
attrgetterr   r8   r   r   r   get_viable_backendsc   s   z"KeyringBackend.get_viable_backendsstrc                 C  s.   | j d\}}}|dd}d|| jgS )zr
        The keyring name, suitable for display.

        The name is derived from module and class name.
        ._ )r.   
rpartitionreplacejoinr-   )r   parentsepmod_namer   r   r   r   l   s   zKeyringBackend.namec                 C  s&   t | }|j d|j d|jddS )NrA   z (priority: g))typer.   r-   r   )r"   keyring_classr   r   r   __str__x   s   zKeyringBackend.__str__servicer$   
str | Nonec                 C     dS )z,Get password of the username for the serviceNr   r"   rO   r$   r   r   r   get_password|      zKeyringBackend.get_passwordNonec                 C  s   |st jdtdd dS dS )z3
        Ensure the username is not empty.
        z(Empty usernames are deprecated. See #668   )
stacklevelN)warningswarnDeprecationWarning)r"   r$   r   r   r   r!      s   
z!KeyringBackend._validate_usernamepasswordc                 C  
   t d)zSet password for the username of the service.

        If the backend cannot store passwords, raise
        PasswordSetError.
        reason)r   PasswordSetErrorr"   rO   r$   r[   r   r   r   r*      s   
zKeyringBackend.set_passwordc                 C  r\   )zDelete the password for the username of the service.

        If the backend cannot delete passwords, raise
        PasswordDeleteError.
        r]   )r   PasswordDeleteErrorrR   r   r   r   delete_password   s   
zKeyringBackend.delete_passwordcredentials.Credential | Nonec                 C  s,   |dur|  ||}|durt||S dS )a   Gets the username and password for the service.
        Returns a Credential instance.

        The *username* argument is optional and may be omitted by
        the caller or ignored by the backend. Callers must use the
        returned username.
        N)rS   r   SimpleCredentialr_   r   r   r   get_credential   s
   zKeyringBackend.get_credentialc                 C  s>   ddd}t dt|tj }|D ]
\}}t| || qdS )z6For all KEYRING_PROPERTY_* env var, set that property.itemtuple[str, str]c                 S  s(   | \}}| d\}}}|o| |fS )NKEYRING_PROPERTY_)	partitionlower)re   keyvalueprerH   r   r   r   r   parse   s   z5KeyringBackend.set_properties_from_env.<locals>.parseN)re   rf   )r<   maposenvironitemssetattr)r"   rm   propsr   rk   r   r   r   r3      s
   
z&KeyringBackend.set_properties_from_envr&   
typing.Anyc                 K  s   t  | }t|| |S r   )copyvarsupdate)r"   r&   altr   r   r   with_properties   s   
zKeyringBackend.with_propertiesN)r5   r6   )r   r:   r5   r;   )r5   r@   )rO   r@   r$   r@   r5   rP   )r$   r@   r5   rU   )rO   r@   r$   r@   r[   r@   r5   rU   )rO   r@   r$   r@   r5   rU   )rO   r@   r$   rP   r5   rb   r5   rU   )r&   rt   r5   r2   )r-   r.   r/   r0   r   r	   classpropertyr   r   passesr9   classmethodr?   r   rN   abcabstractmethodrS   r!   r*   ra   rd   r3   ry   r   r   r   r   r2   A   s.    




r2   )	metaclassc                   @  s,   e Zd ZdZejdd Zejdd ZdS )Crypterz.Base class providing encryption and decryptionc                 C  rQ   )zEncrypt the value.Nr   r"   rk   r   r   r   encrypt   rT   zCrypter.encryptc                 C  rQ   )zDecrypt the value.Nr   r   r   r   r   decrypt   rT   zCrypter.decryptN)r-   r.   r/   r0   r~   r   r   r   r   r   r   r   r      s    
r   c                   @  s    e Zd ZdZdd Zdd ZdS )NullCrypterzA crypter that does nothingc                 C     |S r   r   r   r   r   r   r         zNullCrypter.encryptc                 C  r   r   r   r   r   r   r   r      r   zNullCrypter.decryptN)r-   r.   r/   r0   r   r   r   r   r   r   r      s    r   r5   rU   c               	   C  sb   t jddD ](} ztd| j |  }t|r|  W q ty.   td|  d Y qw dS )a  
    Locate all setuptools entry points by the name 'keyring backends'
    and initialize them.
    Any third-party library may register an entry point by adding the
    following to their setup.cfg::

        [options.entry_points]
        keyring.backends =
            plugin_name = mylib.mymodule:initialize_func

    `plugin_name` can be anything, and is only used to display the name
    of the plugin at initialization time.

    `initialize_func` is optional, but will be invoked if callable.
    zkeyring.backends)groupz
Loading %szError initializing plugin rA   N)	r
   entry_pointslogdebugr   loadcallable	Exception	exception)ep	init_funcr   r   r   _load_plugins   s   r   list[KeyringBackend]c                  C  s$   t   t } tj| td}t|S )zc
    Return a list of all implemented keyrings that can be constructed without
    parameters.
    )
exceptions)r   r2   r?   r   suppress_exceptions	TypeErrorlist)viable_classesringsr   r   r   get_all_keyring   s   r   c                   @  s>   e Zd ZdZdZeedddeddddZ		ddddZd	S )SchemeSelectablea  
    Allow a backend to select different "schemes" for the
    username and service.

    >>> backend = SchemeSelectable()
    >>> backend._query('contoso', 'alice')
    {'username': 'alice', 'service': 'contoso'}
    >>> backend._query('contoso')
    {'service': 'contoso'}
    >>> backend.scheme = 'KeePassXC'
    >>> backend._query('contoso', 'alice')
    {'UserName': 'alice', 'Title': 'contoso'}
    >>> backend._query('contoso', 'alice', foo='bar')
    {'UserName': 'alice', 'Title': 'contoso', 'foo': 'bar'}
    defaultr$   rO   )r$   rO   UserNameTitle)r   	KeePassXCNr@   rP   basert   r5   dict[str, str]c                 K  s@   | j | j }t|d ur|d ||d |in|d |ifi |S )Nr$   rO   )schemesschemer   )r"   rO   r$   r   r   r   r   r   _query  s   	zSchemeSelectable._queryr   )rO   r@   r$   rP   r   rt   r5   r   )r-   r.   r/   r0   r   r   r   r   r   r   r   r   r     s    

r   rz   )r5   r   )&r0   
__future__r   r~   ru   r+   loggingr=   ro   typingrX   jaraco.contextr   jaraco.functoolsr    r   r   r   compatr	   compat.py312r
   	getLoggerr-   r   r>   by_priorityr   __annotations__ABCMetar   r2   r   r   r   r   r   r   r   r   r   <module>   s6    

# 	

