Skip to content

Commit

Permalink
set staging as the default database for S3 metadata storage (#234)
Browse files Browse the repository at this point in the history
* add default_db for S3 metadata storage

* fix: initialize DBRecipeLoader

* deserialize .env token
  • Loading branch information
rugeli authored Mar 21, 2024
1 parent c78b89b commit 467d341
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
15 changes: 9 additions & 6 deletions cellpack/autopack/FirebaseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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",
}

Expand Down
4 changes: 2 additions & 2 deletions cellpack/autopack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion cellpack/autopack/interface_objects/database_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions cellpack/autopack/upy/simularium/simularium_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 467d341

Please sign in to comment.