a
    ϑ`i                     @   s   d Z ddlmZmZ ddlmZmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlZddlmZ ddlZddlZddlmZ d	d
lmZ dd ZdddZdd ZG dd dejZejddfddZdS )zB
Utilities functions for Anaconda repository command line manager
    )print_functionunicode_literals)makedirsgetenvpath)existsjoinisfile)PY2N)RotatingFileHandler)InsecureRequestWarning   )USER_LOGDIRc                    s\   t  r<t }|  W  d   S 1 s20    Y  t fdddD rXt  S )z~
    If value is a file path and the file exists its contents are stripped and returned,
    otherwise value is returned.
    Nc                 3   s   | ]}| v V  qd S )N ).0charvaluer   6lib/python3.9/site-packages/repo_cli/utils/__init__.py	<genexpr>       z file_or_token.<locals>.<genexpr>z/\.)r	   openreadstripany
ValueError)r   fdr   r   r   file_or_token   s    
*r   Fc                    s    fdd}|S )Nc                    sB   t | tr d d S r2 jd| ||fd n d| d S )Nzexecution interrupted )exc_infoz%s)
issubclassKeyboardInterrupterror)exc_type	exc_valueexc_tracebackloggershow_tracebackr   r   
excepthook%   s    

z&_custom_excepthook.<locals>.excepthookr   )r'   r(   r)   r   r&   r   _custom_excepthook$   s    
r*   c                 C   s   |r|durdS t d}|r>| r>t|dkr>|d dS | d}t|ttfrt|trn|sn|d t	|s|
d|  td |S d	S )
a  
    Tries to determine if SSL certificate verification is
    needed and if needed then the follows priorities below

    1. If `verify_ssl` is given true then it is highest priority,
    2. Then `SSL_NO_VERIFY` comes next if environment variable is set,
    3. Else if `ssl_verify` is set in config and if either string or boolean,
    4. Finally we should verify SSL certificates by default.
    NFZSSL_NO_VERIFYr   z2SSL_NO_VERIFY is set please make sure to unset it.Z
ssl_verifyz;"ssl_verify" is set to false please consider to turn it on.z:Specified "ssl_verify=%s" certificate path does not exist.T)r   isdigitintZwarningget
isinstanceboolstrr   r   r"   sysexit)configZinsecurer'   Zconfig_ssl_verifyr   r   r   get_ssl_verify_option2   s    




r4   c                       s   e Zd Z fddZ  ZS )ConsoleFormatterc                    s8   |j tjkrdnd}tr || _n|| j_tt| |S )Nz%(message)sz[%(levelname)s] %(message)s)	ZlevelnologgingINFOr
   Z_fmtZ_stylesuperr5   format)selfrecordZfmt	__class__r   r   r9   W   s    zConsoleFormatter.format)__name__
__module____qualname__r9   __classcell__r   r   r<   r   r5   V   s   r5   c                 C   s   |  tj ttstt ttd}t|ddd}| tj t }| | |	t
  |	td | | | | t| |dt_|rtjjt d S )Nzcli.logi      )ZmaxBytesZbackupCountz3%(asctime)s %(levelname)-8s %(name)-15s %(message)s)r(   )ZsetLevelr6   DEBUGr   r   r   r   r   ZStreamHandlerZsetFormatterr5   	FormatterZ
addHandlerr*   r1   r)   requestsZpackagesZurllib3Zdisable_warningsr   )r'   Z	log_levelr(   Zdisable_ssl_warningsZlog_fileZfile_handlerZconsole_handlerr   r   r   _setup_logginga   s    



rF   )F)__doc__Z
__future__r   r   osr   r   r   Zos.pathr   r   r	   Zsixr
   r6   Zlogging.handlersr   r1   rE   Z$requests.packages.urllib3.exceptionsr   r3   r   r   r*   r4   rD   r5   r7   rF   r   r   r   r   <module>   s   
$