Skip to content

Commit

Permalink
Add config.default_from_env_as_list
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Sep 20, 2023
1 parent 00d7119 commit ccffe3e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion openeo_driver/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.67.1a1"
__version__ = "0.67.2a1"
2 changes: 1 addition & 1 deletion openeo_driver/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from openeo_driver.config.config import OpenEoBackendConfig, ConfigException
from openeo_driver.config.load import get_backend_config
from openeo_driver.config.env import from_env, from_env_as_list
from openeo_driver.config.env import from_env, from_env_as_list, default_from_env_as_list
32 changes: 30 additions & 2 deletions openeo_driver/config/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get():


def to_list(value: str, *, strip: bool = True, separator: str = ",") -> List[str]:
"""Split a string to a list, properly handling leading/trailing whitespace and empty items."""
"""Split a string to a list, properly stripping leading/trailing whitespace and skipping empty items."""
result = value.split(separator)
if strip:
result = [s.strip() for s in result]
Expand All @@ -52,7 +52,7 @@ def from_env_as_list(
) -> Callable[[], List[str]]:
"""
Attrs default factory to get a list from an env var
(properly handling leading/trailing whitespace and empty items).
(automatically splitting with separator, stripping leading/trailing whitespace and skipping empty items):
Usage example:
Expand Down Expand Up @@ -84,3 +84,31 @@ def get():
return value

return get


def default_from_env_as_list(
var: str, *, default: Union[str, List[str]] = "", strip: bool = True, separator: str = ","
):
"""
Build an attrs default value that, by default, takes the value of given environment variable
and splits it into a list (automatically stripping leading/trailing whitespace and skipping empty items):
>>> @attrs.define
... class Config:
... colors: List[str] = default_from_env_as_list("COLORS")
>>> Config().colors
[]
>>> os.environ["COLORS"] = "blue,black"
>>> Config().color
["blue", "black"]
>>> Config(colors=["green", "white"]).colors
["green", "white"]
:param var: env var name
:param default: fallback value to parse if env var is not set
:param strip: whether to strip surrounding whitespace
:param separator: item separator
:return: default value to use for attributes in a `@attrs.define` class
"""
return attrs.field(factory=from_env_as_list(var=var, default=default, strip=strip, separator=separator))
40 changes: 39 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
import pytest

import openeo_driver.config.load
from openeo_driver.config import ConfigException, OpenEoBackendConfig, get_backend_config, from_env, from_env_as_list
from openeo_driver.config import (
ConfigException,
OpenEoBackendConfig,
get_backend_config,
from_env,
from_env_as_list,
default_from_env_as_list,
)
from openeo_driver.config.load import load_from_py_file
import openeo_driver.config.env
from .conftest import enhanced_logging
Expand Down Expand Up @@ -272,3 +279,34 @@ class Config:
colors: List[str] = attrs.Factory(from_env_as_list("COLORS", default=["red", "blue"]))

assert Config().colors == ["red", "blue"]

def test_default_from_env_as_list_basic(self, monkeypatch):
@attrs.frozen(kw_only=True)
class Config:
colors: List[str] = default_from_env_as_list("COLORS", default="red,blue")

conf = Config()
assert conf.colors == ["red", "blue"]

conf = Config(colors=["green"])
assert conf.colors == ["green"]

monkeypatch.setenv("COLORS", "purple,yellow")

conf = Config()
assert conf.colors == ["purple", "yellow"]

conf = Config(colors=["green"])
assert conf.colors == ["green"]

def test_default_from_env_as_list_empy(self, monkeypatch):
@attrs.frozen(kw_only=True)
class Config:
colors: List[str] = default_from_env_as_list("COLORS")

assert Config().colors == []

monkeypatch.setenv("COLORS", "")
assert Config().colors == []

assert Config(colors=[]).colors == []

0 comments on commit ccffe3e

Please sign in to comment.