
    3fi                        d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 erddl
mZ n	 ddl
mZ  G d de      Zy	# e$ r Y w xY w)
z#Base class for Google Sheets tools.    )annotations)TYPE_CHECKINGOptional)BaseTool)Field)Resourcec                      e Zd ZU dZ edd      Zded<    edd      Zded	<   dd
ZddZ	e
dd       Ze
dd       ZddZddZy)SheetsBaseToola  Base class for Google Sheets tools.

    Authentication:
    - `api_resource`: OAuth2 credentials for full access (read/write private sheets)
    - `api_key`: API key for read-only access (public sheets only)

    !!! note

        Write operations require OAuth2 credentials.
    Nz>Google Sheets API resource, OAuth2 credentials for full access)defaultdescriptionzOptional[Resource]api_resourcez:Google API key for read-only access to public spreadsheetszOptional[str]api_keyc                    | j                   r| j                   S | j                  rddlm}  || j                        S ddlm}  |       S )zGet the appropriate Google Sheets service based on available credentials.

        Returns:
            Resource: Google Sheets API service

        Raises:
            ValueError: If neither `api_resource` nor `api_key` is provided
        r   )!build_sheets_service_with_api_key)build_sheets_service)r   r   'langchain_google_community.sheets.utilsr   r   )selfr   r   s      d/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_google_community/sheets/base.py_get_servicezSheetsBaseTool._get_service+   sJ     $$$\\ 5 
 U'))    c                L    | j                   r| j                  st        d      yy)zCheck if the current authentication method supports write operations.

        Raises:
            ValueError: If using API key for write operations
        zWrite operations require OAuth2 credentials, not API key. Please provide api_resource for write access to private spreadsheets.N)r   r   
ValueError)r   s    r   _check_write_permissionsz'SheetsBaseTool._check_write_permissionsD   s-     << 1 1X  !2<r   c                     | |      S )zCreate a tool from an API resource.

        Args:
            api_resource: The API resource to use.

        Returns:
            A tool with OAuth2 credentials for full access.
        )r    )clsr   s     r   from_api_resourcez SheetsBaseTool.from_api_resourceP   s     --r   c                     | |      S )zCreate a tool from an API key.

        Args:
            api_key: The API key to use.

        Returns:
            A tool with API key for read-only access.
        )r   r   )r   r   s     r   from_api_keyzSheetsBaseTool.from_api_key\   s     7##r   c                   |j                  d      r|d   S |j                  di       j                  d      rt        |d   d         S |j                  di       j                  d      t        |d   d         S |j                  di       j                  d      t        |d   d         S |j                  di       j                  d      rt        |d   d         S |j                  di       j                  d      t        |d   d         S |j                  di       j                  d      t        |d   d         S y)zSafely extract cell value with proper fallback hierarchy.

        Args:
            cell_data: Cell data dictionary from Google Sheets API

        Returns:
            The cell value as a string.
        formattedValueeffectiveValuestringValuenumberValue	boolValueuserEnteredValue )getstr)r   	cell_datas     r   _safe_get_cell_valuez#SheetsBaseTool._safe_get_cell_valueh   sL    ==)*-..]]+R044]Cy!12=ABB]]+R044]COy!12=ABB]]+R044[AMy!12;?@@]]-r266}Ey!34]CDD]]-r266}EQy!34]CDD]]-r266{COy!34[ABBr   c                    g }|D ]?  }t        |d      r |j                  |j                                /|j                  |       A |S )a  Convert a list of items to dictionaries.

        Handles both Pydantic models and dicts. Useful for converting
        user-provided schemas (which may be Pydantic models or plain dicts)
        into the dict format required by Google Sheets API.

        Args:
            items: List of items that may be Pydantic models or dictionaries

        Returns:
            List of dictionaries with all Pydantic models converted.
        
model_dump)hasattrappendr-   )r   itemsresultitems       r   _convert_to_dict_listz$SheetsBaseTool._convert_to_dict_list   sJ      	$Dt\*doo/0 d#	$ r   )returnr   )r4   None)r   r   r4   'SheetsBaseTool')r   r)   r4   r6   )r*   dictr4   r)   )r0   listr4   r8   )__name__
__module____qualname____doc__r   r   __annotations__r   r   r   classmethodr   r   r+   r3   r   r   r   r
   r
      sv    	 (-T(L$ 
 #PG] 
*2
 	. 	. 	$ 	$4r   r
   N)r<   
__future__r   typingr   r   langchain_core.toolsr   pydanticr   googleapiclient.discoveryr   ImportErrorr
   r   r   r   <module>rE      sD    ) " * ) 26
BX B	  s   9 A A