python-simpleconf 0.9.3a1__tar.gz → 0.9.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/Makefile +1 -1
  2. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/PKG-INFO +121 -5
  3. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/README.md +119 -3
  4. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/docs/CHANGELOG.md +11 -0
  5. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/pyproject.toml +5 -2
  6. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/__init__.py +1 -1
  7. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/config.py +32 -1
  8. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/loaders/__init__.py +101 -7
  9. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/utils.py +119 -23
  10. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/tests/fixt_simpleconf.py +91 -0
  11. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/tests/test_config.py +114 -0
  12. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/tests/test_loaders.py +74 -0
  13. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/tests/test_utils.py +79 -0
  14. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/uv.lock +938 -447
  15. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/.coveragerc +0 -0
  16. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/.github/workflows/build.yml +0 -0
  17. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/.gitignore +0 -0
  18. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/.pre-commit-config.yaml +0 -0
  19. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/LICENSE +0 -0
  20. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/setup.py +0 -0
  21. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/caster.py +0 -0
  22. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/exceptions.py +0 -0
  23. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/loaders/dict.py +0 -0
  24. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/loaders/env.py +0 -0
  25. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/loaders/ini.py +0 -0
  26. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/loaders/json.py +0 -0
  27. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/loaders/osenv.py +0 -0
  28. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/loaders/toml.py +0 -0
  29. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/loaders/yaml.py +0 -0
  30. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/simpleconf/py.typed +0 -0
  31. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/tests/__init__.py +0 -0
  32. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/tests/test_caster.py +0 -0
  33. {python_simpleconf-0.9.3a1 → python_simpleconf-0.9.5}/tox.ini +0 -0
@@ -22,7 +22,7 @@ version:
22
22
  fi; \
23
23
  if [ -n "$$COMMITS" ]; then \
24
24
  printf "\n## %s\n\n%s\n\n" "$$NEW_VERSION" "$$COMMITS" | cat - <(tail -n +3 docs/CHANGELOG.md) > docs/CHANGELOG.md.tmp; \
25
- head -n 2 docs/CHANGELOG.md > docs/CHANGELOG.md.new; \
25
+ head -n 1 docs/CHANGELOG.md > docs/CHANGELOG.md.new; \
26
26
  cat docs/CHANGELOG.md.tmp >> docs/CHANGELOG.md.new; \
27
27
  mv docs/CHANGELOG.md.new docs/CHANGELOG.md; \
28
28
  rm -f docs/CHANGELOG.md.tmp; \
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-simpleconf
3
- Version: 0.9.3a1
3
+ Version: 0.9.5
4
4
  Summary: Simple configuration management with python.
5
5
  Project-URL: Homepage, https://github.com/pwwang/simpleconf
6
6
  Project-URL: Repository, https://github.com/pwwang/simpleconf
@@ -29,7 +29,7 @@ Requires-Dist: liquidpy<0.9,>=0.8; extra == 'liquid'
29
29
  Provides-Extra: liquidpy
30
30
  Requires-Dist: liquidpy<0.9,>=0.8; extra == 'liquidpy'
31
31
  Provides-Extra: toml
32
- Requires-Dist: rtoml<0.13,>=0.12; (sys_platform == 'linux') and extra == 'toml'
32
+ Requires-Dist: rtoml<1,>=0.12; (sys_platform == 'linux') and extra == 'toml'
33
33
  Requires-Dist: tomli<3,>=2.0; (sys_platform != 'linux') and extra == 'toml'
34
34
  Provides-Extra: yaml
35
35
  Requires-Dist: pyyaml<7,>=6; extra == 'yaml'
@@ -347,9 +347,9 @@ conf = Config.load('config.liq.toml')
347
347
  # conf.section1.a == 1
348
348
  # conf.section2.a == 2
349
349
  ```
350
- #### Loader directive in the first line
350
+ #### Loader directive
351
351
 
352
- Instead of using a special file extension, you can embed a loader directive in the **first line** of a configuration file as a comment. This allows a plain `config.toml` to be processed by the Liquid engine without renaming the file.
352
+ Instead of using a special file extension, you can embed a loader directive in the first lines of a configuration file as a comment. This allows a plain `config.toml` to be processed by the Liquid engine without renaming the file.
353
353
 
354
354
  The directive format is:
355
355
 
@@ -400,4 +400,120 @@ from simpleconf import Config
400
400
 
401
401
  conf = Config.load('config.yaml')
402
402
  # conf.default.a == 2
403
- ```
403
+ ```
404
+
405
+ #### Loadenv directive
406
+
407
+ You will need `python-dotenv` installed to use the `loadenv` directive. You can install it with:
408
+
409
+ ```shell
410
+ pip install python-simpleconf[toml,env]
411
+ ```
412
+ if you only need TOML and dotenv support, or
413
+
414
+ ```shell
415
+ pip install python-simpleconf[all]
416
+ ```
417
+ if you want all supported formats.
418
+
419
+ You can also embed a `# simpleconf-loadenv` directive in the first few lines of a configuration file to load environment variables from a `.env` file. This allows you to reference environment variables directly in your config values using `$env:VAR` syntax, and makes them available as `env` in Jinja2/Liquid templates via `{{ env.VAR }}` or `{{ env["VAR"] }}`.
420
+
421
+ The directive format is:
422
+
423
+ ```
424
+ # simpleconf-loadenv
425
+ # simpleconf-loadenv: /path/to/.env
426
+ ```
427
+
428
+ When no path is given, `./.env` is loaded (relative to the config file's directory).
429
+
430
+ Supported comment prefixes: `#`, `;`, `//` (case-insensitive).
431
+
432
+ **Example — using `$env:VAR` references:**
433
+
434
+ `.env`
435
+ ```env
436
+ DB_HOST=localhost
437
+ DB_PORT=@int:5432
438
+ ```
439
+
440
+ `config.yaml`
441
+ ```yaml
442
+ # simpleconf-loadenv
443
+ db:
444
+ host: $env:DB_HOST
445
+ port: $env:DB_PORT
446
+ ```
447
+
448
+ ```python
449
+ from simpleconf import Config
450
+
451
+ conf = Config.load('config.yaml')
452
+ # conf.db.host == 'localhost'
453
+ # conf.db.port == 5432 # @int: cast to int
454
+ ```
455
+
456
+ The `$env:VAR` syntax works in all supported formats (YAML, TOML, INI, JSON, .env). In formats that require quoted strings (TOML), wrap the reference in quotes: `"$env:DB_HOST"`.
457
+
458
+ Lookup order: the loaded `.env` file is checked first, then `os.environ` as a fallback.
459
+
460
+ **Modifiers — controlling what happens when a variable is not found**
461
+
462
+ The `$env:` syntax supports optional modifiers to control the behavior when a variable is not found:
463
+
464
+ | Syntax | Behavior when VAR not found |
465
+ |---|---|
466
+ | `$env:VAR` | **Default** — raises `ValueError` |
467
+ | `$env:VAR:required` | Explicit form — raises `ValueError` |
468
+ | `$env:VAR:optional-asis` | Keeps the literal `$env:VAR` string unchanged |
469
+ | `$env:VAR:optional-empty` | Resolves to an empty string `""` |
470
+
471
+ **Example — using modifiers:**
472
+
473
+ `.env`
474
+ ```env
475
+ DB_HOST=localhost
476
+ ```
477
+
478
+ `config.yaml`
479
+ ```yaml
480
+ # simpleconf-loadenv
481
+ db:
482
+ host: $env:DB_HOST
483
+ port: $env:DB_PORT:optional-asis # keeps "$env:DB_PORT" as-is when not found
484
+ timeout: $env:TIMEOUT:optional-empty # becomes "" when not found
485
+ ```
486
+
487
+ ```python
488
+ from simpleconf import Config
489
+
490
+ conf = Config.load('config.yaml')
491
+ # conf.db.host == 'localhost'
492
+ # conf.db.port == '$env:DB_PORT'
493
+ # conf.db.timeout == ''
494
+ ```
495
+
496
+ **Example — using `env` in templates:**
497
+
498
+ `.env`
499
+ ```env
500
+ DB_HOST=localhost
501
+ DB_PORT=5432
502
+ ```
503
+
504
+ `config.toml`
505
+ ```toml
506
+ # simpleconf-loadenv
507
+ # simpleconf-loader: liq
508
+ [default]
509
+ url = "{{ env.DB_HOST }}:{{ env.DB_PORT }}"
510
+ ```
511
+
512
+ ```python
513
+ from simpleconf import Config
514
+
515
+ conf = Config.load('config.toml')
516
+ # conf.default.url == 'localhost:5432'
517
+ ```
518
+
519
+ Both directives (`simpleconf-loader` and `simpleconf-loadenv`) can coexist in the same file on different lines. The order does not matter.
@@ -310,9 +310,9 @@ conf = Config.load('config.liq.toml')
310
310
  # conf.section1.a == 1
311
311
  # conf.section2.a == 2
312
312
  ```
313
- #### Loader directive in the first line
313
+ #### Loader directive
314
314
 
315
- Instead of using a special file extension, you can embed a loader directive in the **first line** of a configuration file as a comment. This allows a plain `config.toml` to be processed by the Liquid engine without renaming the file.
315
+ Instead of using a special file extension, you can embed a loader directive in the first lines of a configuration file as a comment. This allows a plain `config.toml` to be processed by the Liquid engine without renaming the file.
316
316
 
317
317
  The directive format is:
318
318
 
@@ -363,4 +363,120 @@ from simpleconf import Config
363
363
 
364
364
  conf = Config.load('config.yaml')
365
365
  # conf.default.a == 2
366
- ```
366
+ ```
367
+
368
+ #### Loadenv directive
369
+
370
+ You will need `python-dotenv` installed to use the `loadenv` directive. You can install it with:
371
+
372
+ ```shell
373
+ pip install python-simpleconf[toml,env]
374
+ ```
375
+ if you only need TOML and dotenv support, or
376
+
377
+ ```shell
378
+ pip install python-simpleconf[all]
379
+ ```
380
+ if you want all supported formats.
381
+
382
+ You can also embed a `# simpleconf-loadenv` directive in the first few lines of a configuration file to load environment variables from a `.env` file. This allows you to reference environment variables directly in your config values using `$env:VAR` syntax, and makes them available as `env` in Jinja2/Liquid templates via `{{ env.VAR }}` or `{{ env["VAR"] }}`.
383
+
384
+ The directive format is:
385
+
386
+ ```
387
+ # simpleconf-loadenv
388
+ # simpleconf-loadenv: /path/to/.env
389
+ ```
390
+
391
+ When no path is given, `./.env` is loaded (relative to the config file's directory).
392
+
393
+ Supported comment prefixes: `#`, `;`, `//` (case-insensitive).
394
+
395
+ **Example — using `$env:VAR` references:**
396
+
397
+ `.env`
398
+ ```env
399
+ DB_HOST=localhost
400
+ DB_PORT=@int:5432
401
+ ```
402
+
403
+ `config.yaml`
404
+ ```yaml
405
+ # simpleconf-loadenv
406
+ db:
407
+ host: $env:DB_HOST
408
+ port: $env:DB_PORT
409
+ ```
410
+
411
+ ```python
412
+ from simpleconf import Config
413
+
414
+ conf = Config.load('config.yaml')
415
+ # conf.db.host == 'localhost'
416
+ # conf.db.port == 5432 # @int: cast to int
417
+ ```
418
+
419
+ The `$env:VAR` syntax works in all supported formats (YAML, TOML, INI, JSON, .env). In formats that require quoted strings (TOML), wrap the reference in quotes: `"$env:DB_HOST"`.
420
+
421
+ Lookup order: the loaded `.env` file is checked first, then `os.environ` as a fallback.
422
+
423
+ **Modifiers — controlling what happens when a variable is not found**
424
+
425
+ The `$env:` syntax supports optional modifiers to control the behavior when a variable is not found:
426
+
427
+ | Syntax | Behavior when VAR not found |
428
+ |---|---|
429
+ | `$env:VAR` | **Default** — raises `ValueError` |
430
+ | `$env:VAR:required` | Explicit form — raises `ValueError` |
431
+ | `$env:VAR:optional-asis` | Keeps the literal `$env:VAR` string unchanged |
432
+ | `$env:VAR:optional-empty` | Resolves to an empty string `""` |
433
+
434
+ **Example — using modifiers:**
435
+
436
+ `.env`
437
+ ```env
438
+ DB_HOST=localhost
439
+ ```
440
+
441
+ `config.yaml`
442
+ ```yaml
443
+ # simpleconf-loadenv
444
+ db:
445
+ host: $env:DB_HOST
446
+ port: $env:DB_PORT:optional-asis # keeps "$env:DB_PORT" as-is when not found
447
+ timeout: $env:TIMEOUT:optional-empty # becomes "" when not found
448
+ ```
449
+
450
+ ```python
451
+ from simpleconf import Config
452
+
453
+ conf = Config.load('config.yaml')
454
+ # conf.db.host == 'localhost'
455
+ # conf.db.port == '$env:DB_PORT'
456
+ # conf.db.timeout == ''
457
+ ```
458
+
459
+ **Example — using `env` in templates:**
460
+
461
+ `.env`
462
+ ```env
463
+ DB_HOST=localhost
464
+ DB_PORT=5432
465
+ ```
466
+
467
+ `config.toml`
468
+ ```toml
469
+ # simpleconf-loadenv
470
+ # simpleconf-loader: liq
471
+ [default]
472
+ url = "{{ env.DB_HOST }}:{{ env.DB_PORT }}"
473
+ ```
474
+
475
+ ```python
476
+ from simpleconf import Config
477
+
478
+ conf = Config.load('config.toml')
479
+ # conf.default.url == 'localhost:5432'
480
+ ```
481
+
482
+ Both directives (`simpleconf-loader` and `simpleconf-loadenv`) can coexist in the same file on different lines. The order does not matter.
@@ -1,5 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.9.5
4
+
5
+ - fix: fix error for _read_first_lines when file name is too long
6
+ - docs: update README to include installation instructions for loadenv directive
7
+
8
+ ## 0.9.4
9
+
10
+ - test: skip loadenv directive test on non-Linux platforms
11
+ - feat: add support for loadenv directive to inject environment variables from .env files
12
+ - chore: bump up dependencies
13
+ - chore: update rtoml version constraint for linux compatibility
3
14
 
4
15
  ## 0.9.3
5
16
 
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "python-simpleconf"
7
- version = "0.9.3a1"
7
+ dynamic = ["version"]
8
8
  description = "Simple configuration management with python."
9
9
  authors = [{name = "pwwang", email = "pwwang@pwwang.com"}]
10
10
  license = {text = "MIT"}
@@ -15,6 +15,9 @@ dependencies = [
15
15
  "panpath>=0.4.8,<0.5",
16
16
  ]
17
17
 
18
+ [tool.hatch.version]
19
+ path = "simpleconf/__init__.py"
20
+
18
21
  [project.urls]
19
22
  Homepage = "https://github.com/pwwang/simpleconf"
20
23
  Repository = "https://github.com/pwwang/simpleconf"
@@ -25,7 +28,7 @@ env = ["python-dotenv>=1.1,<2"]
25
28
  yaml = ["pyyaml>=6,<7"]
26
29
  # Use rtoml only when the wheel is available (linux)
27
30
  toml = [
28
- "rtoml>=0.12,<0.13; sys_platform == 'linux'",
31
+ "rtoml>=0.12,<1; sys_platform == 'linux'",
29
32
  "tomli>=2.0,<3; sys_platform != 'linux'",
30
33
  ]
31
34
  async = ["aiofiles>=23.0.0"]
@@ -2,4 +2,4 @@ from .config import Config, ProfileConfig
2
2
 
3
3
  __all__ = ["Config", "ProfileConfig"]
4
4
 
5
- __version__ = "0.9.3"
5
+ __version__ = "0.9.5"
@@ -5,12 +5,31 @@ from typing import Any, List, Generator, Union, Sequence
5
5
 
6
6
  from diot import Diot
7
7
 
8
- from .utils import config_to_ext, detect_loader_directive, get_loader, POOL_KEY, META_KEY
8
+ from .utils import (
9
+ config_to_ext,
10
+ detect_loader_directive,
11
+ detect_loadenv_directive,
12
+ load_dotenv_file,
13
+ get_loader,
14
+ POOL_KEY,
15
+ META_KEY,
16
+ )
9
17
  from .loaders import Loader
10
18
 
11
19
  LoaderType = Union[str, Loader, None]
12
20
 
13
21
 
22
+ def _inject_env_vars(loader: Loader, config: Any) -> None:
23
+ """Detect ``# simpleconf-loadenv`` directive and inject env vars into loader.
24
+
25
+ If the config file contains a loadenv directive, the referenced ``.env``
26
+ file is loaded and its contents are stored in ``loader.env_vars``.
27
+ """
28
+ env_path = detect_loadenv_directive(config)
29
+ if env_path is not None:
30
+ loader.env_vars = load_dotenv_file(env_path)
31
+
32
+
14
33
  class Config:
15
34
  """The configuration class"""
16
35
 
@@ -121,6 +140,8 @@ class Config:
121
140
  else:
122
141
  loader = get_loader(loader)
123
142
 
143
+ _inject_env_vars(loader, config)
144
+
124
145
  return loader.load(config, ignore_nonexist)
125
146
 
126
147
  @classmethod
@@ -151,6 +172,8 @@ class Config:
151
172
  else:
152
173
  loader = get_loader(loader)
153
174
 
175
+ _inject_env_vars(loader, config)
176
+
154
177
  return await loader.a_load(config, ignore_nonexist)
155
178
 
156
179
 
@@ -209,6 +232,8 @@ class ProfileConfig:
209
232
  else:
210
233
  lder = get_loader(lder)
211
234
 
235
+ _inject_env_vars(lder, conf)
236
+
212
237
  loaded = lder.load_with_profiles(conf, ignore_nonexist)
213
238
  for profile, value in loaded.items():
214
239
  profile = profile.lower()
@@ -279,6 +304,8 @@ class ProfileConfig:
279
304
  else:
280
305
  lder = get_loader(lder)
281
306
 
307
+ _inject_env_vars(lder, conf)
308
+
282
309
  loaded = await lder.a_load_with_profiles(conf, ignore_nonexist)
283
310
  for profile, value in loaded.items():
284
311
  profile = profile.lower()
@@ -332,6 +359,8 @@ class ProfileConfig:
332
359
  else:
333
360
  loader = get_loader(loader)
334
361
 
362
+ _inject_env_vars(loader, conf)
363
+
335
364
  loaded = loader.load_with_profiles(conf, ignore_nonexist)
336
365
  for profile, value in loaded.items():
337
366
  profile = profile.lower()
@@ -388,6 +417,8 @@ class ProfileConfig:
388
417
  else:
389
418
  loader = get_loader(loader)
390
419
 
420
+ _inject_env_vars(loader, conf)
421
+
391
422
  loaded = await loader.a_load_with_profiles(conf, ignore_nonexist)
392
423
  for profile, value in loaded.items():
393
424
  profile = profile.lower()
@@ -1,18 +1,44 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import os
3
4
  from abc import ABC, abstractmethod
4
5
  from typing import Any, Callable, List, Dict
5
6
  from pathlib import Path
6
7
 
7
8
  from diot import Diot
8
9
  from panpath import PanPath
9
- from ..caster import cast
10
+ from ..caster import (
11
+ cast,
12
+ cast_value,
13
+ int_caster,
14
+ float_caster,
15
+ bool_caster,
16
+ none_caster,
17
+ python_caster,
18
+ py_caster,
19
+ json_caster,
20
+ toml_caster,
21
+ )
22
+
23
+ _ENV_CASTERS = [
24
+ int_caster,
25
+ float_caster,
26
+ bool_caster,
27
+ none_caster,
28
+ python_caster,
29
+ py_caster,
30
+ json_caster,
31
+ toml_caster,
32
+ ]
10
33
 
11
34
 
12
35
  class Loader(ABC):
13
36
 
14
37
  CASTERS: List[Callable[[str, bool], Any]] | None = None
15
38
 
39
+ def __init__(self) -> None:
40
+ self.env_vars: Dict[str, str] = {}
41
+
16
42
  @staticmethod
17
43
  def _convert_path(conf: str | Path) -> Path:
18
44
  """Convert the conf to Path if it is a string"""
@@ -36,6 +62,62 @@ class Loader(ABC):
36
62
 
37
63
  return Diot(loaded)
38
64
 
65
+ def _resolve_env_vars(self, loaded: Diot) -> Diot:
66
+ """Resolve ``$env:VAR`` references in loaded config values.
67
+
68
+ Called after ``_convert()`` (i.e., after casters have been applied).
69
+ Recursively walks the config and replaces string values matching
70
+ ``$env:KEY`` with the corresponding environment variable.
71
+
72
+ Lookup order:
73
+ 1. ``self.env_vars`` (loaded from ``.env`` file via directive)
74
+ 2. ``os.environ`` (system environment variables)
75
+
76
+ Resolved values are re-cast through :attr:`CASTERS`.
77
+
78
+ Returns the mutated Diot for chaining.
79
+ """
80
+ dict_type = type({})
81
+
82
+ KNOWN_MODIFIERS = frozenset(
83
+ {"required", "optional-asis", "optional-empty"}
84
+ )
85
+
86
+ def _walk(value):
87
+ if isinstance(value, dict_type):
88
+ for k, v in value.items():
89
+ value[k] = _walk(v)
90
+ return value
91
+ if isinstance(value, str) and value.startswith("$env:"):
92
+ rest = value[5:]
93
+ if ":" in rest:
94
+ var_name, modifier = rest.rsplit(":", 1)
95
+ if modifier not in KNOWN_MODIFIERS:
96
+ # Unknown modifier — treat whole string as var name
97
+ var_name, modifier = rest, "required"
98
+ else:
99
+ var_name, modifier = rest, "required"
100
+
101
+ resolved = self.env_vars.get(var_name)
102
+ if resolved is None:
103
+ resolved = os.environ.get(var_name)
104
+
105
+ if resolved is not None:
106
+ return cast_value(resolved, _ENV_CASTERS)
107
+
108
+ # Not found — behavior depends on modifier
109
+ if modifier == "optional-empty":
110
+ return ""
111
+ if modifier == "optional-asis":
112
+ return f"$env:{var_name}"
113
+ # required (default)
114
+ raise ValueError(
115
+ f"Environment variable '{var_name}' not found."
116
+ )
117
+ return value
118
+
119
+ return _walk(loaded)
120
+
39
121
  @classmethod
40
122
  def _convert_with_profiles(cls, conf: Any, loaded: Any) -> Diot:
41
123
  """Convert the loaded configuration with profiles to Diot"""
@@ -69,7 +151,7 @@ class Loader(ABC):
69
151
  """
70
152
  path = self.__class__._convert_path(conf)
71
153
  loaded = self.loading(path, ignore_nonexist)
72
- return self.__class__._convert(conf, loaded)
154
+ return self._resolve_env_vars(self.__class__._convert(conf, loaded))
73
155
 
74
156
  async def a_load(self, conf: Any, ignore_nonexist: bool = False) -> Diot:
75
157
  """Asynchronously load the configuration from the path or configurations
@@ -83,7 +165,7 @@ class Loader(ABC):
83
165
  """
84
166
  path = self.__class__._convert_path(conf)
85
167
  loaded = await self.a_loading(path, ignore_nonexist)
86
- return self.__class__._convert(conf, loaded)
168
+ return self._resolve_env_vars(self.__class__._convert(conf, loaded))
87
169
 
88
170
  def load_with_profiles( # type: ignore[override]
89
171
  self,
@@ -101,7 +183,9 @@ class Loader(ABC):
101
183
  """
102
184
  path = self.__class__._convert_path(conf)
103
185
  loaded = self.loading(path, ignore_nonexist)
104
- return self.__class__._convert_with_profiles(conf, loaded)
186
+ return self._resolve_env_vars(
187
+ self.__class__._convert_with_profiles(conf, loaded)
188
+ )
105
189
 
106
190
  async def a_load_with_profiles( # type: ignore[override]
107
191
  self,
@@ -119,7 +203,9 @@ class Loader(ABC):
119
203
  """
120
204
  path = self.__class__._convert_path(conf)
121
205
  loaded = await self.a_loading(path, ignore_nonexist)
122
- return self.__class__._convert_with_profiles(conf, loaded)
206
+ return self._resolve_env_vars(
207
+ self.__class__._convert_with_profiles(conf, loaded)
208
+ )
123
209
 
124
210
 
125
211
  class NoConvertingPathMixin(ABC):
@@ -148,7 +234,11 @@ class J2ModifierMixin(LoaderModifierMixin):
148
234
  def _modifier(self, content: str | bytes) -> str | bytes:
149
235
  """Modify the content of the configuration file before loading"""
150
236
  from jinja2 import Template
151
- return Template(content).render() # type: ignore
237
+ env_vars = getattr(self, "env_vars", {})
238
+ casted_env = {
239
+ k: cast_value(v, _ENV_CASTERS) for k, v in env_vars.items()
240
+ }
241
+ return Template(content).render(env=casted_env) # type: ignore
152
242
 
153
243
 
154
244
  class LiqModifierMixin(LoaderModifierMixin):
@@ -159,4 +249,8 @@ class LiqModifierMixin(LoaderModifierMixin):
159
249
  from liquid import Liquid # type: ignore[import]
160
250
  str_content = content.decode() if isinstance(content, bytes) else content
161
251
  liq = Liquid(str_content, from_file=False, mode="wild") # type: ignore
162
- return liq.render()
252
+ env_vars = getattr(self, "env_vars", {})
253
+ casted_env = {
254
+ k: cast_value(v, _ENV_CASTERS) for k, v in env_vars.items()
255
+ }
256
+ return liq.render(env=casted_env) # type: ignore