Skip to content

Commit

Permalink
Enable staging Firebase database (#218)
Browse files Browse the repository at this point in the history
* choose from dev/stag databases

* refine option box

* add python-dotenv to requirements

* deserialize key

* remove deserialized key
  • Loading branch information
rugeli authored Feb 5, 2024
1 parent e8c834e commit 227e7f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
36 changes: 33 additions & 3 deletions cellpack/autopack/FirebaseHandler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ast
import os
import firebase_admin
from firebase_admin import credentials, firestore
from dotenv import load_dotenv
from google.cloud.exceptions import NotFound
from cellpack.autopack.loaders.utils import read_json_file, write_json_file

Expand All @@ -17,8 +19,12 @@ class FirebaseHandler(object):
def __init__(self):
# check if firebase is already initialized
if not FirebaseHandler._initialized:
cred_path = FirebaseHandler.get_creds()
login = credentials.Certificate(cred_path)
db_choice = FirebaseHandler.which_db()
if db_choice == "staging":
cred = FirebaseHandler.get_staging_creds()
else:
cred = FirebaseHandler.get_dev_creds()
login = credentials.Certificate(cred)
firebase_admin.initialize_app(login)
FirebaseHandler._initialized = True
FirebaseHandler._db = firestore.client()
Expand All @@ -27,6 +33,16 @@ def __init__(self):
self.name = "firebase"

# common utility methods
@staticmethod
def which_db():
options = {"1": "dev", "2": "staging"}
print("Choose database:")
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

@staticmethod
def doc_to_dict(doc):
return doc.to_dict()
Expand Down Expand Up @@ -74,12 +90,26 @@ def upload_doc(self, collection, data):

# Read methods
@staticmethod
def get_creds():
def get_dev_creds():
creds = read_json_file("./.creds")
if creds is None or "firebase" not in creds:
creds = FirebaseHandler.write_creds_path()
return creds["firebase"]

@staticmethod
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_EMAIL = os.getenv("FIREBASE_EMAIL")
return {
"type": "service_account",
"project_id": "cell-pack-database",
"client_email": FIREBASE_EMAIL,
"private_key": FIREBASE_TOKEN,
"token_uri": "https://oauth2.googleapis.com/token",
}

@staticmethod
def get_username():
creds = read_json_file("./.creds")
Expand Down
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"pymunk>=6.2.0",
"trimesh>=3.9.34",
"deepdiff>=5.5.0",
"python-dotenv>=1.0.0",
]

extra_requirements = {
Expand Down Expand Up @@ -103,10 +104,10 @@
name="cellpack",
packages=find_packages(exclude=["tests", "*.tests", "*.tests.*"]),
package_data={
'': [
'cellpack/tests/packing-configs/*',
'cellpack/tests/recipes/*',
'logging.conf'
"": [
"cellpack/tests/packing-configs/*",
"cellpack/tests/recipes/*",
"logging.conf",
]
},
python_requires=">=3.8",
Expand Down

0 comments on commit 227e7f2

Please sign in to comment.