FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add message to user when they use private bigquery without project id… · HpcDataLab/docker-python@d2a38d5 · GitHub

Commit d2a38d5

Browse files
authored
Add message to user when they use private bigquery without project id… (Kaggle#591)
* Add message to user when they use private bigquery without project id which will cause a DefaultCredentialsError. * Avoid nested monkeypatching.
1 parent 7762da7 commit d2a38d5

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

‎patches/kaggle_gcp.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import inspect
23
from google.auth import credentials
34
from google.auth.exceptions import RefreshError
45
from google.cloud import bigquery
@@ -111,6 +112,8 @@ def __init__(self, *args, **kwargs):
111112
# TODO: Remove this once https://github.com/googleapis/google-cloud-python/issues/7122 is implemented.
112113
self._connection = _DataProxyConnection(self)
113114

115+
def has_been_monkeypatched(method):
116+
return "kaggle_gcp" in inspect.getsourcefile(method)
114117

115118
def init_bigquery():
116119
from google.auth import environment_vars
@@ -145,7 +148,6 @@ def monkeypatch_bq(bq_client, *args, **kwargs):
145148
Log.info(msg)
146149
print(msg)
147150
return PublicBigqueryClient(*args, **kwargs)
148-
149151
else:
150152
if specified_credentials is None:
151153
Log.info("No credentials specified, using KaggleKernelCredentials.")
@@ -154,14 +156,19 @@ def monkeypatch_bq(bq_client, *args, **kwargs):
154156
Log.info("No bigquery integration found, creating client anyways.")
155157
print('Please ensure you have selected a BigQuery '
156158
'account in the Kernels Settings sidebar.')
159+
if explicit_project_id is None:
160+
Log.info("No project specified while using the unmodified client.")
161+
print('Please ensure you specify a project id when creating the client'
162+
' in order to use your BigQuery account.')
157163
return bq_client(*args, **kwargs)
158164

159165
# Monkey patches BigQuery client creation to use proxy or user-connected GCP account.
160166
# Deprecated in favor of Kaggle.DataProxyClient().
161167
# TODO: Remove this once uses have migrated to that new interface.
162168
bq_client = bigquery.Client
163-
bigquery.Client = lambda *args, **kwargs: monkeypatch_bq(
164-
bq_client, *args, **kwargs)
169+
if (not has_been_monkeypatched(bigquery.Client)):
170+
bigquery.Client = lambda *args, **kwargs: monkeypatch_bq(
171+
bq_client, *args, **kwargs)
165172
return bigquery
166173

167174
def init_gcs():
@@ -184,7 +191,8 @@ def monkeypatch_gcs(self, *args, **kwargs):
184191
kwargs['credentials'] = KaggleKernelCredentials(target=GcpTarget.GCS)
185192
return gcs_client_init(self, *args, **kwargs)
186193

187-
storage.Client.__init__ = monkeypatch_gcs
194+
if (not has_been_monkeypatched(storage.Client.__init__)):
195+
storage.Client.__init__ = monkeypatch_gcs
188196
return storage
189197

190198
def init():

‎tests/test_bigquery_proxy.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from google.cloud import bigquery
1212
from google.auth.exceptions import DefaultCredentialsError
1313
from google.cloud.bigquery._http import Connection
14-
from kaggle_gcp import KaggleKernelCredentials, PublicBigqueryClient
14+
from kaggle_gcp import KaggleKernelCredentials, PublicBigqueryClient, init_bigquery
1515
import kaggle_secrets
1616

1717

@@ -67,6 +67,15 @@ def test_proxy_no_project(self):
6767
with env:
6868
client = bigquery.Client()
6969
self._test_proxy(client)
70+
71+
def test_monkeypatching_idempotent(self):
72+
env = EnvironmentVarGuard()
73+
env.unset('KAGGLE_USER_SECRETS_TOKEN')
74+
with env:
75+
client1 = bigquery.Client
76+
init_bigquery()
77+
client2 = bigquery.Client
78+
self.assertEqual(client1, client2)
7079

7180
def test_proxy_with_kwargs(self):
7281
env = EnvironmentVarGuard()

‎tests/test_gcs.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ def test_default_credentials_gcs_enabled(self):
4444
init_gcs()
4545
client = storage.Client(project="xyz")
4646
self.assertIsInstance(client._credentials, KaggleKernelCredentials)
47+
48+
def test_monkeypatching_idempotent(self):
49+
env = EnvironmentVarGuard()
50+
env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
51+
env.set('KAGGLE_KERNEL_INTEGRATIONS', 'GCS')
52+
with env:
53+
client1 = storage.Client.__init__
54+
init_gcs()
55+
client2 = storage.Client.__init__
56+
self.assertEqual(client1, client2)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL