@@ -118,11 +118,19 @@ def config(
118
118
"""
119
119
120
120
def wrap (cls ):
121
- def from_environ_fnc (cls , environ = os .environ ):
122
- return __to_config (cls , environ )
123
-
124
- def generate_help_fnc (cls , ** kwargs ):
125
- return __generate_help (cls , ** kwargs )
121
+ def from_environ_fnc (
122
+ cls , environ = os .environ , * , prefix = PREFIX_NOT_SET
123
+ ):
124
+ return __to_config (cls , environ , prefix = prefix )
125
+
126
+ def generate_help_fnc (
127
+ cls ,
128
+ formatter = None ,
129
+ * ,
130
+ prefix = PREFIX_NOT_SET ,
131
+ ** kwargs ,
132
+ ):
133
+ return __generate_help (cls , formatter , prefix = prefix , ** kwargs )
126
134
127
135
cls ._prefix = prefix
128
136
if from_environ is not None :
@@ -168,7 +176,7 @@ def var(
168
176
169
177
converter:
170
178
A callable that is run with the found value and its return value is
171
- used. Please not that it is also run for default values.
179
+ used. Please note that it is also run for default values.
172
180
173
181
validator:
174
182
A callable that is run with the final value. See *attrs*'s `chapter
@@ -256,12 +264,14 @@ class Sub:
256
264
)
257
265
258
266
259
- def _default_getter (environ , metadata , prefix , name ):
267
+ def _default_getter (environ , metadata , prefixes , name ):
260
268
"""
261
269
This default lookup implementation simply gets values from *environ*.
262
270
"""
263
271
ce = metadata [CNF_KEY ]
264
- var = ce .name if ce .name is not None else "_" .join ((* prefix , name )).upper ()
272
+ var = (
273
+ ce .name if ce .name is not None else "_" .join ((* prefixes , name )).upper ()
274
+ )
265
275
log .debug ("looking for env var '%s'." , var )
266
276
try :
267
277
return environ [var ]
@@ -328,24 +338,46 @@ def _to_config_recurse(config_cls, environ, prefixes, default=RAISE):
328
338
return config_cls (** defaulted )
329
339
330
340
331
- def to_config (config_cls : type [T ], environ : dict [str , str ] = os .environ ) -> T :
341
+ def to_config (
342
+ config_cls : type [T ],
343
+ environ : dict [str , str ] = os .environ ,
344
+ * ,
345
+ prefix : str | Sentinel = PREFIX_NOT_SET ,
346
+ ) -> T :
332
347
"""
333
348
Load the configuration as declared by *config_cls* from *environ*.
334
349
335
350
Args:
336
351
config_cls: The configuration class to fill.
337
352
338
- environ: Source of the configuration. `os.environ` by default.
353
+ environ: Source of the configuration. `os.environ` by default.
354
+
355
+ prefix:
356
+ Prefix that is used for environment variables. May be used to
357
+ dynamically change a prefix or to instantiate a nested
358
+ configuration class without all of its parent configuration
359
+ classes.
339
360
340
361
Returns:
341
362
An instance of *config_cls*.
342
363
364
+ Examples:
365
+
366
+ .. code-block:: python
367
+
368
+ environ.to_config(ComponentConfig, prefix="APP_COMPONENT")
369
+
343
370
This is equivalent to calling ``config_cls.from_environ()``.
371
+
372
+ .. versionadded:: 24.2.0 *prefix*
344
373
"""
345
374
# The canonical app prefix might be falsey in which case we'll still set
346
375
# the default prefix for this top level config object
347
- app_prefix = tuple (p for p in (_get_prefix (config_cls ),) if p )
348
- return _to_config_recurse (config_cls , environ , app_prefix )
376
+ if prefix is PREFIX_NOT_SET :
377
+ prefix = _get_prefix (config_cls )
378
+
379
+ prefixes = (prefix ,) if prefix else ()
380
+ return _to_config_recurse (config_cls , environ , prefixes )
349
381
350
382
351
383
def _format_help_dicts (help_dicts , display_defaults = False ):
@@ -457,7 +489,11 @@ def _generate_help_dicts(config_cls, _prefix=PREFIX_NOT_SET):
457
489
458
490
459
491
def generate_help (
460
- config_cls : type [T ], formatter : Callable | None = None , ** kwargs : Any
492
+ config_cls : type [T ],
493
+ formatter : Callable | None = None ,
494
+ * ,
495
+ prefix : str | Sentinel = PREFIX_NOT_SET ,
496
+ ** kwargs : Any ,
461
497
) -> str :
462
498
"""
463
499
Autogenerate a help string for a config class.
@@ -472,16 +508,29 @@ def generate_help(
472
508
When using the default formatter, passing `True` for
473
509
*display_defaults* makes the default values part of the output.
474
510
511
+ prefix:
512
+ Prefix that is used for environment variables. May be used to
513
+ dynamically change a prefix or to generate a help string for a
514
+ nested configuration class without all of its parent configuration
515
+ classes.
516
+
475
517
Returns:
476
518
A help string that can be printed to the user.
477
519
520
+ Examples:
521
+
522
+ .. code-block:: python
523
+
524
+ environ.generate_help(ComponentConfig, prefix="APP_COMPONENT")
525
+
478
526
This is equivalent to calling ``config_cls.generate_help()``.
479
527
480
528
.. versionadded:: 19.1.0
529
+ .. versionadded:: 24.2.0 *prefix*
481
530
"""
482
531
if formatter is None :
483
532
formatter = _format_help_dicts
484
- help_dicts = _generate_help_dicts (config_cls )
533
+ help_dicts = _generate_help_dicts (config_cls , prefix )
485
534
486
535
return formatter (help_dicts , ** kwargs )
487
536
0 commit comments