stdlibx-config 0.1.0__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Lucino772
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.4
2
+ Name: stdlibx-config
3
+ Version: 0.1.0
4
+ Summary: stdlibx-config
5
+ Author: Lucino772
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Requires-Dist: ruamel-yaml>=0.18.16 ; extra == 'yaml'
9
+ Requires-Python: >=3.9
10
+ Project-URL: Documentation, http://stdlibx.lucapalmi.com/
11
+ Project-URL: Repository, https://github.com/Lucino772/stdlibx
12
+ Provides-Extra: yaml
13
+ Description-Content-Type: text/markdown
14
+
15
+ [![docs](https://github.com/Lucino772/stdlibx/actions/workflows/deploy-docs.yaml/badge.svg?branch=main)](https://github.com/Lucino772/stdlibx/actions/workflows/deploy-docs.yaml)
16
+
17
+ # stdlibx
18
+ **stdlibx** is a collection of small, focused Python utilities that simplify common programming patterns and help you write clearer, more composable code.
19
+
20
+ It provides practical tools for handling optional values, explicit error management, cancellation, composition, configuration, pattern matching, and reactive flows.
21
+
22
+ ## Documentation
23
+ Checkout the [documentation](http://stdlibx.lucapalmi.com/)
24
+
25
+ ## Licence
26
+ This project uses a **MIT** Licence [view](https://github.com/Lucino772/stdlibx/blob/main/LICENSE)
@@ -0,0 +1,12 @@
1
+ [![docs](https://github.com/Lucino772/stdlibx/actions/workflows/deploy-docs.yaml/badge.svg?branch=main)](https://github.com/Lucino772/stdlibx/actions/workflows/deploy-docs.yaml)
2
+
3
+ # stdlibx
4
+ **stdlibx** is a collection of small, focused Python utilities that simplify common programming patterns and help you write clearer, more composable code.
5
+
6
+ It provides practical tools for handling optional values, explicit error management, cancellation, composition, configuration, pattern matching, and reactive flows.
7
+
8
+ ## Documentation
9
+ Checkout the [documentation](http://stdlibx.lucapalmi.com/)
10
+
11
+ ## Licence
12
+ This project uses a **MIT** Licence [view](https://github.com/Lucino772/stdlibx/blob/main/LICENSE)
@@ -0,0 +1,28 @@
1
+ [project]
2
+ name = "stdlibx-config"
3
+ version = "0.1.0"
4
+ description = "stdlibx-config"
5
+ license = "MIT"
6
+ license-files = ["LICEN[CS]E*"]
7
+ readme = "README.md"
8
+ authors = [
9
+ { name = "Lucino772" }
10
+ ]
11
+ requires-python = ">=3.9"
12
+ dependencies = []
13
+
14
+ [project.optional-dependencies]
15
+ yaml = [
16
+ "ruamel-yaml>=0.18.16",
17
+ ]
18
+
19
+ [project.urls]
20
+ Documentation = "http://stdlibx.lucapalmi.com/"
21
+ Repository = "https://github.com/Lucino772/stdlibx"
22
+
23
+ [tool.uv.build-backend]
24
+ module-name = "stdlibx.config"
25
+
26
+ [build-system]
27
+ requires = ["uv_build>=0.9.28,<0.10.0"]
28
+ build-backend = "uv_build"
@@ -0,0 +1,14 @@
1
+ from stdlibx.config._lib import load_config
2
+ from stdlibx.config._types import Loader
3
+ from stdlibx.config.loaders.env import EnvLoader
4
+ from stdlibx.config.loaders.file import FileLoader
5
+ from stdlibx.config.loaders.json import JsonLoader
6
+
7
+ __all__ = ["EnvLoader", "FileLoader", "JsonLoader", "Loader", "load_config"]
8
+
9
+ try:
10
+ from stdlibx.config.loaders.yaml import YamlLoader
11
+
12
+ __all__ += ["YamlLoader"]
13
+ except ImportError:
14
+ pass
@@ -0,0 +1,31 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import (
4
+ TYPE_CHECKING,
5
+ Any,
6
+ Callable,
7
+ Mapping,
8
+ Protocol,
9
+ TypeVar,
10
+ runtime_checkable,
11
+ )
12
+
13
+ if TYPE_CHECKING:
14
+ from stdlibx.config._types import Loader
15
+
16
+ T = TypeVar("T", covariant=True)
17
+
18
+
19
+ @runtime_checkable
20
+ class _PydanticValidator(Protocol[T]):
21
+ def model_validate(self, obj: Any, *, from_attributes: bool) -> T: ...
22
+
23
+
24
+ def load_config(
25
+ validator: _PydanticValidator[T] | Callable[[Mapping[str, Any]], T],
26
+ loaders: list[Loader],
27
+ ) -> T:
28
+ _ret = {key: val for loader in loaders for key, val in loader.load().items()}
29
+ if isinstance(validator, _PydanticValidator):
30
+ return validator.model_validate(_ret, from_attributes=False)
31
+ return validator(_ret)
@@ -0,0 +1,12 @@
1
+ from collections.abc import Mapping
2
+ from typing import Any, Protocol, TypeVar
3
+
4
+ T = TypeVar("T", covariant=True)
5
+
6
+
7
+ class Loader(Protocol):
8
+ def load(self) -> Mapping[str, Any]: ...
9
+
10
+
11
+ class SupportsRead(Protocol[T]):
12
+ def read(self, length: int = ..., /) -> T: ...
@@ -0,0 +1,39 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ from typing import Any, Mapping
5
+
6
+
7
+ class EnvLoader:
8
+ def __init__(
9
+ self,
10
+ environ: Mapping[str, str] | None = None,
11
+ env_prefix: str = "STDLIBX_",
12
+ env_nested_delimiter: str = "__",
13
+ ) -> None:
14
+ self.__environ = environ
15
+ self.__env_prefix = env_prefix
16
+ self.__env_nested_delimiter = env_nested_delimiter
17
+
18
+ def load(self) -> Mapping[str, Any]:
19
+ _environ = self.__environ or os.environ
20
+
21
+ _ret = {}
22
+ for key, value in (
23
+ (key.upper().strip(self.__env_prefix + "_").lower(), value)
24
+ for key, value in _environ.items()
25
+ if key.upper().startswith(self.__env_prefix)
26
+ ):
27
+ if self.__env_nested_delimiter not in key:
28
+ _ret[key] = value
29
+ else:
30
+ keys = key.split(self.__env_nested_delimiter)
31
+
32
+ _value = _ret
33
+ for _key in keys[:-1]:
34
+ if _key not in _value:
35
+ _value[_key] = {}
36
+ _value = _value[_key]
37
+ _value[keys[-1]] = value
38
+
39
+ return _ret
@@ -0,0 +1,29 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import TYPE_CHECKING, Any, Callable, Mapping
5
+
6
+ if TYPE_CHECKING:
7
+ from os import PathLike
8
+
9
+ from stdlibx.config._types import SupportsRead
10
+
11
+
12
+ class FileLoader:
13
+ def __init__(
14
+ self,
15
+ path: str | PathLike[str],
16
+ parser: Callable[[SupportsRead[bytes]], Mapping[str, Any]],
17
+ *,
18
+ missing_ok: bool = False,
19
+ ) -> None:
20
+ self.__path = Path(path)
21
+ self.__missing_ok = missing_ok
22
+ self.__parser = parser
23
+
24
+ def load(self) -> Mapping[str, Any]:
25
+ if self.__missing_ok is True and self.__path.exists() is False:
26
+ return {}
27
+
28
+ with open(self.__path, "rb") as fp:
29
+ return self.__parser(fp)
@@ -0,0 +1,14 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from typing import TYPE_CHECKING
5
+
6
+ from stdlibx.config.loaders.file import FileLoader
7
+
8
+ if TYPE_CHECKING:
9
+ from os import PathLike
10
+
11
+
12
+ class JsonLoader(FileLoader):
13
+ def __init__(self, path: str | PathLike[str], *, missing_ok: bool = False) -> None:
14
+ super().__init__(path, json.load, missing_ok=missing_ok)
@@ -0,0 +1,23 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Literal
4
+
5
+ from ruamel.yaml import YAML
6
+ from stdlibx.config.loaders.file import FileLoader
7
+
8
+ if TYPE_CHECKING:
9
+ from os import PathLike
10
+
11
+
12
+ class YamlLoader(FileLoader):
13
+ def __init__(
14
+ self,
15
+ path: str | PathLike[str],
16
+ *,
17
+ missing_ok: bool = False,
18
+ yaml_typ: Literal["rt", "safe", "unsafe", "full", "base"] | None = None,
19
+ yaml_pure: bool = False,
20
+ ) -> None:
21
+ super().__init__(
22
+ path, YAML(typ=yaml_typ, pure=yaml_pure).load, missing_ok=missing_ok
23
+ )