toml-combine 0.5.0__py3-none-any.whl → 1.0.0__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.
- toml_combine/combiner.py +36 -72
- toml_combine/exceptions.py +2 -2
- {toml_combine-0.5.0.dist-info → toml_combine-1.0.0.dist-info}/METADATA +27 -20
- toml_combine-1.0.0.dist-info/RECORD +12 -0
- toml_combine-1.0.0.dist-info/licenses/LICENSE +7 -0
- toml_combine-0.5.0.dist-info/RECORD +0 -11
- {toml_combine-0.5.0.dist-info → toml_combine-1.0.0.dist-info}/WHEEL +0 -0
- {toml_combine-0.5.0.dist-info → toml_combine-1.0.0.dist-info}/entry_points.txt +0 -0
toml_combine/combiner.py
CHANGED
@@ -2,9 +2,7 @@ from __future__ import annotations
|
|
2
2
|
|
3
3
|
import copy
|
4
4
|
import dataclasses
|
5
|
-
import itertools
|
6
5
|
from collections.abc import Iterable, Mapping, Sequence
|
7
|
-
from functools import partial
|
8
6
|
from typing import Any, TypeVar
|
9
7
|
|
10
8
|
from . import exceptions
|
@@ -60,48 +58,6 @@ def clean_dimensions_dict(
|
|
60
58
|
return result
|
61
59
|
|
62
60
|
|
63
|
-
def override_sort_key(
|
64
|
-
override: Override, dimensions: dict[str, list[str]]
|
65
|
-
) -> tuple[int, ...]:
|
66
|
-
"""
|
67
|
-
We sort overrides before applying them, and they are applied in the order of the
|
68
|
-
sorted list, each override replacing the common values of the previous overrides.
|
69
|
-
|
70
|
-
override_sort_key defines the sort key for overrides that ensures less specific
|
71
|
-
overrides come first:
|
72
|
-
- Overrides with fewer dimensions come first (will be overridden
|
73
|
-
by more specific ones)
|
74
|
-
- If two overrides have the same number of dimensions but define different
|
75
|
-
dimensions, we sort by the definition order of the dimensions.
|
76
|
-
|
77
|
-
Example:
|
78
|
-
dimensions = {"env": ["dev", "prod"], "region": ["us", "eu"]}
|
79
|
-
|
80
|
-
- Override with {"env": "dev"} comes before override with
|
81
|
-
{"env": "dev", "region": "us"} (less specific)
|
82
|
-
- Override with {"env": "dev"} comes before override with {"region": "us"} ("env"
|
83
|
-
is defined before "region" in the dimensions list)
|
84
|
-
|
85
|
-
Parameters:
|
86
|
-
-----------
|
87
|
-
override: An Override object that defines the condition when it applies
|
88
|
-
(override.when)
|
89
|
-
dimensions: The dict of all existing dimensions and their values, in order of
|
90
|
-
definition
|
91
|
-
|
92
|
-
Returns:
|
93
|
-
--------
|
94
|
-
A tuple that supports comparisons. Less specific Overrides should return smaller
|
95
|
-
values and vice versa.
|
96
|
-
"""
|
97
|
-
result = [len(override.when)]
|
98
|
-
for i, dimension in enumerate(dimensions):
|
99
|
-
if dimension in override.when:
|
100
|
-
result.append(i)
|
101
|
-
|
102
|
-
return tuple(result)
|
103
|
-
|
104
|
-
|
105
61
|
T = TypeVar("T", dict, list, str, int, float, bool)
|
106
62
|
|
107
63
|
|
@@ -136,21 +92,27 @@ def extract_keys(config: Any) -> Iterable[tuple[str, ...]]:
|
|
136
92
|
yield tuple()
|
137
93
|
|
138
94
|
|
139
|
-
def
|
140
|
-
|
141
|
-
) ->
|
95
|
+
def are_conditions_compatible(
|
96
|
+
a: Mapping[str, list[str]], b: Mapping[str, list[str]], /
|
97
|
+
) -> bool:
|
142
98
|
"""
|
143
|
-
|
99
|
+
`a` and `b` are dictionaries representing override conditions (`when`). Return
|
100
|
+
`True` if the conditions represented by `a` are compatible with `b`. Conditions are
|
101
|
+
compatible if one is stricly more specific than the other or if they're mutually
|
102
|
+
exclusive.
|
144
103
|
"""
|
145
|
-
|
146
|
-
|
147
|
-
|
104
|
+
# Subset
|
105
|
+
if set(a) < set(b) or set(b) < set(a):
|
106
|
+
return True
|
107
|
+
|
108
|
+
# Disjoint or overlapping sets
|
109
|
+
if set(a) != set(b):
|
110
|
+
return False
|
148
111
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
yield (when_definition, *config_key)
|
112
|
+
# Equal sets: it's only compatible if the values are disjoint
|
113
|
+
if any(set(a[key]) & set(b[key]) for key in a.keys()):
|
114
|
+
return False
|
115
|
+
return True
|
154
116
|
|
155
117
|
|
156
118
|
def build_config(config: dict[str, Any]) -> Config:
|
@@ -161,9 +123,6 @@ def build_config(config: dict[str, Any]) -> Config:
|
|
161
123
|
# Parse template
|
162
124
|
default = config.pop("default", {})
|
163
125
|
|
164
|
-
# The rule is: the same exact set of conditions cannot be used twice to define
|
165
|
-
# the same values (on the same or different overrides)
|
166
|
-
seen_conditions_and_keys = set()
|
167
126
|
overrides = []
|
168
127
|
for override in config.pop("override", []):
|
169
128
|
try:
|
@@ -176,22 +135,10 @@ def build_config(config: dict[str, Any]) -> Config:
|
|
176
135
|
type="override",
|
177
136
|
)
|
178
137
|
|
179
|
-
conditions_and_keys = set(
|
180
|
-
extract_conditions_and_keys(when=when, config=override)
|
181
|
-
)
|
182
|
-
if duplicates := (conditions_and_keys & seen_conditions_and_keys):
|
183
|
-
duplicate_str = ", ".join(sorted(key for *_, key in duplicates))
|
184
|
-
raise exceptions.DuplicateError(id=when, details=duplicate_str)
|
185
|
-
|
186
|
-
seen_conditions_and_keys |= conditions_and_keys
|
187
|
-
|
188
138
|
overrides.append(Override(when=when, config=override))
|
189
139
|
|
190
140
|
# Sort overrides by increasing specificity
|
191
|
-
overrides = sorted(
|
192
|
-
overrides,
|
193
|
-
key=partial(override_sort_key, dimensions=dimensions),
|
194
|
-
)
|
141
|
+
overrides = sorted(overrides, key=lambda override: len(override.when))
|
195
142
|
|
196
143
|
return Config(
|
197
144
|
dimensions=dimensions,
|
@@ -219,11 +166,28 @@ def generate_for_mapping(
|
|
219
166
|
mapping: Mapping[str, str],
|
220
167
|
) -> Mapping[str, Any]:
|
221
168
|
result = copy.deepcopy(config.default)
|
169
|
+
keys_to_conditions: dict[tuple[str, ...], list[Mapping[str, list[str]]]] = {}
|
222
170
|
# Apply each matching override
|
223
171
|
for override in config.overrides:
|
224
172
|
# Check if all dimension values in the override match
|
225
173
|
|
226
174
|
if mapping_matches_override(mapping=mapping, override=override):
|
175
|
+
# Check that all applicableoverrides are compatible
|
176
|
+
keys = extract_keys(override.config)
|
177
|
+
|
178
|
+
for key in keys:
|
179
|
+
previous_conditions = keys_to_conditions.setdefault(key, [])
|
180
|
+
|
181
|
+
for previous_condition in previous_conditions:
|
182
|
+
if not are_conditions_compatible(previous_condition, override.when):
|
183
|
+
raise exceptions.IncompatibleOverrides(
|
184
|
+
id=override.when,
|
185
|
+
key=".".join(key),
|
186
|
+
other_override=previous_condition,
|
187
|
+
)
|
188
|
+
|
189
|
+
keys_to_conditions[key].append(override.when)
|
190
|
+
|
227
191
|
result = merge_configs(result, override.config)
|
228
192
|
|
229
193
|
return result
|
toml_combine/exceptions.py
CHANGED
@@ -19,8 +19,8 @@ class TomlEncodeError(TomlCombineError):
|
|
19
19
|
"""Error while encoding configuration file."""
|
20
20
|
|
21
21
|
|
22
|
-
class
|
23
|
-
"""In override {id}: Overrides
|
22
|
+
class IncompatibleOverrides(TomlCombineError):
|
23
|
+
"""In override {id}: Overrides defining the same configuration keys must be included in one another or mutually exclusive.\nKey defined multiple times: {key}\nOther override: {other_override}"""
|
24
24
|
|
25
25
|
|
26
26
|
class DimensionNotFound(TomlCombineError):
|
@@ -1,9 +1,10 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: toml-combine
|
3
|
-
Version: 0.
|
3
|
+
Version: 1.0.0
|
4
4
|
Summary: A tool for combining complex configurations in TOML format.
|
5
5
|
Author-email: Joachim Jablon <ewjoachim@gmail.com>
|
6
6
|
License-Expression: MIT
|
7
|
+
License-File: LICENSE
|
7
8
|
Classifier: Development Status :: 4 - Beta
|
8
9
|
Classifier: Intended Audience :: Developers
|
9
10
|
Classifier: License :: OSI Approved :: MIT License
|
@@ -19,6 +20,12 @@ Description-Content-Type: text/markdown
|
|
19
20
|
|
20
21
|
# Toml-combine
|
21
22
|
|
23
|
+
[](https://pypi.org/pypi/toml-combine)
|
24
|
+
[](https://pypi.org/pypi/toml-combine)
|
25
|
+
[](https://github.com/ewjoachim/toml-combine/)
|
26
|
+
[](https://github.com/ewjoachim/toml-combine/actions?workflow=CI)
|
27
|
+
[](https://github.com/ewjoachim/toml-combine/blob/main/LICENSE)
|
28
|
+
|
22
29
|
`toml-combine` is a Python lib and CLI-tool that reads a TOML configuration file
|
23
30
|
defining a default configuration alongside with overrides, and merges everything
|
24
31
|
following rules you define to get final configurations. Let's say: you have multiple
|
@@ -62,17 +69,19 @@ The common configuration to start from, before we start overlaying overrides on
|
|
62
69
|
### Overrides
|
63
70
|
|
64
71
|
Overrides define a set of condition where they apply (`when.<dimension> =
|
65
|
-
"<value>"`) and the values that are
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
72
|
+
"<value>"`) and the values that are overridgden when they're applicable.
|
73
|
+
|
74
|
+
- In case 2 overrides are applicable and define a value for the same key, if one is more
|
75
|
+
specific than the other (e.g. env=prod,region=us is more specific than env=prod) then
|
76
|
+
its values will have precedence.
|
77
|
+
- If they are mutually exclusive, (env=prod vs env=staging) precedence is irrelevant.
|
78
|
+
- If you try to generate the configuration and 2 applicable overrides define a value for
|
79
|
+
the same key, an error will be raised (e.g. env=staging and region=eu). In that case,
|
80
|
+
you should add dimensions to either override to make them mutually exclusive or make
|
81
|
+
one more specific than the other.
|
82
|
+
|
83
|
+
Note that it's not a problem if incompatible overrides exist in your configuration, as
|
84
|
+
long as they are not both applicable in the same call.
|
76
85
|
|
77
86
|
> [!Note]
|
78
87
|
> Defining a list as the value of one or more conditions in an override
|
@@ -80,14 +89,11 @@ specific to more specific, each one overriding the values of the previous ones:
|
|
80
89
|
|
81
90
|
### The configuration itself
|
82
91
|
|
83
|
-
Under the layer of `dimensions/default/override/mapping` system, what you actually
|
84
|
-
in the configuration is completely up to you. That said, only nested
|
92
|
+
Under the layer of `dimensions/default/override/mapping` system, what you actually
|
93
|
+
define in the configuration is completely up to you. That said, only nested
|
85
94
|
"dictionnaries"/"objects"/"tables"/"mapping" (those are all the same things in
|
86
|
-
Python/JS/Toml lingo) will be merged between the default and the overrides,
|
87
|
-
arrays will just replace one another. See `Arrays` below.
|
88
|
-
|
89
|
-
In the generated configuration, the dimensions of the output will appear in the generated
|
90
|
-
object as an object under the `dimensions` key.
|
95
|
+
Python/JS/Toml lingo) will be merged between the default and the applicable overrides,
|
96
|
+
while arrays will just replace one another. See `Arrays` below.
|
91
97
|
|
92
98
|
### Arrays
|
93
99
|
|
@@ -191,8 +197,9 @@ container.image_name = "my-image-backend"
|
|
191
197
|
container.port = 8080
|
192
198
|
|
193
199
|
[[override]]
|
194
|
-
|
200
|
+
when.service = "backend"
|
195
201
|
when.environment = "dev"
|
202
|
+
name = "service-dev"
|
196
203
|
container.env.DEBUG = true
|
197
204
|
|
198
205
|
[[override]]
|
@@ -0,0 +1,12 @@
|
|
1
|
+
toml_combine/__init__.py,sha256=TDkOwwEM-nS6hOh79u9Qae6g2Q6VfANpPpnKGfSgu80,84
|
2
|
+
toml_combine/__main__.py,sha256=hmF8N8xX6UEApzbKTVZ-4E1HU5-rjgUkdXNLO-mF6vo,100
|
3
|
+
toml_combine/cli.py,sha256=hG03eDKz7xU-ydJIa1kDuu6WlFzNS3GTMJ6zals9M9c,2843
|
4
|
+
toml_combine/combiner.py,sha256=HG7KV3EGwmcHOMruhp7sMkH-uXqWKpsO7_BvhNQ2KQY,5668
|
5
|
+
toml_combine/exceptions.py,sha256=cRAZhxg3OHgzp5hJzyxNGG_jvGUm8gG8XzndQXBhvo8,1116
|
6
|
+
toml_combine/lib.py,sha256=jh6OG57JefpGa-WE-mLSIK6KjyJ0-1yGBynr_kiVTww,1634
|
7
|
+
toml_combine/toml.py,sha256=iBV8xj0qWcvGp2AZaML8FCT3i2X9DL7iA6jd-wcP5Bc,814
|
8
|
+
toml_combine-1.0.0.dist-info/METADATA,sha256=JxCXUhIW1v3MyM1ut0ZXbE6B6IuiXj4bf0YpwvlxGCw,8466
|
9
|
+
toml_combine-1.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
10
|
+
toml_combine-1.0.0.dist-info/entry_points.txt,sha256=dXUQNom54uZt_7ylEG81iNYMamYpaFo9-ItcZJU6Uzc,58
|
11
|
+
toml_combine-1.0.0.dist-info/licenses/LICENSE,sha256=tA7wpipzIPGl7xL5xzMMg0RhhXz9CKOa-ZnlYzgiTKg,1059
|
12
|
+
toml_combine-1.0.0.dist-info/RECORD,,
|
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2025- Joachim Jablon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,11 +0,0 @@
|
|
1
|
-
toml_combine/__init__.py,sha256=TDkOwwEM-nS6hOh79u9Qae6g2Q6VfANpPpnKGfSgu80,84
|
2
|
-
toml_combine/__main__.py,sha256=hmF8N8xX6UEApzbKTVZ-4E1HU5-rjgUkdXNLO-mF6vo,100
|
3
|
-
toml_combine/cli.py,sha256=hG03eDKz7xU-ydJIa1kDuu6WlFzNS3GTMJ6zals9M9c,2843
|
4
|
-
toml_combine/combiner.py,sha256=BCsZOm7Cr2_JxttsKzjJH_HYQkD2XPjylXKlkxvi1EY,6974
|
5
|
-
toml_combine/exceptions.py,sha256=Qg_gGIdXcwTmWDlIfJOidXkViBOVSPdLx0WOELxFPp0,1026
|
6
|
-
toml_combine/lib.py,sha256=jh6OG57JefpGa-WE-mLSIK6KjyJ0-1yGBynr_kiVTww,1634
|
7
|
-
toml_combine/toml.py,sha256=iBV8xj0qWcvGp2AZaML8FCT3i2X9DL7iA6jd-wcP5Bc,814
|
8
|
-
toml_combine-0.5.0.dist-info/METADATA,sha256=XRrnb4YkB_wwoGdoZBDQBGoXi4qY9FmjBhKGQVAJjc4,7585
|
9
|
-
toml_combine-0.5.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
10
|
-
toml_combine-0.5.0.dist-info/entry_points.txt,sha256=dXUQNom54uZt_7ylEG81iNYMamYpaFo9-ItcZJU6Uzc,58
|
11
|
-
toml_combine-0.5.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|