
                             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	Z	ddl
Z
ddlZddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ  G d dej0                        Zy)zThe BigQuery init CLI command.    N)Optional)flags)client_project)bigquery_command)bq_cached_client)utils)utils_flags)utils_formatting)bq_id_utils)
bq_logging)bq_processor_utilsc                   p     e Zd ZdZdedej                  f fdZdefdZ	de
e   fdZde
e   fdZ xZS )	Initz7Create a .bigqueryrc file and set up OAuth credentials.namefvc                 n    t         t        |   ||       d| _        t	        j
                  ddd|       y )NFdelete_credentialszTIf specified, the credentials file associated with this .bigqueryrc file is deleted.)flag_values)superr   __init__surface_in_shellr   DEFINE_boolean)selfr   r   	__class__s      $platform/bq/frontend/command_init.pyr   zInit.__init__!   s8    	$tR(!D		    returnc                      y)z/Init never needs to call itself before running.F )r   s    r   
_NeedsInitzInit._NeedsInit,   s    r   c                    t        j                          t        j                  j                  xs t        j
                  j                  }t        j                  j                  |      st        d|d       y	 dt        j                  d|d      k7  rt        d|d       yt        j                  |       y# t        $ r}t        d	|d
|       Y d}~yd}~ww xY w)z$Deletes this user's credential file.zCredential file z does not exist.r   yzDelete credential file ? (y/N) zNOT deleting 
, exiting.Error removing : N   )bq_utilsProcessBigqueryrcbq_auth_flagsSERVICE_ACCOUNT_CREDENTIAL_FILEvalueCREDENTIAL_FILEospathexistsprintfrontend_utilsPromptYNremoveOSError)r   filenamees      r   DeleteCredentialszInit.DeleteCredentials0   s     55;; 	/((..  77>>(#H>?		''19
; 
 	X78ii x34s   ;+B= 'B= =	C!CC!c                 Z
   d}t        j                  dj                  t        j                  |d            ddgt
        j                         t        j                  j                  }t        j                          t        j                  t        j                  j                         | j                  r| j!                         S t        j"                         }t%                t%        d       t%        d	       t%                t&        j(                  j+                  |      rt%        d
       t%        d|d       t%        d       t%        d       t%                dt-        j.                  d|d      k7  rt%        d|d       yt%                	 t'        j0                  |       t%        d       t%        d       t%                t4        j6                  j9                         }dt:        j<                  j                  i}|rt%        d|z         t%                ||d<   nIt?        j@                  |jB                  d      }t%        d        t%                |s-t%        d!       t%        d"       t%        d#       t%                nt%        d$       tE        jF                         }|jI                  d%       tK        jL                  |tN        jP                  jR                         tU        |      D ]A  \  }	}
tK        jV                  |
      }|jY                  d%|	dz   i       |j[                  |       C |j]                          t_        |      dk(  rAta        jb                  |d         }t%        d&|d'       t%                |jd                  |d<   nt%        d(       t%        d)       t%        d*       t%                d}tg        |th              set-        jj                  d+t_        |      d,      }	 |r dti        |      cxk  rt_        |      k  rn nti        |xs d      }tg        |th              set%                |r*ta        jb                  ||dz
           }|jd                  |d<   	 to        |d-      5 }|jq                         D ]  \  }}t%        |d.||/        	 ddd       t%        d1       t%                y# t2        $ r}t%        d|d|       Y d}~yd}~ww xY w# tl        $ r Y w xY w# 1 sw Y   UxY w# tr        $ r}t%        d0|d|       Y d}~yd}~ww xY w)2z3Authenticate and create a default .bigqueryrc file.zBQ CLI will soon require all users to log in using `gcloud auth login`. `bq init` will no longer handle authentication after January 1, 2024.

P   )widthredbold)colorattrsfilez;Welcome to BigQuery! This script will walk you through the z<process of initializing your .bigqueryrc configuration file.z **** NOTE! ****z*An existing .bigqueryrc file was found at .z>Are you sure you want to continue and overwrite your existing zconfiguration?r"   z
Overwrite r#   zNOT overwriting r$   r   r%   r&   Nr'   z9First, we need to set up your credentials if they do not zalready exist.credential_filez%Setting project_id %s as the default.
project_idi  )	apiclientmax_resultszCCredential creation complete. Now we will select a default project.z.No projects found for this user. Please go to z#  https://console.cloud.google.com/zand create a project.zList of projects:#z Found only one project, setting z as the default.z6Found multiple projects. Please enter a selection for z3which should be the default, or leave blank to not zset a default.zEnter a selection (1 - z): wz = )rA   zError writing z:BigQuery configuration complete! Type "bq" to get started.):	termcolorcprintjointextwrapwrapsysstdoutbq_flags
PROJECT_IDr,   r(   r)   r   ConfigureLoggingAPILOGr   r8   GetBigqueryRcFilenamer1   r.   r/   r0   r2   r3   r4   r5   r   ClientGetr*   r-   r   list_projectsrE   r	   get_formatter_from_flags	AddColumnr
   configure_formatterr   ApiClientHelperProjectReference	enumerateformat_project_infoupdateAddDictPrintlenr   ConstructObjectReference	projectId
isinstanceintPromptWithDefault
ValueErroropenitemsIOError)r   messageproject_id_flag
bigqueryrcr7   cliententriesprojects	formatterindexprojectresultproject_referenceresponsercfileflagr,   s                    r   RunWithArgszInit.RunWithArgsE   s1   	3 
 		(--r23hZZ	 ))//O  5 56##%%//1J	G	
GH	
HI	G	ww~~j!zKLLMg	''*(NO	O
<=g
		*
 

EF	
	G
 $$((*F -"?"?"E"EFG3oEFg-gl--$$$h 
O g>?34%&!"88:	C ,,{22CC	
 (1NE7#77@&
--eai(
)


F
# 2 	x=A0IIqk
 "% '"3"="=',

H
I
E
F
 
!
'(x-%7725h-AHc(m!Ds8}!Dx}1- x- ' 2 K KA&! %6$?$?GL!
C F"==?KD%
T5)
7 + ! 

FG	Go  Q78H   !   j!45sZ   S 51S+ .T :+S:&T 	S(S##S(+	S76S7:T?T 	T*T%%T*)__name__
__module____qualname____doc__strr   
FlagValuesr   boolr    r   rf   r8   rz   __classcell__)r   s   @r   r   r      sM    ?	3 	E$4$4 	$ # *8C= r   r   )r~   r.   rN   rL   typingr   abslr   rI   r*   rP   r(   clientsr   frontendr   r   r   r2   r	   r
   r   r   r   BigqueryCmdr   r   r   r   <module>r      sS    $ 	 
        " % % ,   %   $f'' fr   