Skip to content
Snippets Groups Projects
Commit 779d96ce authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

adjusted MergingSource a bit

parent 74395c56
No related branches found
No related tags found
No related merge requests found
# v2.7.2
* _TBA_
* changed behaviour of MergingSource a bit
# v2.7.1
......
__version__ = '2.7.2_a1'
__version__ = '2.7.2_a2'
......@@ -56,24 +56,35 @@ class OptionalSource(AlternativeSource):
class MergingSource(BaseSource):
"""
Source that merges configuration from a bunch of sources
Source that merges configuration from a bunch of sources. The configuration has to be a
dictionary!!
:param sources: Sources to examine. Source later in queue will override earlier's entries, so
take care.
:param on_fail: how to behave when a source fails
:param fail_if_no_sources_are_correct: even if on_fail == MergingSource.SILENT,
if all sources fail, this will fail as well
"""
RAISE = 0 # Raise ConfigurationError if one of sources fails
SILENT = 1 # Silently continue loading from next files if one fails
__slots__ = ('sources', 'on_fail')
__slots__ = ('sources', 'on_fail', 'fail_if_no_sources_are_correct')
def __init__(self, *sources: BaseSource, on_fail: int = RAISE):
def __init__(self, *sources: BaseSource, on_fail: int = RAISE,
fail_if_no_sources_are_correct: bool = True):
super().__init__()
self.sources = sources
self.on_fail = on_fail
self.fail_if_no_sources_are_correct = fail_if_no_sources_are_correct
def provide(self) -> dict:
cfg = {}
correct_sources = 0
for source in self.sources:
try:
p = source.provide()
correct_sources += 1
except ConfigurationError as e:
if self.on_fail == MergingSource.RAISE:
raise e
......@@ -85,6 +96,9 @@ class MergingSource(BaseSource):
cfg = merge_dicts(cfg, p)
assert isinstance(cfg, dict), 'what merge_dicts returned wasn''t a dict'
if correct_sources == 0 and self.sources and self.fail_if_no_sources_are_correct:
raise ConfigurationError('No source was able to load the configuration')
return cfg
def __repr__(self) -> str:
......
......@@ -12,6 +12,19 @@ class TestMergingSource(SourceTestCase):
)
self.assertSourceHas(s, {"a": [5, 6]})
def test_fails_on_none(self):
s = MergingSource(
JSONSource('{"a: [5]}'),
fail_if_no_sources_are_correct=True
)
self.assertRaises(ConfigurationError, s.provide)
s = MergingSource(
JSONSource('{"a: [5]}'),
fail_if_no_sources_are_correct=False
)
self.assertEqual(s.provide(), {})
def test_empty(self):
self.assertSourceEmpty(MergingSource())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment