
    .                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r	J
r
JrJrJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJr  \R8                  rS	rS
rSrSr Sr!S\"4S jr#\#" 5       r$S\%4S jr&\&" 5       r'S\\"   4S jr(S\"SS4S jr)S3S jr* S4S\"S\\"   S\	\"\"4   4S jjr+S\S\"S\	\"\"4   4S jr,S\\"   SS4S jr-S\\"   S\\"   S\\"   4S jr.S\\"   S\\"   S\\"   4S jr/S\"S\"S \%S!S"S\\"   4
S# jr0S\"4S$ jr1S\"4S% jr2 S5S&\3S'\S(   4S) jjr4S\
\"   4S* jr5S\
\"   4S+ jr6S,\"S\	\"\"4   4S- jr7S.\"S\
\"   4S/ jr8S\"4S0 jr9S1\"S\%4S2 jr:g)6z7A library of functions to handle bq flags consistently.    N)DictListLiteralOptionalTextIO)app)flags)version)credentials)
stringutilz%https://www.googleapis.com/auth/drivez(https://www.googleapis.com/auth/bigqueryz.https://www.googleapis.com/auth/cloud-platformz/https://www.googleapis.com/auth/accounts.reauthVERSIONreturnc                      [         n  [        R                  " U [        5      nW(       d  Sn[
        R                  " U5      R                  5       nSU;  d   S5       eU$ ! [         a     NHf = f)zDReturns content of VERSION file found in same dir as the cli binary.zunknown-version
z0New lines are not allowed in the version string.)__name__pkgutilget_data_VERSION_FILENAMEFileNotFoundErrorr   
ensure_strstrip)rootversion_strs     platform/bq/bq_utils.py_GetVersionr   #   st    	$	""4):;K 
#K%%k288:+
+878	 
 		s   A 
A,+A,c                  ,    [         R                  S5      $ )z4Returns true if the current binary is targeting TPC.ztpc-)VERSION_NUMBER
startswith     r   _IsTpcBinaryr!   7   s    		"	"6	**r    c                      [         S   R                  =(       a    [         R                  =(       d6    [        R                  R                  S5      =(       d    [         R                  $ )zReturn the name of the bigqueryrc file to use.

In order, we look for a flag the user specified, an environment
variable, and finally the default value for the flag.

Returns:
  bigqueryrc filename as a string.

bigqueryrc
BIGQUERYRC)FLAGSpresentr#   osenvirongetr   r    r   GetBigqueryRcFilenamer*   ?   sE     \""7u'7'7 		%			r    flagc                 T    X   R                  U5        [        X[        X5      5        g N)parsesetattrgetattr)flag_valuesr+   values      r   
UpdateFlagr3   O   s"    % 	+W[78r    c                  $    [        S[        5        g)z7Updates FLAGS with values found in the bigqueryrc file.N)ProcessBigqueryrcSectionr%   r   r    r   ProcessBigqueryrcr6   V   s    4'r    filenamesection_namec                     0 n[         R                  R                  U 5      (       d  U$  [        U 5       n[	        X15      nSSS5        U$ ! , (       d  f       U$ = f! [
         a     U$ f = f)a   Read a configuration file section returned as a dictionary.

Args:
  filename: The filename of the configuration file.
  section_name: if None, read the global flag settings.

Returns:
  A dictionary of flag names and values from that section of the file.
N)r'   pathexistsopen_ProcessSingleConfigSectionIOError)r7   r8   
dictionaryrcfiles       r   _ProcessConfigSectionrA   [   sp     *		!	!		h6.vDj 
 
	 
 
 
 			s.   A AA 
AA A 
A+*A+filec                 `   0 nU(       + nU  GH  nUR                  5       R                  S5      (       a=  UR                  5       R                  S5      (       a  UR	                  5       SS nX:H  nMe  U(       d  Mn  UR                  5       R                  S5      (       d  UR	                  5       (       d  M  UR                  S5      u  pgnU(       d  SnUR	                  5       nUR	                  5       nUR                  S5      (       a  USS	 nUR                  S5      (       a  M  XU'   GM      U$ )
zRead a configuration file section returned as a dictionary.

Args:
  file: The opened configuration file object.
  section_name: Name of the section to read.

Returns:
  A dictionary of flag names and values from that section of the file.
[]   #=true-N)lstripr   rstripendswithr   	partition)	rB   r8   r?   
in_sectionlinenext_sectionr+   	equalsignr2   s	            r   r=   r=   t   s     **d{{}$$)?)?)D)DZZ\!B'l/j		!	!#	&	&djjll!^^C0DUe::<DKKME
//#

!"Xd //#

t# $ 
r    c           
         [        5       n[        X S9nUR                  5        H  u  pEXA;  a'  [        R                  " SU< SU (       a  U OS< 35      eX   R
                  (       d  [        XU5        MR  X   R                  5       nUR                  S5      (       d  M|  [        X5      nX   R                  U5        [        XU[        X5      -   5        M     g)zRead the bigqueryrc file into flag_values for section section_name.

Args:
  section_name: if None, read the global flag settings.
  flag_values: FLAGS instance.

Raises:
  UsageError: Unknown flag found.
)r7   r8   zUnknown flag z% found in bigqueryrc file in section globalmultiN)r*   rA   itemsr   
UsageErrorr&   r3   	flag_typer   r0   r.   r/   )r8   r1   r#   r?   r+   r2   rY   	old_values           r   r5   r5      s     %&*$*  %%'kd NN<<X=?  $$E*#--/i			g	&	&K.	&9w{/I#IJ (r    quota_project_idfallback_project_idc                 6    U (       d	  U(       a  U$ [        U US9$ )zUReturn the final resolved quota project ID after cross-referencing gcloud properties.r[   r\   ) _GetResolvedGcloudQuotaProjectIDr^   s     r   GetResolvedQuotaProjectIDr`      s#    
 
1	)'-
 r    c                 2    U (       a  U S;   a  U$ SU :X  a  gU $ )zReturn the resolved quota project ID after cross-referencing gcloud properties.

Args:
  quota_project_id: The quota project ID to resolve.
  fallback_project_id: The fallback project ID to use.
)CURRENT_PROJECTCURRENT_PROJECT_WITH_FALLBACKLEGACYNr   r^   s     r   r_   r_      s.     * /  !!	r    
project_iduse_google_authr   zgoogle_oauth2.Credentialsc                 \    U(       a  [        US5      (       a  UR                  $ [        XS9$ )zGReturn the effective quota project ID to be set in the API HTTP header._quota_project_idr^   )hasattrrh   r_   )r[   re   rf   r   s       r   'GetEffectiveQuotaProjectIDForHTTPHeaderrj      s0     .ABB(((	)'
 r    c                      SR                  [        R                  " 5       [        R                  " 5       [        R                  " 5       /5      $ )N:)joinplatformpython_implementationpython_versionr   r    r   GetPlatformStringrq      s<    	$$& 
 r    c                     [        5       n  [        R                  n [
        R                  S   n [
        R                  S   n[        R                  " SR                  [        U [        R                  " 5       [        R                  R!                  SS5      U["        R                  [$        R                  [&        R                  [
        R(                  R+                  [        R,                  5      UUS95      $ ! [         a    [        R                  R                  n GNf = f! [         a    Sn GNf = f! [         a    Sn GNf = f)z/Gets the info string for the current execution.PATHN
PYTHONPATHa        BigQuery CLI [{version}]

      Platform: [{platform_str}] {uname}
      Python Version: [{python_version}]

      Requests Version: [{requests_version}]
      Urllib3 Version: [{urllib3_version}]
      Httplib2: [{httplib2_version}]
      Google Auth Version: [{google_auth_version}]

      System PATH: [{sys_path}]
      Shell PATH: [{shell_path}]
      Python PATH: [{python_path}]

      r    )r
   platform_strunamerp   httplib2_versiongoogle_auth_versionrequests_versionurllib3_versionsys_path
shell_pathpython_path)rq   httplib2__version__AttributeErrorpython3r'   r(   KeyErrortextwrapdedentformatr   rn   rw   sysr
   replacery   requestsurllib3pathseprm   r:   )rv   rx   r}   r~   s       r   GetInfoStringr      s   "$,4++F#J**\*K 

 & # ,,T37+1==#//!--::??388,!  !
  
 4  ''334 
 J 
 Ks3   C; D& D9 ;$D#"D#&D65D69E	E	objdefault_format)json
prettyjsonc                    SS/n[         R                  U;   a  [         R                  nOUnUS:X  a  [        [        R                  " U SS95        g
US:X  a   [        [        R                  " U SSS95        g
[        SU< S	U< 35      e)zPrints obj in a JSON format according to the "--format" flag.

Args:
  obj: The object to print.
  default_format: The format to use if the "--format" flag does not specify a
    valid json format: 'json' or 'prettyjson'.
r   r   ),rl   )
separatorsT   )	sort_keysindentz#Invalid json format for printing: 'z', expected one of: N)r%   r   printr   dumps
ValueError)r   r   json_formats
use_formats       r   PrintFormattedJsonObjectr   !  sw     ,',
\\\!JJ6	$**SZ
01\!	$**SD
34
|	% r    c                      [         [        /n [        R                  (       a  U R	                  [
        5        U R	                  [        5        U $ )z1Returns auth scopes based on user supplied flags.)_BIGQUERY_SCOPE_CLOUD_PLATFORM_SCOPEr%   enable_gdriveappend_GDRIVE_SCOPE_REAUTH_SCOPE)client_scopes    r   GetClientScopesFromFlagsr   <  s8    !#89,
&m$	r    c                      [         /$ )z:Returns the scopes list for 3rd Party Identity Federation.)r   r   r    r   GetClientScopesFor3pir   E  s    
	  r    tagsc                    U R                  5       n U (       d  [        R                  " S5      e0 nU R                  S5       H  nUR	                  S5      u  p4nUR                  5       nU(       d  [        R                  " S5      eUR                  5       nU(       d  [        R                  " S5      eX1;   a  [        R                  " SU-  5      eXQU'   M     U$ )a2  Parses user-supplied string representing tags.

Args:
  tags: A comma separated user-supplied string representing tags. It is
    expected to be in the format "key1:value1,key2:value2".

Returns:
  A dictionary mapping tag keys to tag values.

Raises:
  UsageError: Incorrect tags or no tags are supplied.
zNo tags suppliedr   rl   Tag key cannot be NonezTag value cannot be None*Cannot specify tag key "%s" multiple times)r   r   rX   split
rpartition)r   	tags_dict	key_valuek_vs         r   	ParseTagsr   J  s     
$	
..+
,,)::c?i""3'GA!		ANN344		ANN566~NNG!KLLaL # 
r    tag_keysc                 n   U R                  5       n U (       d  [        R                  " S5      e[        5       nU R	                  S5       H_  nUR                  5       nU(       d  [        R                  " S5      eX!;   a  [        R                  " SU-  5      eUR                  U5        Ma     [        U5      $ )aQ  Parses user-supplied string representing tag keys.

Args:
  tag_keys: A comma separated user-supplied string representing tag keys.  It
    is expected to be in the format "key1,key2" or
    "tpczero-system:key1,tpczero-system:key2".

Returns:
  A list of tag keys.

Raises:
  UsageError: Incorrect tag_keys or no tag_keys are supplied.
zNo tag keys suppliedr   r   r   )r   r   rX   setr   addlist)r   tags_setkeys      r   ParseTagKeysr   i  s     ^^(	
../
00U(^^C c
))+CNN344
NNG#MNNLL ! 
hr    c                      Sn [         R                  R                  S5      S:X  a-  S[         R                  R                  S[        5      -   S-   U -   $ S[        -   S-   U -   $ )zRReturns the user agent for BigQuery API requests based on environment and version.zgoogle-api-python-clientCLOUDSDK_WRAPPER1zgoogle-cloud-sdkCLOUDSDK_VERSIONru   zbq/)r'   r(   r)   r   )google_python_client_names    r   GetUserAgentr     sh    8ZZ^^&'3.
**..+^
<	=
	 $	$ >!C'*CCCr    accountc                 4    [         R                  " SU 5      SL$ )znReturns whether the account may be a service account based on the user-created or system-created account name.z!^.+@(.+)(\.gserviceaccount\.com)$N)re	fullmatch)r   s    r   IsServiceAccountr     s    	:G	DD	PPr    )r   Nr-   )r   );__doc__r   r'   r   rn   r   r   r   typingr   r   r   r   r   abslr   r	   google.authr
   ry   google.oauth2r   google_oauth2r   r   r   pyglibr   r%   r   r   r   r   r   strr   r   boolr!   IS_TPC_BINARYr*   r3   r6   rA   r=   r5   r`   r_   rj   rq   r   objectr   r   r   r   r   r   r   r   r    r   <module>r      sb   =  	   	 
  8 8   6 6     	7<H A  S " +d +
 x}  9# 9 9( 26!)#	#s(^2 
  # 	#s(^ FK8C= K$ KB
sm
!#
 c]
sm!# c](  -	
 c]3 /s /f BH	!()=!>6$s) !tCy !
C DcN >3 49 8Dc DQc Qd Qr    