
    rh                       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
<   	 ddlZeenej,                  j.                  Z G d de      ZddZ G d dej6                        Z G d de      Z G d de      Zdaded<   ddZ ddZ!ddZ"y# e$ rZeZdZY dZ[vdZ[ww xY w)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                      e Zd Zy)SpreadsheetNotFoundErrorN)__name__
__module____qualname__     ~/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/google/generativeai/notebook/gspread_client.pyr   r   *   s    r   r   c                 >    t        dj                  t                    S )Nz&"gspread" module not imported, got: {})RuntimeErrorformatr   r   r   r   _get_import_errorr   .   s    @GGH]^__r   c                      e Zd ZdZej
                  dd       Zej
                  	 	 	 	 	 	 dd       Zej
                  	 	 	 	 	 	 dd       Zy)	GSpreadClientztWrapper around gspread.client.Client.

    This adds a layer of indirection for us to inject mocks for testing.
    c                     y)zValidates that `name` is the name of a Google Sheets document.

        Raises an exception if false.

        Args:
          sid: The identifier for the document.
        Nr   selfsids     r   validatezGSpreadClient.validate8       r   c                     y)z2Returns all records for a Google Sheets worksheet.Nr   r   r   worksheet_ids      r   get_all_recordszGSpreadClient.get_all_recordsB   r    r   c                     y)z@Writes results to a new worksheet to the Google Sheets document.Nr   r   r   rowss      r   write_recordszGSpreadClient.write_recordsJ   r    r   Nr   sheets_id.SheetsIdentifierreturnNoner   r*   r#   intr+   z6tuple[Sequence[Mapping[str, str]], Callable[[], None]]r   r*   r'   zSequence[Sequence[Any]]r+   r,   )	r   r   r   __doc__abcabstractmethodr   r$   r(   r   r   r   r   r   2   s    
 	  	A'A A 
@	A A 	O'O &O 
	O Or   r   c                  P    e Zd ZdZddZd	dZd
dZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZy)GSpreadClientImplz)Concrete implementation of GSpreadClient.c                     || _         || _        y)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   clientenvs      r   __init__zGSpreadClientImpl.__init__V   s     r   c                   	 |j                         r)| j                  j                  |j                               S |j                         r2| j                  j	                  t        |j                                     S |j                         r2| j                  j                  t        |j                                     S 	 t        d      # t        $ r }t        dj                  |            |d}~ww xY 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)namer6   openkeyopen_by_keystrurlopen_by_urlGSpreadExceptionr   r   )r   r   excs      r   _openzGSpreadClientImpl._opena   s    	axxz||((44wwy||//CGGI??wwy||//CGGI??  ''KLL   	a*+J+Q+QRU+VW]``	as$   8C AC =AC 	C4C//C4c                &    | j                  |       y N)rE   r   s     r   r   zGSpreadClientImpl.validatez   s    

3r   c                    | j                  |      j                  |      | j                  | j                  fd}nfd}j                         |fS )Nc                      j                  dj                  t        j                  t	        j
                  j                        dj                  j                  j                                           y )Nz Reading inputs from worksheet {}{} in {}rA   text)display_htmlr   r	   get_anchor_tagr   	SheetsURLrA   title)r9   sheet	worksheets   r   _display_fnz6GSpreadClientImpl.get_all_records.<locals>._display_fn   sW      6=="11 ) 3 3IMM B!+!2!29??EKK!Pr   c                 d    t        dj                  j                   j                               y )Nz&Reading inputs from worksheet {} in {})printr   rP   )rQ   rR   s   r   rS   z6GSpreadClientImpl.get_all_records.<locals>._display_fn   s$    >EEiooW\WbWbcdr   )rE   get_worksheetr7   r$   )r   r   r#   rS   r9   rQ   rR   s       @@@r   r$   z!GSpreadClientImpl.get_all_records}   sZ    
 

3''5	(##Ce ((*K77r   c                <   | j                  |      }t        j                  j                         }d|dd|dd}|j                  |dd      }|j	                  |       | j
                  | j
                  j                  d	j                  t        j                  t        j                  |j                        d
j                  |j                  |j                                           y t        dj                  |j                  |j                               y )NzResults z%Y_%m_%dz (z%s)   )rP   r'   cols)valuesz#Results written to new worksheet {}rJ   rK   z)Results written to new worksheet {} in {})rE   datetimenowadd_worksheetappend_rowsr7   rM   r   r	   rN   r   rO   rA   rP   rU   )r   r   r'   rQ   current_datetimerP   rR   s          r   r(   zGSpreadClientImpl.write_records   s    
 

3
 $,,002+H5R8H7LAN ''e!!'D	T*(**5<<--%//	>'..yL =DDY__V[VaVabcr   N)r8   r   r9   ipython_env.IPythonEnv | None)r   r*   r)   r-   r/   )	r   r   r   r0   r:   rE   r   r$   r(   r   r   r   r4   r4   S   s\    3	 M28'8 8 
@	88d'd &d 
	dr   r4   c                  @    e Zd ZdZddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZy)	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.
    c                    t               rG   r   r   s     r   r   zNullGSpreadClient.validate   s    !!r   c                    t               rG   re   r"   s      r   r$   z!NullGSpreadClient.get_all_records       
  !!r   c                    t               rG   re   r&   s      r   r(   zNullGSpreadClient.write_records   rg   r   Nr)   r-   r/   )r   r   r   r0   r   r$   r(   r   r   r   rc   rc      sO    ""'" " 
@	""'" &" 
	"r   rc   zGSpreadClient | None_gspread_clientc                j    t         #t        j                  |       }t        ||      ayt	               ay)z Sets up credential for gspreads.N)r8   r9   )gspread	authorizer4   ri   rc   )credsr9   r8   s      r   rl   rl      s.     ""5)+6sC+-r   c                 0    t         st        d      t         S )NzMust call authorize() first)ri   r   r   r   r   
get_clientro      s    899r   c                    | a y)z(Overrides the global client for testing.N)ri   )r8   s    r   testonly_set_clientrq      s	     Or   )r+   	Exception)rm   zcredentials.Credentialsr9   ra   r+   r,   )r+   r   )r8   r   r+   r,   )#r0   
__future__r   r1   r\   typingr   r   r   r   google.authr   google.generativeai.notebookr	   r
   r   r   __annotations__rk   ImportErrorerr   
exceptionsrC   r   r   r   ABCr   r4   rc   ri   rl   ro   rq   r   r   r   <module>r|      s    8 " 
  3 3 # 3 4 2
 +/ ' . !(9W5G5G5X5X 	| 	`OCGG OBbd bdJ" "4 )-% ,.I  Gs   B* *B=/B88B=