| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7762da7 commit d2a38d5
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | import os | |
| 2 | + import inspect | ||
| 2 | 3 | from google.auth import credentials | |
| 3 | 4 | from google.auth.exceptions import RefreshError | |
| 4 | 5 | from google.cloud import bigquery | |
@@ -111,6 +112,8 @@ def __init__(self, *args, **kwargs): | |||
| 111 | 112 | # TODO: Remove this once https://github.com/googleapis/google-cloud-python/issues/7122 is implemented. | |
| 112 | 113 | self._connection = _DataProxyConnection(self) | |
| 113 | 114 | ||
| 115 | + def has_been_monkeypatched(method): | ||
| 116 | + return "kaggle_gcp" in inspect.getsourcefile(method) | ||
| 114 | 117 | ||
| 115 | 118 | def init_bigquery(): | |
| 116 | 119 | from google.auth import environment_vars | |
@@ -145,7 +148,6 @@ def monkeypatch_bq(bq_client, *args, **kwargs): | |||
| 145 | 148 | Log.info(msg) | |
| 146 | 149 | print(msg) | |
| 147 | 150 | return PublicBigqueryClient(*args, **kwargs) | |
| 148 | - | ||
| 149 | 151 | else: | |
| 150 | 152 | if specified_credentials is None: | |
| 151 | 153 | Log.info("No credentials specified, using KaggleKernelCredentials.") | |
@@ -154,14 +156,19 @@ def monkeypatch_bq(bq_client, *args, **kwargs): | |||
| 154 | 156 | Log.info("No bigquery integration found, creating client anyways.") | |
| 155 | 157 | print('Please ensure you have selected a BigQuery ' | |
| 156 | 158 | '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.') | ||
| 157 | 163 | return bq_client(*args, **kwargs) | |
| 158 | 164 | ||
| 159 | 165 | # Monkey patches BigQuery client creation to use proxy or user-connected GCP account. | |
| 160 | 166 | # Deprecated in favor of Kaggle.DataProxyClient(). | |
| 161 | 167 | # TODO: Remove this once uses have migrated to that new interface. | |
| 162 | 168 | 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) | ||
| 165 | 172 | return bigquery | |
| 166 | 173 | ||
| 167 | 174 | def init_gcs(): | |
@@ -184,7 +191,8 @@ def monkeypatch_gcs(self, *args, **kwargs): | |||
| 184 | 191 | kwargs['credentials'] = KaggleKernelCredentials(target=GcpTarget.GCS) | |
| 185 | 192 | return gcs_client_init(self, *args, **kwargs) | |
| 186 | 193 | ||
| 187 | - storage.Client.__init__ = monkeypatch_gcs | ||
| 194 | + if (not has_been_monkeypatched(storage.Client.__init__)): | ||
| 195 | + storage.Client.__init__ = monkeypatch_gcs | ||
| 188 | 196 | return storage | |
| 189 | 197 | ||
| 190 | 198 | def init(): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,7 +11,7 @@ | |||
| 11 | 11 | from google.cloud import bigquery | |
| 12 | 12 | from google.auth.exceptions import DefaultCredentialsError | |
| 13 | 13 | from google.cloud.bigquery._http import Connection | |
| 14 | - from kaggle_gcp import KaggleKernelCredentials, PublicBigqueryClient | ||
| 14 | + from kaggle_gcp import KaggleKernelCredentials, PublicBigqueryClient, init_bigquery | ||
| 15 | 15 | import kaggle_secrets | |
| 16 | 16 | ||
| 17 | 17 | ||
@@ -67,6 +67,15 @@ def test_proxy_no_project(self): | |||
| 67 | 67 | with env: | |
| 68 | 68 | client = bigquery.Client() | |
| 69 | 69 | 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) | ||
| 70 | 79 | ||
| 71 | 80 | def test_proxy_with_kwargs(self): | |
| 72 | 81 | env = EnvironmentVarGuard() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,3 +44,13 @@ def test_default_credentials_gcs_enabled(self): | |||
| 44 | 44 | init_gcs() | |
| 45 | 45 | client = storage.Client(project="xyz") | |
| 46 | 46 | 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) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments