Skip to content

Commit ccffe3e

Browse files
committed
Add config.default_from_env_as_list
1 parent 00d7119 commit ccffe3e

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

openeo_driver/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.67.1a1"
1+
__version__ = "0.67.2a1"

openeo_driver/config/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from openeo_driver.config.config import OpenEoBackendConfig, ConfigException
22
from openeo_driver.config.load import get_backend_config
3-
from openeo_driver.config.env import from_env, from_env_as_list
3+
from openeo_driver.config.env import from_env, from_env_as_list, default_from_env_as_list

openeo_driver/config/env.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get():
3939

4040

4141
def to_list(value: str, *, strip: bool = True, separator: str = ",") -> List[str]:
42-
"""Split a string to a list, properly handling leading/trailing whitespace and empty items."""
42+
"""Split a string to a list, properly stripping leading/trailing whitespace and skipping empty items."""
4343
result = value.split(separator)
4444
if strip:
4545
result = [s.strip() for s in result]
@@ -52,7 +52,7 @@ def from_env_as_list(
5252
) -> Callable[[], List[str]]:
5353
"""
5454
Attrs default factory to get a list from an env var
55-
(properly handling leading/trailing whitespace and empty items).
55+
(automatically splitting with separator, stripping leading/trailing whitespace and skipping empty items):
5656
5757
Usage example:
5858
@@ -84,3 +84,31 @@ def get():
8484
return value
8585

8686
return get
87+
88+
89+
def default_from_env_as_list(
90+
var: str, *, default: Union[str, List[str]] = "", strip: bool = True, separator: str = ","
91+
):
92+
"""
93+
Build an attrs default value that, by default, takes the value of given environment variable
94+
and splits it into a list (automatically stripping leading/trailing whitespace and skipping empty items):
95+
96+
>>> @attrs.define
97+
... class Config:
98+
... colors: List[str] = default_from_env_as_list("COLORS")
99+
100+
>>> Config().colors
101+
[]
102+
>>> os.environ["COLORS"] = "blue,black"
103+
>>> Config().color
104+
["blue", "black"]
105+
>>> Config(colors=["green", "white"]).colors
106+
["green", "white"]
107+
108+
:param var: env var name
109+
:param default: fallback value to parse if env var is not set
110+
:param strip: whether to strip surrounding whitespace
111+
:param separator: item separator
112+
:return: default value to use for attributes in a `@attrs.define` class
113+
"""
114+
return attrs.field(factory=from_env_as_list(var=var, default=default, strip=strip, separator=separator))

tests/test_config.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
import pytest
1010

1111
import openeo_driver.config.load
12-
from openeo_driver.config import ConfigException, OpenEoBackendConfig, get_backend_config, from_env, from_env_as_list
12+
from openeo_driver.config import (
13+
ConfigException,
14+
OpenEoBackendConfig,
15+
get_backend_config,
16+
from_env,
17+
from_env_as_list,
18+
default_from_env_as_list,
19+
)
1320
from openeo_driver.config.load import load_from_py_file
1421
import openeo_driver.config.env
1522
from .conftest import enhanced_logging
@@ -272,3 +279,34 @@ class Config:
272279
colors: List[str] = attrs.Factory(from_env_as_list("COLORS", default=["red", "blue"]))
273280

274281
assert Config().colors == ["red", "blue"]
282+
283+
def test_default_from_env_as_list_basic(self, monkeypatch):
284+
@attrs.frozen(kw_only=True)
285+
class Config:
286+
colors: List[str] = default_from_env_as_list("COLORS", default="red,blue")
287+
288+
conf = Config()
289+
assert conf.colors == ["red", "blue"]
290+
291+
conf = Config(colors=["green"])
292+
assert conf.colors == ["green"]
293+
294+
monkeypatch.setenv("COLORS", "purple,yellow")
295+
296+
conf = Config()
297+
assert conf.colors == ["purple", "yellow"]
298+
299+
conf = Config(colors=["green"])
300+
assert conf.colors == ["green"]
301+
302+
def test_default_from_env_as_list_empy(self, monkeypatch):
303+
@attrs.frozen(kw_only=True)
304+
class Config:
305+
colors: List[str] = default_from_env_as_list("COLORS")
306+
307+
assert Config().colors == []
308+
309+
monkeypatch.setenv("COLORS", "")
310+
assert Config().colors == []
311+
312+
assert Config(colors=[]).colors == []

0 commit comments

Comments
 (0)