
    ; i|                        d Z ddlm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dlmZ ddZddZddZddZ G d dej                  Z G d dej                  ZdS )zSheetsInputs.    )annotations)AnyCallableMappingSequence)parse)gspread_client)	sheets_id)llmfn_inputs_source)llmfn_outputsvaluestrreturn!sheets_id.SheetsIdentifier | Nonec                    	 t          j        |           }n# t          $ r Y dS w xY w|j        rOt	          j        t	          j        |                     }t          j                    	                    |           |S dS )z4Try to open a Sheets document with `value` as a URL.N)url)
r   urlparse
ValueErrorschemer
   SheetsIdentifier	SheetsURLr	   
get_clientvalidate)r   parse_resultsids      /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/google/generativeai/notebook/sheets_utils.py_try_sheet_id_as_urlr      s    ~e,,   tt   (Y-@-G-GHHH!##,,S111
4s    
%%c                    	 t          j        t          j        |                     }n# t          $ r Y dS w xY w	 t	          j                                        |           n# t          j        $ r Y dS w xY w|S )z4Try to open a Sheets document with `value` as a key.)keyN)r
   r   	SheetsKeyr   r	   r   r   SpreadsheetNotFoundErrorr   r   s     r   _try_sheet_id_as_keyr#   -   s    (Y-@-G-GHHH   tt!##,,S11112   ttJs   '* 
88&A# #A65A6c                    t          j        |           }	 t          j                                        |           n# t          j        $ r Y dS w xY w|S )z5Try to open a Sheets document with `value` as a name.)nameN)r
   r   r	   r   r   r!   r"   s     r   _try_sheet_id_as_namer&   <   sd    

$%
0
0
0C!##,,S11112   ttJs   &> AAsheets_id.SheetsIdentifierc                    t          |           x}r|S t          |           x}r|S t          |           x}r|S t          d                    |                     )Nz-No Sheets found with "{}" as URL, key or name)r   r#   r&   RuntimeErrorformatr"   s     r   get_sheets_id_from_strr+   F   sm    "5)))s 
"5)))s 
#E***s 

FMMeTT
U
UU    c                  .     e Zd ZdZdd fdZdd
Z xZS )SheetsInputsz,Inputs to an LLMFunction from Google Sheets.r   r   r'   worksheet_idintc                d    t                                                       || _        || _        d S N)super__init___sid_worksheet_id)selfr   r/   	__class__s      r   r4   zSheetsInputs.__init__S   s.    	)r,   r   6tuple[Sequence[Mapping[str, str]], Callable[[], None]]c                f    t          j                                        | j        | j                  S )N)r   r/   )r	   r   get_all_recordsr5   r6   )r7   s    r   _to_normalized_inputs_implz'SheetsInputs._to_normalized_inputs_implX   s4     (**::	(: ; 
 
 	
r,   )r   )r   r'   r/   r0   )r   r9   )__name__
__module____qualname____doc__r4   r<   __classcell__)r8   s   @r   r.   r.   P   s\        66* * * * * * *

 
 
 
 
 
 
 
r,   r.   c                  "    e Zd ZdZddZdd	Zd
S )SheetsOutputsz4Writes outputs from an LLMFunction to Google Sheets.r   r'   c                    || _         d S r2   )r5   )r7   r   s     r   r4   zSheetsOutputs.__init__c   s    			r,   outputsllmfn_outputs.LLMFnOutputsBaser   Nonec                <   |                                 }t          |                                          g}|                    d t	          |                                 D                        t          j                                        | j	        |           d S )Nc                ,    g | ]}t          |          S  )list).0xs     r   
<listcomp>z/SheetsOutputs.write_outputs.<locals>.<listcomp>j   s    JJJT!WWJJJr,   )r   rows)
as_dictrK   keysextendzipvaluesr	   r   write_recordsr5   )r7   rE   outputs_dictoutputs_rowss       r   write_outputszSheetsOutputs.write_outputsf   s    ((-1,2C2C2E2E-F-F,GJJc<3F3F3H3H.IJJJKKK!##11	 	2 	
 	
 	
 	
 	
r,   N)r   r'   )rE   rF   r   rG   )r=   r>   r?   r@   r4   rX   rJ   r,   r   rC   rC   `   sB        >>   	
 	
 	
 	
 	
 	
r,   rC   N)r   r   r   r   )r   r   r   r'   )r@   
__future__r   typingr   r   r   r   urllibr   google.generativeai.notebookr	   r
    google.generativeai.notebook.libr   r   r   r#   r&   r+   LLMFnInputsSourcer.   LLMFnOutputsSinkrC   rJ   r,   r   <module>r`      sV     " " " " " " 3 3 3 3 3 3 3 3 3 3 3 3       7 7 7 7 7 7 2 2 2 2 2 2 @ @ @ @ @ @ : : : : : :   &      V V V V
 
 
 
 
&8 
 
 
 
 
 
 
 
M2 
 
 
 
 
r,   