o
    Ďi
                     @   s  d dl Z d dlZd dl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mZmZ d dlmZ eeZdZd	Zd
Zedeejddf fddZdefddZdeddfddZdddededB fddZddeddfddZdddedefddZ dedB fddZ!dS )    N)	Generator)contextmanager)ApiGetModelRequest)build_kaggle_client)get_kaggle_credentialsset_kaggle_api_tokenset_kaggle_credentials)UnauthenticatedErrori  z<center> <img
src=https://www.kaggle.com/static/images/site-logo.png
alt='Kaggle'> <br> Create an API token from <a
href="https://www.kaggle.com/settings/account" target="_blank">your Kaggle
settings page</a> and paste it below. <br> </center>z
<b>Thank You</b></center>returnc               	   c   sB    t  } t| }t| z| V  W t| dS t| w )zCapture output that is logged using the logger.

    Example:
    ```py
    >>> output = capture_logger_output()
    >>> logger.info("This is an info message")
    >>> logger.error("This is an error message")
    >>> print(output)
    ```
    N)ioStringIOloggingStreamHandler_logger
addHandlerremoveHandler)bufferhandler r   J/home/jeff/fluffinator/venv/lib/python3.10/site-packages/kagglehub/auth.py_capture_logger_output    s   

r   c               	   C   sR   zddl m}  |  j}|jD ]}|jdkr W dS qW dS  ttfy(   Y dS w )zReturn `True` if code is executed in a notebook (Jupyter, Kaggle Notebook, Colab, QTconsole).

    Taken from https://stackoverflow.com/a/39662359.
    Adapted to make it work with Google colab as well.
    r   )get_ipythonZMQInteractiveShellTF)IPythonr   	__class____mro____name__	NameErrorModuleNotFoundError)r   shell_classparent_classr   r   r   _is_in_notebook5   s   

r!   validate_credentialsc                    s   d}zddl m} ddlm W n ty   t|w jddddd	}jd
djdd}j	t
|	tg|d |  dtddf fdd}|| dS )zRPrompt the user for their Kaggle token and save it in a widget (Jupyter or Colab).z;You need the `ipywidgets` module: `pip install ipywidgets`.r   )displaywidgetsflexcolumncenterz50%)r#   	flex_flowalign_itemswidthz
API Token:)descriptionLogin)layout_r
   Nc              
      s   j }d_ dg _z$t }t| du rt  W d    n1 s'w   Y  | }W n tyG } z
t|}W Y d }~nd }~ww fdd|	dD  _d S )N zConnecting...Tc                    s   g | ]}|  r |qS r   )stripLabel).0liner$   r   r   
<listcomp>s   s    zB_notebook_login.<locals>.on_click_login_button.<locals>.<listcomp>
)
valuer2   childrenr   r   _validate_credentials_helpergetvalue	Exceptionstrsplit)r/   tokencapturedmessageerrorlogin_token_widgettoken_widgetr"   r%   r   r   on_click_login_button`   s    z._notebook_login.<locals>.on_click_login_button)IPython.displayr#   
ipywidgetsr%   ImportErrorLayoutPasswordButtonVBoxHTMLNOTEBOOK_LOGIN_TOKEN_HTML_STARTNOTEBOOK_LOGIN_TOKEN_HTML_ENDr<   on_click)r"   library_errorr#   
box_layoutlogin_buttonrE   r   rB   r   _notebook_loginG   s*   	rT   TverboserV   c                 C   s   t  Q}| }|  |ddt }|j| }d|v r2| r't	d |d W  d    S d|v rD|d t
krD| rCtd n| rKtd 	 W d    d S 1 sWw   Y  d S )Nzapi.v1.DiagnosticsServiceHellouserNamez*Kaggle credentials successfully validated.codezInvalid Kaggle credentials. You can obtain a Kaggle API token on your [Kaggle settings page](https://www.kaggle.com/settings/account).z3Unable to validate Kaggle credentials at this time.)r   http_client_init_session_prepare_requestr   _sessionsendjsonr   infoINVALID_CREDENTIALS_ERRORrA   warning)rV   
api_clientrZ   requestresponser   r   r   r9   x   s(   


$r9   c                 C   s6   t  r	t|  dS td}t| | sdS t  dS )z@Prompt the user for their Kaggle API token and save it globally.Nz9Enter your Kaggle API token (input will not be visible): )r!   rT   getpassr   r9   )r"   r>   r   r   r   login   s   

rg   c              
   C   sL   t  st zt| d}|rd|iW S t  ty% } zt |d}~ww )zv
    Return a dictionary with the username of the authenticated Kaggle user or raise an error if unauthenticated.
    rU   usernameN)r   r	   r9   r;   )rV   rh   er   r   r   whoami   s   

rj   c                   C   s&   zt ddd W S  ty   Y dS w )zWReturns the username of the authenticated logged-in user if configured, otherwise None.FrU   rh   N)rj   r;   r   r   r   r   get_username   s
   rk   )T)"rf   r   r   collections.abcr   
contextlibr   (kagglesdk.models.types.model_api_servicer   kagglehub.clientsr   kagglehub.configr   r   r   kagglehub.exceptionsr	   	getLoggerr   r   ra   rN   rO   r   r   boolr!   rT   r<   r9   rg   dictrj   rk   r   r   r   r   <module>   s*    
1