o
    ß­j  ã                
   @  s>  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
< zddlZW n eyW Z z
eZdZW Y dZ[ndZ[ww edu r^enejjZG dd„ deƒZd%dd„ZG dd„ dejƒZG dd„ deƒZG dd„ deƒZdaded< d&dd„Z d'd d!„Z!d(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                   @  s   e Zd ZdS )ÚSpreadsheetNotFoundErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úh/var/www/html/CropPilot/venv/lib/python3.10/site-packages/google/generativeai/notebook/gspread_client.pyr   *   s    r   ÚreturnÚ	Exceptionc                   C  s   t d t¡ƒS )Nz&"gspread" module not imported, got: {})ÚRuntimeErrorÚformatr   r   r   r   r   Ú_get_import_error.   ó   r   c                   @  s@   e Zd ZdZejddd„ƒZejddd„ƒZejd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                 C  ó   dS )z­Validates that `name` is the name of a Google Sheets document.

        Raises an exception if false.

        Args:
          sid: The identifier for the document.
        Nr   ©Úselfr   r   r   r   Úvalidate8   ó    zGSpreadClient.validateÚworksheet_idÚintú6tuple[Sequence[Mapping[str, str]], Callable[[], None]]c                 C  r   )z2Returns all records for a Google Sheets worksheet.Nr   ©r   r   r!   r   r   r   Úget_all_recordsB   r    zGSpreadClient.get_all_recordsÚrowsúSequence[Sequence[Any]]c                 C  r   )z@Writes results to a new worksheet to the Google Sheets document.Nr   ©r   r   r&   r   r   r   Úwrite_recordsJ   r    zGSpreadClient.write_recordsN©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    	r   c                   @  sB   e Zd ZdZddd„Zdd
d„Zddd„Zddd„Zddd„ZdS )ÚGSpreadClientImplz)Concrete implementation of GSpreadClient.Úclientr   Úenvúipython_env.IPythonEnv | Nonec                 C  s   || _ || _dS )zßConstructor.

        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   r1   r2   r   r   r   Ú__init__V   s   
zGSpreadClientImpl.__init__r   r   c              
   C  sŠ   z0|  ¡ r| j |  ¡ ¡W S | ¡ r| j t| ¡ ƒ¡W S | ¡ r,| j t| ¡ ƒ¡W S W t	dƒ‚ tyD } zt	d 
|¡ƒ|‚d}~w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)Únamer4   ÚopenÚkeyÚopen_by_keyÚstrÚurlÚopen_by_urlÚGSpreadExceptionr   r   )r   r   Úexcr   r   r   Ú_opena   s   ÿþ€ÿzGSpreadClientImpl._openr   r   c                 C  s   |   |¡ d S ©N)r@   r   r   r   r   r   z   r   zGSpreadClientImpl.validater!   r"   r#   c                   sP   |   |¡‰ˆ |¡‰| jd ur| j‰ ‡ ‡‡fdd„}n‡‡fdd„}ˆ ¡ |fS )Nc                
     s2   ˆ   d tjt ˆj¡d ˆjˆj¡d�¡¡ d S )Nz Reading inputs from worksheet {}ú{} in {}©r<   Útext)Údisplay_htmlr   r   Úget_anchor_tagr
   Ú	SheetsURLr<   Útitler   ©r2   ÚsheetÚ	worksheetr   r   Ú_display_fnˆ   s   
þÿÿz6GSpreadClientImpl.get_all_records.<locals>._display_fnc                     s   t d ˆjˆ j¡ƒ d S )Nz&Reading inputs from worksheet {} in {})Úprintr   rH   r   )rJ   rK   r   r   rL   ”   s   )r@   Úget_worksheetr5   r%   )r   r   r!   rL   r   rI   r   r%   }   s   


z!GSpreadClientImpl.get_all_recordsr&   r'   c              
   C  sœ   |   |¡}tj ¡ }d|d›d|d›d�}|j|ddd�}|j|d� | jd urB| j d	 tj	t
 |j¡d
 |j|j¡d�¡¡ d S td |j|j¡ƒ d S )NzResults z%Y_%m_%dz (z%sú)é   )rH   r&   Úcols)Úvaluesz#Results written to new worksheet {}rB   rC   z)Results written to new worksheet {} in {})r@   ÚdatetimeÚnowÚadd_worksheetÚappend_rowsr5   rE   r   r   rF   r
   rG   r<   rH   rM   )r   r   r&   rJ   Úcurrent_datetimerH   rK   r   r   r   r)   ™   s   



þÿÿ	zGSpreadClientImpl.write_recordsN)r1   r   r2   r3   )r   r   r*   r+   r,   )	r   r   r   r-   r6   r@   r   r%   r)   r   r   r   r   r0   S   s    



r0   c                   @  s.   e Zd ZdZddd„Zddd„Zddd„ZdS )ÚNullGSpreadClientz­Null-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                 C  ó   t ƒ ‚rA   ©r   r   r   r   r   r   ¿   s   zNullGSpreadClient.validater!   r"   r#   c                 C  rY   rA   rZ   r$   r   r   r   r%   Â   ó   z!NullGSpreadClient.get_all_recordsr&   r'   c                 C  rY   rA   rZ   r(   r   r   r   r)   É   r[   zNullGSpreadClient.write_recordsNr*   r+   r,   )r   r   r   r-   r   r%   r)   r   r   r   r   rX   ¸   s
    

rX   zGSpreadClient | NoneÚ_gspread_clientÚcredsúcredentials.Credentialsr2   r3   r   c                 C  s,   t durt  | ¡}t||d�adS tƒ adS )z Sets up credential for gspreads.N)r1   r2   )ÚgspreadÚ	authorizer0   r\   rX   )r]   r2   r1   r   r   r   r`   Õ   s   

r`   c                   C  s   t stdƒ‚t S )NzMust call authorize() first)r\   r   r   r   r   r   Ú
get_clientß   s   ra   r1   c                 C  s   | a dS )z(Overrides the global client for testing.N)r\   )r1   r   r   r   Útestonly_set_clientå   s   rb   )r   r   )r]   r^   r2   r3   r   r   )r   r   )r1   r   r   r   )#r-   Ú
__future__r   r.   rS   Útypingr   r   r   r   Úgoogle.authr   Úgoogle.generativeai.notebookr   r	   r
   r   Ú__annotations__r_   ÚImportErrorÚer   Ú
exceptionsr>   r   r   r   ÚABCr   r0   rX   r\   r`   ra   rb   r   r   r   r   Ú<module>   s6   €þ
!e


