o
    j6h                  
   @  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ZW n eyW Z z
eZdZW Y dZ[ndZ[ww edu r^enejjZG dd deZ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Z d'd d!Z!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_errorc                   @  s   e Zd ZdS )SpreadsheetNotFoundErrorN)__name__
__module____qualname__ r   r   z/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/generativeai/notebook/gspread_client.pyr   *   s    r   return	Exceptionc                   C  s   t dtS )Nz&"gspread" module not imported, got: {})RuntimeErrorformatr   r   r   r   r   _get_import_error.      r   c                   @  s@   e Zd ZdZejdddZejdddZejdddZdS )GSpreadClientztWrapper around gspread.client.Client.

    This adds a layer of indirection for us to inject mocks for testing.
    sidsheets_id.SheetsIdentifierr   Nonec                 C     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   validate8       zGSpreadClient.validateworksheet_idint6tuple[Sequence[Mapping[str, str]], Callable[[], None]]c                 C  r   )z2Returns all records for a Google Sheets worksheet.Nr   r   r   r!   r   r   r   get_all_recordsB   r    zGSpreadClient.get_all_recordsrowsSequence[Sequence[Any]]c                 C  r   )z@Writes results to a new worksheet to the Google Sheets document.Nr   r   r   r&   r   r   r   write_recordsJ   r    zGSpreadClient.write_recordsNr   r   r   r   r   r   r!   r"   r   r#   r   r   r&   r'   r   r   )	r   r   r   __doc__abcabstractmethodr   r%   r)   r   r   r   r   r   2   s    	r   c                   @  sB   e Zd ZdZdddZdd
dZdddZdddZdddZdS )GSpreadClientImplz)Concrete implementation of GSpreadClient.clientr   envipython_env.IPythonEnv | Nonec                 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__V   s   
zGSpreadClientImpl.__init__r   r   c              
   C  s   z0|  r| j|  W S | r| jt| W S | r,| jt| W S W t	d tyD } zt	d
||d}~ww )ac  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)namer4   openkeyopen_by_keystrurlopen_by_urlGSpreadExceptionr   r   )r   r   excr   r   r   _opena   s   zGSpreadClientImpl._openr   r   c                 C  s   |  | d S N)r@   r   r   r   r   r   z   r   zGSpreadClientImpl.validater!   r"   r#   c                   sP   |  ||| jd ur| 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   rH   r   )rJ   rK   r   r   rL      s   )r@   get_worksheetr5   r%   )r   r   r!   rL   r   rI   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 urB| jd	tj	t
|jd
|j|jd d S td|j|j d S )NzResults z%Y_%m_%dz (z%s)   )rH   r&   cols)valuesz#Results written to new worksheet {}rB   rC   z)Results written to new worksheet {} in {})r@   datetimenowadd_worksheetappend_rowsr5   rE   r   r   rF   r
   rG   r<   rH   rM   )r   r   r&   rJ   current_datetimerH   rK   r   r   r   r)      s   



	zGSpreadClientImpl.write_recordsN)r1   r   r2   r3   )r   r   r*   r+   r,   )	r   r   r   r-   r6   r@   r   r%   r)   r   r   r   r   r0   S   s    



r0   c                   @  s.   e Zd ZdZdddZdddZ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   r   c                 C     t  rA   r   r   r   r   r   r      s   zNullGSpreadClient.validater!   r"   r#   c                 C  rY   rA   rZ   r$   r   r   r   r%         z!NullGSpreadClient.get_all_recordsr&   r'   c                 C  rY   rA   rZ   r(   r   r   r   r)      r[   zNullGSpreadClient.write_recordsNr*   r+   r,   )r   r   r   r-   r   r%   r)   r   r   r   r   rX      s
    

rX   zGSpreadClient | None_gspread_clientcredscredentials.Credentialsr2   r3   r   c                 C  s,   t durt | }t||dadS t adS )z Sets up credential for gspreads.N)r1   r2   )gspread	authorizer0   r\   rX   )r]   r2   r1   r   r   r   r`      s   

r`   c                   C  s   t stdt S )NzMust call authorize() first)r\   r   r   r   r   r   
get_client   s   ra   r1   c                 C  s   | a dS )z(Overrides the global client for testing.N)r\   )r1   r   r   r   testonly_set_client   s   rb   )r   r   )r]   r^   r2   r3   r   r   )r   r   )r1   r   r   r   )#r-   
__future__r   r.   rS   typingr   r   r   r   google.authr   google.generativeai.notebookr   r	   r
   r   __annotations__r_   ImportErrorer   
exceptionsr>   r   r   r   ABCr   r0   rX   r\   r`   ra   rb   r   r   r   r   <module>   s6   
!e


