B
    T[#                 @   s:  d Z ddlmZm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mZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! e"dZ#dd Z$d"ddZ%G dd dej&Z'ej(ddfddZ)d#ddZ*d$ddZ+d%dd Z,e-d!kr6e,  dS )&z*
Anaconda repository command line manager
    )print_functionunicode_literalsN)ArgumentParserRawDescriptionHelpFormatter)makedirs)joinexistsisfile)RotatingFileHandler)add_subparser_modules)InsecureRequestWarning)PY2)__version__)commands)interactive_login)errors)USER_LOGDIRZbinstarc          	      sD   t  r$t }|  S Q R X t fdddD r@t  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   | ]}| kV  qd S )N ).0char)valuer   Ylib/python3.7/site-packages/anaconda_client-1.7.2-py3.7.egg/binstar_client/scripts/cli.py	<genexpr>&   s    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                sD   t | tst | tjsd S r4 jd| ||fd n d| d S )N )exc_infoz%s)
issubclassKeyboardInterruptr   ZServerErrorerror)exc_type	exc_valueexc_traceback)loggershow_tracebackr   r   
excepthook/   s
    z&_custom_excepthook.<locals>.excepthookr   )r(   r)   r*   r   )r(   r)   r   _custom_excepthook.   s    	r+   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superr,   format)selfrecordZfmt)	__class__r   r   r0   <   s    zConsoleFormatter.format)__name__
__module____qualname__r0   __classcell__r   r   )r3   r   r,   ;   s   r,   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setLevelr-   DEBUGr   r   r   r   r
   ZStreamHandlerZsetFormatterr,   	FormatterZ
addHandlerr+   sysr*   requestsZpackagesZurllib3Zdisable_warningsr   )r(   	log_levelr)   disable_ssl_warningsZlog_fileZfile_handlerZconsole_handlerr   r   r   _setup_loggingF   s    



r?   c          	   C   s   |  d}|jddddd |jdddd	 |jd
ddddtjtjd |jdddddtjd |r|| jdddd|f d d S )Noutputz--disable-ssl-warnings
store_trueFz+Disable SSL warnings (default: %(default)s))actiondefaulthelpz--show-tracebackzGShow the full traceback for chalmers user errors (default: %(default)s))rB   rD   z-vz	--verboseZstore_constz&print debug information on the consoler=   )rB   rD   destrC   constz-qz--quietz+Only show warnings or errors on the console)rB   rD   rE   rF   z-Vz	--versionversionz*%%(prog)s Command line client (version %s))rB   rG   )add_argument_groupadd_argumentr-   r.   r9   ZWARNING)parserrG   Zoutput_groupr   r   r   add_default_arguments`   s    


rK   Tc             C   s  t ||td}t|| |d}|jddtdd |jddd	d d
 t|| d ||}tt	|j
|j|jd ydyt|ds|d ||S  tjk
r   tj r|jr t	d t| ||S X W n6 tjk
r   |j  |rtdndS Y nX d S )N)descriptionepilogZformatter_classzanaconda-client optionsz-tz--tokenzRAuthentication token to use. May be a token or a path to a file containing a token)typerD   z-sz--sitez&select the anaconda-client site to use)rD   rC   zconda_server.subcommand)r=   r)   r>   mainzUA sub command must be given. To show all available sub commands, run:

	 anaconda -h
zFThe action you are performing requires authentication, please sign in:   )r   r   rK   rH   rI   r   r   
parse_argsr?   r(   r=   r)   r>   hasattrr$   rO   r   ZUnauthorizedr;   stdinisattytokeninfor   ZShowHelpZ
sub_parserZ
print_help
SystemExit)Zsub_command_moduleargsexitrL   rG   rM   rJ   Zbgroupr   r   r   binstar_mains   s8    











rZ   c             C   s   t t| |ttd d S )N)rL   rG   )rZ   command_module__doc__rG   )rX   rY   r   r   r   rO      s    rO   __main__)F)N)NTNNN)NT).r\   Z
__future__r   r   r-   r;   r<   argparser   r   osr   os.pathr   r   r	   Zlogging.handlersr
   Zclyentr   Z$requests.packages.urllib3.exceptionsr   Zsixr   Zbinstar_clientr   rG   r   r[   Zbinstar_client.commands.loginr   r   Zbinstar_client.utilsr   Z	getLoggerr(   r   r+   r:   r,   r.   r?   rK   rZ   rO   r4   r   r   r   r   <module>   s4   



+

