a
    bg;                     @  s  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 erd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ddddZddddZddddZdgZdZdZd)dddddddZd*d d!d!d"d#d$d%Z d!d!d&d'd(Z!dS )+zGmail tool utils.    )annotationsN)TYPE_CHECKINGListOptionalTuple)guard_import)Request)Credentials)InstalledAppFlow)Resource)buildzTuple[Request, Credentials])returnc                   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-httplib2module_nameZpip_namezgoogle.oauth2.credentials)r   r   r	    r   r   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/tools/gmail/utils.pyimport_google   s    r   r
   c                   C  s   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&   s    r   build_resourcec                   C  s   t dddjS )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_builder1   s    r   zhttps://mail.google.com/z
token.jsonzcredentials.jsonzOptional[str]zOptional[List[str]]r	   )
token_fileclient_secrets_filescopesr   c           	      C  s   t  \}}t }d}|pt}| p"t} |p*t}tj| rD|| |}|rN|j	s|rl|j
rl|jrl||  n|||}|jddd}t| d}||  W d   n1 s0    Y  |S )zGet credentials.Nr   F)portZopen_browser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_gmail_credentialsA   s$    

,r'   gmailv1zOptional[Credentials]strr   )credentialsservice_nameservice_versionr   c                 C  s   | pt  } t }|||| dS )zBuild a Gmail service.)r+   )r'   r   )r+   r,   r-   Zbuilderr   r   r   build_resource_servicec   s    
r.   )bodyr   c              
   C  s   zlddl m} z"|t| d}| } t| W W S  tyh } z t| t| W  Y d}~W S d}~0 0 W n$ ty   td t|  Y S 0 dS )zClean email body.r   )BeautifulSoupzhtml.parserNz/BeautifulSoup not installed. Skipping cleaning.)	Zbs4r0   r*   Zget_text	ExceptionloggererrorImportErrorwarning)r/   r0   Zsouper   r   r   clean_email_bodyn   s    
$
r7   )NNN)Nr(   r)   )"__doc__
__future__r   loggingr   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__r2   r   r   r   r   r   r   r'   r.   r7   r   r   r   r   <module>   s6   
   #   