python-simpleconf 0.9.5__py3-none-any.whl → 0.9.6__py3-none-any.whl

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.
@@ -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:**
@@ -1,10 +1,10 @@
1
- simpleconf/__init__.py,sha256=wrkpYialYF4KkMHvjDPalFW7VZOusyaCU7iGDEa6Gc8,104
1
+ simpleconf/__init__.py,sha256=Oca2Sf9Ql-3Rpg5jW1k_Er8SQ-wWXl8HI0cQ4vUsR-4,104
2
2
  simpleconf/caster.py,sha256=qHnegp1FZzbUWm-UbCaGjQKN6d9rN1v_0Wa4I_CMEi4,2379
3
3
  simpleconf/config.py,sha256=Wf6zCoUs3QGHVFdYTFldcIVk6vmmqnf9bth4Wz0F2Lw,18677
4
4
  simpleconf/exceptions.py,sha256=Tb7w9gMO2XU4mE-Rc2HAnuHWDUpMzdl39oGVjeAv254,171
5
5
  simpleconf/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  simpleconf/utils.py,sha256=JXnmdGd3JE6QDPvL9QSQi_lC07vQLDuIbof6b4idEIA,8856
7
- simpleconf/loaders/__init__.py,sha256=ZuWgb_D8UUwniWKltg7jtjAhEUm0J6sCZrFkXVSSweM,8491
7
+ simpleconf/loaders/__init__.py,sha256=GTWb6Iq1C9YYd_Jb8QgtI3hM-1bLVA4H6jawIRfZ95c,9202
8
8
  simpleconf/loaders/dict.py,sha256=8Az511i9OLLXosSBMMXiEfiQVScQ5SIqsF6EHlAkKdM,801
9
9
  simpleconf/loaders/env.py,sha256=cOYZbHk08MrBjxtNfV8Cv5LHrZg_flzap1lOM3GVdRk,3729
10
10
  simpleconf/loaders/ini.py,sha256=a8ahJIzw24SIEnGbZmYNoxo8O1tYi6pG_GrOil0Rcx0,3739
@@ -12,7 +12,7 @@ simpleconf/loaders/json.py,sha256=0NTKB9JidB8vbeODxNYAY8fJP6__znetuIUAClFeNIo,19
12
12
  simpleconf/loaders/osenv.py,sha256=K829KFiHLl3pS8HSCep8JoPVG_d7VRxPAb7SPYFRypg,1779
13
13
  simpleconf/loaders/toml.py,sha256=OJCukg_R3zlmas_q_CrdeKUsw8Wj5LoSnkNJ7Flt86w,2525
14
14
  simpleconf/loaders/yaml.py,sha256=IoKMaQF5y5HWEjQsnUwL2UvABVj9A-0AmBhn8s7lE54,2146
15
- python_simpleconf-0.9.5.dist-info/METADATA,sha256=7Nj2Ku_RsdJGLpeReGByUurGdh9-dp6eF_yVDQfaBOo,11876
16
- python_simpleconf-0.9.5.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
17
- python_simpleconf-0.9.5.dist-info/licenses/LICENSE,sha256=2j7OLS2vXIEshgbSkiCU9RgYJY1M4OBBtO6ECbo7OB8,1056
18
- python_simpleconf-0.9.5.dist-info/RECORD,,
15
+ python_simpleconf-0.9.6.dist-info/METADATA,sha256=7LEraoirpYCiBQFY2lnIwfCEu4_IC8PvidnCWJK_QJM,12378
16
+ python_simpleconf-0.9.6.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
17
+ python_simpleconf-0.9.6.dist-info/licenses/LICENSE,sha256=2j7OLS2vXIEshgbSkiCU9RgYJY1M4OBBtO6ECbo7OB8,1056
18
+ python_simpleconf-0.9.6.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.31.0
2
+ Generator: hatchling 1.32.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
simpleconf/__init__.py CHANGED
@@ -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 ""