From 043a21005fc4f6fcc26a7e1cc16de8e3b41200b6 Mon Sep 17 00:00:00 2001 From: Michael Smit Date: Mon, 19 May 2025 09:39:22 -0700 Subject: [PATCH] Revert "Merge pull request #133 from PolicyEngine/350_use_caching_google_storage_client" This reverts commit df9d647ed2c1384fa80f488c4d85df5d2017347f, reversing changes made to 9ca329c3379b6140c8118584319273e6f08f2eb6. related to #PolicyEngine/issues#350 This change resulted in an asyncio error because fastAPI calls from an event loop and you can't run asyncio.run if already in an event loop --- changelog_entry.yaml | 4 ++ policyengine/utils/google_cloud_bucket.py | 38 ++++++++----------- tests/utils/data/test_google_cloud_bucket.py | 39 -------------------- 3 files changed, 19 insertions(+), 62 deletions(-) delete mode 100644 tests/utils/data/test_google_cloud_bucket.py diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29b..287d2dc0 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,4 @@ +- bump: patch + changes: + fixed: + - revert cached downloads from google storage diff --git a/policyengine/utils/google_cloud_bucket.py b/policyengine/utils/google_cloud_bucket.py index d65e872e..14dc9031 100644 --- a/policyengine/utils/google_cloud_bucket.py +++ b/policyengine/utils/google_cloud_bucket.py @@ -1,23 +1,3 @@ -from .data.caching_google_storage_client import CachingGoogleStorageClient -import asyncio -from pathlib import Path - -_caching_client: CachingGoogleStorageClient | None = None - - -def _clear_client(): - global _caching_client - _caching_client = None - - -def _get_client(): - global _caching_client - if _caching_client is not None: - return _caching_client - _caching_client = CachingGoogleStorageClient() - return _caching_client - - def download_file_from_gcs( bucket_name: str, file_name: str, destination_path: str ) -> None: @@ -32,6 +12,18 @@ def download_file_from_gcs( Returns: None """ - asyncio.run( - _get_client().download(bucket_name, file_name, Path(destination_path)) - ) + from google.cloud import storage + + # Initialize a client + client = storage.Client() + + # Get the bucket + bucket = client.bucket(bucket_name) + + # Create a blob object from the file name + blob = bucket.blob(file_name) + + # Download the file to a local path + blob.download_to_filename(destination_path) + + return destination_path diff --git a/tests/utils/data/test_google_cloud_bucket.py b/tests/utils/data/test_google_cloud_bucket.py deleted file mode 100644 index c141c0f9..00000000 --- a/tests/utils/data/test_google_cloud_bucket.py +++ /dev/null @@ -1,39 +0,0 @@ -from unittest import TestCase -from unittest.mock import patch -import pytest -from pathlib import Path -from policyengine.utils.google_cloud_bucket import ( - download_file_from_gcs, - _clear_client, -) - - -class TestGoogleCloudBucket(TestCase): - def setUp(self): - _clear_client() - - @patch( - "policyengine.utils.google_cloud_bucket.CachingGoogleStorageClient", - autospec=True, - ) - def test_download_uses_storage_client(self, client_class): - client_instance = client_class.return_value - download_file_from_gcs( - "TEST_BUCKET", "TEST/FILE/NAME.TXT", "TARGET/PATH" - ) - client_instance.download.assert_called_with( - "TEST_BUCKET", "TEST/FILE/NAME.TXT", Path("TARGET/PATH") - ) - - @patch( - "policyengine.utils.google_cloud_bucket.CachingGoogleStorageClient", - autospec=True, - ) - def test_download_only_creates_client_once(self, client_class): - download_file_from_gcs( - "TEST_BUCKET", "TEST/FILE/NAME.TXT", "TARGET/PATH" - ) - download_file_from_gcs( - "TEST_BUCKET", "TEST/FILE/NAME.TXT", "ANOTHER/PATH" - ) - client_class.assert_called_once()