Skip to content

Commit

Permalink
Use python-dotenv to read in env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
mmwinther committed Jan 25, 2024
1 parent 3d87f27 commit 52ce768
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Project spesific ignores
# Project specific ignores
tests/resources/*.json
src/datadoc/.env.dev

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
1 change: 1 addition & 0 deletions src/datadoc/.env.default
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
JUPYTERHUB_USER=
8 changes: 4 additions & 4 deletions src/datadoc/backend/datadoc_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

import json
import logging
import os
import pathlib
import typing as t
import uuid
from typing import TYPE_CHECKING

from datadoc_model import model

from datadoc import config
from datadoc.backend.dataset_parser import DatasetParser
from datadoc.backend.model_backwards_compatibility import upgrade_metadata
from datadoc.backend.storage_adapter import StorageAdapter
Expand All @@ -23,6 +23,7 @@
from datadoc.utils import get_timestamp_now

if TYPE_CHECKING:
import os
from datetime import datetime

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -78,9 +79,8 @@ def __init__(

self.extract_metadata_from_files()

try:
self.current_user = os.environ["JUPYTERHUB_USER"]
except KeyError:
self.current_user = config.get_jupyterhub_user()
if not self.current_user:
self.current_user = PLACEHOLDER_USERNAME
logger.warning(
"JUPYTERHUB_USER env variable not set, using %s as placeholder",
Expand Down
17 changes: 17 additions & 0 deletions src/datadoc/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Centralised configuration management for Datadoc."""
from __future__ import annotations

import os

from dotenv import dotenv_values

_config: dict[str, str | None] = {
**dotenv_values(".env.default"), # load default config
**dotenv_values(".env.dev"), # load local dev config
**os.environ, # override loaded values with environment variables
}


def get_jupyterhub_user() -> str | None:
"""Get the JupyterHub user name."""
return _config.get("JUPYTERHUB_USER")

0 comments on commit 52ce768

Please sign in to comment.