Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration from file-based env variables #114

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

!crawler/**/*.py

!patch_environ/**/*.py

!requirements

!sample/sample.sqlite3
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ will be used.
To enable Google Tag Manager on all pages on the viewer application,
define the `GOOGLE_TAG_ID` environment variable.

### Using files as an alternative to environment variables

As alternative to configuration via environment variable,
values may be provided in files instead.

Set the `PATCH_ENVIRON_PATH` environment variable to the location of a directory
containing files named for environment variables to set,
each containing the desired value for that variable.

When configured this way,
the application environment will be populated with those file-based values.
Values that already exist in the environment will not be overwritten.

## Development

### Sample test data
Expand Down
22 changes: 22 additions & 0 deletions patch_environ/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import os.path
import re


ENV_FILENAME_RE = re.compile(r"[A-Z_]+")


def patch_environ(path, environ=os.environ):
"""Patch the environment with file-based values.

Given a path to a directory containing files named for environment
variables, set those variables with the content of the files.
Do not overwrite variables already set in the environment.
"""
if not path:
return

for filename in os.listdir(path):
if ENV_FILENAME_RE.match(filename):
with open(os.path.join(path, filename)) as f:
environ.setdefault(filename, f.read())
50 changes: 50 additions & 0 deletions patch_environ/tests/test_patch_environ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os.path
from tempfile import TemporaryDirectory
from unittest import TestCase

from patch_environ import patch_environ


def make_file(filename, contents):
with open(filename, "w") as f:
f.write(contents)


class PatchEnvironTests(TestCase):
def test_pass_none_does_nothing(self):
environ = {
"FOO": "foo",
"BAR": "bar",
}

patch_environ(None, environ)

self.assertEqual(
environ,
{
"FOO": "foo",
"BAR": "bar",
},
)

def test_patching(self):
environ = {
"FOO": "foo",
"BAR": "bar",
}

with TemporaryDirectory() as tempdir:
make_file(os.path.join(tempdir, "BAR"), "test")
make_file(os.path.join(tempdir, "FOO_BAR"), "baz")
make_file(os.path.join(tempdir, "ignore_lowercase"), "ignore")

patch_environ(tempdir, environ)

self.assertEqual(
environ,
{
"FOO": "foo",
"BAR": "bar",
"FOO_BAR": "baz",
},
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[tool.pytest.ini_options]
addopts = [
"--cov=crawler",
"--cov=patch_environ",
"--cov=viewer",
"--cov-branch",
"--cov-fail-under=100",
Expand Down
6 changes: 5 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import os
import sys
from pathlib import Path

import dj_database_url

from patch_environ import patch_environ

# Optionally patch the environment with file-based variables.
patch_environ(os.getenv("PATCH_ENVIRON_PATH"))


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent
Expand Down
Loading