cfgable 0.1.0__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.
- cfgable-0.1.0/.gitignore +25 -0
- cfgable-0.1.0/LICENSE +21 -0
- cfgable-0.1.0/PKG-INFO +143 -0
- cfgable-0.1.0/README.md +116 -0
- cfgable-0.1.0/pyproject.toml +37 -0
- cfgable-0.1.0/src/cfgable/__init__.py +75 -0
- cfgable-0.1.0/src/cfgable/_typing.py +28 -0
- cfgable-0.1.0/src/cfgable/_vendor.py +19 -0
- cfgable-0.1.0/src/cfgable/core.py +393 -0
- cfgable-0.1.0/src/cfgable/enums.py +44 -0
- cfgable-0.1.0/src/cfgable/frozen.py +62 -0
- cfgable-0.1.0/src/cfgable/hydra_utils.py +67 -0
- cfgable-0.1.0/src/cfgable/reflection.py +29 -0
- cfgable-0.1.0/tests/test_core.py +236 -0
- cfgable-0.1.0/tests/test_enums.py +29 -0
- cfgable-0.1.0/tests/test_frozen.py +58 -0
- cfgable-0.1.0/tests/test_hydra.py +35 -0
- cfgable-0.1.0/tests/test_reflection.py +19 -0
cfgable-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
.eggs/
|
|
6
|
+
build/
|
|
7
|
+
dist/
|
|
8
|
+
*.egg
|
|
9
|
+
|
|
10
|
+
# Tooling / caches
|
|
11
|
+
.pytest_cache/
|
|
12
|
+
.mypy_cache/
|
|
13
|
+
.ruff_cache/
|
|
14
|
+
.coverage
|
|
15
|
+
htmlcov/
|
|
16
|
+
|
|
17
|
+
# Virtual environments
|
|
18
|
+
.venv/
|
|
19
|
+
venv/
|
|
20
|
+
env/
|
|
21
|
+
|
|
22
|
+
# Editors / OS
|
|
23
|
+
.idea/
|
|
24
|
+
.vscode/
|
|
25
|
+
.DS_Store
|
cfgable-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OpenGHz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
cfgable-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cfgable
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Reusable pydantic/dataclass-driven configuration framework with an optional Hydra bridge.
|
|
5
|
+
Project-URL: Repository, https://github.com/OpenGHz/cfgable
|
|
6
|
+
Author: OpenGHz
|
|
7
|
+
License: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: config,configuration,hydra,pydantic,settings
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Typing :: Typed
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Requires-Dist: pydantic-yaml
|
|
15
|
+
Requires-Dist: pydantic>=2
|
|
16
|
+
Requires-Dist: typing-extensions
|
|
17
|
+
Provides-Extra: cli
|
|
18
|
+
Requires-Dist: pydantic-settings; extra == 'cli'
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
Requires-Dist: hydra-core; extra == 'dev'
|
|
21
|
+
Requires-Dist: omegaconf; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
23
|
+
Provides-Extra: hydra
|
|
24
|
+
Requires-Dist: hydra-core; extra == 'hydra'
|
|
25
|
+
Requires-Dist: omegaconf; extra == 'hydra'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# cfgable
|
|
29
|
+
|
|
30
|
+
A small, reusable configuration framework for Python, built on
|
|
31
|
+
[pydantic](https://docs.pydantic.dev/). It lets every component declare a **single
|
|
32
|
+
`config` object** and be constructed uniformly — from Python, from a YAML file, or from
|
|
33
|
+
Hydra — with validation happening once at the boundary.
|
|
34
|
+
|
|
35
|
+
The core depends only on pydantic; the Hydra bridge is optional.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install cfgable # core
|
|
41
|
+
pip install "cfgable[hydra]" # + Hydra bridge (hydra-core, omegaconf)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## The idea
|
|
45
|
+
|
|
46
|
+
1. A component's settings are a pydantic model (`*Config`), documented with attribute
|
|
47
|
+
docstrings.
|
|
48
|
+
2. Every class takes **one** `config` parameter and reads fields off it.
|
|
49
|
+
3. Inheriting `ConfigurableBasis` lets the same class be built from an explicit config,
|
|
50
|
+
a plain mapping, or **flat keyword arguments** — the shape Hydra's `instantiate`
|
|
51
|
+
passes — because the `InitConfigMeta` metaclass assembles and validates the config
|
|
52
|
+
for you, then calls `config_post_init()`.
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from typing import Optional
|
|
56
|
+
from pydantic import BaseModel, ConfigDict, PositiveInt
|
|
57
|
+
from cfgable import ConfigurableBasis
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class CameraConfig(BaseModel, frozen=True):
|
|
61
|
+
"""Configuration for a camera."""
|
|
62
|
+
|
|
63
|
+
model_config = ConfigDict(use_attribute_docstrings=True, extra="forbid")
|
|
64
|
+
|
|
65
|
+
device: str = "/dev/video0"
|
|
66
|
+
"""Video device path."""
|
|
67
|
+
fps: PositiveInt = 30
|
|
68
|
+
"""Capture rate in frames per second."""
|
|
69
|
+
serial: Optional[str] = None
|
|
70
|
+
"""Optional camera serial number for disambiguation."""
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class Camera(ConfigurableBasis):
|
|
74
|
+
"""A camera built from a single validated config."""
|
|
75
|
+
|
|
76
|
+
def __init__(self, config: CameraConfig):
|
|
77
|
+
self.config = config
|
|
78
|
+
|
|
79
|
+
def config_post_init(self):
|
|
80
|
+
super().config_post_init()
|
|
81
|
+
self._opened = False
|
|
82
|
+
|
|
83
|
+
def on_configure(self) -> bool:
|
|
84
|
+
self._opened = True
|
|
85
|
+
return True
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# All three build the same object:
|
|
89
|
+
cam = Camera(CameraConfig(fps=60)) # explicit config
|
|
90
|
+
cam = Camera({"fps": 60}) # mapping
|
|
91
|
+
cam = Camera(fps=60) # flat kwargs (Hydra-style)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## With Hydra
|
|
95
|
+
|
|
96
|
+
Point `_target_` at the class and list the config fields as siblings; the metaclass turns
|
|
97
|
+
them into the `config` model:
|
|
98
|
+
|
|
99
|
+
```yaml
|
|
100
|
+
# camera.yaml
|
|
101
|
+
_target_: my_pkg.Camera
|
|
102
|
+
fps: 60
|
|
103
|
+
device: /dev/video2
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from cfgable.hydra_utils import init_hydra_config, hydra_instance
|
|
108
|
+
|
|
109
|
+
cam = hydra_instance(init_hydra_config("camera.yaml"))
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
For plain classes you can also nest the config under its own `_target_` so standard
|
|
113
|
+
`hydra.utils.instantiate` builds `Camera(config=CameraConfig(...))`.
|
|
114
|
+
|
|
115
|
+
## Round-tripping
|
|
116
|
+
|
|
117
|
+
Because a component keeps its whole `config`, it can serialize back to the config that
|
|
118
|
+
would rebuild it:
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
cam.dump() # -> dict of fields + a "_target_" pointing at the class
|
|
122
|
+
cam.save_config("cam.yaml")
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## What's in the box
|
|
126
|
+
|
|
127
|
+
- `ConfigurableBasis`, `InitConfigMixin`, `InitConfigABCMixin` — base classes for the
|
|
128
|
+
single-`config` construction protocol.
|
|
129
|
+
- `InitConfigMeta` / `InitConfigABCMeta` — the metaclass that assembles configs.
|
|
130
|
+
- `NoConfig` — placeholder for components that need no settings.
|
|
131
|
+
- `StrEnum`, `ReprEnum` — a string-enum backport (use this `StrEnum`, not `enum.StrEnum`,
|
|
132
|
+
for consistent behavior across Python 3.9–3.13).
|
|
133
|
+
- `ForceSetAttr` / `force_set_attr` — controlled mutation of otherwise-frozen configs.
|
|
134
|
+
- `import_string`, `get_fully_qualified_class_name`, `dump_or_repr`, `fetch_config`.
|
|
135
|
+
- `cfgable.hydra_utils` — `init_hydra_config`, `hydra_instance`,
|
|
136
|
+
`hydra_instance_from_dict`, `hydra_instance_from_config_path` (needs `[hydra]`).
|
|
137
|
+
|
|
138
|
+
`import cfgable` never imports Hydra — the bridge is loaded only when you import
|
|
139
|
+
`cfgable.hydra_utils`.
|
|
140
|
+
|
|
141
|
+
## License
|
|
142
|
+
|
|
143
|
+
MIT.
|
cfgable-0.1.0/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# cfgable
|
|
2
|
+
|
|
3
|
+
A small, reusable configuration framework for Python, built on
|
|
4
|
+
[pydantic](https://docs.pydantic.dev/). It lets every component declare a **single
|
|
5
|
+
`config` object** and be constructed uniformly — from Python, from a YAML file, or from
|
|
6
|
+
Hydra — with validation happening once at the boundary.
|
|
7
|
+
|
|
8
|
+
The core depends only on pydantic; the Hydra bridge is optional.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install cfgable # core
|
|
14
|
+
pip install "cfgable[hydra]" # + Hydra bridge (hydra-core, omegaconf)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## The idea
|
|
18
|
+
|
|
19
|
+
1. A component's settings are a pydantic model (`*Config`), documented with attribute
|
|
20
|
+
docstrings.
|
|
21
|
+
2. Every class takes **one** `config` parameter and reads fields off it.
|
|
22
|
+
3. Inheriting `ConfigurableBasis` lets the same class be built from an explicit config,
|
|
23
|
+
a plain mapping, or **flat keyword arguments** — the shape Hydra's `instantiate`
|
|
24
|
+
passes — because the `InitConfigMeta` metaclass assembles and validates the config
|
|
25
|
+
for you, then calls `config_post_init()`.
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from typing import Optional
|
|
29
|
+
from pydantic import BaseModel, ConfigDict, PositiveInt
|
|
30
|
+
from cfgable import ConfigurableBasis
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class CameraConfig(BaseModel, frozen=True):
|
|
34
|
+
"""Configuration for a camera."""
|
|
35
|
+
|
|
36
|
+
model_config = ConfigDict(use_attribute_docstrings=True, extra="forbid")
|
|
37
|
+
|
|
38
|
+
device: str = "/dev/video0"
|
|
39
|
+
"""Video device path."""
|
|
40
|
+
fps: PositiveInt = 30
|
|
41
|
+
"""Capture rate in frames per second."""
|
|
42
|
+
serial: Optional[str] = None
|
|
43
|
+
"""Optional camera serial number for disambiguation."""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class Camera(ConfigurableBasis):
|
|
47
|
+
"""A camera built from a single validated config."""
|
|
48
|
+
|
|
49
|
+
def __init__(self, config: CameraConfig):
|
|
50
|
+
self.config = config
|
|
51
|
+
|
|
52
|
+
def config_post_init(self):
|
|
53
|
+
super().config_post_init()
|
|
54
|
+
self._opened = False
|
|
55
|
+
|
|
56
|
+
def on_configure(self) -> bool:
|
|
57
|
+
self._opened = True
|
|
58
|
+
return True
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# All three build the same object:
|
|
62
|
+
cam = Camera(CameraConfig(fps=60)) # explicit config
|
|
63
|
+
cam = Camera({"fps": 60}) # mapping
|
|
64
|
+
cam = Camera(fps=60) # flat kwargs (Hydra-style)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## With Hydra
|
|
68
|
+
|
|
69
|
+
Point `_target_` at the class and list the config fields as siblings; the metaclass turns
|
|
70
|
+
them into the `config` model:
|
|
71
|
+
|
|
72
|
+
```yaml
|
|
73
|
+
# camera.yaml
|
|
74
|
+
_target_: my_pkg.Camera
|
|
75
|
+
fps: 60
|
|
76
|
+
device: /dev/video2
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from cfgable.hydra_utils import init_hydra_config, hydra_instance
|
|
81
|
+
|
|
82
|
+
cam = hydra_instance(init_hydra_config("camera.yaml"))
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
For plain classes you can also nest the config under its own `_target_` so standard
|
|
86
|
+
`hydra.utils.instantiate` builds `Camera(config=CameraConfig(...))`.
|
|
87
|
+
|
|
88
|
+
## Round-tripping
|
|
89
|
+
|
|
90
|
+
Because a component keeps its whole `config`, it can serialize back to the config that
|
|
91
|
+
would rebuild it:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
cam.dump() # -> dict of fields + a "_target_" pointing at the class
|
|
95
|
+
cam.save_config("cam.yaml")
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## What's in the box
|
|
99
|
+
|
|
100
|
+
- `ConfigurableBasis`, `InitConfigMixin`, `InitConfigABCMixin` — base classes for the
|
|
101
|
+
single-`config` construction protocol.
|
|
102
|
+
- `InitConfigMeta` / `InitConfigABCMeta` — the metaclass that assembles configs.
|
|
103
|
+
- `NoConfig` — placeholder for components that need no settings.
|
|
104
|
+
- `StrEnum`, `ReprEnum` — a string-enum backport (use this `StrEnum`, not `enum.StrEnum`,
|
|
105
|
+
for consistent behavior across Python 3.9–3.13).
|
|
106
|
+
- `ForceSetAttr` / `force_set_attr` — controlled mutation of otherwise-frozen configs.
|
|
107
|
+
- `import_string`, `get_fully_qualified_class_name`, `dump_or_repr`, `fetch_config`.
|
|
108
|
+
- `cfgable.hydra_utils` — `init_hydra_config`, `hydra_instance`,
|
|
109
|
+
`hydra_instance_from_dict`, `hydra_instance_from_config_path` (needs `[hydra]`).
|
|
110
|
+
|
|
111
|
+
`import cfgable` never imports Hydra — the bridge is loaded only when you import
|
|
112
|
+
`cfgable.hydra_utils`.
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
MIT.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cfgable"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Reusable pydantic/dataclass-driven configuration framework with an optional Hydra bridge."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [{ name = "OpenGHz" }]
|
|
13
|
+
keywords = ["pydantic", "hydra", "configuration", "config", "settings"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Typing :: Typed",
|
|
18
|
+
]
|
|
19
|
+
dependencies = [
|
|
20
|
+
"pydantic >=2",
|
|
21
|
+
"typing-extensions",
|
|
22
|
+
"pydantic-yaml",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
hydra = ["hydra-core", "omegaconf"]
|
|
27
|
+
cli = ["pydantic-settings"]
|
|
28
|
+
dev = ["pytest", "hydra-core", "omegaconf"]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Repository = "https://github.com/OpenGHz/cfgable"
|
|
32
|
+
|
|
33
|
+
[tool.hatch.build.targets.wheel]
|
|
34
|
+
packages = ["src/cfgable"]
|
|
35
|
+
|
|
36
|
+
[tool.pytest.ini_options]
|
|
37
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""cfgable: a reusable pydantic/dataclass-driven configuration framework.
|
|
2
|
+
|
|
3
|
+
Components declare a single ``config`` parameter and (optionally) inherit
|
|
4
|
+
``ConfigurableBasis``; the ``InitConfigMeta`` metaclass assembles that config from
|
|
5
|
+
explicit objects, plain mappings, or flat keyword arguments (the shape Hydra's
|
|
6
|
+
``instantiate`` passes), validating it once at construction.
|
|
7
|
+
|
|
8
|
+
The core exported here depends only on pydantic. The optional Hydra bridge lives
|
|
9
|
+
in ``cfgable.hydra_utils`` and is imported on demand, so ``import cfgable``
|
|
10
|
+
never pulls in hydra/omegaconf.
|
|
11
|
+
|
|
12
|
+
NOTE: do NOT add ``from .hydra_utils import ...`` to this module — it would make
|
|
13
|
+
hydra a hard dependency of the core. Import it explicitly where you need it.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from ._typing import (
|
|
17
|
+
AllConfigType,
|
|
18
|
+
BaseModelT,
|
|
19
|
+
ConfigType,
|
|
20
|
+
DataClassProto,
|
|
21
|
+
OtherCfgType,
|
|
22
|
+
T,
|
|
23
|
+
)
|
|
24
|
+
from .core import (
|
|
25
|
+
ConfigurableBasis,
|
|
26
|
+
InitConfigABCMeta,
|
|
27
|
+
InitConfigABCMixin,
|
|
28
|
+
InitConfigMeta,
|
|
29
|
+
InitConfigMixin,
|
|
30
|
+
InitConfigMixinBasis,
|
|
31
|
+
NoConfig,
|
|
32
|
+
dump_omegaconf,
|
|
33
|
+
dump_or_repr,
|
|
34
|
+
fetch_config,
|
|
35
|
+
)
|
|
36
|
+
from .enums import ReprEnum, StrEnum
|
|
37
|
+
from .frozen import ForceSetAttr, force_set_attr, force_validate_field, validate_field
|
|
38
|
+
from .reflection import (
|
|
39
|
+
get_full_class_name,
|
|
40
|
+
get_fully_qualified_class_name,
|
|
41
|
+
import_string,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
__all__ = [
|
|
45
|
+
# typing
|
|
46
|
+
"ConfigType",
|
|
47
|
+
"AllConfigType",
|
|
48
|
+
"OtherCfgType",
|
|
49
|
+
"DataClassProto",
|
|
50
|
+
"BaseModelT",
|
|
51
|
+
"T",
|
|
52
|
+
# core
|
|
53
|
+
"NoConfig",
|
|
54
|
+
"InitConfigMeta",
|
|
55
|
+
"InitConfigABCMeta",
|
|
56
|
+
"InitConfigMixinBasis",
|
|
57
|
+
"InitConfigMixin",
|
|
58
|
+
"InitConfigABCMixin",
|
|
59
|
+
"ConfigurableBasis",
|
|
60
|
+
"dump_or_repr",
|
|
61
|
+
"dump_omegaconf",
|
|
62
|
+
"fetch_config",
|
|
63
|
+
# enums
|
|
64
|
+
"StrEnum",
|
|
65
|
+
"ReprEnum",
|
|
66
|
+
# frozen
|
|
67
|
+
"ForceSetAttr",
|
|
68
|
+
"force_set_attr",
|
|
69
|
+
"validate_field",
|
|
70
|
+
"force_validate_field",
|
|
71
|
+
# reflection
|
|
72
|
+
"import_string",
|
|
73
|
+
"get_fully_qualified_class_name",
|
|
74
|
+
"get_full_class_name",
|
|
75
|
+
]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Shared typing primitives for the cfgable framework."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any, Dict, Optional, Protocol, Type, TypeVar, Union
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
from typing_extensions import runtime_checkable
|
|
8
|
+
|
|
9
|
+
BaseModelT = TypeVar("BaseModelT", bound=BaseModel)
|
|
10
|
+
T = TypeVar("T")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@runtime_checkable
|
|
14
|
+
class DataClassProto(Protocol):
|
|
15
|
+
"""Protocol for dataclass types."""
|
|
16
|
+
|
|
17
|
+
@classmethod
|
|
18
|
+
def __dataclass_fields__(cls) -> Dict[str, Any]: ...
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# A config is a pydantic model or a dataclass instance.
|
|
22
|
+
ConfigType = Union[BaseModel, DataClassProto]
|
|
23
|
+
# Things that can be *resolved into* a config: a config type, a mapping of
|
|
24
|
+
# fields, or a path/dotted-string pointing at one.
|
|
25
|
+
OtherCfgType = Optional[
|
|
26
|
+
Union[Type[Union[DataClassProto, BaseModel]], Dict[str, Any], str, Path]
|
|
27
|
+
]
|
|
28
|
+
AllConfigType = Union[ConfigType, OtherCfgType]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Small vendored helpers, kept here to avoid extra runtime dependencies."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def get_in(keys, coll, default=None, no_default=False):
|
|
5
|
+
"""Return ``coll[k0][k1][...]`` following the sequence ``keys``.
|
|
6
|
+
|
|
7
|
+
Vendored equivalent of ``toolz.dicttoolz.get_in``. On a missing key/index
|
|
8
|
+
(``KeyError``, ``IndexError``, ``TypeError``) return ``default`` — unless
|
|
9
|
+
``no_default`` is set, in which case the original exception propagates.
|
|
10
|
+
"""
|
|
11
|
+
try:
|
|
12
|
+
out = coll
|
|
13
|
+
for key in keys:
|
|
14
|
+
out = out[key]
|
|
15
|
+
return out
|
|
16
|
+
except (KeyError, IndexError, TypeError):
|
|
17
|
+
if no_default:
|
|
18
|
+
raise
|
|
19
|
+
return default
|