11import os
22import inspect
3+ import logging
34from google .auth import credentials , environment_vars
45from google .auth .exceptions import RefreshError
56from google .api_core .gapic_v1 .client_info import ClientInfo
89from google .cloud .bigquery ._http import Connection
910from kaggle_secrets import GcpTarget , UserSecretsClient
1011
11- from log import Log
12-
1312KAGGLE_GCP_CLIENT_USER_AGENT = "kaggle-gcp-client/1.0"
1413
1514def get_integrations ():
@@ -22,7 +21,7 @@ def get_integrations():
2221 target = GcpTarget [integration .upper ()]
2322 kernel_integrations .add_integration (target )
2423 except KeyError as e :
25- Log . error (f"Unknown integration target: { integration .upper ()} " )
24+ logging . debug (f"Unknown integration target: { integration .upper ()} " )
2625 return kernel_integrations
2726
2827
@@ -66,14 +65,14 @@ def refresh(self, request):
6665 elif self .target == GcpTarget .CLOUDAI :
6766 self .token , self .expiry = client ._get_cloudai_access_token ()
6867 except ConnectionError as e :
69- Log .error (f"Connection error trying to refresh access token: { e } " )
68+ logging .error (f"Connection error trying to refresh access token: { e } " )
7069 print ("There was a connection error trying to fetch the access token. "
7170 f"Please ensure internet is on in order to use the { self .target .service } Integration." )
7271 raise RefreshError ('Unable to refresh access token due to connection error.' ) from e
7372 except Exception as e :
74- Log .error (f"Error trying to refresh access token: { e } " )
73+ logging .error (f"Error trying to refresh access token: { e } " )
7574 if (not get_integrations ().has_integration (self .target )):
76- Log .error (f"No { self .target .service } integration found." )
75+ logging .error (f"No { self .target .service } integration found." )
7776 print (
7877 f"Please ensure you have selected a { self .target .service } account in the Notebook Add-ons menu." )
7978 raise RefreshError ('Unable to refresh access token.' ) from e
@@ -102,7 +101,7 @@ def api_request(self, *args, **kwargs):
102101 msg = ("Permission denied using Kaggle's public BigQuery integration. "
103102 "Did you mean to select a BigQuery account in the Notebook Add-ons menu?" )
104103 print (msg )
105- Log .info (msg )
104+ logging .info (msg )
106105 raise e
107106
108107
@@ -156,23 +155,23 @@ def monkeypatch_bq(bq_client, *args, **kwargs):
156155 # Remove these two lines once this is resolved:
157156 # https://github.com/googleapis/google-cloud-python/issues/8108
158157 if explicit_project_id :
159- Log .info (f"Explicit project set to { explicit_project_id } " )
158+ logging .info (f"Explicit project set to { explicit_project_id } " )
160159 kwargs ['project' ] = explicit_project_id
161160 if explicit_project_id is None and specified_credentials is None and not has_bigquery :
162161 msg = "Using Kaggle's public dataset BigQuery integration."
163- Log .info (msg )
162+ logging .info (msg )
164163 print (msg )
165164 return PublicBigqueryClient (* args , ** kwargs )
166165 else :
167166 if specified_credentials is None :
168- Log .info ("No credentials specified, using KaggleKernelCredentials." )
167+ logging .info ("No credentials specified, using KaggleKernelCredentials." )
169168 kwargs ['credentials' ] = KaggleKernelCredentials ()
170169 if (not has_bigquery ):
171- Log .info ("No bigquery integration found, creating client anyways." )
170+ logging .info ("No bigquery integration found, creating client anyways." )
172171 print ('Please ensure you have selected a BigQuery '
173172 'account in the Notebook Add-ons menu.' )
174173 if explicit_project_id is None :
175- Log .info ("No project specified while using the unmodified client." )
174+ logging .info ("No project specified while using the unmodified client." )
176175 print ('Please ensure you specify a project id when creating the client'
177176 ' in order to use your BigQuery account.' )
178177 kwargs ['client_info' ] = set_kaggle_user_agent (kwargs .get ('client_info' ))
@@ -196,20 +195,20 @@ def monkeypatch_aiplatform_init(aiplatform_klass, kaggle_kernel_credentials):
196195 def patched_init (* args , ** kwargs ):
197196 specified_credentials = kwargs .get ('credentials' )
198197 if specified_credentials is None :
199- Log .info ("No credentials specified, using KaggleKernelCredentials." )
198+ logging .info ("No credentials specified, using KaggleKernelCredentials." )
200199 kwargs ['credentials' ] = kaggle_kernel_credentials
201200 return aiplatform_init (* args , ** kwargs )
202201
203202 if (not has_been_monkeypatched (aiplatform_klass .init )):
204203 aiplatform_klass .init = patched_init
205- Log .info ("aiplatform.init patched" )
204+ logging .info ("aiplatform.init patched" )
206205
207206def monkeypatch_client (client_klass , kaggle_kernel_credentials ):
208207 client_init = client_klass .__init__
209208 def patched_init (self , * args , ** kwargs ):
210209 specified_credentials = kwargs .get ('credentials' )
211210 if specified_credentials is None :
212- Log .info ("No credentials specified, using KaggleKernelCredentials." )
211+ logging .info ("No credentials specified, using KaggleKernelCredentials." )
213212 # Some GCP services demand the billing and target project must be the same.
214213 # To avoid using default service account based credential as caller credential
215214 # user need to provide ClientOptions with quota_project_id:
@@ -227,7 +226,7 @@ def patched_init(self, *args, **kwargs):
227226
228227 if (not has_been_monkeypatched (client_klass .__init__ )):
229228 client_klass .__init__ = patched_init
230- Log .info (f"Client patched: { client_klass } " )
229+ logging .info (f"Client patched: { client_klass } " )
231230
232231def set_kaggle_user_agent (client_info : ClientInfo ):
233232 # Add kaggle client user agent in order to attribute usage.
@@ -360,4 +359,4 @@ def init():
360359# google.cloud.* and kaggle_gcp. By calling init here, we guarantee
361360# that regardless of the original import that caused google.cloud.* to be
362361# loaded, the monkeypatching will be done.
363- init ()
362+ init ()
0 commit comments