python-simpleconf 0.9.4__tar.gz → 0.9.6__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.4 → python_simpleconf-0.9.6}/Makefile +1 -1
  2. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/PKG-INFO +19 -2
  3. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/README.md +17 -0
  4. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/docs/CHANGELOG.md +9 -0
  5. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/__init__.py +1 -1
  6. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/loaders/__init__.py +15 -1
  7. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/utils.py +5 -1
  8. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/tests/test_config.py +15 -0
  9. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/tests/test_loaders.py +40 -0
  10. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/.coveragerc +0 -0
  11. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/.github/workflows/build.yml +0 -0
  12. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/.gitignore +0 -0
  13. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/.pre-commit-config.yaml +0 -0
  14. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/LICENSE +0 -0
  15. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/pyproject.toml +0 -0
  16. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/setup.py +0 -0
  17. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/caster.py +0 -0
  18. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/config.py +0 -0
  19. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/exceptions.py +0 -0
  20. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/loaders/dict.py +0 -0
  21. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/loaders/env.py +0 -0
  22. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/loaders/ini.py +0 -0
  23. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/loaders/json.py +0 -0
  24. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/loaders/osenv.py +0 -0
  25. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/loaders/toml.py +0 -0
  26. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/loaders/yaml.py +0 -0
  27. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/simpleconf/py.typed +0 -0
  28. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/tests/__init__.py +0 -0
  29. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/tests/fixt_simpleconf.py +0 -0
  30. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/tests/test_caster.py +0 -0
  31. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/tests/test_utils.py +0 -0
  32. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/tox.ini +0 -0
  33. {python_simpleconf-0.9.4 → python_simpleconf-0.9.6}/uv.lock +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
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: python-simpleconf
3
- Version: 0.9.4
3
+ Version: 0.9.6
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
@@ -404,6 +404,18 @@ conf = Config.load('config.yaml')
404
404
 
405
405
  #### Loadenv directive
406
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
+
407
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"] }}`.
408
420
 
409
421
  The directive format is:
@@ -455,6 +467,9 @@ The `$env:` syntax supports optional modifiers to control the behavior when a va
455
467
  | `$env:VAR:required` | Explicit form — raises `ValueError` |
456
468
  | `$env:VAR:optional-asis` | Keeps the literal `$env:VAR` string unchanged |
457
469
  | `$env:VAR:optional-empty` | Resolves to an empty string `""` |
470
+ | `$env:VAR:default:<value>` | Resolves to `<value>` instead |
471
+
472
+ When found, the environment variable's value always wins over the `default`. Default values go through the same casting as env values, so cast prefixes like `@int:` work there too (e.g. `$env:PORT:default:@int:8080` resolves to the integer `8080`). The default value itself may contain colons (e.g. `$env:URL:default:http://localhost:8080`).
458
473
 
459
474
  **Example — using modifiers:**
460
475
 
@@ -470,6 +485,7 @@ db:
470
485
  host: $env:DB_HOST
471
486
  port: $env:DB_PORT:optional-asis # keeps "$env:DB_PORT" as-is when not found
472
487
  timeout: $env:TIMEOUT:optional-empty # becomes "" when not found
488
+ user: $env:DB_USER:default:admin # becomes "admin" when not found
473
489
  ```
474
490
 
475
491
  ```python
@@ -479,6 +495,7 @@ conf = Config.load('config.yaml')
479
495
  # conf.db.host == 'localhost'
480
496
  # conf.db.port == '$env:DB_PORT'
481
497
  # conf.db.timeout == ''
498
+ # conf.db.user == 'admin'
482
499
  ```
483
500
 
484
501
  **Example — using `env` in templates:**
@@ -367,6 +367,18 @@ conf = Config.load('config.yaml')
367
367
 
368
368
  #### Loadenv directive
369
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
+
370
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"] }}`.
371
383
 
372
384
  The directive format is:
@@ -418,6 +430,9 @@ The `$env:` syntax supports optional modifiers to control the behavior when a va
418
430
  | `$env:VAR:required` | Explicit form — raises `ValueError` |
419
431
  | `$env:VAR:optional-asis` | Keeps the literal `$env:VAR` string unchanged |
420
432
  | `$env:VAR:optional-empty` | Resolves to an empty string `""` |
433
+ | `$env:VAR:default:<value>` | Resolves to `<value>` instead |
434
+
435
+ When found, the environment variable's value always wins over the `default`. Default values go through the same casting as env values, so cast prefixes like `@int:` work there too (e.g. `$env:PORT:default:@int:8080` resolves to the integer `8080`). The default value itself may contain colons (e.g. `$env:URL:default:http://localhost:8080`).
421
436
 
422
437
  **Example — using modifiers:**
423
438
 
@@ -433,6 +448,7 @@ db:
433
448
  host: $env:DB_HOST
434
449
  port: $env:DB_PORT:optional-asis # keeps "$env:DB_PORT" as-is when not found
435
450
  timeout: $env:TIMEOUT:optional-empty # becomes "" when not found
451
+ user: $env:DB_USER:default:admin # becomes "admin" when not found
436
452
  ```
437
453
 
438
454
  ```python
@@ -442,6 +458,7 @@ conf = Config.load('config.yaml')
442
458
  # conf.db.host == 'localhost'
443
459
  # conf.db.port == '$env:DB_PORT'
444
460
  # conf.db.timeout == ''
461
+ # conf.db.user == 'admin'
445
462
  ```
446
463
 
447
464
  **Example — using `env` in templates:**
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.9.6
4
+
5
+ - feat: add support for default values in environment variable resolution
6
+
7
+ ## 0.9.5
8
+
9
+ - fix: fix error for _read_first_lines when file name is too long
10
+ - docs: update README to include installation instructions for loadenv directive
11
+
3
12
  ## 0.9.4
4
13
 
5
14
  - test: skip loadenv directive test on non-Linux platforms
@@ -2,4 +2,4 @@ from .config import Config, ProfileConfig
2
2
 
3
3
  __all__ = ["Config", "ProfileConfig"]
4
4
 
5
- __version__ = "0.9.4"
5
+ __version__ = "0.9.6"
@@ -73,6 +73,10 @@ class Loader(ABC):
73
73
  1. ``self.env_vars`` (loaded from ``.env`` file via directive)
74
74
  2. ``os.environ`` (system environment variables)
75
75
 
76
+ ``$env:KEY:default:<value>`` falls back to ``<value>`` when the
77
+ variable is not found (modifiers ``required``, ``optional-asis`` and
78
+ ``optional-empty`` control the not-found behavior otherwise).
79
+
76
80
  Resolved values are re-cast through :attr:`CASTERS`.
77
81
 
78
82
  Returns the mutated Diot for chaining.
@@ -90,7 +94,13 @@ class Loader(ABC):
90
94
  return value
91
95
  if isinstance(value, str) and value.startswith("$env:"):
92
96
  rest = value[5:]
93
- if ":" in rest:
97
+ default = None
98
+ if ":default:" in rest:
99
+ # $env:VAR:default:<value> — split on first occurrence so
100
+ # the default itself may contain colons
101
+ var_name, _, default = rest.partition(":default:")
102
+ modifier = "required"
103
+ elif ":" in rest:
94
104
  var_name, modifier = rest.rsplit(":", 1)
95
105
  if modifier not in KNOWN_MODIFIERS:
96
106
  # Unknown modifier — treat whole string as var name
@@ -105,6 +115,10 @@ class Loader(ABC):
105
115
  if resolved is not None:
106
116
  return cast_value(resolved, _ENV_CASTERS)
107
117
 
118
+ # Not found — use the default value if given
119
+ if default is not None:
120
+ return cast_value(default, _ENV_CASTERS)
121
+
108
122
  # Not found — behavior depends on modifier
109
123
  if modifier == "optional-empty":
110
124
  return ""
@@ -38,7 +38,11 @@ def _read_first_lines(conf: Any) -> List[str]:
38
38
  return []
39
39
 
40
40
  path = Path(conf)
41
- if not path.exists():
41
+ try:
42
+ if not path.exists():
43
+ return []
44
+ except Exception: # pragma: no cover
45
+ # e.g. OSError: Filename too long
42
46
  return []
43
47
 
44
48
  try:
@@ -469,3 +469,18 @@ def test_loadenv_unknown_modifier_falls_back_to_required(tmp_path):
469
469
  )
470
470
  with pytest.raises(ValueError, match="NONEXISTENT:unknown"):
471
471
  Config.load(f)
472
+
473
+
474
+ def test_loadenv_default_used_when_not_found(tmp_path):
475
+ """$env:NONEXISTENT:default:<value> falls back to <value>."""
476
+ env = tmp_path / ".env"
477
+ env.write_text("A=1\n")
478
+ f = tmp_path / "config.yaml"
479
+ f.write_text(
480
+ "# simpleconf-loadenv\n"
481
+ "a: $env:A:default:x\n"
482
+ "b: $env:NONEXISTENT:default:abc\n"
483
+ )
484
+ config = Config.load(f)
485
+ assert config.a == "1" # found env var wins over the default
486
+ assert config.b == "abc"
@@ -567,6 +567,46 @@ def test_resolve_env_vars_optional_empty():
567
567
  assert out.a == ""
568
568
 
569
569
 
570
+ def test_resolve_env_vars_default_used_when_not_found():
571
+ """$env:VAR:default:<value> falls back to <value> when not found."""
572
+ loader = get_loader("dict")
573
+ loader.env_vars = {}
574
+ out = loader._resolve_env_vars(Diot({"a": "$env:X:default:abc"}))
575
+ assert out.a == "abc"
576
+
577
+
578
+ def test_resolve_env_vars_default_env_wins():
579
+ """Found env var wins over the default value."""
580
+ loader = get_loader("dict")
581
+ loader.env_vars = {"X": "1"}
582
+ out = loader._resolve_env_vars(Diot({"a": "$env:X:default:abc"}))
583
+ assert out.a == "1"
584
+
585
+
586
+ def test_resolve_env_vars_default_caster_applied():
587
+ """Default value is cast like an env var (@int:3 -> int 3)."""
588
+ loader = get_loader("dict")
589
+ loader.env_vars = {}
590
+ out = loader._resolve_env_vars(Diot({"a": "$env:X:default:@int:3"}))
591
+ assert out.a == 3
592
+
593
+
594
+ def test_resolve_env_vars_default_with_colons():
595
+ """Default value itself may contain colons."""
596
+ loader = get_loader("dict")
597
+ loader.env_vars = {}
598
+ out = loader._resolve_env_vars(Diot({"a": "$env:URL:default:http://a:1"}))
599
+ assert out.a == "http://a:1"
600
+
601
+
602
+ def test_resolve_env_vars_default_empty():
603
+ """$env:VAR:default: (empty) resolves to '' when not found."""
604
+ loader = get_loader("dict")
605
+ loader.env_vars = {}
606
+ out = loader._resolve_env_vars(Diot({"a": "$env:X:default:"}))
607
+ assert out.a == ""
608
+
609
+
570
610
  def test_resolve_env_vars_recursive():
571
611
  """$env:VAR inside nested dicts resolves correctly."""
572
612
  loader = get_loader("dict")