o
    ß­jš  ã                   @  s´   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d„Zddd„Zddd„Zddd„ZG dd„ dejƒZG d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                 C  sR   zt  | ¡}W n
 ty   Y dS w |jr'tjt | ¡d�}t ¡  	|¡ |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Úsid© r   úf/var/www/html/CropPilot/venv/lib/python3.10/site-packages/google/generativeai/notebook/sheets_utils.pyÚ_try_sheet_id_as_url   s   þr   c                 C  sX   zt jt  | ¡d�}W n
 ty   Y dS w z
t ¡  |¡ W |S  tjy+   Y dS w )z4Try to open a Sheets document with `value` as a key.)ÚkeyN)r	   r   Ú	SheetsKeyr   r   r   r   ÚSpreadsheetNotFoundError©r   r   r   r   r   Ú_try_sheet_id_as_key-   s   þþÿr!   c                 C  s8   t j| d�}z
t ¡  |¡ W |S  tjy   Y dS w )z5Try to open a Sheets document with `value` as a name.)ÚnameN)r	   r   r   r   r   r   r    r   r   r   Ú_try_sheet_id_as_name<   s   þÿr#   úsheets_id.SheetsIdentifierc                 C  s>   t | ƒ }r|S t| ƒ }r|S t| ƒ }r|S td | ¡ƒ‚)Nz-No Sheets found with "{}" as URL, key or name)r   r!   r#   ÚRuntimeErrorÚformatr    r   r   r   Úget_sheets_id_from_strF   s   r'   c                      s.   e Zd ZdZdd‡ fdd„Zddd„Z‡  ZS )ÚSheetsInputsz,Inputs to an LLMFunction from Google Sheets.r   r   r$   Úworksheet_idÚintc                   s   t ƒ  ¡  || _|| _d S ©N)ÚsuperÚ__init__Ú_sidÚ_worksheet_id)Úselfr   r)   ©Ú	__class__r   r   r-   S   s   

zSheetsInputs.__init__r   ú6tuple[Sequence[Mapping[str, str]], Callable[[], None]]c                 C  s   t  ¡ j| j| jd�S )N)r   r)   )r   r   Úget_all_recordsr.   r/   )r0   r   r   r   Ú_to_normalized_inputs_implX   s   ÿz'SheetsInputs._to_normalized_inputs_impl)r   )r   r$   r)   r*   )r   r3   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r5   Ú__classcell__r   r   r1   r   r(   P   s    r(   c                   @  s$   e Zd ZdZddd„Zdd
d„ZdS )ÚSheetsOutputsz4Writes outputs from an LLMFunction to Google Sheets.r   r$   c                 C  s
   || _ d S r+   )r.   )r0   r   r   r   r   r-   c   s   
zSheetsOutputs.__init__Úoutputsúllmfn_outputs.LLMFnOutputsBaser   ÚNonec                 C  sR   |  ¡ }g }| t| ¡ ƒ¡ | dd„ t| ¡ Ž D ƒ¡ t ¡ j	| j
|d� d S )Nc                 S  s   g | ]}t |ƒ‘qS r   )Úlist)Ú.0Úxr   r   r   Ú
<listcomp>k   s    z/SheetsOutputs.write_outputs.<locals>.<listcomp>)r   Úrows)Úas_dictÚappendr?   ÚkeysÚextendÚzipÚvaluesr   r   Úwrite_recordsr.   )r0   r<   Úoutputs_dictÚoutputs_rowsr   r   r   Úwrite_outputsf   s   
þzSheetsOutputs.write_outputsN)r   r$   )r<   r=   r   r>   )r6   r7   r8   r9   r-   rM   r   r   r   r   r;   `   s    
r;   N)r   r   r   r   )r   r   r   r$   )r9   Ú
__future__r   Útypingr   r   r   r   Úurllibr   Úgoogle.generativeai.notebookr   r	   Ú google.generativeai.notebook.libr
   r   r   r!   r#   r'   ÚLLMFnInputsSourcer(   ÚLLMFnOutputsSinkr;   r   r   r   r   Ú<module>   s   





