
                         N   S 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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  SSK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Jr  SSKJr  \R>                  rS\	\R@                     4S jr! " S S\"5      r# " S S5      r$g)z,BigQuery client that exists for some reason.    N)AnyDictListOptionalType)app)flags)main_credential_loader)bigquery_client)bigquery_client_extended)wait_printer)utils)bq_api_utils)bq_gcloud_utils)
bq_loggingreturnc                      [         R                  R                  (       a  [        R                  $ [         R
                  R                  (       a  [        R                  $ [        R                  $ )zGReturns the default wait_printer_factory to use while waiting for jobs.)bq_flagsQUIETvaluer   QuietWaitPrinterHEADLESSTransitionWaitPrinterVerboseWaitPrinter     (platform/bq/frontend/bq_cached_client.py_GetWaitPrinterFactoryFromFlagsr   !   sB    ^^(((---		(	((r   c                   $   \ rS rSrSr0 r\SS\S\\	\
4   4S jj5       r\ SS\R                  S\R                  4S jj5       r\  SS\S\R                  S\R                   4S	 jj5       r\S\	4S
 j5       r\S\R                   4S j5       rSrg)Client*   z@Class caching bigquery_client.BigqueryClient based on arguments.config_loggingr   c                   ^ S[         4U4S jjn[        R                  " 5         U (       a.  [        R                  " [
        R                  R                  5        [        R                  " [        S9  [
        R                  R                  (       ah  [        R                  " [
        R                  R                  5      (       d5  [        R                   R                  (       d  ["        R$                  " S5        [
        R&                  R                  (       ae  [
        R&                  R                  [(        l        [-        [(        S5      (       a-  [
        R&                  R                  [(        R.                  l        0 nSnU H  nU" U5      X5'   M     ["        R0                  " S[        R2                  5        ["        R4                  " SU5        [7        5       US	'   [
        R8                  R                  (       a?  [;        [
        R8                  R                  5       nUR=                  5       US
'   SSS5        [
        R>                  R                  c  SO[
        R>                  R                  US'   [
        R@                  R                  (       a  [
        R@                  R                  US'   ["        R0                  " SU5        U$ ! , (       d  f       N= f)zCollect and combine FLAGS and kwds to create BQ Client.

Args:
  config_logging: if True, set python logging according to --apilog.
  **kwds: keyword arguments for creating BigqueryClient.
namec                 :   > U T;   a  TU    $ [        [        U 5      $ N)getattrFLAGS)r$   kwdss    r   KwdsOrFlags(Client._CollectArgs.<locals>.KwdsOrFlags8   s    4<T$ZAWUD-AAr   )flag_valueszAttempting to use a non-GDU universe domain without setting `use_google_auth`. You might need to set `use_google_auth` to True.python3)credential_filejob_property
project_id
dataset_idtracesyncuse_google_authapiapi_versionquota_project_idrequest_reasonz&Creating client for BQ CLI version: %szGlobal args collected: %swait_printer_factorydiscovery_documentNTenable_resumable_uploadsmax_rows_per_requestzClient args collected: %s)!strbq_utilsProcessBigqueryrcr   ConfigureLoggingr   APILOGr   r   process_configr(   UNIVERSE_DOMAINpresentr   is_gdu_universebq_auth_flagsUSE_GOOGLE_AUTHloggingwarningHTTPLIB2_DEBUGLEVELhttplib2
debuglevelhasattrr-   infoVERSION_NUMBERdebugr   DISCOVERY_FILEopenreadENABLE_RESUMABLE_UPLOADSMAX_ROWS_PER_REQUEST)r"   r)   r*   client_argsglobal_argsr$   fs    `     r   _CollectArgsClient._CollectArgs/   s   B# B
  !!(//"7"78""u5 	  ((,,X-E-E-K-KLL--33ooQ
 ##))$88>>h	9	%	%&.&B&B&H&H#KK %d+k  LL0(2I2I MM-{;*I*KK&'$$''--.!,-FFH() / ,,22: 	..44 *+
 $$**,4,I,I,O,Ok()LL,k: /.s   K


KNcredentialsc                 0   U b  [         R                  " S5        Ov[        R                  R                  (       a,  [         R                  " S5        [
        R                  " 5       n O+[         R                  " S5        [        R                  " 5       n U c   eU $ )zA function to lookup the credentials to use for this BQ CLI invocation.

Args:
  credentials: bypass the credential lookup and use these instead.

Returns:
  The credentials ot use for this BQ CLI invocation.
zCredentials passed in directlyz$Credentials loaded using Google Authz%Credentials loaded using oauth2client)rH   rN   rF   rG   r   r
   GetCredentialsFromFlagscredential_loader)r[   s    r   GetCredentialsClient.GetCredentialsw   sn     ll34		&	&	,	,ll9:*BBDkll:;%==?k"""r   c                     [         R                  " SU5        [        R                  " U 40 UD6n[        R                  5       nU" SS[        R                  U5      0UD6$ )a/  Build a new BigqueryClient configured from kwds and FLAGS.

Args:
  config_logging: if True, set python logging according to --apilog.
  credentials: bypass the credential lookup and use these instead.
  **kwds: keyword arguments for creating BigqueryClient.

Returns:
  A BigqueryClient from the kwds.
z4Collecting args before creating a BigqueryClient: %sr[   r   )rH   rP   r    rY   FactoryGetBigqueryClientFactoryr_   )r"   r[   r)   rV   bigquery_client_factorys        r   CreateClient.Create   s_    " MMH$O%%n==K%>>@" ))+6:E r   c                     [         R                  " SU5        [        R                  " S0 UD6nSR	                  U[
        R                  R                  [        R                  R                  S9$ )Nz In Client._GetClientCacheKey: %szlclient_args={client_args},service_account_credential_file={service_account_credential_file},apilog={apilog},)rV   service_account_credential_fileapilogr   )
rH   rP   r    rY   formatrF   SERVICE_ACCOUNT_CREDENTIAL_FILEr   r   rA   )clsr)   rV   s      r   _GetClientCacheKeyClient._GetClientCacheKey   s\    MM4d;%%--K	!6#,9,Y,Y,_,_??(( " 
r   c                    [         R                  5       nXR                  ;   a  [        R                  " SU5        O8 [         R                  5       U R                  U'   [        R                  " S5        U R                  U   $ ! [         a1  n[        R                  " S5        [        R                  " U5      eSnAff = f)z/Return a BigqueryClient initialized from flags.zRUsing a cached client with previous auth and discovery docs from the cache_key: %sz"Successfully created a new client.zFailed to create a new client.N)	r    rm   client_cacherH   rN   re   
ValueErrorr   
UsageError)rl   	cache_keyes      r   Get
Client.Get   s     ))+I$$$ll
 &,mmo#9: I&&   56nnQ s   7B 
B>,B99B>r   )Tr&   )TN)__name__
__module____qualname____firstlineno____doc__rp   staticmethodboolr   r=   r   rY   r   'LegacyAndGoogleAuthCredentialsUnionTyper_   r   BigqueryClientExtendedre   classmethodrm   ru   __static_attributes__r   r   r   r    r    *   s    H,E4 E4S> E EN MQ"JJ>> , !MQ"JJ  66	 2    '*AA ' 'r   r    c                   b    \ rS rSrSrSr " S S5      r\S\\	R                     4S j5       rSrg)	rb      z7Class encapsulating factory creation of BigqueryClient.Nc                   H    \ rS rSrSrSr\S\R                  4S j5       r	Sr
g)Factory.ClientTablePrinter   z5Class encapsulating factory creation of TablePrinter.Nr   c                 h    U R                   c  [        R                  " 5       U l         U R                   $ r&   )_TABLE_PRINTERbq_frontend_utilsTablePrinterrl   s    r   GetTablePrinter*Factory.ClientTablePrinter.GetTablePrinter   s+    				#.;;=r   r   )rw   rx   ry   rz   r{   r   r   r   r   r   r   r   r   r   ClientTablePrinterr      s*    ?N  1 > >    r   r   r   c                 ^    U R                   c  [        R                  U l         U R                   $ r&   )_BIGQUERY_CLIENT_FACTORYr   r   r   s    r   rc    Factory.GetBigqueryClientFactory   s/     ##+
"
9
9 
" '''r   r   )rw   rx   ry   rz   r{   r   r   r   r   r   r   rc   r   r   r   r   rb   rb      s=    ?!	  	  ($;;<( (r   rb   )%r{   rH   systextwraptypingr   r   r   r   r   abslr   r	   rK   	termcolorrF   r   r>   r^   authr
   clientsr   r   r   frontendr   r   r   r   r   r(   WaitPrinterr   objectr    rb   r   r   r   <module>r      s    2  
  2 2         ' # ,   /  !  	
)l.F.F)G )_'V _'D( (r   