
                             d Z ddlZddlZddlZddlmZ ddlmZ ddlZddl	m
Z
 dadej                  j                  dedeeef   d	efd
Zdej                  j                  ddfdZdeeeeef   f   fdZy)z0BQ CLI helper functions for gcloud interactions.    N)Dict)flags)gcloud_runnerflag_values	flag_namegcloud_config_sectiongcloud_property_namec                 0   |sy||vry| |   }||   }t        j                  d|       |j                  r/t        j                  d||       t	        j
                  | ||       y|j                  |k7  r$t        j                  d||j                  ||       yy)zCUpdates flag if it's using the default and the gcloud value exists.NzGcloud config exists for %szSThe `%s` flag is using a default value and a value is set in gcloud, using that: %szExecuting with different configuration than in gcloud.The flag "%s" has become set to "%s" but gcloud sets "%s" as "%s".To update the gcloud value, start from `gcloud config list`.)loggingdebugusing_default_valueinfobq_utils
UpdateFlagvaluewarning)r   r   r   r	   flaggcloud_values         $platform/bq/utils/bq_gcloud_utils.py5_use_gcloud_value_if_exists_and_flag_is_default_valuer      s     

!66
	Y	$&';<,	---/CD	LL		 Y=zz\!OO	G 	

 "    returnc                 Z   t               }|j                  di       }|j                  di       }|j                  di       }|j                  di       }t        | d|d       t        | d|d	       t        | d
|d
       t        | d|d       t        | d|d       t        | d|d       |r|sy	 |d   }|d
   }|r|r| d   j                  r| d   j                  st        j                  d       yt        j                  d       	 t        |      5 }|j                         j                         }	ddd       t        j                  | d	       t        j                  | dd       yyy# t        $ r Y yw xY w# 1 sw Y   IxY w# t        $ r t        j                  d       Y yw xY w)zHProcesses the user configs from gcloud and sets flag values accordingly.corebillingauthapi_endpoint_overrides
project_idproject)r   r   r   r	   quota_project_idquota_projectuniverse_domainrequest_reasonapibigquerybigquery_discovery_api_keyapi_keyNaccess_token_fileoauth_access_tokenuse_google_authzpUsers gcloud config file and bigqueryrc file have incompatible configurations. Defaulting to the bigqueryrc filezNUsing the gcloud configuration to get TPC authorisation from access_token_fileTzACould not open `access_token_file` file, ignoring gcloud settings)load_configgetr   KeyErrorr   r   r   r   openreadstripr   r   IOError)
r   configscore_configbilling_configauth_configr   r(   r"   
token_filetokens
             r   process_configr8   5   s   M'FB'+;;y"-.FB'+";;'?D7'$	 8"**	 8!',	 8 '+	 82%	 8,'$	 
K
#$78!"34O ?,-AA,-AAoo? LL		@!"j!'') # +';UC+'8$?1 + 
 
& #" oo
Ms<   3
E. F	 E=5F	 .	E:9E:=FF	 	F*)F*c                     t         t        j                  d       t         S i a 	 t        j                  g dt
        j                        } | j                         \  }}|rt        j                  d|       t         S 	 t        j                  |      a t         S # t        $ r/}t        j                  dt        |             t         cY d}~S d}~ww xY w# t        j                  $ r%}t        j                  d|       Y d}~t         S d}~ww xY w)zWLoads the user configs from gcloud, cache the result, and returns them as a dictionary.NzUsing cached gcloud config)configlistz--format=json)stderrz9Continuing with empty gcloud config data due to error: %szIContinuing with empty gcloud config data due to invalid config format: %s)_config_cacher   r   r   run_gcloud_command
subprocessSTDOUTcommunicateFileNotFoundErrorr   strjsonloadsJSONDecodeError)processouterres       r   r+   r+      s     LL-.-
..+J4E4EG ""$HC 	OOCS JJsOM 
+ 
 OOCSV  
		 OO		 
 
s5   :B >C 	C"$CCCD'DD)__doc__rD   r   r?   typingr   abslr   r   gcloud_wrapperr   r=   _flagvalues
FlagValuesrC   r   r8   r+    r   r   <module>rR      s    6       ( ""--  S> 	DS@ 1 1 < < S@ S@l#T#tCH~-. #r   