toml-combine 0.5.0__tar.gz → 0.6.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.
- {toml_combine-0.5.0 → toml_combine-0.6.0}/PKG-INFO +20 -20
- {toml_combine-0.5.0 → toml_combine-0.6.0}/README.md +19 -19
- {toml_combine-0.5.0 → toml_combine-0.6.0}/tests/test.toml +4 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/tests/test_cli.py +2 -1
- {toml_combine-0.5.0 → toml_combine-0.6.0}/tests/test_combiner.py +68 -68
- {toml_combine-0.5.0 → toml_combine-0.6.0}/tests/test_lib.py +52 -23
- {toml_combine-0.5.0 → toml_combine-0.6.0}/toml_combine/combiner.py +36 -72
- {toml_combine-0.5.0 → toml_combine-0.6.0}/toml_combine/exceptions.py +1 -1
- {toml_combine-0.5.0 → toml_combine-0.6.0}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/.github/renovate.json5 +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/.github/workflows/ci.yml +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/.pre-commit-config.yaml +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/pyproject.toml +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/tests/result.json +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/toml_combine/__init__.py +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/toml_combine/__main__.py +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/toml_combine/cli.py +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/toml_combine/lib.py +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/toml_combine/toml.py +0 -0
- {toml_combine-0.5.0 → toml_combine-0.6.0}/uv.lock +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: toml-combine
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.6.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
|
@@ -62,17 +62,19 @@ The common configuration to start from, before we start overlaying overrides on
|
|
62
62
|
### Overrides
|
63
63
|
|
64
64
|
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
|
-
|
65
|
+
"<value>"`) and the values that are overridden when they're applicable.
|
66
|
+
|
67
|
+
- In case 2 overrides are applicable and define a value for the same key, if one is more
|
68
|
+
specific than the other (e.g. env=prod,region=us is more specific than env=prod) then
|
69
|
+
its values will have precedence.
|
70
|
+
- If they are mutually exclusive, (env=prod vs env=staging) precedence is irrelevant.
|
71
|
+
- If you try to generate the configuration and 2 applicable overrides define a value for
|
72
|
+
the same key, an error will be raised (e.g. env=staging and region=eu). In that case,
|
73
|
+
you should add dimensions to either override to make them mutually exclusive or make
|
74
|
+
one more specific than the other.
|
75
|
+
|
76
|
+
Note that it's not a problem if incompatible overrides exist in your configuration, as
|
77
|
+
long as they are not both applicable in the same call.
|
76
78
|
|
77
79
|
> [!Note]
|
78
80
|
> Defining a list as the value of one or more conditions in an override
|
@@ -80,14 +82,11 @@ specific to more specific, each one overriding the values of the previous ones:
|
|
80
82
|
|
81
83
|
### The configuration itself
|
82
84
|
|
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
|
85
|
+
Under the layer of `dimensions/default/override/mapping` system, what you actually
|
86
|
+
define in the configuration is completely up to you. That said, only nested
|
85
87
|
"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.
|
88
|
+
Python/JS/Toml lingo) will be merged between the default and the applicable overrides,
|
89
|
+
while arrays will just replace one another. See `Arrays` below.
|
91
90
|
|
92
91
|
### Arrays
|
93
92
|
|
@@ -191,8 +190,9 @@ container.image_name = "my-image-backend"
|
|
191
190
|
container.port = 8080
|
192
191
|
|
193
192
|
[[override]]
|
194
|
-
|
193
|
+
when.service = "backend"
|
195
194
|
when.environment = "dev"
|
195
|
+
name = "service-dev"
|
196
196
|
container.env.DEBUG = true
|
197
197
|
|
198
198
|
[[override]]
|
@@ -43,17 +43,19 @@ The common configuration to start from, before we start overlaying overrides on
|
|
43
43
|
### Overrides
|
44
44
|
|
45
45
|
Overrides define a set of condition where they apply (`when.<dimension> =
|
46
|
-
"<value>"`) and the values that are
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
46
|
+
"<value>"`) and the values that are overridden when they're applicable.
|
47
|
+
|
48
|
+
- In case 2 overrides are applicable and define a value for the same key, if one is more
|
49
|
+
specific than the other (e.g. env=prod,region=us is more specific than env=prod) then
|
50
|
+
its values will have precedence.
|
51
|
+
- If they are mutually exclusive, (env=prod vs env=staging) precedence is irrelevant.
|
52
|
+
- If you try to generate the configuration and 2 applicable overrides define a value for
|
53
|
+
the same key, an error will be raised (e.g. env=staging and region=eu). In that case,
|
54
|
+
you should add dimensions to either override to make them mutually exclusive or make
|
55
|
+
one more specific than the other.
|
56
|
+
|
57
|
+
Note that it's not a problem if incompatible overrides exist in your configuration, as
|
58
|
+
long as they are not both applicable in the same call.
|
57
59
|
|
58
60
|
> [!Note]
|
59
61
|
> Defining a list as the value of one or more conditions in an override
|
@@ -61,14 +63,11 @@ specific to more specific, each one overriding the values of the previous ones:
|
|
61
63
|
|
62
64
|
### The configuration itself
|
63
65
|
|
64
|
-
Under the layer of `dimensions/default/override/mapping` system, what you actually
|
65
|
-
in the configuration is completely up to you. That said, only nested
|
66
|
+
Under the layer of `dimensions/default/override/mapping` system, what you actually
|
67
|
+
define in the configuration is completely up to you. That said, only nested
|
66
68
|
"dictionnaries"/"objects"/"tables"/"mapping" (those are all the same things in
|
67
|
-
Python/JS/Toml lingo) will be merged between the default and the overrides,
|
68
|
-
arrays will just replace one another. See `Arrays` below.
|
69
|
-
|
70
|
-
In the generated configuration, the dimensions of the output will appear in the generated
|
71
|
-
object as an object under the `dimensions` key.
|
69
|
+
Python/JS/Toml lingo) will be merged between the default and the applicable overrides,
|
70
|
+
while arrays will just replace one another. See `Arrays` below.
|
72
71
|
|
73
72
|
### Arrays
|
74
73
|
|
@@ -172,8 +171,9 @@ container.image_name = "my-image-backend"
|
|
172
171
|
container.port = 8080
|
173
172
|
|
174
173
|
[[override]]
|
175
|
-
|
174
|
+
when.service = "backend"
|
176
175
|
when.environment = "dev"
|
176
|
+
name = "service-dev"
|
177
177
|
container.env.DEBUG = true
|
178
178
|
|
179
179
|
[[override]]
|
@@ -49,6 +49,7 @@ cloudsql_instance = "staging-postgres"
|
|
49
49
|
containers.app.env.APP_FOO = "qux"
|
50
50
|
|
51
51
|
[[override]]
|
52
|
+
when.stack = "django"
|
52
53
|
# The following line defines when in an array. It's not useful, as there's only one
|
53
54
|
# value, but we want to test that arrays work too.
|
54
55
|
when.service = ["admin"]
|
@@ -57,6 +58,7 @@ containers.app.env.APP_ADMIN_ENABLED = true
|
|
57
58
|
containers.app.env.APP_ID = 1234
|
58
59
|
|
59
60
|
[[override]]
|
61
|
+
when.stack = "django"
|
60
62
|
when.service = "admin"
|
61
63
|
when.environment = "staging"
|
62
64
|
containers.app.env.APP_ID = 5678
|
@@ -66,11 +68,13 @@ when.type = "job"
|
|
66
68
|
containers.app.job.max_retries = 1
|
67
69
|
|
68
70
|
[[override]]
|
71
|
+
when.stack = "django"
|
69
72
|
when.job = "manage"
|
70
73
|
containers.app.name = "manage"
|
71
74
|
containers.app.command = ["./manage.py"]
|
72
75
|
|
73
76
|
[[override]]
|
77
|
+
when.stack = "django"
|
74
78
|
when.job = "special-command"
|
75
79
|
containers.app.name = "special-command"
|
76
80
|
containers.app.container_cpu = 8
|
@@ -8,7 +8,7 @@ from toml_combine import cli, toml
|
|
8
8
|
|
9
9
|
def test_cli__json(capsys):
|
10
10
|
"""Test the CLI."""
|
11
|
-
cli.cli(
|
11
|
+
exit_code = cli.cli(
|
12
12
|
argv=[
|
13
13
|
"tests/test.toml",
|
14
14
|
"--format",
|
@@ -28,6 +28,7 @@ def test_cli__json(capsys):
|
|
28
28
|
print(out)
|
29
29
|
print("err:")
|
30
30
|
print(err)
|
31
|
+
assert exit_code == 0
|
31
32
|
|
32
33
|
expected = json.loads((pathlib.Path(__file__).parent / "result.json").read_text())
|
33
34
|
assert json.loads(out) == expected["staging-service-django-admin"]
|
@@ -5,39 +5,6 @@ import pytest
|
|
5
5
|
from toml_combine import combiner, exceptions, toml
|
6
6
|
|
7
7
|
|
8
|
-
@pytest.mark.parametrize(
|
9
|
-
"small_override, large_override, dimensions",
|
10
|
-
[
|
11
|
-
pytest.param(
|
12
|
-
{"env": "prod"},
|
13
|
-
{"env": "prod", "region": "eu"},
|
14
|
-
{"env": ["prod"], "region": ["eu"]},
|
15
|
-
id="less_specific_override_comes_first",
|
16
|
-
),
|
17
|
-
pytest.param(
|
18
|
-
{"env": "prod", "region": "eu"},
|
19
|
-
{"env": "prod", "service": "web"},
|
20
|
-
{"env": ["prod"], "region": ["eu"], "service": ["web"]},
|
21
|
-
id="different_dimensions_sorted_by_dimension",
|
22
|
-
),
|
23
|
-
pytest.param(
|
24
|
-
{"env": "prod"},
|
25
|
-
{"region": "eu"},
|
26
|
-
{"env": ["prod"], "region": ["eu"]},
|
27
|
-
id="completely_different_dimensions",
|
28
|
-
),
|
29
|
-
],
|
30
|
-
)
|
31
|
-
def test_override_sort_key(small_override, large_override, dimensions):
|
32
|
-
small_key = combiner.override_sort_key(
|
33
|
-
combiner.Override(when=small_override, config={}), dimensions
|
34
|
-
)
|
35
|
-
large_key = combiner.override_sort_key(
|
36
|
-
combiner.Override(when=large_override, config={}), dimensions
|
37
|
-
)
|
38
|
-
assert small_key < large_key
|
39
|
-
|
40
|
-
|
41
8
|
@pytest.mark.parametrize(
|
42
9
|
"a, b, expected",
|
43
10
|
[
|
@@ -224,7 +191,7 @@ def test_build_config():
|
|
224
191
|
)
|
225
192
|
|
226
193
|
|
227
|
-
def
|
194
|
+
def test_generate_for_mapping__duplicate_overrides():
|
228
195
|
raw_config = """
|
229
196
|
[dimensions]
|
230
197
|
env = ["prod"]
|
@@ -238,9 +205,9 @@ def test_build_config__duplicate_overrides():
|
|
238
205
|
foo = "qux"
|
239
206
|
"""
|
240
207
|
|
241
|
-
config = toml.loads(raw_config)
|
208
|
+
config = combiner.build_config(toml.loads(raw_config))
|
242
209
|
with pytest.raises(exceptions.DuplicateError):
|
243
|
-
combiner.
|
210
|
+
combiner.generate_for_mapping(config=config, mapping={"env": "prod"})
|
244
211
|
|
245
212
|
|
246
213
|
def test_build_config__duplicate_overrides_different_vars():
|
@@ -257,8 +224,11 @@ def test_build_config__duplicate_overrides_different_vars():
|
|
257
224
|
baz = "qux"
|
258
225
|
"""
|
259
226
|
|
260
|
-
config = toml.loads(raw_config)
|
261
|
-
assert
|
227
|
+
config = combiner.build_config(toml.loads(raw_config))
|
228
|
+
assert combiner.generate_for_mapping(config=config, mapping={"env": "prod"}) == {
|
229
|
+
"foo": "baz",
|
230
|
+
"baz": "qux",
|
231
|
+
}
|
262
232
|
|
263
233
|
|
264
234
|
def test_build_config__duplicate_overrides_list():
|
@@ -268,24 +238,24 @@ def test_build_config__duplicate_overrides_list():
|
|
268
238
|
|
269
239
|
[[override]]
|
270
240
|
when.env = ["prod"]
|
271
|
-
|
272
|
-
hello = 1
|
241
|
+
hello.world = 1
|
273
242
|
|
274
243
|
[[override]]
|
275
244
|
when.env = ["prod", "dev"]
|
276
|
-
|
277
|
-
hello = 1
|
245
|
+
hello.world = 2
|
278
246
|
"""
|
279
247
|
|
280
|
-
config = toml.loads(raw_config)
|
248
|
+
config = combiner.build_config(toml.loads(raw_config))
|
281
249
|
with pytest.raises(exceptions.DuplicateError) as excinfo:
|
282
|
-
combiner.
|
250
|
+
combiner.generate_for_mapping(config=config, mapping={"env": "prod"})
|
283
251
|
|
284
252
|
# Message is a bit complex so we test it too.
|
285
253
|
assert (
|
286
|
-
str(excinfo.value)
|
287
|
-
"
|
288
|
-
"
|
254
|
+
str(excinfo.value)
|
255
|
+
== "In override {'env': ['prod', 'dev']}: Overrides defining the same "
|
256
|
+
"configuration keys must be included in one another or mutually exclusive.\n"
|
257
|
+
"Key defined multiple times: hello.world\n"
|
258
|
+
"Other override: {'env': ['prod']}"
|
289
259
|
)
|
290
260
|
|
291
261
|
|
@@ -372,24 +342,54 @@ def test_extract_keys():
|
|
372
342
|
]
|
373
343
|
|
374
344
|
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
)
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
(
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
345
|
+
@pytest.mark.parametrize(
|
346
|
+
"a, b, expected",
|
347
|
+
[
|
348
|
+
pytest.param(
|
349
|
+
{"env": ["dev"], "region": ["eu"]},
|
350
|
+
{"env": ["dev"]},
|
351
|
+
True,
|
352
|
+
id="subset1",
|
353
|
+
),
|
354
|
+
pytest.param(
|
355
|
+
{"env": ["dev"]},
|
356
|
+
{"env": ["dev"], "region": ["eu"]},
|
357
|
+
True,
|
358
|
+
id="subset2",
|
359
|
+
),
|
360
|
+
pytest.param(
|
361
|
+
{"env": ["prod"], "region": ["eu"]},
|
362
|
+
{"env": ["dev"]},
|
363
|
+
True,
|
364
|
+
id="subset3",
|
365
|
+
),
|
366
|
+
pytest.param(
|
367
|
+
{"env": ["dev"]},
|
368
|
+
{"env": ["prod"], "region": ["eu"]},
|
369
|
+
True,
|
370
|
+
id="subset4",
|
371
|
+
),
|
372
|
+
pytest.param({"env": ["dev"]}, {"region": ["eu"]}, False, id="disjoint"),
|
373
|
+
pytest.param(
|
374
|
+
{"env": ["dev"], "service": ["frontend"]},
|
375
|
+
{"region": ["eu"], "service": ["frontend"]},
|
376
|
+
False,
|
377
|
+
id="overlap",
|
378
|
+
),
|
379
|
+
pytest.param({"env": ["dev"]}, {"env": ["dev"]}, False, id="same_keys1"),
|
380
|
+
pytest.param(
|
381
|
+
{"env": ["dev", "prod"]}, {"env": ["dev"]}, False, id="same_keys1"
|
382
|
+
),
|
383
|
+
pytest.param(
|
384
|
+
{"env": ["prod"]}, {"env": ["dev"]}, True, id="same_keys_disjoint"
|
385
|
+
),
|
386
|
+
pytest.param(
|
387
|
+
{"env": ["prod", "staging"]},
|
388
|
+
{"env": ["dev", "sandbox"]},
|
389
|
+
True,
|
390
|
+
id="multiple_keys_disjoint",
|
391
|
+
),
|
392
|
+
],
|
393
|
+
)
|
394
|
+
def test_are_conditions_compatible(a, b, expected):
|
395
|
+
assert combiner.are_conditions_compatible(a, b) == expected
|
@@ -16,23 +16,19 @@ def expected():
|
|
16
16
|
return json.loads((pathlib.Path(__file__).parent / "result.json").read_text())
|
17
17
|
|
18
18
|
|
19
|
-
@pytest.mark.parametrize(
|
20
|
-
"kwargs",
|
21
|
-
[
|
22
|
-
{"config_file": config_file},
|
23
|
-
{"config_file": str(config_file)},
|
24
|
-
{"config": config_file.read_text()},
|
25
|
-
{"config": toml.loads(config_file.read_text())},
|
26
|
-
],
|
27
|
-
)
|
28
19
|
@pytest.mark.parametrize(
|
29
20
|
"mapping, expected_key",
|
30
21
|
[
|
31
|
-
(
|
32
|
-
{
|
22
|
+
pytest.param(
|
23
|
+
{
|
24
|
+
"environment": "staging",
|
25
|
+
"type": "service",
|
26
|
+
"stack": "next",
|
27
|
+
},
|
33
28
|
"staging-service-next",
|
29
|
+
id="staging-service-next",
|
34
30
|
),
|
35
|
-
(
|
31
|
+
pytest.param(
|
36
32
|
{
|
37
33
|
"environment": "staging",
|
38
34
|
"type": "service",
|
@@ -40,8 +36,9 @@ def expected():
|
|
40
36
|
"service": "api",
|
41
37
|
},
|
42
38
|
"staging-service-django-api",
|
39
|
+
id="staging-service-django-api",
|
43
40
|
),
|
44
|
-
(
|
41
|
+
pytest.param(
|
45
42
|
{
|
46
43
|
"environment": "staging",
|
47
44
|
"type": "service",
|
@@ -49,8 +46,9 @@ def expected():
|
|
49
46
|
"service": "admin",
|
50
47
|
},
|
51
48
|
"staging-service-django-admin",
|
49
|
+
id="staging-service-django-admin",
|
52
50
|
),
|
53
|
-
(
|
51
|
+
pytest.param(
|
54
52
|
{
|
55
53
|
"environment": "staging",
|
56
54
|
"type": "job",
|
@@ -58,8 +56,9 @@ def expected():
|
|
58
56
|
"job": "manage",
|
59
57
|
},
|
60
58
|
"staging-job-django-manage",
|
59
|
+
id="staging-job-django-manage",
|
61
60
|
),
|
62
|
-
(
|
61
|
+
pytest.param(
|
63
62
|
{
|
64
63
|
"environment": "staging",
|
65
64
|
"type": "job",
|
@@ -67,12 +66,18 @@ def expected():
|
|
67
66
|
"job": "special-command",
|
68
67
|
},
|
69
68
|
"staging-job-django-special-command",
|
69
|
+
id="staging-job-django-special-command",
|
70
70
|
),
|
71
|
-
(
|
72
|
-
{
|
71
|
+
pytest.param(
|
72
|
+
{
|
73
|
+
"environment": "production",
|
74
|
+
"type": "service",
|
75
|
+
"stack": "next",
|
76
|
+
},
|
73
77
|
"production-service-next",
|
78
|
+
id="production-service-next",
|
74
79
|
),
|
75
|
-
(
|
80
|
+
pytest.param(
|
76
81
|
{
|
77
82
|
"environment": "production",
|
78
83
|
"type": "service",
|
@@ -80,8 +85,9 @@ def expected():
|
|
80
85
|
"service": "api",
|
81
86
|
},
|
82
87
|
"production-service-django-api",
|
88
|
+
id="production-service-django-api",
|
83
89
|
),
|
84
|
-
(
|
90
|
+
pytest.param(
|
85
91
|
{
|
86
92
|
"environment": "production",
|
87
93
|
"type": "service",
|
@@ -89,8 +95,9 @@ def expected():
|
|
89
95
|
"service": "admin",
|
90
96
|
},
|
91
97
|
"production-service-django-admin",
|
98
|
+
id="production-service-django-admin",
|
92
99
|
),
|
93
|
-
(
|
100
|
+
pytest.param(
|
94
101
|
{
|
95
102
|
"environment": "production",
|
96
103
|
"type": "job",
|
@@ -98,8 +105,9 @@ def expected():
|
|
98
105
|
"job": "manage",
|
99
106
|
},
|
100
107
|
"production-job-django-manage",
|
108
|
+
id="production-job-django-manage",
|
101
109
|
),
|
102
|
-
(
|
110
|
+
pytest.param(
|
103
111
|
{
|
104
112
|
"environment": "production",
|
105
113
|
"type": "job",
|
@@ -107,9 +115,30 @@ def expected():
|
|
107
115
|
"job": "special-command",
|
108
116
|
},
|
109
117
|
"production-job-django-special-command",
|
118
|
+
id="production-job-django-special-command",
|
110
119
|
),
|
111
120
|
],
|
112
121
|
)
|
113
|
-
def
|
114
|
-
result = toml_combine.combine(
|
122
|
+
def test_full_config(mapping, expected, expected_key):
|
123
|
+
result = toml_combine.combine(config_file=config_file, **mapping)
|
115
124
|
assert result == expected[expected_key]
|
125
|
+
|
126
|
+
|
127
|
+
@pytest.mark.parametrize(
|
128
|
+
"kwargs",
|
129
|
+
[
|
130
|
+
pytest.param({"config_file": config_file}, id="path"),
|
131
|
+
pytest.param({"config_file": str(config_file)}, id="path_str"),
|
132
|
+
pytest.param({"config": config_file.read_text()}, id="text"),
|
133
|
+
pytest.param({"config": toml.loads(config_file.read_text())}, id="parsed"),
|
134
|
+
],
|
135
|
+
)
|
136
|
+
def test_full_load_kwargs(kwargs, expected):
|
137
|
+
result = toml_combine.combine(
|
138
|
+
**kwargs,
|
139
|
+
environment="production",
|
140
|
+
type="service",
|
141
|
+
stack="django",
|
142
|
+
service="api",
|
143
|
+
)
|
144
|
+
assert result == expected["production-service-django-api"]
|
@@ -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. 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[dict[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.DuplicateError(
|
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
|
@@ -20,7 +20,7 @@ class TomlEncodeError(TomlCombineError):
|
|
20
20
|
|
21
21
|
|
22
22
|
class DuplicateError(TomlCombineError):
|
23
|
-
"""In override {id}: Overrides
|
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):
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|