Skip to content

Commit 32f4a93

Browse files
committed
move environment vars to a configuration file
ref #266
1 parent 2113fd7 commit 32f4a93

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

build_docs.py

+41
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
Without any arguments builds docs for all active versions and
66
languages.
77
8+
Environment variables for:
9+
- SENTRY_DSN (Error reporting)
10+
- FASTLY_SERVICE_ID/FASTLY_TOKEN (CDN purges)
11+
- PYTHON_DOCS_ENABLE_ANALYTICS (enable plausible for online docs)
12+
are read from the site configuration path for your platform
13+
(/etc/xdg/docsbuild-scripts on linux) if available,
14+
and can be overriden by writing a file to the user config dir
15+
for your platform ($HOME/.config/docsbuild-scripts on linux).
16+
The contents of the file is parsed as toml:
17+
18+
[env]
19+
SENTRY_DSN = "https://[email protected]/69420"
20+
FASTLY_SERVICE_ID = "deadbeefdeadbeefdead"
21+
FASTLY_TOKEN = "secureme!"
22+
PYTHON_DOCS_ENABLE_ANALYTICS = "1"
23+
824
Languages are stored in `config.toml` while versions are discovered
925
from the devguide.
1026
@@ -48,6 +64,31 @@
4864
import tomlkit
4965
import urllib3
5066
import zc.lockfile
67+
from platformdirs import user_config_path, site_config_path
68+
69+
ENV_CONF_FILE = None
70+
_user_config_path = user_config_path("docsbuild-scripts")
71+
_site_config_path = site_config_path("docsbuild-scripts")
72+
if _user_config_path.is_file():
73+
ENV_CONF_FILE = _user_config_path
74+
elif _site_config_path.is_file():
75+
ENV_CONF_FILE = _site_config_path
76+
77+
if ENV_CONF_FILE:
78+
print(f"Reading environment variables from {ENV_CONF_FILE}")
79+
if ENV_CONF_FILE == _site_config_path:
80+
print(f"You can override settings in {_user_config_path}")
81+
elif _site_config_path.is_file():
82+
print(f"Overriding {_site_config_path}")
83+
with open(ENV_CONF_FILE, "r") as f:
84+
for key, value in tomlkit.parse(f.read()).get("env", {}).items():
85+
print(f"Setting {key} in environment")
86+
os.environ[key] = value
87+
else:
88+
print(
89+
"No environment variables configured. "
90+
f"Configure in {_site_config_path} or {_user_config_path}"
91+
)
5192

5293
TYPE_CHECKING = False
5394
if TYPE_CHECKING:

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
jinja2
2+
platformdirs
23
sentry-sdk>=2
34
tomlkit>=0.13
45
urllib3>=2

0 commit comments

Comments
 (0)