| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b358d22 commit 663e0cd
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -130,7 +130,7 @@ def init_bigquery(): | |||
| 130 | 130 | return bigquery | |
| 131 | 131 | ||
| 132 | 132 | # If this Kernel has bigquery integration on startup, preload the Kaggle Credentials | |
| 133 | - # object for magics to work. | ||
| 133 | + # object for magics to work. | ||
| 134 | 134 | if get_integrations().has_bigquery(): | |
| 135 | 135 | from google.cloud.bigquery import magics | |
| 136 | 136 | magics.context.credentials = KaggleKernelCredentials() | |
@@ -139,7 +139,7 @@ def monkeypatch_bq(bq_client, *args, **kwargs): | |||
| 139 | 139 | from kaggle_gcp import get_integrations, PublicBigqueryClient, KaggleKernelCredentials | |
| 140 | 140 | specified_credentials = kwargs.get('credentials') | |
| 141 | 141 | has_bigquery = get_integrations().has_bigquery() | |
| 142 | - # Prioritize passed in project id, but if it is missing look for env var. | ||
| 142 | + # Prioritize passed in project id, but if it is missing look for env var. | ||
| 143 | 143 | arg_project = kwargs.get('project') | |
| 144 | 144 | explicit_project_id = arg_project or os.environ.get(environment_vars.PROJECT) | |
| 145 | 145 | # This is a hack to get around the bug in google-cloud library. | |
@@ -200,9 +200,70 @@ def monkeypatch_gcs(self, *args, **kwargs): | |||
| 200 | 200 | storage.Client.__init__ = monkeypatch_gcs | |
| 201 | 201 | return storage | |
| 202 | 202 | ||
| 203 | + def init_automl(): | ||
| 204 | + is_user_secrets_token_set = "KAGGLE_USER_SECRETS_TOKEN" in os.environ | ||
| 205 | + from google.cloud import automl_v1beta1 as automl | ||
| 206 | + if not is_user_secrets_token_set: | ||
| 207 | + return automl | ||
| 208 | + | ||
| 209 | + from kaggle_gcp import get_integrations | ||
| 210 | + if not get_integrations().has_automl(): | ||
| 211 | + return automl | ||
| 212 | + | ||
| 213 | + from kaggle_secrets import GcpTarget | ||
| 214 | + from kaggle_gcp import KaggleKernelCredentials | ||
| 215 | + kaggle_kernel_credentials = KaggleKernelCredentials(target=GcpTarget.AUTOML) | ||
| 216 | + | ||
| 217 | + # The AutoML client library exposes 4 different client classes (AutoMlClient, | ||
| 218 | + # TablesClient, PredictionServiceClient and GcsClient), so patch each of them. | ||
| 219 | + # The same KaggleKernelCredentials are passed to all of them. | ||
| 220 | + | ||
| 221 | + automl_client_init = automl.AutoMlClient.__init__ | ||
| 222 | + def monkeypatch_automl(self, *args, **kwargs): | ||
| 223 | + specified_credentials = kwargs.get('credentials') | ||
| 224 | + if specified_credentials is None: | ||
| 225 | + Log.info("No credentials specified, using KaggleKernelCredentials.") | ||
| 226 | + kwargs['credentials'] = kaggle_kernel_credentials | ||
| 227 | + # Note: This is only here so that unit tests can check whether | ||
| 228 | + # credentials were set properly. | ||
| 229 | + self._kaggle_credentials = kwargs['credentials'] | ||
| 230 | + return automl_client_init(self, *args, **kwargs) | ||
| 231 | + | ||
| 232 | + if (not has_been_monkeypatched(automl.AutoMlClient.__init__)): | ||
| 233 | + automl.AutoMlClient.__init__ = monkeypatch_automl | ||
| 234 | + | ||
| 235 | + | ||
| 236 | + automl_tablesclient_init = automl.TablesClient.__init__ | ||
| 237 | + def monkeypatch_tablesclient(self, *args, **kwargs): | ||
| 238 | + specified_credentials = kwargs.get('credentials') | ||
| 239 | + if specified_credentials is None: | ||
| 240 | + Log.info("No credentials specified, using KaggleKernelCredentials.") | ||
| 241 | + kwargs['credentials'] = kaggle_kernel_credentials | ||
| 242 | + self._kaggle_credentials = kwargs['credentials'] | ||
| 243 | + return automl_tablesclient_init(self, *args, **kwargs) | ||
| 244 | + | ||
| 245 | + if (not has_been_monkeypatched(automl.TablesClient.__init__)): | ||
| 246 | + automl.TablesClient.__init__ = monkeypatch_tablesclient | ||
| 247 | + | ||
| 248 | + | ||
| 249 | + automl_predictionclient_init = automl.PredictionServiceClient.__init__ | ||
| 250 | + def monkeypatch_predictionclient(self, *args, **kwargs): | ||
| 251 | + specified_credentials = kwargs.get('credentials') | ||
| 252 | + if specified_credentials is None: | ||
| 253 | + Log.info("No credentials specified, using KaggleKernelCredentials.") | ||
| 254 | + kwargs['credentials'] = kaggle_kernel_credentials | ||
| 255 | + self._kaggle_credentials = kwargs['credentials'] | ||
| 256 | + return automl_predictionclient_init(self, *args, **kwargs) | ||
| 257 | + | ||
| 258 | + if (not has_been_monkeypatched(automl.PredictionServiceClient.__init__)): | ||
| 259 | + automl.PredictionServiceClient.__init__ = monkeypatch_predictionclient | ||
| 260 | + | ||
| 261 | + return automl | ||
| 262 | + | ||
| 203 | 263 | def init(): | |
| 204 | 264 | init_bigquery() | |
| 205 | 265 | init_gcs() | |
| 266 | + init_automl() | ||
| 206 | 267 | ||
| 207 | 268 | # We need to initialize the monkeypatching of the client libraries | |
| 208 | 269 | # here since there is a circular dependency between our import hook version | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ | |||
| 7 | 7 | import importlib.machinery | |
| 8 | 8 | ||
| 9 | 9 | class GcpModuleFinder(importlib.abc.MetaPathFinder): | |
| 10 | - _MODULES = ['google.cloud.bigquery', 'google.cloud.storage'] | ||
| 10 | + _MODULES = ['google.cloud.bigquery', 'google.cloud.storage', 'google.cloud.automl_v1beta1'] | ||
| 11 | 11 | _KAGGLE_GCP_PATH = 'kaggle_gcp.py' | |
| 12 | 12 | def __init__(self): | |
| 13 | 13 | pass | |
@@ -39,7 +39,8 @@ def create_module(self, spec): | |||
| 39 | 39 | import kaggle_gcp | |
| 40 | 40 | _LOADERS = { | |
| 41 | 41 | 'google.cloud.bigquery': kaggle_gcp.init_bigquery, | |
| 42 | - 'google.cloud.storage': kaggle_gcp.init_gcs | ||
| 42 | + 'google.cloud.storage': kaggle_gcp.init_gcs, | ||
| 43 | + 'google.cloud.automl_v1beta1': kaggle_gcp.init_automl, | ||
| 43 | 44 | } | |
| 44 | 45 | monkeypatch_gcp_module = _LOADERS[spec.name]() | |
| 45 | 46 | return monkeypatch_gcp_module | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,66 @@ | |||
| 1 | 1 | import unittest | |
| 2 | 2 | ||
| 3 | - from google.cloud import automl_v1beta1 as automl | ||
| 3 | + from unittest.mock import Mock | ||
| 4 | + | ||
| 5 | + from kaggle_gcp import KaggleKernelCredentials, init_automl | ||
| 6 | + from test.support import EnvironmentVarGuard | ||
| 7 | + from google.cloud import storage, automl_v1beta1 as automl | ||
| 8 | + | ||
| 9 | + def _make_credentials(): | ||
| 10 | + import google.auth.credentials | ||
| 11 | + return Mock(spec=google.auth.credentials.Credentials) | ||
| 4 | 12 | ||
| 5 | 13 | class TestAutoMl(unittest.TestCase): | |
| 6 | 14 | ||
| 7 | 15 | def test_version(self): | |
| 8 | 16 | self.assertIsNotNone(automl.auto_ml_client._GAPIC_LIBRARY_VERSION) | |
| 17 | + version_parts = automl.auto_ml_client._GAPIC_LIBRARY_VERSION.split('.') | ||
| 18 | + version = float('.'.join(version_parts[0:2])); | ||
| 19 | + self.assertGreaterEqual(version, 0.5); | ||
| 20 | + | ||
| 21 | + def test_user_provided_credentials(self): | ||
| 22 | + credentials = _make_credentials() | ||
| 23 | + env = EnvironmentVarGuard() | ||
| 24 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 25 | + env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML') | ||
| 26 | + with env: | ||
| 27 | + init_automl() | ||
| 28 | + client = automl.AutoMlClient(credentials=credentials) | ||
| 29 | + self.assertNotIsInstance(client._kaggle_credentials, KaggleKernelCredentials) | ||
| 30 | + self.assertIsNotNone(client._kaggle_credentials) | ||
| 31 | + | ||
| 32 | + | ||
| 33 | + def test_tables_gcs_client(self): | ||
| 34 | + # The GcsClient can't currently be monkeypatched for default | ||
| 35 | + # credentials because it requires a project which can't be set. | ||
| 36 | + # Verify that creating an automl.GcsClient given an actual | ||
| 37 | + # storage.Client sets the client properly. | ||
| 38 | + gcs_client = storage.Client(project="xyz", credentials=_make_credentials()) | ||
| 39 | + tables_gcs_client = automl.GcsClient(client=gcs_client) | ||
| 40 | + self.assertIs(tables_gcs_client.client, gcs_client) | ||
| 41 | + | ||
| 42 | + def test_default_credentials_automl_enabled(self): | ||
| 43 | + env = EnvironmentVarGuard() | ||
| 44 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 45 | + env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML') | ||
| 46 | + with env: | ||
| 47 | + init_automl() | ||
| 48 | + automl_client = automl.AutoMlClient() | ||
| 49 | + self.assertIsNotNone(automl_client._kaggle_credentials) | ||
| 50 | + self.assertIsInstance(automl_client._kaggle_credentials, KaggleKernelCredentials) | ||
| 51 | + tables_client = automl.TablesClient() | ||
| 52 | + self.assertIsNotNone(automl_client._kaggle_credentials) | ||
| 53 | + self.assertIsInstance(automl_client._kaggle_credentials, KaggleKernelCredentials) | ||
| 54 | + prediction_client = automl.PredictionServiceClient() | ||
| 55 | + self.assertIsNotNone(automl_client._kaggle_credentials) | ||
| 56 | + self.assertIsInstance(automl_client._kaggle_credentials, KaggleKernelCredentials) | ||
| 9 | 57 | ||
| 58 | + def test_monkeypatching_idempotent(self): | ||
| 59 | + env = EnvironmentVarGuard() | ||
| 60 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 61 | + env.set('KAGGLE_KERNEL_INTEGRATIONS', 'GCS') | ||
| 62 | + with env: | ||
| 63 | + client1 = automl.AutoMlClient.__init__ | ||
| 64 | + init_automl() | ||
| 65 | + client2 = automl.AutoMlClient.__init__ | ||
| 66 | + self.assertEqual(client1, client2) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments