python-simpleconf 0.9.5__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.5 → python_simpleconf-0.9.6}/PKG-INFO +7 -2
  2. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/README.md +5 -0
  3. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/docs/CHANGELOG.md +4 -0
  4. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/__init__.py +1 -1
  5. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/loaders/__init__.py +15 -1
  6. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/tests/test_config.py +15 -0
  7. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/tests/test_loaders.py +40 -0
  8. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/.coveragerc +0 -0
  9. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/.github/workflows/build.yml +0 -0
  10. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/.gitignore +0 -0
  11. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/.pre-commit-config.yaml +0 -0
  12. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/LICENSE +0 -0
  13. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/Makefile +0 -0
  14. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/pyproject.toml +0 -0
  15. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/setup.py +0 -0
  16. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/caster.py +0 -0
  17. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/config.py +0 -0
  18. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/exceptions.py +0 -0
  19. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/loaders/dict.py +0 -0
  20. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/loaders/env.py +0 -0
  21. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/loaders/ini.py +0 -0
  22. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/loaders/json.py +0 -0
  23. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/loaders/osenv.py +0 -0
  24. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/loaders/toml.py +0 -0
  25. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/loaders/yaml.py +0 -0
  26. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/py.typed +0 -0
  27. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/simpleconf/utils.py +0 -0
  28. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/tests/__init__.py +0 -0
  29. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/tests/fixt_simpleconf.py +0 -0
  30. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/tests/test_caster.py +0 -0
  31. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/tests/test_utils.py +0 -0
  32. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/tox.ini +0 -0
  33. {python_simpleconf-0.9.5 → python_simpleconf-0.9.6}/uv.lock +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: python-simpleconf
3
- Version: 0.9.5
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
@@ -467,6 +467,9 @@ The `$env:` syntax supports optional modifiers to control the behavior when a va
467
467
  | `$env:VAR:required` | Explicit form — raises `ValueError` |
468
468
  | `$env:VAR:optional-asis` | Keeps the literal `$env:VAR` string unchanged |
469
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`).
470
473
 
471
474
  **Example — using modifiers:**
472
475
 
@@ -482,6 +485,7 @@ db:
482
485
  host: $env:DB_HOST
483
486
  port: $env:DB_PORT:optional-asis # keeps "$env:DB_PORT" as-is when not found
484
487
  timeout: $env:TIMEOUT:optional-empty # becomes "" when not found
488
+ user: $env:DB_USER:default:admin # becomes "admin" when not found
485
489
  ```
486
490
 
487
491
  ```python
@@ -491,6 +495,7 @@ conf = Config.load('config.yaml')
491
495
  # conf.db.host == 'localhost'
492
496
  # conf.db.port == '$env:DB_PORT'
493
497
  # conf.db.timeout == ''
498
+ # conf.db.user == 'admin'
494
499
  ```
495
500
 
496
501
  **Example — using `env` in templates:**
@@ -430,6 +430,9 @@ The `$env:` syntax supports optional modifiers to control the behavior when a va
430
430
  | `$env:VAR:required` | Explicit form — raises `ValueError` |
431
431
  | `$env:VAR:optional-asis` | Keeps the literal `$env:VAR` string unchanged |
432
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`).
433
436
 
434
437
  **Example — using modifiers:**
435
438
 
@@ -445,6 +448,7 @@ db:
445
448
  host: $env:DB_HOST
446
449
  port: $env:DB_PORT:optional-asis # keeps "$env:DB_PORT" as-is when not found
447
450
  timeout: $env:TIMEOUT:optional-empty # becomes "" when not found
451
+ user: $env:DB_USER:default:admin # becomes "admin" when not found
448
452
  ```
449
453
 
450
454
  ```python
@@ -454,6 +458,7 @@ conf = Config.load('config.yaml')
454
458
  # conf.db.host == 'localhost'
455
459
  # conf.db.port == '$env:DB_PORT'
456
460
  # conf.db.timeout == ''
461
+ # conf.db.user == 'admin'
457
462
  ```
458
463
 
459
464
  **Example — using `env` in templates:**
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.9.6
4
+
5
+ - feat: add support for default values in environment variable resolution
6
+
3
7
  ## 0.9.5
4
8
 
5
9
  - fix: fix error for _read_first_lines when file name is too long
@@ -2,4 +2,4 @@ from .config import Config, ProfileConfig
2
2
 
3
3
  __all__ = ["Config", "ProfileConfig"]
4
4
 
5
- __version__ = "0.9.5"
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 ""
@@ -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")