Skip to content

Commit 00d7119

Browse files
committed
minor config.env finetuning
1 parent 133e6aa commit 00d7119

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

openeo_driver/_version.py

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

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_list
3+
from openeo_driver.config.env import from_env, from_env_as_list

openeo_driver/config/env.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
import attrs
99

1010

11-
def _get_env() -> Mapping:
12-
return os.environ
13-
14-
1511
def from_env(var: str, *, default=None) -> Callable[[], Optional[str]]:
1612
"""
1713
Attrs default factory to get a value from an env var
@@ -36,33 +32,34 @@ def from_env(var: str, *, default=None) -> Callable[[], Optional[str]]:
3632
"""
3733

3834
def get():
39-
value = _get_env().get(var, default=default)
35+
value = os.environ.get(var, default=default)
4036
return value
4137

4238
return get
4339

4440

4541
def to_list(value: str, *, strip: bool = True, separator: str = ",") -> List[str]:
46-
"""Split a string to a list, properly handling leading/trailing whitespace and empty items"""
42+
"""Split a string to a list, properly handling leading/trailing whitespace and empty items."""
4743
result = value.split(separator)
4844
if strip:
4945
result = [s.strip() for s in result]
5046
result = [s for s in result if s]
5147
return result
5248

5349

54-
def from_env_list(
50+
def from_env_as_list(
5551
var: str, *, default: Union[str, List[str]] = "", strip: bool = True, separator: str = ","
5652
) -> Callable[[], List[str]]:
5753
"""
5854
Attrs default factory to get a list from an env var
55+
(properly handling leading/trailing whitespace and empty items).
5956
6057
Usage example:
6158
6259
>>> @attrs.define
6360
... class Config:
6461
... colors: List[str] = attrs.field(
65-
... factory=from_env_list("COLORS", default="red,blue")
62+
... factory=from_env_as_list("COLORS", default="red,blue")
6663
... )
6764
6865
>>> Config().colors
@@ -81,7 +78,7 @@ def from_env_list(
8178
"""
8279

8380
def get():
84-
value = _get_env().get(var, default=default)
81+
value = os.environ.get(var, default=default)
8582
if isinstance(value, str):
8683
value = to_list(value, strip=strip, separator=separator)
8784
return value

tests/test_config.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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_list
12+
from openeo_driver.config import ConfigException, OpenEoBackendConfig, get_backend_config, from_env, from_env_as_list
1313
from openeo_driver.config.load import load_from_py_file
1414
import openeo_driver.config.env
1515
from .conftest import enhanced_logging
@@ -192,7 +192,7 @@ def test_to_list(self):
192192
def test_from_env_list_basic(self, monkeypatch):
193193
@attrs.frozen(kw_only=True)
194194
class Config:
195-
colors: List[str] = attrs.field(factory=from_env_list("COLORS", default="red,blue"))
195+
colors: List[str] = attrs.field(factory=from_env_as_list("COLORS", default="red,blue"))
196196

197197
conf = Config()
198198
assert conf.colors == ["red", "blue"]
@@ -220,7 +220,7 @@ class Config:
220220
def test_from_env_list_splitting(self, monkeypatch, default, env_value, expected_from_default, expected_from_env):
221221
@attrs.frozen(kw_only=True)
222222
class Config:
223-
colors: List[str] = attrs.Factory(from_env_list("COLORS", default=default))
223+
colors: List[str] = attrs.Factory(from_env_as_list("COLORS", default=default))
224224

225225
assert Config().colors == expected_from_default
226226

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

247247
monkeypatch.setenv("COLORS", env_value)
248248

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

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

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

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

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

0 commit comments

Comments
 (0)