python-simpleconf 0.9.4__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.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:**
@@ -1,10 +1,10 @@
1
- simpleconf/__init__.py,sha256=VOsGsKvRKKpZnFcYYRKxs40KZjm4AfIRgwtOYP5Uk1Y,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
- simpleconf/utils.py,sha256=Oyuthm2RdK2mhhjwDU28LAvDwEh2abRXVGZ4_fx8ecE,8737
7
- simpleconf/loaders/__init__.py,sha256=ZuWgb_D8UUwniWKltg7jtjAhEUm0J6sCZrFkXVSSweM,8491
6
+ simpleconf/utils.py,sha256=JXnmdGd3JE6QDPvL9QSQi_lC07vQLDuIbof6b4idEIA,8856
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.4.dist-info/METADATA,sha256=kPT35p8JYK_3W0hEwmiwhh8Bj1OJQDRKNxqhHeAULeA,11595
16
- python_simpleconf-0.9.4.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
17
- python_simpleconf-0.9.4.dist-info/licenses/LICENSE,sha256=2j7OLS2vXIEshgbSkiCU9RgYJY1M4OBBtO6ECbo7OB8,1056
18
- python_simpleconf-0.9.4.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.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 ""
simpleconf/utils.py CHANGED
@@ -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: