|
| 1 | +from inspect import isfunction |
1 | 2 | from pathlib import Path
|
2 |
| -from typing import Any, Dict, Optional, Tuple, TypeVar, Union |
| 3 | +from typing import Any, Callable, Dict, Optional, Tuple, TypeVar, Union |
3 | 4 |
|
4 | 5 | from pydantic import BaseModel, Extra
|
5 | 6 | from typing_extensions import Self
|
6 | 7 |
|
7 | 8 | from easyconfig.__const__ import MISSING, MISSING_TYPE
|
8 |
| -from .object_config import ConfigObj |
9 | 9 | from easyconfig.yaml import cmap_from_model, CommentedMap, write_aligned_yaml, yaml_rt
|
10 | 10 |
|
| 11 | +from .object_config import ConfigObj |
| 12 | + |
11 | 13 |
|
12 | 14 | class AppConfig(ConfigObj):
|
13 | 15 | def __init__(self, model: BaseModel, path: Tuple[str, ...] = ('__root__',),
|
@@ -59,13 +61,18 @@ def load_config_file(self, path: Union[Path, str] = None):
|
59 | 61 |
|
60 | 62 |
|
61 | 63 | def create_app_config(model: TYPE_WRAPPED,
|
62 |
| - file_values: Union[MISSING_TYPE, None, BaseModel, Dict[str, Any]] = MISSING, |
| 64 | + file_values: Union[MISSING_TYPE, None, BaseModel, Dict[str, Any], |
| 65 | + Callable[[], Union[BaseModel, Dict[str, Any]]]] = MISSING, |
63 | 66 | validate_file_values=True) -> TYPE_WRAPPED:
|
64 | 67 |
|
65 | 68 | # Implicit default
|
66 | 69 | if file_values is MISSING:
|
67 | 70 | file_values = model
|
68 | 71 |
|
| 72 | + # if it's a callback we get the values |
| 73 | + if isfunction(file_values): |
| 74 | + file_values = file_values() |
| 75 | + |
69 | 76 | # Validate default
|
70 | 77 | if file_values is not None:
|
71 | 78 | if isinstance(file_values, dict):
|
|
0 commit comments