
    ; i                    ~   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n# e$ rZeZdZY dZ[ndZ[ww xY weenej        j        Z G d de          Zd!dZ G d dej                  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"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                      e Zd ZdS )SpreadsheetNotFoundErrorN)__name__
__module____qualname__     /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/google/generativeai/notebook/gspread_client.pyr   r   *   s        Dr   r   return	Exceptionc                 P    t          d                    t                              S )Nz&"gspread" module not imported, got: {})RuntimeErrorformatr   r   r   r   _get_import_errorr   .   s     @GGH]^^___r   c                  x    e Zd ZdZej        dd            Zej        dd
            Zej        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                    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   s     r   validatezGSpreadClient.validate8         r   worksheet_idint6tuple[Sequence[Mapping[str, str]], Callable[[], None]]c                    dS )z2Returns all records for a Google Sheets worksheet.Nr   r"   r   r%   s      r   get_all_recordszGSpreadClient.get_all_recordsB   r$   r   rowsSequence[Sequence[Any]]c                    dS )z@Writes results to a new worksheet to the Google Sheets document.Nr   r"   r   r+   s      r   write_recordszGSpreadClient.write_recordsJ   r$   r   N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         
 	    	A A A A 	O O O O O Or   r   c                  :    e Zd ZdZddZdd	ZddZddZddZdS )GSpreadClientImplz)Concrete implementation of GSpreadClient.clientr   envipython_env.IPythonEnv | Nonec                "    || _         || _        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"   r8   r9   s      r   __init__zGSpreadClientImpl.__init__V   s     r   r   r   c                B   	 |                                 r,| j                            |                                           S |                                r9| j                            t          |                                                    S |                                r9| j                            t          |                                                    S n5# t          $ r(}t          d
                    |                    |d}~ww xY wt          d          )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)namer<   openkeyopen_by_keystrurlopen_by_urlGSpreadExceptionr   r   )r"   r   excs      r   _openzGSpreadClientImpl._opena   s    	axxzz 5|((444wwyy @|//CGGII???wwyy @|//CGGII???@ 	a 	a 	a*+J+Q+QRU+V+VWW]``	a&'KLLLs%   ?C AC AC 
D'#D

Dr   r   c                0    |                      |           d S N)rI   r!   s     r   r#   zGSpreadClientImpl.validatez   s    

3r   r%   r&   r'   c                    |                      |                              |          | j        | j        fd}nfd}                                |fS )Nc                                           d                    t          j        t	          j        j                  d                    j        j                                                 d S )Nz Reading inputs from worksheet {}{} in {}rE   text)display_htmlr   r	   get_anchor_tagr   	SheetsURLrE   title)r9   sheet	worksheets   r   _display_fnz6GSpreadClientImpl.get_all_records.<locals>._display_fn   st      6=="1 ) 3IM B B!+!2!29?EK!P!P       r   c                 b    t          d                    j         j                             d S )Nz&Reading inputs from worksheet {} in {})printr   rT   )rU   rV   s   r   rW   z6GSpreadClientImpl.get_all_records.<locals>._display_fn   s.    >EEioW\Wbccdddddr   )rI   get_worksheetr=   r*   )r"   r   r%   rW   r9   rU   rV   s       @@@r   r*   z!GSpreadClientImpl.get_all_records}   s    
 

3''55	(#C       e e e e e e ((**K77r   r+   r,   c                6   |                      |          }t          j                                        }d|dd|dd}|                    |dd          }|                    |           | j        x| j                            d	                    t          j	        t          j        |j                  d
                    |j        |j                                                 d S t          d                    |j        |j                             d S )NzResults z%Y_%m_%dz (z%s)   )rT   r+   cols)valuesz#Results written to new worksheet {}rN   rO   z)Results written to new worksheet {} in {})rI   datetimenowadd_worksheetappend_rowsr=   rQ   r   r	   rR   r   rS   rE   rT   rY   )r"   r   r+   rU   current_datetimerT   rV   s          r   r/   zGSpreadClientImpl.write_records   s'   
 

3
 $,0022N+NNN8HNNNN ''e!!'DD	T***(**5<<-%/	>>'..yLL        =DDY_V[Vabbcccccr   N)r8   r   r9   r:   )r   r   r0   r1   r2   )	r   r   r   r3   r>   rI   r#   r*   r/   r   r   r   r7   r7   S   s        33	  	  	  	 M M M M2   8 8 8 88d d d d d dr   r7   c                  *    e Zd ZdZddZdd
Z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                    t                      rK   r   r!   s     r   r#   zNullGSpreadClient.validate   s    !!!r   r%   r&   r'   c                    t                      rK   rh   r)   s      r   r*   z!NullGSpreadClient.get_all_records       
  !!!r   r+   r,   c                    t                      rK   rh   r.   s      r   r/   zNullGSpreadClient.write_records   rj   r   Nr0   r1   r2   )r   r   r   r3   r#   r*   r/   r   r   r   rf   rf      sZ         " " " "" " " "" " " " " "r   rf   zGSpreadClient | None_gspread_clientcredscredentials.Credentialsr9   r:   r   c                ~    t           't          j        |           }t          ||          adS t	                      adS )z Sets up credential for gspreads.N)r8   r9   )gspread	authorizer7   rl   rf   )rm   r9   r8   s      r   rq   rq      s?     "5))+6sCCC+--r   c                 <    t           st          d          t           S )NzMust call authorize() first)rl   r   r   r   r   
get_clientrs      s     :8999r   r8   c                
    | a dS )z(Overrides the global client for testing.N)rl   )r8   s    r   testonly_set_clientru      s     OOOr   )r   r   )rm   rn   r9   r:   r   r   )r   r   )r8   r   r   r   )#r3   
__future__r   r4   r`   typingr   r   r   r   google.authr   google.generativeai.notebookr	   r
   r   r   __annotations__rp   ImportErrorer   
exceptionsrG   r   r   r   ABCr   r7   rf   rl   rq   rs   ru   r   r   r   <module>r      sC   8 7 7 " " " " " " 



  3 3 3 3 3 3 3 3 3 3 3 3 # # # # # # 3 3 3 3 3 3 4 4 4 4 4 4 2 2 2 2 2 2
 +/  . . . .NNNN   GGGGGG !(99W5G5X 	 	 	 	 	| 	 	 	` ` ` `O O O O OCG O O OBbd bd bd bd bd bd bd bdJ" " " " " " " "4 )- , , , ,. . . .        s   A AAA