configaroo 0.3.0__tar.gz → 0.4.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. {configaroo-0.3.0/src/configaroo.egg-info → configaroo-0.4.1}/PKG-INFO +1 -1
  2. {configaroo-0.3.0 → configaroo-0.4.1}/pyproject.toml +1 -1
  3. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo/__init__.py +1 -1
  4. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo/configuration.py +70 -50
  5. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo/loaders/__init__.py +6 -1
  6. {configaroo-0.3.0 → configaroo-0.4.1/src/configaroo.egg-info}/PKG-INFO +1 -1
  7. {configaroo-0.3.0 → configaroo-0.4.1}/tests/test_configuration.py +0 -6
  8. {configaroo-0.3.0 → configaroo-0.4.1}/tests/test_environment.py +60 -1
  9. {configaroo-0.3.0 → configaroo-0.4.1}/tests/test_json.py +6 -0
  10. {configaroo-0.3.0 → configaroo-0.4.1}/tests/test_toml.py +6 -0
  11. {configaroo-0.3.0 → configaroo-0.4.1}/LICENSE +0 -0
  12. {configaroo-0.3.0 → configaroo-0.4.1}/README.md +0 -0
  13. {configaroo-0.3.0 → configaroo-0.4.1}/setup.cfg +0 -0
  14. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo/exceptions.py +0 -0
  15. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo/loaders/json.py +0 -0
  16. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo/loaders/toml.py +0 -0
  17. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo/py.typed +0 -0
  18. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo.egg-info/SOURCES.txt +0 -0
  19. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo.egg-info/dependency_links.txt +0 -0
  20. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo.egg-info/requires.txt +0 -0
  21. {configaroo-0.3.0 → configaroo-0.4.1}/src/configaroo.egg-info/top_level.txt +0 -0
  22. {configaroo-0.3.0 → configaroo-0.4.1}/tests/test_dynamic.py +0 -0
  23. {configaroo-0.3.0 → configaroo-0.4.1}/tests/test_loaders.py +0 -0
  24. {configaroo-0.3.0 → configaroo-0.4.1}/tests/test_print.py +0 -0
  25. {configaroo-0.3.0 → configaroo-0.4.1}/tests/test_validation.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: configaroo
3
- Version: 0.3.0
3
+ Version: 0.4.1
4
4
  Summary: Bouncy handling of configuration files
5
5
  Author-email: Geir Arne Hjelle <geirarne@gmail.com>
6
6
  Maintainer-email: Geir Arne Hjelle <geirarne@gmail.com>
@@ -79,7 +79,7 @@ python_version = "3.11"
79
79
  strict = true
80
80
 
81
81
  [tool.bumpver]
82
- current_version = "v0.3.0"
82
+ current_version = "v0.4.1"
83
83
  version_pattern = "vMAJOR.MINOR.PATCH"
84
84
  commit_message = "bump version {old_version} -> {new_version}"
85
85
  tag_message = "{new_version}"
@@ -15,4 +15,4 @@ __all__ = [
15
15
  "print_configuration",
16
16
  ]
17
17
 
18
- __version__ = "0.3.0"
18
+ __version__ = "0.4.1"
@@ -6,6 +6,7 @@ import re
6
6
  from collections import UserDict
7
7
  from collections.abc import Callable
8
8
  from pathlib import Path
9
+ from types import UnionType
9
10
  from typing import Any, Self, TypeVar
10
11
 
11
12
  from pydantic import BaseModel
@@ -36,33 +37,20 @@ class Configuration(UserDict[str, Any]):
36
37
  def from_file(
37
38
  cls,
38
39
  file_path: str | Path,
40
+ *,
39
41
  loader: str | None = None,
40
- envs: dict[str, str] | None = None,
41
- env_prefix: str = "",
42
- extra_dynamic: dict[str, Any] | None = None,
42
+ not_exist_ok: bool = False,
43
43
  ) -> Self:
44
- """Read a Configuration from a file."""
45
- config_dict = loaders.from_file(file_path, loader=loader)
46
- return cls(config_dict).initialize(
47
- envs=envs, env_prefix=env_prefix, extra_dynamic=extra_dynamic
48
- )
44
+ """Read a Configuration from a file.
49
45
 
50
- def initialize(
51
- self,
52
- envs: dict[str, str] | None = None,
53
- env_prefix: str = "",
54
- extra_dynamic: dict[str, Any] | None = None,
55
- ) -> Self:
56
- """Initialize a configuration.
57
-
58
- The initialization adds environment variables and parses dynamic values.
46
+ If not_exist_ok is True, then a missing file returns an empty
47
+ configuration. This may be useful if the configuration is potentially
48
+ populated by environment variables.
59
49
  """
60
- self = self if envs is None else self.add_envs(envs, prefix=env_prefix) # noqa: PLW0642
61
- return self.parse_dynamic(extra_dynamic)
62
-
63
- def with_model(self, model: type[ModelT]) -> ModelT:
64
- """Apply a pydantic model to a configuration."""
65
- return self.validate_model(model).convert_model(model)
50
+ config_dict = loaders.from_file(
51
+ file_path, loader=loader, not_exist_ok=not_exist_ok
52
+ )
53
+ return cls(config_dict)
66
54
 
67
55
  def __getitem__(self, key: str) -> Any: # noqa: ANN401
68
56
  """Make sure nested sections have type Configuration."""
@@ -112,30 +100,6 @@ class Configuration(UserDict[str, Any]):
112
100
  cls = type(self)
113
101
  return self | {prefix: cls(self.setdefault(prefix, {})).add(rest, value)}
114
102
 
115
- def add_envs(self, envs: dict[str, str] | None = None, prefix: str = "") -> Self:
116
- """Add environment variables to configuration.
117
-
118
- If you don't specify which environment variables to read, you'll
119
- automatically add any that matches a top-level value of the
120
- configuration.
121
- """
122
- if envs is None:
123
- # Automatically add top-level configuration items
124
- envs = {
125
- re.sub(r"\W", "_", key).upper(): key
126
- for key, value in self.data.items()
127
- if isinstance(value, str | int | float)
128
- }
129
-
130
- # Read environment variables
131
- for env, key in envs.items():
132
- env_key = f"{prefix}{env}"
133
- if env_value := os.getenv(env_key):
134
- self = self.add(key, env_value) # noqa: PLW0642
135
- elif key not in self:
136
- raise MissingEnvironmentVariableError(env_key)
137
- return self
138
-
139
103
  def parse_dynamic(
140
104
  self, extra: dict[str, Any] | None = None, *, _include_self: bool = True
141
105
  ) -> Self:
@@ -163,6 +127,60 @@ class Configuration(UserDict[str, Any]):
163
127
  # Continue parsing until no more replacements are made.
164
128
  return parsed.parse_dynamic(extra=extra, _include_self=_include_self)
165
129
 
130
+ def add_envs(self, envs: dict[str, str] | None = None, prefix: str = "") -> Self:
131
+ """Add environment variables to configuration.
132
+
133
+ If you don't specify which environment variables to read, you'll
134
+ automatically add any that matches a simple top-level value of the
135
+ configuration.
136
+ """
137
+ if envs is None:
138
+ # Automatically add top-level configuration items
139
+ envs = {
140
+ re.sub(r"\W", "_", key).upper(): key
141
+ for key, value in self.data.items()
142
+ if isinstance(value, str | int | float)
143
+ }
144
+
145
+ # Read environment variables
146
+ for env, key in envs.items():
147
+ env_key = f"{prefix}{env}"
148
+ if env_value := os.getenv(env_key):
149
+ self = self.add(key, env_value) # noqa: PLW0642
150
+ elif key not in self:
151
+ raise MissingEnvironmentVariableError(env_key)
152
+ return self
153
+
154
+ def add_envs_from_model(
155
+ self,
156
+ model: type[BaseModel],
157
+ prefix: str = "",
158
+ types: type | UnionType = str | bool | int | float,
159
+ ) -> Self:
160
+ """Add environment variables to configuration based on the given model.
161
+
162
+ Top level string, bool, integer, and float fields from the model are
163
+ looked for among environment variables.
164
+ """
165
+
166
+ def _get_class_from_annotation(annotation: type) -> type:
167
+ """Unpack generic annotations and return the underlying class."""
168
+ return (
169
+ _get_class_from_annotation(annotation.__origin__)
170
+ if hasattr(annotation, "__origin__")
171
+ else annotation
172
+ )
173
+
174
+ envs = {
175
+ re.sub(r"\W", "_", key).upper(): key
176
+ for key, field in model.model_fields.items()
177
+ if (
178
+ field.annotation is not None
179
+ and issubclass(_get_class_from_annotation(field.annotation), types)
180
+ )
181
+ }
182
+ return self.add_envs(envs, prefix=prefix)
183
+
166
184
  def validate_model(self, model: type[BaseModel]) -> Self:
167
185
  """Validate the configuration against the given model."""
168
186
  model.model_validate(self.data)
@@ -172,6 +190,10 @@ class Configuration(UserDict[str, Any]):
172
190
  """Convert data types to match the given model."""
173
191
  return model(**self.data)
174
192
 
193
+ def with_model(self, model: type[ModelT]) -> ModelT:
194
+ """Apply a pydantic model to a configuration."""
195
+ return self.validate_model(model).convert_model(model)
196
+
175
197
  def to_dict(self) -> dict[str, Any]:
176
198
  """Dump the configuration into a Python dictionary."""
177
199
  return {
@@ -217,9 +239,7 @@ def _get_rich_print() -> Callable[[str], None]: # pragma: no cover
217
239
 
218
240
  return Console().print
219
241
  except ImportError:
220
- import builtins # noqa: PLC0415
221
-
222
- return builtins.print
242
+ return print
223
243
 
224
244
 
225
245
  def _print_dict_as_tree(
@@ -26,9 +26,14 @@ def loader_names() -> list[str]:
26
26
  return sorted(pyplugs.names(PACKAGE))
27
27
 
28
28
 
29
- def from_file(path: str | Path, loader: str | None = None) -> dict[str, Any]:
29
+ def from_file(
30
+ path: str | Path, *, loader: str | None = None, not_exist_ok: bool = False
31
+ ) -> dict[str, Any]:
30
32
  """Load a file using a loader defined by the suffix if necessary."""
31
33
  path = Path(path)
34
+ if not path.exists() and not_exist_ok:
35
+ return {}
36
+
32
37
  loader = path.suffix.lstrip(".") if loader is None else loader
33
38
  try:
34
39
  return load(loader, path=path)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: configaroo
3
- Version: 0.3.0
3
+ Version: 0.4.1
4
4
  Summary: Bouncy handling of configuration files
5
5
  Author-email: Geir Arne Hjelle <geirarne@gmail.com>
6
6
  Maintainer-email: Geir Arne Hjelle <geirarne@gmail.com>
@@ -8,12 +8,6 @@ import configaroo
8
8
  from configaroo import Configuration, configuration
9
9
 
10
10
 
11
- @pytest.fixture
12
- def file_path() -> Path:
13
- """Return the path to the current file."""
14
- return Path(__file__).resolve()
15
-
16
-
17
11
  def test_read_simple_values_as_attributes(config: Configuration) -> None:
18
12
  """Test attribute access for simple values."""
19
13
  assert config.number == 42
@@ -1,6 +1,7 @@
1
1
  """Test handling of environment variables."""
2
2
 
3
3
  import pytest
4
+ from pydantic import BaseModel, SecretStr
4
5
 
5
6
  from configaroo import Configuration, MissingEnvironmentVariableError
6
7
 
@@ -25,6 +26,7 @@ def test_several_envs(config: Configuration, monkeypatch: pytest.MonkeyPatch) ->
25
26
  """Test that we can read several environment variables."""
26
27
  monkeypatch.setenv("WORD", "platypus")
27
28
  monkeypatch.setenv("NEW_PATH", "files/config.json")
29
+
28
30
  config_w_env = config.add_envs({"WORD": "nested.word", "NEW_PATH": "path"})
29
31
  assert config_w_env.nested.word == "platypus"
30
32
  assert config_w_env.path == "files/config.json"
@@ -48,6 +50,7 @@ def test_env_prefix(config: Configuration, monkeypatch: pytest.MonkeyPatch) -> N
48
50
  """Test that a common prefix can be used for environment variables."""
49
51
  monkeypatch.setenv("EXAMPLE_NUMBER", "14")
50
52
  monkeypatch.setenv("EXAMPLE_WORD", "platypus")
53
+
51
54
  config_w_env = config.add_envs(
52
55
  {"NUMBER": "number", "WORD": "nested.word"}, prefix="EXAMPLE_"
53
56
  )
@@ -61,9 +64,65 @@ def test_env_automatic(config: Configuration, monkeypatch: pytest.MonkeyPatch) -
61
64
  monkeypatch.setenv("WORD", "kangaroo")
62
65
  monkeypatch.setenv("A_B_D_KEY_", "works")
63
66
  monkeypatch.setenv("NESTED", "should not be replaced")
64
- config_w_env = (config | {"A b@d-key!": ""}).add_envs()
65
67
 
68
+ config_w_env = (config | {"A b@d-key!": ""}).add_envs()
66
69
  assert config_w_env.number == "28"
67
70
  assert config_w_env.word == "kangaroo"
68
71
  assert config_w_env["A b@d-key!"] == "works"
69
72
  assert config_w_env.nested != "should not be replaced"
73
+
74
+
75
+ def test_env_from_model(monkeypatch: pytest.MonkeyPatch) -> None:
76
+ """Test that environment variables can be found and set based on a model."""
77
+
78
+ class TestModel(BaseModel):
79
+ first_name: str
80
+ age: int
81
+ check: bool
82
+ scores: dict[str, int]
83
+
84
+ monkeypatch.setenv("TEST_FIRST_NAME", "Michael J.")
85
+ monkeypatch.setenv("TEST_AGE", "47")
86
+ monkeypatch.setenv("TEST_CHECK", "true")
87
+ monkeypatch.setenv("TEST_SCORES", '{"England": 1, "Spain": 0}')
88
+ monkeypatch.setenv("TEST_UNKNOWN", "Will be ignored")
89
+
90
+ config_w_env = Configuration().add_envs_from_model(TestModel, prefix="TEST_")
91
+ assert config_w_env.first_name == "Michael J."
92
+ assert config_w_env.age == "47"
93
+ assert config_w_env.check == "true"
94
+ assert "scores" not in config_w_env.data # Complex types are ignored
95
+ assert "unknown" not in config_w_env.data # Unspecified fields are ignored
96
+
97
+
98
+ def test_env_from_model_w_custom_types(monkeypatch: pytest.MonkeyPatch) -> None:
99
+ """Test that custom types can be used when discovering env variables from models."""
100
+
101
+ class TestModel(BaseModel):
102
+ first_name: str
103
+ age: int
104
+ hush: SecretStr
105
+ countries: list[str]
106
+
107
+ monkeypatch.setenv("TEST_FIRST_NAME", "Michael J.")
108
+ monkeypatch.setenv("TEST_AGE", "47")
109
+ monkeypatch.setenv("TEST_HUSH", "hush-hush")
110
+ monkeypatch.setenv("TEST_COUNTRIES", "England Australia Norway Spain")
111
+
112
+ config_w_env = Configuration().add_envs_from_model(
113
+ TestModel, prefix="TEST_", types=str | SecretStr | list
114
+ )
115
+ assert config_w_env.first_name == "Michael J."
116
+ assert config_w_env.hush == "hush-hush"
117
+ assert config_w_env.countries.split() == ["England", "Australia", "Norway", "Spain"]
118
+ assert "age" not in config_w_env # int is not specified as a type to include
119
+
120
+
121
+ def test_env_from_model_raises_if_missing() -> None:
122
+ """Test that missing environment variables defined in a model raises an error."""
123
+
124
+ class TestModel(BaseModel):
125
+ nonexistent_env: str
126
+
127
+ with pytest.raises(MissingEnvironmentVariableError):
128
+ Configuration().add_envs_from_model(TestModel)
@@ -38,6 +38,12 @@ def test_error_on_wrong_format(toml_path: Path) -> None:
38
38
  Configuration.from_file(toml_path, loader="json")
39
39
 
40
40
 
41
+ def test_file_may_be_allowed_to_not_exist() -> None:
42
+ """Test that not_exist_ok can suppress error when file doesn't exist."""
43
+ config = Configuration.from_file("non-existent.json", not_exist_ok=True)
44
+ assert config.data == {}
45
+
46
+
41
47
  def test_can_read_json_values(json_path: Path) -> None:
42
48
  """Test that values can be accessed."""
43
49
  config = Configuration.from_file(json_path)
@@ -38,6 +38,12 @@ def test_error_on_wrong_format(json_path: Path) -> None:
38
38
  Configuration.from_file(json_path, loader="toml")
39
39
 
40
40
 
41
+ def test_file_may_be_allowed_to_not_exist() -> None:
42
+ """Test that not_exist_ok can suppress error when file doesn't exist."""
43
+ config = Configuration.from_file("non-existent.toml", not_exist_ok=True)
44
+ assert config.data == {}
45
+
46
+
41
47
  def test_can_read_toml_values(toml_path: Path) -> None:
42
48
  """Test that values can be accessed."""
43
49
  config = Configuration.from_file(toml_path)
File without changes
File without changes
File without changes