From 329f9f5935bddef302c8c8a3d814a2d12fd27ea2 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 27 Feb 2024 19:00:42 -0800 Subject: [PATCH 1/3] add default_db for S3 metadata storage --- cellpack/autopack/FirebaseHandler.py | 12 +++++++----- cellpack/autopack/interface_objects/database_ids.py | 5 ++++- .../autopack/upy/simularium/simularium_helper.py | 8 ++++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cellpack/autopack/FirebaseHandler.py b/cellpack/autopack/FirebaseHandler.py index 69e1f0fe..8879d032 100644 --- a/cellpack/autopack/FirebaseHandler.py +++ b/cellpack/autopack/FirebaseHandler.py @@ -16,10 +16,10 @@ class FirebaseHandler(object): _initialized = False _db = None - def __init__(self): + def __init__(self, default_db=None): # check if firebase is already initialized if not FirebaseHandler._initialized: - db_choice = FirebaseHandler.which_db() + db_choice = FirebaseHandler.which_db(default_db=default_db) if db_choice == "staging": cred = FirebaseHandler.get_staging_creds() else: @@ -34,14 +34,16 @@ def __init__(self): # common utility methods @staticmethod - def which_db(): + def which_db(default_db=None): options = {"1": "dev", "2": "staging"} - print("Choose database:") + if default_db in options.values(): + print(f"Using {default_db} database -------------") + return default_db for key, value in options.items(): print(f"[{key}] {value}") choice = input("Enter number: ").strip() print(f"Using {options.get(choice, 'dev')} database -------------") - return options.get(choice, "dev") # default to dev db + return options.get(choice, "dev") # default to dev db for recipe uploads @staticmethod def doc_to_dict(doc): diff --git a/cellpack/autopack/interface_objects/database_ids.py b/cellpack/autopack/interface_objects/database_ids.py index c9a7520e..d20c3a96 100644 --- a/cellpack/autopack/interface_objects/database_ids.py +++ b/cellpack/autopack/interface_objects/database_ids.py @@ -21,8 +21,11 @@ def create_aws_handler(bucket_name, sub_folder_name, region_name): region_name=region_name, ) + def create_firebase_handler(default_db): + return FirebaseHandler(default_db=default_db) + handlers_dict = { - cls.FIREBASE: FirebaseHandler(), + cls.FIREBASE: create_firebase_handler, cls.AWS: create_aws_handler, } return handlers_dict diff --git a/cellpack/autopack/upy/simularium/simularium_helper.py b/cellpack/autopack/upy/simularium/simularium_helper.py index 031dccd8..6227920d 100644 --- a/cellpack/autopack/upy/simularium/simularium_helper.py +++ b/cellpack/autopack/upy/simularium/simularium_helper.py @@ -1423,8 +1423,12 @@ def store_result_file(file_path, storage=None): @staticmethod def store_metadata(file_name, url, db=None): if db == "firebase": - db_handler = DBUploader(DATABASE_IDS.handlers().get(db)) - db_handler.upload_result_metadata(file_name, url) + handler = DATABASE_IDS.handlers().get(db) + initialized_db = handler( + default_db="staging" + ) # default to staging for metadata uploads + db_uploader = DBUploader(initialized_db) + db_uploader.upload_result_metadata(file_name, url) @staticmethod def open_in_simularium(aws_url): From e95eb52bdcb290c7650e51c10bb560646462853f Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Thu, 29 Feb 2024 12:53:28 -0800 Subject: [PATCH 2/3] fix: initialize DBRecipeLoader --- cellpack/autopack/__init__.py | 3 ++- cellpack/autopack/interface_objects/database_ids.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cellpack/autopack/__init__.py b/cellpack/autopack/__init__.py index 18c7667b..143544f0 100755 --- a/cellpack/autopack/__init__.py +++ b/cellpack/autopack/__init__.py @@ -386,7 +386,8 @@ def load_file(filename, destination="", cache="geometries", force=None): # command example: `pack -r firebase:recipes/[FIREBASE-RECIPE-ID] -c [CONFIG-FILE-PATH]` if database_name == "firebase": db = DATABASE_IDS.handlers().get(database_name) - db_handler = DBRecipeLoader(db) + initialize_db = db() + db_handler = DBRecipeLoader(initialize_db) recipe_id = file_path.split("/")[-1] db_doc, _ = db_handler.collect_docs_by_id( collection="recipes", id=recipe_id diff --git a/cellpack/autopack/interface_objects/database_ids.py b/cellpack/autopack/interface_objects/database_ids.py index d20c3a96..7ef4203a 100644 --- a/cellpack/autopack/interface_objects/database_ids.py +++ b/cellpack/autopack/interface_objects/database_ids.py @@ -21,7 +21,7 @@ def create_aws_handler(bucket_name, sub_folder_name, region_name): region_name=region_name, ) - def create_firebase_handler(default_db): + def create_firebase_handler(default_db=None): return FirebaseHandler(default_db=default_db) handlers_dict = { From 47469bc5c1a1593bf94f3e542d2e6d0897b522de Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Thu, 29 Feb 2024 13:34:51 -0800 Subject: [PATCH 3/3] deserialize .env token --- cellpack/autopack/FirebaseHandler.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cellpack/autopack/FirebaseHandler.py b/cellpack/autopack/FirebaseHandler.py index 8879d032..9231e949 100644 --- a/cellpack/autopack/FirebaseHandler.py +++ b/cellpack/autopack/FirebaseHandler.py @@ -103,12 +103,13 @@ def get_staging_creds(): # set override=True to refresh the .env file if softwares or tokens updated load_dotenv(dotenv_path="./.env", override=False) FIREBASE_TOKEN = os.getenv("FIREBASE_TOKEN") + firebase_key = FIREBASE_TOKEN.replace("\\n", "\n") FIREBASE_EMAIL = os.getenv("FIREBASE_EMAIL") return { "type": "service_account", "project_id": "cell-pack-database", "client_email": FIREBASE_EMAIL, - "private_key": FIREBASE_TOKEN, + "private_key": firebase_key, "token_uri": "https://oauth2.googleapis.com/token", }