Refactor code to handle missing .env file and set environment variable for ArangoDB password

main
lasseedfast 1 year ago
parent 6e9ead6adf
commit 9e905c83b0
  1. 5
      env_manager/env_manager.py

@ -1,7 +1,6 @@
import os import os
from arango import ArangoClient from arango import ArangoClient
from dotenv import load_dotenv from dotenv import load_dotenv
import warnings
def get_env(): def get_env():
""" """
Initializes a connection to an ArangoDB database and retrieves all documents from the 'enviroment' collection. Initializes a connection to an ArangoDB database and retrieves all documents from the 'enviroment' collection.
@ -21,8 +20,10 @@ def get_env():
load_dotenv(".env") load_dotenv(".env")
pwd = os.environ['ARANGO_PWD_ENV_MANAGER'] pwd = os.environ['ARANGO_PWD_ENV_MANAGER']
except (FileNotFoundError, KeyError): except (FileNotFoundError, KeyError):
import getpass
import warnings
warnings.warn("No .env file found or 'ARANGO_PWD_ENV_MANAGER' not set.", UserWarning) warnings.warn("No .env file found or 'ARANGO_PWD_ENV_MANAGER' not set.", UserWarning)
pwd = input("Enter the ArangoDB password for the user 'env_manager': ") pwd = getpass("Enter the ArangoDB password for the user 'env_manager': ")
with open(".env", "a+") as f: with open(".env", "a+") as f:
f.write(f"\nARANGO_PWD_ENV_MANAGER={pwd}") f.write(f"\nARANGO_PWD_ENV_MANAGER={pwd}")
load_dotenv(".env") # Reload the .env file to set the new environment variable load_dotenv(".env") # Reload the .env file to set the new environment variable

Loading…
Cancel
Save