pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/mkdocs/mkdocs/commit/f4de3c738767b23fce0529d9614eca5bdb5341e4

cd9af82350aeda.css" /> Support Environment Variables in config file · mkdocs/mkdocs@f4de3c7 · GitHub
Skip to content

Commit f4de3c7

Browse files
committed
Support Environment Variables in config file
Environment variables can be assigned as values in the configuration file using the !ENV tag. Resolves #1954. The behavior is defined in the third-party package pyyaml_env_tag: https://github.com/waylan/pyyaml-env-tag
1 parent 30dc6a0 commit f4de3c7

File tree

7 files changed

+85
-4
lines changed

7 files changed

+85
-4
lines changed

docs/about/release-notes.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ The current and past members of the MkDocs team.
2525

2626
### Major Additions to Version 1.2
2727

28+
#### Support added for Environment Variables in the configuration file (#1954)
29+
30+
Environments variables may now be specified in the configuration file with the
31+
`!ENV` tag. The value of the variable will be parsed by the YAML parser and
32+
converted to the appropriate type.
33+
34+
```yaml
35+
somekey: !ENV VAR_NAME
36+
otherkey: !ENV [VAR_NAME, FALLBACK_VAR, 'default value']
37+
```
38+
39+
See [Environment Variables](../user-guide/configuration.md#environment-variables)
40+
in the Configuration documentation for details.
41+
2842
#### A `--wait` flag has been added to the `serve` command (#2061)
2943

3044
To delay a rebuild of the site when using the livereload server, use the
@@ -34,7 +48,7 @@ To delay a rebuild of the site when using the livereload server, use the
3448
mkdocs serve --wait 60
3549
```
3650

37-
#### Update `gh-deloy` command (#2170)
51+
#### Update `gh-deploy` command (#2170)
3852

3953
The vendored (and modified) copy of ghp_import has been replaced with a
4054
dependency on the upstream library. As of version 1.0.0, [ghp_import] includes a

docs/user-guide/configuration.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@ project directory named `mkdocs.yml`.
1212
As a minimum, this configuration file must contain the `site_name` setting. All
1313
other settings are optional.
1414

15+
### Environment Variables
16+
17+
In most cases, the value of a configuration option is set directly in the
18+
configuration file. However, as an option, the value of a configuration option
19+
may be set to the value of an environment variable using the `!ENV` tag. For
20+
example, to set the value of the `site_name` option to the value of the
21+
variable `SITE_NAME` the YAML file may contain the following:
22+
23+
```yaml
24+
site_name: !ENV SITE_NAME
25+
```
26+
27+
If the environment variable is not defined, then the configuration setting
28+
would be assigned a `null` (or `None` in Python) value. A default value can be
29+
defined as the last value in a list. Like this:
30+
31+
```yaml
32+
site_name: !ENV [SITE_NAME, 'My default site name']
33+
```
34+
35+
Multiple fallback variables can be used as well. Note that the last value is
36+
not an environment variable, but must be a value to use as a default if none
37+
of the specified environment variables are defined.
38+
39+
```yaml
40+
site_name: !ENV [SITE_NAME, OTHER_NAME, 'My default site name']
41+
```
42+
43+
Simple types defined within an environment variable such as string, bool,
44+
integer, float, datestamp and null are parsed as if they were defined directly
45+
in the YAML file, which means that the value will be converted to the
46+
appropriate type. However, complex types such as lists and key/value pairs
47+
cannot be defined within a single environment variable.
48+
49+
For more details, see the [pyyaml_env_tag](https://github.com/waylan/pyyaml-env-tag)
50+
project.
51+
1552
## Project information
1653

1754
### site_name

mkdocs/tests/utils/utils_tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,28 @@ def test_unicode_yaml(self):
305305
self.assertTrue(isinstance(config['key'], str))
306306
self.assertTrue(isinstance(config['key2'][0], str))
307307

308+
@mock.patch.dict(os.environ, {'VARNAME': 'Hello, World!', 'BOOLVAR': 'false'})
309+
def test_env_var_in_yaml(self):
310+
311+
yaml_src = dedent(
312+
'''
313+
key1: !ENV VARNAME
314+
key2: !ENV UNDEFINED
315+
key3: !ENV [UNDEFINED, default]
316+
key4: !ENV [UNDEFINED, VARNAME, default]
317+
key5: !ENV BOOLVAR
318+
'''
319+
)
320+
config = utils.yaml_load(yaml_src)
321+
self.assertIsInstance(config['key1'], str)
322+
self.assertEqual(config['key1'], 'Hello, World!')
323+
self.assertIsNone(config['key2'])
324+
self.assertIsInstance(config['key3'], str)
325+
self.assertEqual(config['key3'], 'default')
326+
self.assertIsInstance(config['key4'], str)
327+
self.assertEqual(config['key4'], 'Hello, World!')
328+
self.assertIs(config['key5'], False)
329+
308330
def test_copy_files(self):
309331
src_paths = [
310332
'foo.txt',

mkdocs/utils/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import functools
1818
from datetime import datetime, timezone
1919
from urllib.parse import urlparse
20+
from yaml_env_tag import construct_env_tag
2021

2122
from mkdocs import exceptions
2223

@@ -56,6 +57,10 @@ class Loader(loader):
5657
# will be unicode on translation.
5758
Loader.add_constructor('tag:yaml.org,2002:str', construct_yaml_str)
5859

60+
# Attach Environment Variable constructor.
61+
# See https://github.com/waylan/pyyaml-env-tag
62+
Loader.add_constructor('!ENV', construct_env_tag)
63+
5964
try:
6065
return yaml.load(source, Loader)
6166
finally:

requirements/project-min.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ Markdown==3.2.1
55
PyYAML==5.1
66
tornado==4.1
77
mdx_gh_links==0.2
8-
ghp-import==1.0
8+
ghp-import==1.0
9+
pyyaml_env_tag==0.1

requirements/project.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ Markdown>=3.2.1
55
PyYAML>=5.2
66
tornado>=5.1.1
77
mdx_gh_links>=0.2
8-
ghp-import>=1.0
8+
ghp-import>=1.0
9+
pyyaml_env_tag>=0.1

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ def get_packages(package):
6161
'Markdown>=3.2.1',
6262
'PyYAML>=3.10',
6363
'tornado>=5.0',
64-
'ghp-import>=1.0'
64+
'ghp-import>=1.0',
65+
'pyyaml_env_tag>=0.1'
6566
],
6667
python_requires='>=3.6',
6768
entry_points={

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy