U
    ܦ\                     @   s   d 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
 ddlmZ ddlmZ eeZdad	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd%ddZdd Zdd Zdd  Zd!d" Z d#d$ Z!e  dS )&z1
Core API functions and initialization routines.
    N   )configparserfilter)maxbackend)	platform_)failc                 C   s   t | tjstd| adS )z!Set current keyring backend.
    z0The keyring must be a subclass of KeyringBackendN)
isinstancer   ZKeyringBackend	TypeError_keyring_backend)keyring r   ./usr/lib/python3/dist-packages/keyring/core.pyset_keyring   s    r   c                   C   s   t S )z!Get current keyring backend.
    )r   r   r   r   r   get_keyring   s    r   c               	   C   s   t  } zt|  W n tk
r*   Y nX tj| d}tj|r\djf t	 }t
|t|d}|d W 5 Q R X dS )z4
    Configure the null keyring as the default.
    keyringrc.cfgz Refusing to overwrite {filename}wz7[backend]
default-keyring=keyring.backends.null.KeyringN)platformconfig_rootosmakedirsOSErrorpathjoinexistsformatlocalsRuntimeErroropenwrite)rootfilenamemsgfiler   r   r   disable%   s    r%   c                 C   s   t | |S )z-Get password from the specified service.
    )r   get_passwordservice_nameusernamer   r   r   r&   6   s    r&   c                 C   s   t | || dS )z8Set password for the user in the specified service.
    N)r   set_password)r(   r)   Zpasswordr   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+   B   s    r+   c                 C   s   t | |S )z0Get a Credential for the specified service.
    )r   get_credentialr'   r   r   r   r,   H   s    r,   c                 C   s
   | j dkS )Nr   )priorityr   r   r   r   recommendedN   s    r.   c                 C   s<   | t _t| t  }tt p4t p4t|t	 t j
d dS )z
    Load 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keyN)r   Z_limitr   Zget_all_keyringr   load_envload_configr   r	   ZKeyringZby_priority)limitZkeyringsr   r   r   init_backendR   s    r4   c                 C   s,   |  d\}}}t| tj| }t||S )aF  
    Load the keyring class indicated by name.

    These popular names are tested to ensure their presence.

    >>> popular_names = [
    ...      'keyring.backends.Windows.WinVaultKeyring',
    ...      'keyring.backends.OS_X.Keyring',
    ...      'keyring.backends.kwallet.DBusKeyring',
    ...      'keyring.backends.SecretService.Keyring',
    ...  ]
    >>> list(map(_load_keyring_class, popular_names))
    [...]

    These legacy names are retained for compatibility.

    >>> legacy_names = [
    ...  ]
    >>> list(map(_load_keyring_class, legacy_names))
    [...]
    .)
rpartition
__import__sysmodulesgetattr)keyring_nameZmodule_namesep
class_namemoduler   r   r   _load_keyring_classf   s    
r?   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')
    )r?   r-   )r;   Zclass_r   r   r   load_keyring   s    r@   c                   C   s*   zt tjd W S  tk
r$   Y nX dS )z6Load a keyring configured in the environment variable.ZPYTHON_KEYRING_BACKENDN)r@   r   environKeyErrorr   r   r   r   r1      s    r1   c               	   C   s   d} t jt | }t j|s&dS t }|| t	| z,|
dr^|dd }ntddW n8 tjtfk
r   td}|dd|   Y dS X t|S )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   r   ZRawConfigParserread_load_keyring_pathZhas_sectiongetstripNoOptionErrorImportErrorlogging	getLoggerZwarningr@   )r"   Zkeyring_cfgconfigr;   Zloggerr   r   r   r2      s$    


r2   c              	   C   sD   z"|  dd }tjd| W n tjtjfk
r>   Y nX dS )z)load the keyring-path option (if present)r   zkeyring-pathr   N)rE   rF   r8   r   insertr   rG   NoSectionError)rK   r   r   r   r   rD      s
    rD   )N)"__doc__r   r8   rI   Z
py27compatr   r   Z
py33compatr    r   utilr   r   Zbackendsr	   rJ   __name__logr   r   r   r%   r&   r*   r+   r,   r.   r4   r?   r@   r1   r2   rD   r   r   r   r   <module>   s2   
	

