o
    Zh                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZ erHddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ eeZd0ddZd1ddZd2ddZdgZdZdZ			d3d4dd Z 		!	"d5d6d)d*Z!d7d.d/Z"dS )8zGoogle Calendar tool utils.    )annotationsN)datetime)TYPE_CHECKINGListOptionalTuple)guard_import)Request)Credentials)InstalledAppFlow)Resource)buildreturnTuple[Request, Credentials]c                   C  s   t dddjt dddjfS )zqImport google libraries.

    Returns:
        Tuple[Request, Credentials]: Request and Credentials classes.
    zgoogle.auth.transport.requestszgoogle-authmodule_nameZpip_namezgoogle.oauth2.credentials)r   r	   r
    r   r   `/var/www/html/lang_env/lib/python3.10/site-packages/langchain_google_community/calendar/utils.pyimport_google   s   r   r   c                   C     t dddjS )zcImport InstalledAppFlow class.

    Returns:
        InstalledAppFlow: InstalledAppFlow class.
    zgoogle_auth_oauthlib.flowzgoogle-auth-oauthlibr   )r   r   r   r   r   r   import_installed_app_flow'   
   r   build_resourcec                   C  r   )zImport googleapiclient.discovery.build function.

    Returns:
        build_resource: googleapiclient.discovery.build function.
    zgoogleapiclient.discoveryzgoogle-api-python-clientr   )r   r   r   r   r   r   'import_googleapiclient_resource_builder2   r   r   z(https://www.googleapis.com/auth/calendarz
token.jsonzcredentials.json
token_fileOptional[str]client_secrets_filescopesOptional[List[str]]r
   c           	      C  s   t  \}}t }d}|pt}| pt} |pt}tj| r"|| |}|r'|j	s_|r6|j
r6|jr6||  n|||}|jdd}t| d}||  W d   |S 1 sZw   Y  |S )zGet credentials.Nr   )portw)r   r   DEFAULT_SCOPESDEFAULT_CREDS_TOKEN_FILEDEFAULT_CLIENT_SECRETS_FILEospathexistsZfrom_authorized_user_fileZvalidZexpiredZrefresh_tokenrefreshZfrom_client_secrets_fileZrun_local_serveropenwriteto_json)	r   r   r   r	   r
   r   credsZflowtokenr   r   r   get_google_credentialsB   s*   


r-   calendarv3credentialsOptional[Credentials]service_namestrservice_versionr   c                 C  s   | pt  } t }|||| dS )z Build a Google Calendar service.)r0   )r-   r   )r0   r2   r4   builderr   r   r   build_resource_serviced   s   
r6   start_datetimeend_datetimeboolc                 C  s4   zt | d t |d W dS  ty   Y dS w )zCheck if the event is all day.z%Y-%m-%dTF)r   strptime
ValueError)r7   r8   r   r   r   is_all_day_evento   s   r<   )r   r   )r   r   )r   r   )NNN)r   r   r   r   r   r   r   r
   )Nr.   r/   )r0   r1   r2   r3   r4   r3   r   r   )r7   r3   r8   r3   r   r9   )#__doc__
__future__r   loggingr$   r   typingr   r   r   r   Zlangchain_core.utilsr   Zgoogle.auth.transport.requestsr	   Zgoogle.oauth2.credentialsr
   Zgoogle_auth_oauthlib.flowr   Zgoogleapiclient.discoveryr   r   r   	getLogger__name__loggerr   r   r   r!   r"   r#   r-   r6   r<   r   r   r   r   <module>   s:    



#