pattern: Fix and improve config overriding in from_configfile method
Fix error when a configuration value loaded from a config file is also given as keyword parameter to the from_configfile method. Override configuration loaded from config file only if the provided value is not None.
This commit is contained in:
parent
a41c03e4c8
commit
9fd91f007d
2 changed files with 28 additions and 3 deletions
|
@ -251,13 +251,15 @@ class Lister(Generic[StateType, PageType]):
|
|||
@classmethod
|
||||
def from_configfile(cls, **kwargs: Any):
|
||||
"""Instantiate a lister from the configuration loaded from the
|
||||
SWH_CONFIG_FILENAME envvar, with potential extra keyword arguments.
|
||||
SWH_CONFIG_FILENAME envvar, with potential extra keyword arguments
|
||||
if their value is not None.
|
||||
|
||||
Args:
|
||||
kwargs: kwargs passed to the lister instantiation
|
||||
"""
|
||||
config = load_from_envvar()
|
||||
return cls.from_config(**config, **kwargs)
|
||||
config = dict(load_from_envvar())
|
||||
config.update({k: v for k, v in kwargs.items() if v is not None})
|
||||
return cls.from_config(**config)
|
||||
|
||||
|
||||
class StatelessLister(Lister[None, PageType], Generic[PageType]):
|
||||
|
|
|
@ -39,6 +39,29 @@ def test_instantiation(swh_scheduler):
|
|||
lister.run()
|
||||
|
||||
|
||||
def test_instantiation_from_configfile(swh_scheduler, mocker):
|
||||
mock_load_from_envvar = mocker.patch("swh.lister.pattern.load_from_envvar")
|
||||
mock_get_scheduler = mocker.patch("swh.lister.pattern.get_scheduler")
|
||||
mock_load_from_envvar.return_value = {
|
||||
"scheduler": {},
|
||||
"url": "foo",
|
||||
"instance": "bar",
|
||||
}
|
||||
mock_get_scheduler.return_value = swh_scheduler
|
||||
|
||||
lister = InstantiableLister.from_configfile()
|
||||
assert lister.url == "foo"
|
||||
assert lister.instance == "bar"
|
||||
|
||||
lister = InstantiableLister.from_configfile(url="bar", instance="foo")
|
||||
assert lister.url == "bar"
|
||||
assert lister.instance == "foo"
|
||||
|
||||
lister = InstantiableLister.from_configfile(url=None, instance="foo")
|
||||
assert lister.url == "foo"
|
||||
assert lister.instance == "foo"
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
_Base = pattern.Lister[Any, PageType]
|
||||
else:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue