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@    
ÿÿ

þ

 