configaroo 0.1.3__py3-none-any.whl → 0.2.0__py3-none-any.whl

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.
configaroo/__init__.py CHANGED
@@ -7,4 +7,4 @@ from configaroo.exceptions import ( # noqa
7
7
  UnsupportedLoaderError,
8
8
  )
9
9
 
10
- __version__ = "0.1.3"
10
+ __version__ = "0.2.0"
@@ -5,13 +5,15 @@ import os
5
5
  import re
6
6
  from collections import UserDict
7
7
  from pathlib import Path
8
- from typing import Any, Self, Type
8
+ from typing import Any, Self, Type, TypeVar
9
9
 
10
10
  from pydantic import BaseModel
11
11
 
12
12
  from configaroo import loaders
13
13
  from configaroo.exceptions import MissingEnvironmentVariableError
14
14
 
15
+ ModelT = TypeVar("ModelT", bound=BaseModel)
16
+
15
17
 
16
18
  class Configuration(UserDict):
17
19
  """A Configuration is a dict-like structure with some conveniences"""
@@ -24,12 +26,11 @@ class Configuration(UserDict):
24
26
  envs: dict[str, str] | None = None,
25
27
  env_prefix: str = "",
26
28
  extra_dynamic: dict[str, Any] | None = None,
27
- model: Type[BaseModel] | None = None,
28
29
  ) -> Self:
29
30
  """Read a Configuration from a file"""
30
31
  config_dict = loaders.from_file(file_path, loader=loader)
31
32
  return cls(config_dict).initialize(
32
- envs=envs, env_prefix=env_prefix, extra_dynamic=extra_dynamic, model=model
33
+ envs=envs, env_prefix=env_prefix, extra_dynamic=extra_dynamic
33
34
  )
34
35
 
35
36
  def initialize(
@@ -37,18 +38,17 @@ class Configuration(UserDict):
37
38
  envs: dict[str, str] | None = None,
38
39
  env_prefix: str = "",
39
40
  extra_dynamic: dict[str, Any] | None = None,
40
- model: Type[BaseModel] | None = None,
41
41
  ) -> Self:
42
42
  """Initialize a configuration.
43
43
 
44
- The initialization adds environment variables, parses dynamic values,
45
- validates against a Pydantic model, and converts value types using the
46
- same model.
44
+ The initialization adds environment variables and parses dynamic values.
47
45
  """
48
46
  self = self if envs is None else self.add_envs(envs, prefix=env_prefix)
49
- self = self.parse_dynamic(extra_dynamic)
50
- self = self if model is None else self.validate(model).convert(model)
51
- return self
47
+ return self.parse_dynamic(extra_dynamic)
48
+
49
+ def with_model(self, model: Type[ModelT]) -> ModelT:
50
+ """Apply a pydantic model to a configuration."""
51
+ return self.validate_model(model).convert_model(model)
52
52
 
53
53
  def __getitem__(self, key: str) -> Any:
54
54
  """Make sure nested sections have type Configuration"""
@@ -129,15 +129,14 @@ class Configuration(UserDict):
129
129
  }
130
130
  )
131
131
 
132
- def validate(self, model: Type[BaseModel]) -> Self:
132
+ def validate_model(self, model: Type[BaseModel]) -> Self:
133
133
  """Validate the configuration against the given model."""
134
134
  model.model_validate(self.data)
135
135
  return self
136
136
 
137
- def convert(self, model: Type[BaseModel]) -> Self:
137
+ def convert_model(self, model: Type[ModelT]) -> ModelT:
138
138
  """Convert data types to match the given model"""
139
- cls = type(self)
140
- return cls(model(**self.data).model_dump())
139
+ return model(**self.data)
141
140
 
142
141
  def to_dict(self) -> dict[str, Any]:
143
142
  """Dump the configuration into a Python dictionary"""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: configaroo
3
- Version: 0.1.3
3
+ Version: 0.2.0
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>
@@ -0,0 +1,12 @@
1
+ configaroo/__init__.py,sha256=RNKHr1va5QmIdwDkkiGTnF8rR7EJMZZRfPunREdKjE4,255
2
+ configaroo/configuration.py,sha256=lEpOBf3qwRFAM0T9Ur9Og1fYy65PzIY6xs1xSNsrHEM,7492
3
+ configaroo/exceptions.py,sha256=1h-6hV7VqqKaRFnu539pjsO0ESSNDibt2kUeF7jAM2M,374
4
+ configaroo/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ configaroo/loaders/__init__.py,sha256=-6GAR4g7vYW7Zagq9Ev479kpkfY7kbvDl4JPXd4wQQ0,756
6
+ configaroo/loaders/json.py,sha256=6gs_ZegUEe70vb6uSJfKBos7UjfuJnyWgPE_kjDQISw,258
7
+ configaroo/loaders/toml.py,sha256=ZVbHutZ7V-Z_jf2aHjV18jwaqCXumlr06j48rojHcBM,264
8
+ configaroo-0.2.0.dist-info/licenses/LICENSE,sha256=2cbzsB4lU8qOO8fO1XVNJD7XPxzPhaWXm6U9DenSc0s,1089
9
+ configaroo-0.2.0.dist-info/METADATA,sha256=JfEO1NNJw5qCXIB64Uf1fqdipAlnpkKCpLYpEbfJZGU,1789
10
+ configaroo-0.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ configaroo-0.2.0.dist-info/top_level.txt,sha256=JVYICl1cWSjvSOZuZMYm976z9lnZaWtHVRSt373QCxg,11
12
+ configaroo-0.2.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.8.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,12 +0,0 @@
1
- configaroo/__init__.py,sha256=aiOAkxMR9x3VgmQI9ppQ8uzx-gHGvZ-AjQabE1HpZ2E,255
2
- configaroo/configuration.py,sha256=PJLWR2mA_bYM7iuRXqfBQ_rnZoughNCFA5kAEjjZz8A,7591
3
- configaroo/exceptions.py,sha256=1h-6hV7VqqKaRFnu539pjsO0ESSNDibt2kUeF7jAM2M,374
4
- configaroo/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- configaroo/loaders/__init__.py,sha256=-6GAR4g7vYW7Zagq9Ev479kpkfY7kbvDl4JPXd4wQQ0,756
6
- configaroo/loaders/json.py,sha256=6gs_ZegUEe70vb6uSJfKBos7UjfuJnyWgPE_kjDQISw,258
7
- configaroo/loaders/toml.py,sha256=ZVbHutZ7V-Z_jf2aHjV18jwaqCXumlr06j48rojHcBM,264
8
- configaroo-0.1.3.dist-info/licenses/LICENSE,sha256=2cbzsB4lU8qOO8fO1XVNJD7XPxzPhaWXm6U9DenSc0s,1089
9
- configaroo-0.1.3.dist-info/METADATA,sha256=kv0NQFHsskjUQBuDGe56FdZOUh4FtUWYzbZ3Y5fvju4,1789
10
- configaroo-0.1.3.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
11
- configaroo-0.1.3.dist-info/top_level.txt,sha256=JVYICl1cWSjvSOZuZMYm976z9lnZaWtHVRSt373QCxg,11
12
- configaroo-0.1.3.dist-info/RECORD,,