From 467d341cf37e97e3b2dca9f80c0b7d8c137676ae Mon Sep 17 00:00:00 2001 From: Ruge Li <91452427+rugeli@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:18:31 -0700 Subject: [PATCH] set staging as the default database for S3 metadata storage (#234) * add default_db for S3 metadata storage * fix: initialize DBRecipeLoader * deserialize .env token --- cellpack/autopack/FirebaseHandler.py | 15 +++++++++------ cellpack/autopack/__init__.py | 4 ++-- .../autopack/interface_objects/database_ids.py | 5 ++++- .../autopack/upy/simularium/simularium_helper.py | 8 ++++++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cellpack/autopack/FirebaseHandler.py b/cellpack/autopack/FirebaseHandler.py index 96b91532..b05f2cbc 100644 --- a/cellpack/autopack/FirebaseHandler.py +++ b/cellpack/autopack/FirebaseHandler.py @@ -19,10 +19,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: @@ -37,14 +37,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): @@ -112,12 +114,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", } diff --git a/cellpack/autopack/__init__.py b/cellpack/autopack/__init__.py index 2e5e44af..143544f0 100755 --- a/cellpack/autopack/__init__.py +++ b/cellpack/autopack/__init__.py @@ -386,8 +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) - db_handler.validate_input_recipe_path(filename) + 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 c9a7520e..7ef4203a 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=None): + 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 1caa908e..1e047e56 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):