o
    a_                     @   s:  d 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mZ ddl	m
Z ddlmZ eeZdadd Zd	ejfd
dZdd Zdeded	eje fddZdededed	dfddZdeded	dfddZdedeje d	ejej fddZdd Zd)ddZd)ddZdd  Z d!d" Z!d#d$ Z"d%d& Z#d'd( Z$dS )*z1
Core API functions and initialization routines.
    N   )backendcredentials)	platform_)failc                 C   s   t | tjs
td| adS )zSet current keyring backend.z1The keyring must be an instance of KeyringBackendN)
isinstancer   KeyringBackend	TypeError_keyring_backend)keyring r   ./usr/lib/python3/dist-packages/keyring/core.pyset_keyring   s   r   returnc                   C   s   t du rt  ttjt S )zGet current keyring backend.N)r
   init_backendtypingcastr   r   r   r   r   r   get_keyring   s   r   c                  C   s   t  } zt|  W n	 ty   Y nw tj| d}tj|r+d| }t|t	|d}|
d W d   dS 1 sAw   Y  dS )z4
    Configure the null keyring as the default.
    keyringrc.cfgzRefusing to overwrite wz7[backend]
default-keyring=keyring.backends.null.KeyringN)platformconfig_rootosmakedirsOSErrorpathjoinexistsRuntimeErroropenwrite)rootfilenamemsgfiler   r   r   disable$   s   
"r%   service_nameusernamec                 C      t  | |S )z(Get password from the specified service.)r   get_passwordr&   r'   r   r   r   r)   5   s   r)   passwordc                 C   s   t  | || dS )z3Set password for the user in the specified service.N)r   set_password)r&   r'   r+   r   r   r   r,   :   s   r,   c                 C   s   t  | | dS )z:Delete the password for the user in the specified service.N)r   delete_passwordr*   r   r   r   r-   ?   s   r-   c                 C   r(   )z+Get a Credential for the specified service.)r   get_credentialr*   r   r   r   r.   D   s   r.   c                 C   s
   | j dkS )Nr   )priority)r   r   r   r   recommendedK   s   
r0   c                 C   s   t t|  dS )z"
    Load a detected backend.
    N)r   _detect_backendlimitr   r   r   r   O   s   r   c                 C   s0   | t _t pt ptt| t  t t j	dS )z
    Return a keyring specified in the config file or infer the best available.

    Limit, if supplied, should be a callable taking a backend and returning
    True if that backend should be included for consideration.
    )defaultkey)
r   _limitload_envload_configmaxfilterget_all_keyringr   Keyringby_priorityr2   r   r   r   r1   V   s   	r1   c                 C   s,   |  d\}}}t| tj| }t||S )a  
    Load the keyring class indicated by name.

    These popular names are tested to ensure their presence.

    >>> popular_names = [
    ...      'keyring.backends.Windows.WinVaultKeyring',
    ...      'keyring.backends.macOS.Keyring',
    ...      'keyring.backends.kwallet.DBusKeyring',
    ...      'keyring.backends.SecretService.Keyring',
    ...  ]
    >>> list(map(_load_keyring_class, popular_names))
    [...]
    .)
rpartition
__import__sysmodulesgetattr)keyring_namemodule_namesep
class_namemoduler   r   r   _load_keyring_classl   s   

rI   c                 C   s   t | }|j | S )z
    Load the specified keyring by name (a fully-qualified name to the
    keyring, such as 'keyring.backends.file.PlaintextKeyring')
    )rI   r/   )rD   class_r   r   r   load_keyring   s   rK   c                   C   s&   zt tjd W S  ty   Y dS w )z6Load a keyring configured in the environment variable.PYTHON_KEYRING_BACKENDN)rK   r   environKeyErrorr   r   r   r   r7      s
   r7   c               	   C   s   d} t jt | }t j|sdS t }|| t	| z|
dr/|dd }ntddW t|S  tjtfyT   td}|dd|   Y dS w )z8Load a keyring using the config file in the config root.r   Nr   zdefault-keyringr   z/Keyring config file contains incorrect values.
zConfig file: %s)r   r   r   r   r   r   configparserRawConfigParserread_load_keyring_pathhas_sectiongetstripNoOptionErrorImportErrorlogging	getLoggerwarningrK   )r"   keyring_cfgconfigrD   loggerr   r   r   r8      s,   


r8   c              	   C   sB   z|  dd }tjd| W dS  tjtjfy    Y dS w )z)load the keyring-path option (if present)r   zkeyring-pathr   N)rT   rU   rA   r   insertrO   rV   NoSectionError)r\   r   r   r   r   rR      s   rR   )N)%__doc__rO   r   rA   rX   r    r   r   utilr   r   backendsr   rY   __name__logr
   r   r   r   r%   strOptionalr)   r,   r-   
Credentialr.   r0   r   r1   rI   rK   r7   r8   rR   r   r   r   r   <module>   s@    




 