tidyconf 1.0.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.
- tidyconf-1.0.0/.gitignore +140 -0
- tidyconf-1.0.0/LICENSE +21 -0
- tidyconf-1.0.0/PKG-INFO +220 -0
- tidyconf-1.0.0/README.md +196 -0
- tidyconf-1.0.0/pyproject.toml +60 -0
- tidyconf-1.0.0/tests/resources/base.json +5 -0
- tidyconf-1.0.0/tests/resources/base.toml +11 -0
- tidyconf-1.0.0/tests/resources/base.yaml +11 -0
- tidyconf-1.0.0/tests/resources/override.yml +6 -0
- tidyconf-1.0.0/tests/test_config.py +171 -0
- tidyconf-1.0.0/tests/test_env.py +180 -0
- tidyconf-1.0.0/tests/test_loading.py +195 -0
- tidyconf-1.0.0/tidyconf/__init__.py +8 -0
- tidyconf-1.0.0/tidyconf/config.py +205 -0
- tidyconf-1.0.0/tidyconf/exceptions.py +13 -0
- tidyconf-1.0.0/tidyconf/loaders.py +51 -0
- tidyconf-1.0.0/tidyconf/py.typed +0 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
98
|
+
__pypackages__/
|
|
99
|
+
|
|
100
|
+
# Celery stuff
|
|
101
|
+
celerybeat-schedule
|
|
102
|
+
celerybeat.pid
|
|
103
|
+
|
|
104
|
+
# SageMath parsed files
|
|
105
|
+
*.sage.py
|
|
106
|
+
|
|
107
|
+
# Environments
|
|
108
|
+
.env
|
|
109
|
+
.venv
|
|
110
|
+
env/
|
|
111
|
+
venv/
|
|
112
|
+
ENV/
|
|
113
|
+
env.bak/
|
|
114
|
+
venv.bak/
|
|
115
|
+
|
|
116
|
+
# Spyder project settings
|
|
117
|
+
.spyderproject
|
|
118
|
+
.spyproject
|
|
119
|
+
|
|
120
|
+
# Rope project settings
|
|
121
|
+
.ropeproject
|
|
122
|
+
|
|
123
|
+
# mkdocs documentation
|
|
124
|
+
/site
|
|
125
|
+
|
|
126
|
+
# mypy
|
|
127
|
+
.mypy_cache/
|
|
128
|
+
.dmypy.json
|
|
129
|
+
dmypy.json
|
|
130
|
+
|
|
131
|
+
# Pyre type checker
|
|
132
|
+
.pyre/
|
|
133
|
+
|
|
134
|
+
# pytype static type analyzer
|
|
135
|
+
.pytype/
|
|
136
|
+
.vscode/
|
|
137
|
+
# Cython debug symbols
|
|
138
|
+
cython_debug/
|
|
139
|
+
# IDE
|
|
140
|
+
.idea/
|
tidyconf-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 pgoelter
|
|
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.
|
tidyconf-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: tidyconf
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Simple, typed configuration for small apps: JSON/TOML/YAML files, layering and env overrides.
|
|
5
|
+
Project-URL: Homepage, https://github.com/pgoelter/tidyconf
|
|
6
|
+
Project-URL: Source, https://github.com/pgoelter/tidyconf
|
|
7
|
+
Project-URL: Issues, https://github.com/pgoelter/tidyconf/issues
|
|
8
|
+
Author: Philipp Gölter
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: config,configuration,env,json,settings,toml,yaml
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Provides-Extra: yaml
|
|
22
|
+
Requires-Dist: pyyaml>=6; extra == 'yaml'
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
[](https://github.com/pgoelter/tidyconf/actions/workflows/main.yml)
|
|
26
|
+
|
|
27
|
+
# tidyconf
|
|
28
|
+
|
|
29
|
+
Simple configuration for small, self-hosted apps. Load settings from JSON, TOML or
|
|
30
|
+
YAML files, layer a local override file on top of the defaults, let environment
|
|
31
|
+
variables override single values, and read everything through dotted routes like
|
|
32
|
+
`config["database.port"]`. Fully typed, Python 3.11+, no dependencies (PyYAML only
|
|
33
|
+
if you use YAML files).
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
pip install tidyconf # JSON and TOML
|
|
39
|
+
pip install "tidyconf[yaml]" # plus YAML support (PyYAML)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
`config.toml` (defaults, committed to the repo):
|
|
45
|
+
|
|
46
|
+
```toml
|
|
47
|
+
debug = false
|
|
48
|
+
|
|
49
|
+
[server]
|
|
50
|
+
host = "127.0.0.1"
|
|
51
|
+
port = 8000
|
|
52
|
+
|
|
53
|
+
[database]
|
|
54
|
+
url = "sqlite:///app.db"
|
|
55
|
+
pool_size = 5
|
|
56
|
+
|
|
57
|
+
[features]
|
|
58
|
+
enabled = ["search"]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`config.local.yml` (optional, per machine, not committed):
|
|
62
|
+
|
|
63
|
+
```yaml
|
|
64
|
+
server:
|
|
65
|
+
host: 0.0.0.0
|
|
66
|
+
database:
|
|
67
|
+
url: postgresql://localhost/myapp
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Environment (e.g. set by Docker or systemd):
|
|
71
|
+
|
|
72
|
+
```sh
|
|
73
|
+
export MYAPP_SERVER__PORT=9000
|
|
74
|
+
export MYAPP_DEBUG=true
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Load and read:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from tidyconf import Config, optional
|
|
81
|
+
|
|
82
|
+
config = Config.load(
|
|
83
|
+
"config.toml",
|
|
84
|
+
optional("config.local.yml"), # skipped if the file does not exist
|
|
85
|
+
env_prefix="MYAPP",
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
config["server.host"] # '0.0.0.0' (from config.local.yml)
|
|
89
|
+
config["server.port"] # 9000 (from env, converted to int)
|
|
90
|
+
config.get("debug") # True (from env, converted to bool)
|
|
91
|
+
config.get("database") # {'url': 'postgresql://localhost/myapp', 'pool_size': 5}
|
|
92
|
+
config.get("cache.ttl", default=60) # 60 (missing key, default used)
|
|
93
|
+
config["cache.ttl"] # raises KeyNotFoundError
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Sharing one config across your app
|
|
97
|
+
|
|
98
|
+
There is no global singleton. Load the config once in a module and import it
|
|
99
|
+
wherever you need it; Python's module cache makes it a single shared object:
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
# myapp/settings.py
|
|
103
|
+
from pathlib import Path
|
|
104
|
+
|
|
105
|
+
from tidyconf import Config, optional
|
|
106
|
+
|
|
107
|
+
BASE_DIR = Path(__file__).resolve().parent
|
|
108
|
+
|
|
109
|
+
config = Config.load(
|
|
110
|
+
BASE_DIR / "config.toml",
|
|
111
|
+
optional(BASE_DIR / "config.local.yml"),
|
|
112
|
+
env_prefix="MYAPP",
|
|
113
|
+
)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
# myapp/server.py
|
|
118
|
+
from myapp.settings import config
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def run() -> None:
|
|
122
|
+
serve(host=config["server.host"], port=config["server.port"])
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Relative paths are resolved against the current working directory, so building
|
|
126
|
+
them from `__file__` as above keeps loading independent of where the app is started.
|
|
127
|
+
|
|
128
|
+
A runnable version of this setup, a tiny web server, lives in
|
|
129
|
+
[`examples/app.py`](examples/app.py): run `python examples/app.py` and try
|
|
130
|
+
`HELLO_SERVER__PORT=9000`.
|
|
131
|
+
|
|
132
|
+
## API
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
from tidyconf import Config, optional, ConfigError, KeyNotFoundError
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
| API | Description |
|
|
139
|
+
| --- | --- |
|
|
140
|
+
| `Config(data=None)` | Create a config from a mapping (deep-copied). Handy in tests. |
|
|
141
|
+
| `Config.load(*sources, env_prefix=None)` | Load and deep-merge files in order (later wins), then apply env overrides if `env_prefix` is given. Env variables can only override keys defined in the files. |
|
|
142
|
+
| `optional(path)` | Mark a source file as optional: skipped if it does not exist. |
|
|
143
|
+
| `config.get(route, default=...)` | Value at a dotted route such as `"server.port"`. Returns a deep copy. Raises `KeyNotFoundError` if missing, unless `default` is given. |
|
|
144
|
+
| `config[route]` | Same as `config.get(route)` without a default. |
|
|
145
|
+
| `route in config` | Whether the route exists, e.g. `"database.port" in config`. Also `True` for keys set to `None`. |
|
|
146
|
+
| `config.set(route, value)` | Set a value, creating intermediate sections as needed. Raises `TypeError` if a part of the route is not a mapping. |
|
|
147
|
+
| `config.reset()` | Restore the state at construction / `load()` time. |
|
|
148
|
+
| `config.to_dict()` | Deep copy of all data as a plain `dict`. |
|
|
149
|
+
|
|
150
|
+
Because `get()` and `to_dict()` return copies, mutating a returned value never
|
|
151
|
+
changes the config; use `set()` for that.
|
|
152
|
+
|
|
153
|
+
File formats are chosen by suffix (case-insensitive): `.json`, `.toml`, `.yaml` / `.yml`.
|
|
154
|
+
Files are read as UTF-8 and the top level must be a mapping. An empty YAML file
|
|
155
|
+
counts as an empty mapping.
|
|
156
|
+
|
|
157
|
+
### Errors
|
|
158
|
+
|
|
159
|
+
| Exception | When |
|
|
160
|
+
| --- | --- |
|
|
161
|
+
| `ConfigError` | Unsupported file type, parse error, top level not a mapping, YAML file without PyYAML installed, invalid env override. |
|
|
162
|
+
| `KeyNotFoundError` | Route not found in `get()` / `[]`. Subclass of both `ConfigError` and `KeyError`. |
|
|
163
|
+
| `FileNotFoundError` | A required (non-`optional`) source file does not exist. |
|
|
164
|
+
| `TypeError` | `set()` would have to go through a value that is not a mapping. |
|
|
165
|
+
|
|
166
|
+
## Layering rules
|
|
167
|
+
|
|
168
|
+
- Sources are merged left to right; later sources win.
|
|
169
|
+
- If both sides have a mapping for the same key, they are merged recursively.
|
|
170
|
+
Any other value (including lists) is replaced as a whole, not combined.
|
|
171
|
+
- Environment overrides are applied last, on top of the merged files.
|
|
172
|
+
- `reset()` returns to this fully loaded state.
|
|
173
|
+
|
|
174
|
+
## Environment variables
|
|
175
|
+
|
|
176
|
+
With `env_prefix="MYAPP"`, every environment variable starting with `MYAPP_` is an
|
|
177
|
+
override:
|
|
178
|
+
|
|
179
|
+
- The prefix is matched case-insensitively.
|
|
180
|
+
- The rest of the name is split on double underscores into a route:
|
|
181
|
+
`MYAPP_DATABASE__POOL_SIZE` sets `database.pool_size`. Single underscores are part
|
|
182
|
+
of the key.
|
|
183
|
+
- Each part matches an existing key case-insensitively.
|
|
184
|
+
- **The key must already exist in your config files.** Environment variables only
|
|
185
|
+
override values, they never add new ones. An unknown variable such as
|
|
186
|
+
`MYAPP_DATABSE__PORT` (note the typo) raises `ConfigError` at load time instead of
|
|
187
|
+
being silently ignored. Give every setting a default in your config file; for
|
|
188
|
+
secrets, use an empty placeholder like `password = ""`.
|
|
189
|
+
- Variables are applied in sorted order of their names.
|
|
190
|
+
|
|
191
|
+
Values are converted based on the type of the value that already exists at that
|
|
192
|
+
route:
|
|
193
|
+
|
|
194
|
+
| Existing value | Env value | Result |
|
|
195
|
+
| --- | --- | --- |
|
|
196
|
+
| `bool` | `true` / `1` / `yes` / `on`, `false` / `0` / `no` / `off` (any case) | `True` / `False` |
|
|
197
|
+
| `int` | `"9000"` | `9000` |
|
|
198
|
+
| `float` | `"0.5"` | `0.5` |
|
|
199
|
+
| `list` | JSON array, e.g. `'["search", "export"]'` | `["search", "export"]` |
|
|
200
|
+
| mapping (section) | anything | `ConfigError`: a whole section cannot be overridden |
|
|
201
|
+
| `str` or `None` | anything | the plain string |
|
|
202
|
+
|
|
203
|
+
A value that cannot be converted raises `ConfigError` naming the environment
|
|
204
|
+
variable, as does a route that runs through a non-mapping value.
|
|
205
|
+
|
|
206
|
+
### Limitations
|
|
207
|
+
|
|
208
|
+
- Keys containing `.` cannot be reached with dotted routes (no escaping).
|
|
209
|
+
- Keys containing `.` or `-` cannot be set from environment variables: shells and
|
|
210
|
+
most tools do not allow those characters in variable names.
|
|
211
|
+
- An environment variable can only replace a single value, not a whole section or a
|
|
212
|
+
single list element.
|
|
213
|
+
|
|
214
|
+
## Development
|
|
215
|
+
|
|
216
|
+
See [CLAUDE.md](CLAUDE.md) for setup, tests, linting and project conventions.
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
MIT, see [LICENSE](LICENSE).
|
tidyconf-1.0.0/README.md
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
[](https://github.com/pgoelter/tidyconf/actions/workflows/main.yml)
|
|
2
|
+
|
|
3
|
+
# tidyconf
|
|
4
|
+
|
|
5
|
+
Simple configuration for small, self-hosted apps. Load settings from JSON, TOML or
|
|
6
|
+
YAML files, layer a local override file on top of the defaults, let environment
|
|
7
|
+
variables override single values, and read everything through dotted routes like
|
|
8
|
+
`config["database.port"]`. Fully typed, Python 3.11+, no dependencies (PyYAML only
|
|
9
|
+
if you use YAML files).
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pip install tidyconf # JSON and TOML
|
|
15
|
+
pip install "tidyconf[yaml]" # plus YAML support (PyYAML)
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
`config.toml` (defaults, committed to the repo):
|
|
21
|
+
|
|
22
|
+
```toml
|
|
23
|
+
debug = false
|
|
24
|
+
|
|
25
|
+
[server]
|
|
26
|
+
host = "127.0.0.1"
|
|
27
|
+
port = 8000
|
|
28
|
+
|
|
29
|
+
[database]
|
|
30
|
+
url = "sqlite:///app.db"
|
|
31
|
+
pool_size = 5
|
|
32
|
+
|
|
33
|
+
[features]
|
|
34
|
+
enabled = ["search"]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`config.local.yml` (optional, per machine, not committed):
|
|
38
|
+
|
|
39
|
+
```yaml
|
|
40
|
+
server:
|
|
41
|
+
host: 0.0.0.0
|
|
42
|
+
database:
|
|
43
|
+
url: postgresql://localhost/myapp
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Environment (e.g. set by Docker or systemd):
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
export MYAPP_SERVER__PORT=9000
|
|
50
|
+
export MYAPP_DEBUG=true
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Load and read:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from tidyconf import Config, optional
|
|
57
|
+
|
|
58
|
+
config = Config.load(
|
|
59
|
+
"config.toml",
|
|
60
|
+
optional("config.local.yml"), # skipped if the file does not exist
|
|
61
|
+
env_prefix="MYAPP",
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
config["server.host"] # '0.0.0.0' (from config.local.yml)
|
|
65
|
+
config["server.port"] # 9000 (from env, converted to int)
|
|
66
|
+
config.get("debug") # True (from env, converted to bool)
|
|
67
|
+
config.get("database") # {'url': 'postgresql://localhost/myapp', 'pool_size': 5}
|
|
68
|
+
config.get("cache.ttl", default=60) # 60 (missing key, default used)
|
|
69
|
+
config["cache.ttl"] # raises KeyNotFoundError
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Sharing one config across your app
|
|
73
|
+
|
|
74
|
+
There is no global singleton. Load the config once in a module and import it
|
|
75
|
+
wherever you need it; Python's module cache makes it a single shared object:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
# myapp/settings.py
|
|
79
|
+
from pathlib import Path
|
|
80
|
+
|
|
81
|
+
from tidyconf import Config, optional
|
|
82
|
+
|
|
83
|
+
BASE_DIR = Path(__file__).resolve().parent
|
|
84
|
+
|
|
85
|
+
config = Config.load(
|
|
86
|
+
BASE_DIR / "config.toml",
|
|
87
|
+
optional(BASE_DIR / "config.local.yml"),
|
|
88
|
+
env_prefix="MYAPP",
|
|
89
|
+
)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
# myapp/server.py
|
|
94
|
+
from myapp.settings import config
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def run() -> None:
|
|
98
|
+
serve(host=config["server.host"], port=config["server.port"])
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Relative paths are resolved against the current working directory, so building
|
|
102
|
+
them from `__file__` as above keeps loading independent of where the app is started.
|
|
103
|
+
|
|
104
|
+
A runnable version of this setup, a tiny web server, lives in
|
|
105
|
+
[`examples/app.py`](examples/app.py): run `python examples/app.py` and try
|
|
106
|
+
`HELLO_SERVER__PORT=9000`.
|
|
107
|
+
|
|
108
|
+
## API
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from tidyconf import Config, optional, ConfigError, KeyNotFoundError
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
| API | Description |
|
|
115
|
+
| --- | --- |
|
|
116
|
+
| `Config(data=None)` | Create a config from a mapping (deep-copied). Handy in tests. |
|
|
117
|
+
| `Config.load(*sources, env_prefix=None)` | Load and deep-merge files in order (later wins), then apply env overrides if `env_prefix` is given. Env variables can only override keys defined in the files. |
|
|
118
|
+
| `optional(path)` | Mark a source file as optional: skipped if it does not exist. |
|
|
119
|
+
| `config.get(route, default=...)` | Value at a dotted route such as `"server.port"`. Returns a deep copy. Raises `KeyNotFoundError` if missing, unless `default` is given. |
|
|
120
|
+
| `config[route]` | Same as `config.get(route)` without a default. |
|
|
121
|
+
| `route in config` | Whether the route exists, e.g. `"database.port" in config`. Also `True` for keys set to `None`. |
|
|
122
|
+
| `config.set(route, value)` | Set a value, creating intermediate sections as needed. Raises `TypeError` if a part of the route is not a mapping. |
|
|
123
|
+
| `config.reset()` | Restore the state at construction / `load()` time. |
|
|
124
|
+
| `config.to_dict()` | Deep copy of all data as a plain `dict`. |
|
|
125
|
+
|
|
126
|
+
Because `get()` and `to_dict()` return copies, mutating a returned value never
|
|
127
|
+
changes the config; use `set()` for that.
|
|
128
|
+
|
|
129
|
+
File formats are chosen by suffix (case-insensitive): `.json`, `.toml`, `.yaml` / `.yml`.
|
|
130
|
+
Files are read as UTF-8 and the top level must be a mapping. An empty YAML file
|
|
131
|
+
counts as an empty mapping.
|
|
132
|
+
|
|
133
|
+
### Errors
|
|
134
|
+
|
|
135
|
+
| Exception | When |
|
|
136
|
+
| --- | --- |
|
|
137
|
+
| `ConfigError` | Unsupported file type, parse error, top level not a mapping, YAML file without PyYAML installed, invalid env override. |
|
|
138
|
+
| `KeyNotFoundError` | Route not found in `get()` / `[]`. Subclass of both `ConfigError` and `KeyError`. |
|
|
139
|
+
| `FileNotFoundError` | A required (non-`optional`) source file does not exist. |
|
|
140
|
+
| `TypeError` | `set()` would have to go through a value that is not a mapping. |
|
|
141
|
+
|
|
142
|
+
## Layering rules
|
|
143
|
+
|
|
144
|
+
- Sources are merged left to right; later sources win.
|
|
145
|
+
- If both sides have a mapping for the same key, they are merged recursively.
|
|
146
|
+
Any other value (including lists) is replaced as a whole, not combined.
|
|
147
|
+
- Environment overrides are applied last, on top of the merged files.
|
|
148
|
+
- `reset()` returns to this fully loaded state.
|
|
149
|
+
|
|
150
|
+
## Environment variables
|
|
151
|
+
|
|
152
|
+
With `env_prefix="MYAPP"`, every environment variable starting with `MYAPP_` is an
|
|
153
|
+
override:
|
|
154
|
+
|
|
155
|
+
- The prefix is matched case-insensitively.
|
|
156
|
+
- The rest of the name is split on double underscores into a route:
|
|
157
|
+
`MYAPP_DATABASE__POOL_SIZE` sets `database.pool_size`. Single underscores are part
|
|
158
|
+
of the key.
|
|
159
|
+
- Each part matches an existing key case-insensitively.
|
|
160
|
+
- **The key must already exist in your config files.** Environment variables only
|
|
161
|
+
override values, they never add new ones. An unknown variable such as
|
|
162
|
+
`MYAPP_DATABSE__PORT` (note the typo) raises `ConfigError` at load time instead of
|
|
163
|
+
being silently ignored. Give every setting a default in your config file; for
|
|
164
|
+
secrets, use an empty placeholder like `password = ""`.
|
|
165
|
+
- Variables are applied in sorted order of their names.
|
|
166
|
+
|
|
167
|
+
Values are converted based on the type of the value that already exists at that
|
|
168
|
+
route:
|
|
169
|
+
|
|
170
|
+
| Existing value | Env value | Result |
|
|
171
|
+
| --- | --- | --- |
|
|
172
|
+
| `bool` | `true` / `1` / `yes` / `on`, `false` / `0` / `no` / `off` (any case) | `True` / `False` |
|
|
173
|
+
| `int` | `"9000"` | `9000` |
|
|
174
|
+
| `float` | `"0.5"` | `0.5` |
|
|
175
|
+
| `list` | JSON array, e.g. `'["search", "export"]'` | `["search", "export"]` |
|
|
176
|
+
| mapping (section) | anything | `ConfigError`: a whole section cannot be overridden |
|
|
177
|
+
| `str` or `None` | anything | the plain string |
|
|
178
|
+
|
|
179
|
+
A value that cannot be converted raises `ConfigError` naming the environment
|
|
180
|
+
variable, as does a route that runs through a non-mapping value.
|
|
181
|
+
|
|
182
|
+
### Limitations
|
|
183
|
+
|
|
184
|
+
- Keys containing `.` cannot be reached with dotted routes (no escaping).
|
|
185
|
+
- Keys containing `.` or `-` cannot be set from environment variables: shells and
|
|
186
|
+
most tools do not allow those characters in variable names.
|
|
187
|
+
- An environment variable can only replace a single value, not a whole section or a
|
|
188
|
+
single list element.
|
|
189
|
+
|
|
190
|
+
## Development
|
|
191
|
+
|
|
192
|
+
See [CLAUDE.md](CLAUDE.md) for setup, tests, linting and project conventions.
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
|
|
196
|
+
MIT, see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tidyconf"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Simple, typed configuration for small apps: JSON/TOML/YAML files, layering and env overrides."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
authors = [{ name = "Philipp Gölter" }]
|
|
13
|
+
requires-python = ">=3.11"
|
|
14
|
+
dependencies = []
|
|
15
|
+
keywords = ["config", "configuration", "settings", "toml", "yaml", "json", "env"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
yaml = ["PyYAML>=6"]
|
|
29
|
+
|
|
30
|
+
[dependency-groups]
|
|
31
|
+
dev = ["pytest", "pytest-cov", "mypy", "ruff", "types-PyYAML", "PyYAML>=6"]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://github.com/pgoelter/tidyconf"
|
|
35
|
+
Source = "https://github.com/pgoelter/tidyconf"
|
|
36
|
+
Issues = "https://github.com/pgoelter/tidyconf/issues"
|
|
37
|
+
|
|
38
|
+
[tool.hatch.version]
|
|
39
|
+
path = "tidyconf/__init__.py"
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.wheel]
|
|
42
|
+
packages = ["tidyconf"]
|
|
43
|
+
|
|
44
|
+
[tool.hatch.build.targets.sdist]
|
|
45
|
+
include = ["tidyconf", "tests", "README.md", "LICENSE"]
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
testpaths = ["tests"]
|
|
49
|
+
addopts = "--cov=tidyconf --cov-report=term-missing --cov-fail-under=90"
|
|
50
|
+
|
|
51
|
+
[tool.mypy]
|
|
52
|
+
strict = true
|
|
53
|
+
files = ["tidyconf", "tests", "examples"]
|
|
54
|
+
|
|
55
|
+
[tool.ruff]
|
|
56
|
+
line-length = 88
|
|
57
|
+
target-version = "py311"
|
|
58
|
+
|
|
59
|
+
[tool.ruff.lint]
|
|
60
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|