You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.3 KiB

import os
from arango import ArangoClient
from dotenv import load_dotenv
def get_env():
"""
Initializes a connection to an ArangoDB database and retrieves all documents from the 'enviroment' collection.
This function attempts to load environment variables from a .env file to get the ArangoDB password. If the .env file
is not found, it prompts the user to input the password manually. It then connects to the ArangoDB instance using
the provided credentials and returns the first document from the 'enviroment' collection.
Returns:
dict: The first document from the 'enviroment' collection in the ArangoDB database.
Raises:
FileNotFoundError: If the .env file is not found.
KeyError: If the 'ARANGO_PWD_ENV_MANAGER' environment variable is not set.
"""
try:
load_dotenv(".env")
pwd = os.environ['ARANGO_PWD_ENV_MANAGER']
except (FileNotFoundError, KeyError):
from getpass import getpass
import warnings
warnings.warn("No .env file found or 'ARANGO_PWD_ENV_MANAGER' not set.", UserWarning)
pwd = getpass("Enter the ArangoDB password for the user 'env_manager': ")
with open(".env", "a+") as f:
f.write(f"\nARANGO_PWD_ENV_MANAGER={pwd}")
load_dotenv(".env") # Reload the .env file to set the new environment variable
pwd = os.environ['ARANGO_PWD_ENV_MANAGER']
host = "https://arango.lasseedfast.se"
username = "env_manager"
db = 'div'
# Initialize the database for ArangoDB.
db = ArangoClient(hosts=host).db(db, username=username, password=pwd)
env_doc = db.collection('enviroment').all().next()
return env_doc
def set_env():
# Set the environment variables
for key, value in get_env().items():
if isinstance(value, dict):
continue
os.environ[key] = str(value)
def print_env():
for key, value in get_env().items():
if isinstance(value, dict):
continue
print(f"{key}: {value}")
def print_info():
env_info = get_env()['INFO']
for i, (k, v) in enumerate(env_info.items()):
if i % 2 == 0:
print(f"\033[90m{k}: {v}\033[0m") # Grey color for even indices
else:
print(f"{k}: {v}") # Default color for odd indices