U
    h                     @  s|  U d Z ddlmZ ddlZddlZddlmZ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Zd	ed
< zddlmZ W nP eefk
r   zddlZW n* ek
r Z zeZdZW 5 dZ[X Y nX Y nX edkrenejjZG dd deZddddZG dd dejZG dd deZG dd deZda ded< ddddddZ!ddd d!Z"ddd"d#d$Z#dS )%z1Module that holds a global gspread.client.Client.    )annotationsN)AnyCallableMappingSequence)credentials)
html_utils)ipython_env)	sheets_idzException | None_gspread_import_error)gspreadc                   @  s   e Zd ZdS )SpreadsheetNotFoundErrorN)__name__
__module____qualname__ r   r   O/tmp/pip-unpacked-wheel-doshhd5e/google/generativeai/notebook/gspread_client.pyr   .   s   r   	Exception)returnc                   C  s   t dtS )Nz&"gspread" module not imported, got: {})RuntimeErrorformatr   r   r   r   r   _get_import_error2   s    r   c                   @  sV   e Zd ZdZejdddddZejdddd	d
dZejddddddZdS )GSpreadClientzpWrapper around gspread.client.Client.

  This adds a layer of indirection for us to inject mocks for testing.
  sheets_id.SheetsIdentifierNonesidr   c                 C  s   dS )zValidates that `name` is the name of a Google Sheets document.

    Raises an exception if false.

    Args:
      sid: The identifier for the document.
    Nr   selfr   r   r   r   validate>   s    zGSpreadClient.validateint6tuple[Sequence[Mapping[str, str]], Callable[[], None]]r   worksheet_idr   c                 C  s   dS )z2Returns all records for a Google Sheets worksheet.Nr   r   r   r#   r   r   r   get_all_recordsH   s    zGSpreadClient.get_all_recordsSequence[Sequence[Any]]r   rowsr   c                 C  s   dS )z@Writes results to a new worksheet to the Google Sheets document.Nr   r   r   r(   r   r   r   write_recordsP   s    zGSpreadClient.write_recordsN)	r   r   r   __doc__abcabstractmethodr   r%   r*   r   r   r   r   r   8   s   	r   c                   @  sb   e Zd ZdZdddddZddd	d
ZdddddZddddddZddddddZdS )GSpreadClientImplz)Concrete implementation of GSpreadClient.r   ipython_env.IPythonEnv | Noneclientenvc                 C  s   || _ || _dS )zConstructor.

    Args:
      client: Instance of gspread.client.Client.
      env: Optional instance of IPythonEnv. This is used to display messages
        such as the URL of the output Worksheet.
    N)_client_ipython_env)r   r1   r2   r   r   r   __init__\   s    zGSpreadClientImpl.__init__r   )r   c              
   C  s   zZ|  r| j|  W S | r:| jt| W S | rX| jt| W S W n2 tk
r } zt	d
||W 5 d}~X Y nX t	ddS )a?  Opens a Sheets document from `sid`.

    Args:
      sid: The identifier for the Sheets document.

    Raises:
      SpreadsheetNotFoundError: If the Sheets document cannot be found or
        cannot be opened.

    Returns:
      A gspread.Worksheet instance representing the worksheet referred to by
      `sid`.
    zUnable to find Sheets with {}Nz"Invalid sheets_id.SheetsIdentifier)namer3   openkeyZopen_by_keystrurlZopen_by_urlGSpreadExceptionr   r   )r   r   excr   r   r   _openg   s    zGSpreadClientImpl._openr   r   c                 C  s   |  | d S N)r=   r   r   r   r   r      s    zGSpreadClientImpl.validater    r!   r"   c                   sP   |  ||| jd k	r6| j  fdd}nfdd} |fS )Nc                
     s2     dtjtjdjjd d S )Nz Reading inputs from worksheet {}{} in {}r:   text)display_htmlr   r   get_anchor_tagr
   	SheetsURLr:   titler   r2   sheet	worksheetr   r   _display_fn   s    
z6GSpreadClientImpl.get_all_records.<locals>._display_fnc                     s   t dj j d S )Nz&Reading inputs from worksheet {} in {})printr   rE   r   )rG   rH   r   r   rI      s     )r=   Zget_worksheetr4   r%   )r   r   r#   rI   r   rF   r   r%      s    


z!GSpreadClientImpl.get_all_recordsr&   r'   c              
   C  s   |  |}tj }d|dd|dd}|j|ddd}|j|d | jd k	r| jd	tj	t
|jd
|j|jd ntd|j|j d S )NzResults z%Y_%m_%dz (z%s)   )rE   r(   cols)valuesz#Results written to new worksheet {}r?   r@   z)Results written to new worksheet {} in {})r=   datetimenowZadd_worksheetZappend_rowsr4   rB   r   r   rC   r
   rD   r:   rE   rJ   )r   r   r(   rG   Zcurrent_datetimerE   rH   r   r   r   r*      s(    



	 zGSpreadClientImpl.write_recordsN)	r   r   r   r+   r5   r=   r   r%   r*   r   r   r   r   r.   Y   s    r.   c                   @  sD   e Zd ZdZdddddZdddd	d
dZddddddZdS )NullGSpreadClientzNull-object implementation of GSpreadClient.

  This class raises an error if any of its methods are called. It is used when
  the gspread library is not available.
  r   r   r   c                 C  s
   t  d S r>   r   r   r   r   r   r      s    zNullGSpreadClient.validater    r!   r"   c                 C  s
   t  d S r>   rR   r$   r   r   r   r%      s    z!NullGSpreadClient.get_all_recordsr&   r'   c                 C  s
   t  d S r>   rR   r)   r   r   r   r*      s    zNullGSpreadClient.write_recordsN)r   r   r   r+   r   r%   r*   r   r   r   r   rQ      s   rQ   zGSpreadClient | None_gspread_clientzcredentials.Credentialsr/   r   )credsr2   r   c                 C  s*   t dk	r t | }t||dant adS )z Sets up credential for gspreads.Nr0   )r   	authorizer.   rS   rQ   )rT   r2   r1   r   r   r   rU      s    
rU   c                   C  s   t stdt S )NzMust call authorize() first)rS   r   r   r   r   r   
get_client   s    rV   )r1   r   c                 C  s   | a dS )z(Overrides the global client for testing.N)rS   )r1   r   r   r   testonly_set_client   s    rW   )$r+   
__future__r   r,   rO   typingr   r   r   r   Zgoogle.authr   Zgoogle.generativeai.notebookr   r	   r
   r   __annotations__r   ImportErrorModuleNotFoundErrorer   
exceptionsr;   r   r   r   ABCr   r.   rQ   rS   rU   rV   rW   r   r   r   r   <module>   s6   !o