Skip to content

Commit

Permalink
minor config.env finetuning
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Sep 19, 2023
1 parent 133e6aa commit 00d7119
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 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.0a1"
__version__ = "0.67.1a1"
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_list
from openeo_driver.config.env import from_env, from_env_as_list
15 changes: 6 additions & 9 deletions openeo_driver/config/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import attrs


def _get_env() -> Mapping:
return os.environ


def from_env(var: str, *, default=None) -> Callable[[], Optional[str]]:
"""
Attrs default factory to get a value from an env var
Expand All @@ -36,33 +32,34 @@ def from_env(var: str, *, default=None) -> Callable[[], Optional[str]]:
"""

def get():
value = _get_env().get(var, default=default)
value = os.environ.get(var, default=default)
return value

return 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 handling leading/trailing whitespace and empty items."""
result = value.split(separator)
if strip:
result = [s.strip() for s in result]
result = [s for s in result if s]
return result


def from_env_list(
def from_env_as_list(
var: str, *, default: Union[str, List[str]] = "", strip: bool = True, separator: str = ","
) -> Callable[[], List[str]]:
"""
Attrs default factory to get a list from an env var
(properly handling leading/trailing whitespace and empty items).
Usage example:
>>> @attrs.define
... class Config:
... colors: List[str] = attrs.field(
... factory=from_env_list("COLORS", default="red,blue")
... factory=from_env_as_list("COLORS", default="red,blue")
... )
>>> Config().colors
Expand All @@ -81,7 +78,7 @@ def from_env_list(
"""

def get():
value = _get_env().get(var, default=default)
value = os.environ.get(var, default=default)
if isinstance(value, str):
value = to_list(value, strip=strip, separator=separator)
return value
Expand Down
12 changes: 6 additions & 6 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest

import openeo_driver.config.load
from openeo_driver.config import ConfigException, OpenEoBackendConfig, get_backend_config, from_env, from_env_list
from openeo_driver.config import ConfigException, OpenEoBackendConfig, get_backend_config, from_env, 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 @@ -192,7 +192,7 @@ def test_to_list(self):
def test_from_env_list_basic(self, monkeypatch):
@attrs.frozen(kw_only=True)
class Config:
colors: List[str] = attrs.field(factory=from_env_list("COLORS", default="red,blue"))
colors: List[str] = attrs.field(factory=from_env_as_list("COLORS", default="red,blue"))

conf = Config()
assert conf.colors == ["red", "blue"]
Expand Down Expand Up @@ -220,7 +220,7 @@ class Config:
def test_from_env_list_splitting(self, monkeypatch, default, env_value, expected_from_default, expected_from_env):
@attrs.frozen(kw_only=True)
class Config:
colors: List[str] = attrs.Factory(from_env_list("COLORS", default=default))
colors: List[str] = attrs.Factory(from_env_as_list("COLORS", default=default))

assert Config().colors == expected_from_default

Expand All @@ -242,7 +242,7 @@ class Config:
def test_from_env_list_strip(self, monkeypatch, env_value, strip, expected):
@attrs.frozen(kw_only=True)
class Config:
colors: List[str] = attrs.Factory(from_env_list("COLORS", default="red,blue", strip=strip))
colors: List[str] = attrs.Factory(from_env_as_list("COLORS", default="red,blue", strip=strip))

monkeypatch.setenv("COLORS", env_value)

Expand All @@ -260,7 +260,7 @@ class Config:
def test_from_env_list_separator(self, monkeypatch, default, separator):
@attrs.frozen(kw_only=True)
class Config:
colors: List[str] = attrs.Factory(from_env_list("COLORS", default=default, separator=separator))
colors: List[str] = attrs.Factory(from_env_as_list("COLORS", default=default, separator=separator))

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

Expand All @@ -269,6 +269,6 @@ def test_from_env_list_default_as_list(self, monkeypatch):

@attrs.frozen(kw_only=True)
class Config:
colors: List[str] = attrs.Factory(from_env_list("COLORS", default=["red", "blue"]))
colors: List[str] = attrs.Factory(from_env_as_list("COLORS", default=["red", "blue"]))

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

0 comments on commit 00d7119

Please sign in to comment.