
    
!i                       % S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	  SSK
Jr  SSKJr  SSKJr  SSKJr  SrS	\S
'    SSKr\c  \O\R,                  R.                  r " S S\5      rSS jr " S S\R6                  5      r " S S\5      r " S S\5      rSqS\S'   SS jr SS jr!SS jr"g! \ a  r\rSr SrCN|SrCff = f)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                      \ rS rSrSrg)SpreadsheetNotFoundError*    N)__name__
__module____qualname____firstlineno____static_attributes__r       e/home/james-whalen/.local/lib/python3.13/site-packages/google/generativeai/notebook/gspread_client.pyr   r   *   s    r   r   c                 >    [        SR                  [        5      5      $ )Nz&"gspread" module not imported, got: {})RuntimeErrorformatr   r   r   r   _get_import_errorr   .   s    @GGH]^__r   c                      \ rS rSrSr\R                  SS j5       r\R                        S	S j5       r\R                        S
S j5       r	Sr
g)GSpreadClient2   zlWrapper around gspread.client.Client.

This adds a layer of indirection for us to inject mocks for testing.
c                    g)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   validateGSpreadClient.validate8       r   c                    g)z2Returns all records for a Google Sheets worksheet.Nr   r!   r"   worksheet_ids      r   get_all_recordsGSpreadClient.get_all_recordsB   r%   r   c                    g)z@Writes results to a new worksheet to the Google Sheets document.Nr   r!   r"   rowss      r   write_recordsGSpreadClient.write_recordsJ   r%   r   r   Nr"   sheets_id.SheetsIdentifierreturnNoner"   r1   r(   intr2   z6tuple[Sequence[Mapping[str, str]], Callable[[], None]]r"   r1   r-   zSequence[Sequence[Any]]r2   r3   )r   r   r   r   __doc__abcabstractmethodr#   r)   r.   r   r   r   r   r   r   2   s    
 	  	A'A A 
@	A A 	O'O &O 
	O Or   r   c                  b    \ rS rSrSrS
S jrSS jrSS jr      SS jr      SS jr	Sr
g	)GSpreadClientImplS   z)Concrete implementation of GSpreadClient.c                    Xl         X l        g)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__GSpreadClientImpl.__init__V   s     r   c                    UR                  5       (       a)  U R                  R                  UR                  5       5      $ UR                  5       (       a2  U R                  R	                  [        UR                  5       5      5      $ UR                  5       (       a2  U R                  R                  [        UR                  5       5      5      $  [        S5      e! [         a   n[        SR                  U5      5      UeSnAff = f)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)namer?   openkeyopen_by_keystrurlopen_by_urlGSpreadExceptionr   r   )r!   r"   excs      r   _openGSpreadClientImpl._opena   s    	axxzz||((44wwyy||//CGGI??wwyy||//CGGI??  ''KLL   	a*+J+Q+QRU+VW]``	as%   =C  AC AC 
D$C??Dc                &    U R                  U5        g N)rO   r    s     r   r#   GSpreadClientImpl.validatez   s    

3r   c                   ^^^ U R                  U5      mTR                  U5      mU R                  b  U R                  mUUU4S jnOUU4S jnTR                  5       U4$ )Nc                    > T R                  SR                  [        R                  " [        R
                  " TR                  5      SR                  TR                  TR                  5      S95      5        g )Nz Reading inputs from worksheet {}{} in {}rK   text)display_htmlr   r	   get_anchor_tagr   	SheetsURLrK   title)rB   sheet	worksheets   r   _display_fn6GSpreadClientImpl.get_all_records.<locals>._display_fn   sW      6=="11 ) 3 3IMM B!+!2!29??EKK!Pr   c                 d   > [        SR                  TR                  T R                  5      5        g )Nz&Reading inputs from worksheet {} in {})printr   r\   )r]   r^   s   r   r_   r`      s$    >EEiooW\WbWbcdr   )rO   get_worksheetr@   r)   )r!   r"   r(   r_   rB   r]   r^   s       @@@r   r)   !GSpreadClientImpl.get_all_records}   s_    
 

3''5	(##C e ((*K77r   c                0   U R                  U5      n[        R                  R                  5       nSUS SUS S3nUR                  USSS9nUR	                  US9  U R
                  b  U R
                  R                  S	R                  [        R                  " [        R                  " UR                  5      S
R                  UR                  UR                  5      S95      5        g [        SR                  UR                  UR                  5      5        g )NzResults z%Y_%m_%dz (z%s)   )r\   r-   cols)valuesz#Results written to new worksheet {}rV   rW   z)Results written to new worksheet {} in {})rO   datetimenowadd_worksheetappend_rowsr@   rY   r   r	   rZ   r   r[   rK   r\   rb   )r!   r"   r-   r]   current_datetimer\   r^   s          r   r.   GSpreadClientImpl.write_records   s    
 

3
 $,,002+H5R8H7LAN ''e!!'D	T*(**5<<--%//	>'..yL =DDY__V[VaVabcr   r>   N)rA   r   rB   ipython_env.IPythonEnv | None)r"   r1   r0   r4   r6   )r   r   r   r   r7   rC   rO   r#   r)   r.   r   r   r   r   r;   r;   S   s\    3	 M28'8 8 
@	88d'd &d 
	dr   r;   c                  N    \ rS rSrSrSS jr      S	S jr      S
S jrSrg)NullGSpreadClient   zNull-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                    [        5       erR   r   r    s     r   r#   NullGSpreadClient.validate   s    !!r   c                    [        5       erR   ru   r'   s      r   r)   !NullGSpreadClient.get_all_records       
  !!r   c                    [        5       erR   ru   r,   s      r   r.   NullGSpreadClient.write_records   ry   r   r   Nr0   r4   r6   )	r   r   r   r   r7   r#   r)   r.   r   r   r   r   rr   rr      sO    ""'" " 
@	""'" &" 
	"r   rr   zGSpreadClient | None_gspread_clientc                f    [         b   [         R                  " U 5      n[        X!S9qg[	        5       qg)z Sets up credential for gspreads.N)rA   rB   )gspread	authorizer;   r|   rr   )credsrB   rA   s      r   r   r      s,     ""5)+6C+-r   c                 :    [         (       d  [        S5      e[         $ )NzMust call authorize() first)r|   r   r   r   r   
get_clientr      s    ?899r   c                    U q g)z(Overrides the global client for testing.N)r|   )rA   s    r   testonly_set_clientr      s	     Or   )r2   	Exception)r   zcredentials.CredentialsrB   rp   r2   r3   )r2   r   )rA   r   r2   r3   )#r7   
__future__r   r8   rj   typingr   r   r   r   google.authr   google.generativeai.notebookr	   r
   r   r   __annotations__r~   ImportErrorer   
exceptionsrM   r   r   r   ABCr   r;   rr   r|   r   r   r   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/ /C5B>>C