a
    w=ic                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZ dZdZ	dZ
dZdZd	Zd
ZdZdd Zdd Zdd Zdd ZdddZdS )z9Helpers for reading the Google Cloud SDK's configuration.    N)environment_vars)
exceptionsZgcloudAPPDATAz$application_default_credentials.jsonz
gcloud.cmd)configzconfig-helperz--formatjson)authzprint-access-tokenzH764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.comc                  C   s   zt jtj W S  ty    Y n0 t jdkrDt jt jddt	S zt jt jt
 t	W S  ty   t jdd} t j| dt	 Y S 0 dS )zReturns the absolute path the the Cloud SDK's configuration directory.

    Returns:
        str: The Cloud SDK config path.
    nt~z.configZSystemDrivezC:\N)osenvironr   ZCLOUD_SDK_CONFIG_DIRKeyErrornamepathjoin
expanduser_CONFIG_DIRECTORY_WINDOWS_CONFIG_ROOT_ENV_VARget)drive r   g/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/google/auth/_cloud_sdk.pyget_config_path/   s    

r   c                  C   s   t  } tj| tS )zGets the path to the application default credentials file.

    The path may or may not exist.

    Returns:
        str: The full path to application default credentials.
    )r   r   r   r   _CREDENTIALS_FILENAME)Zconfig_pathr   r   r   (get_application_default_credentials_pathK   s    r   c                 C   s>   t tjd}tj| |d}W d   n1 s00    Y  |S )zJ Return subprocess.check_output with the given command and ignores stderr.wstderrN)openr   devnull
subprocesscheck_output)commandr   outputr   r   r   _run_subprocess_ignore_stderrW   s    ,r$   c               
   C   s   t jdkrt} nt} zt| ft }W n tjtt	fyB   Y dS 0 zt
|d}W n tyl   Y dS 0 z|d d d d W S  ty   Y dS 0 dS )zaGets the project ID from the Cloud SDK.

    Returns:
        Optional[str]: The project ID.
    r   Nutf-8configuration
propertiescoreproject)r   r   _CLOUD_SDK_WINDOWS_COMMAND_CLOUD_SDK_POSIX_COMMANDr$   _CLOUD_SDK_CONFIG_COMMANDr    CalledProcessErrorOSErrorIOErrorr   loadsdecode
ValueErrorr   )r"   r#   r&   r   r   r   get_project_id^   s    
r3   c              
   C   s   t jdkrt}nt}zD| r0|ft d|  f }n
|ft }tj|tjd}|d	 W S  tj
ttfy } z$td|}t|| W Y d}~n
d}~0 0 dS )a  Load user access token with the ``gcloud auth print-access-token`` command.

    Args:
        account (Optional[str]): Account to get the access token for. If not
            specified, the current active account will be used.

    Returns:
        str: The user access token.

    Raises:
        google.auth.exceptions.UserAccessTokenError: if failed to get access
            token from gcloud.
    r   z
--account=r   r%   zFailed to obtain access tokenN)r   r   r*   r+   $_CLOUD_SDK_USER_ACCESS_TOKEN_COMMANDr    r!   STDOUTr1   stripr-   r.   r/   r   ZUserAccessTokenErrorsix
raise_from)accountr"   Zaccess_tokenZ
caught_excnew_excr   r   r   get_auth_access_token{   s&    

r;   )N)__doc__r   r   r    r7   Zgoogle.authr   r   r   r   r   r+   r*   r,   r4   ZCLOUD_SDK_CLIENT_IDr   r   r$   r3   r;   r   r   r   r   <module>   s(   