etlplus 0.15.0__py3-none-any.whl → 0.15.2__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.
etlplus/workflow/utils.py DELETED
@@ -1,120 +0,0 @@
1
- """
2
- :mod:`etlplus.config.utils` module.
3
-
4
- A module defining utility helpers for ETL pipeline configuration.
5
-
6
- Notes
7
- -----
8
- - Inputs to parsers favor ``Mapping[str, Any]`` to remain permissive and
9
- avoid unnecessary copies; normalization returns concrete types.
10
- - Substitution is shallow for strings and recursive for containers.
11
- - Numeric coercion helpers are intentionally forgiving: invalid values
12
- become ``None`` rather than raising.
13
- """
14
-
15
- from __future__ import annotations
16
-
17
- from collections.abc import Iterable
18
- from collections.abc import Mapping
19
- from typing import Any
20
-
21
- from ..types import StrAnyMap
22
-
23
- # SECTION: EXPORTS ========================================================== #
24
-
25
-
26
- __all__ = [
27
- # Functions
28
- 'deep_substitute',
29
- ]
30
-
31
-
32
- # SECTION: FUNCTIONS ======================================================== #
33
-
34
-
35
- def deep_substitute(
36
- value: Any,
37
- vars_map: StrAnyMap | None,
38
- env_map: Mapping[str, str] | None,
39
- ) -> Any:
40
- """
41
- Recursively substitute ``${VAR}`` tokens in nested structures.
42
-
43
- Only strings are substituted; other types are returned as-is.
44
-
45
- Parameters
46
- ----------
47
- value : Any
48
- The value to perform substitutions on.
49
- vars_map : StrAnyMap | None
50
- Mapping of variable names to replacement values (lower precedence).
51
- env_map : Mapping[str, str] | None
52
- Mapping of environment variables overriding ``vars_map`` values (higher
53
- precedence).
54
-
55
- Returns
56
- -------
57
- Any
58
- New structure with substitutions applied where tokens were found.
59
- """
60
- substitutions = _prepare_substitutions(vars_map, env_map)
61
-
62
- def _apply(node: Any) -> Any:
63
- match node:
64
- case str():
65
- return _replace_tokens(node, substitutions)
66
- case Mapping():
67
- return {k: _apply(v) for k, v in node.items()}
68
- case list() | tuple() as seq:
69
- apply = [_apply(item) for item in seq]
70
- return apply if isinstance(seq, list) else tuple(apply)
71
- case set():
72
- return {_apply(item) for item in node}
73
- case frozenset():
74
- return frozenset(_apply(item) for item in node)
75
- case _:
76
- return node
77
-
78
- return _apply(value)
79
-
80
-
81
- # SECTION: INTERNAL FUNCTIONS ============================================== #
82
-
83
-
84
- def _prepare_substitutions(
85
- vars_map: StrAnyMap | None,
86
- env_map: Mapping[str, Any] | None,
87
- ) -> tuple[tuple[str, Any], ...]:
88
- """Merge variable and environment maps into an ordered substitutions list.
89
-
90
- Parameters
91
- ----------
92
- vars_map : StrAnyMap | None
93
- Mapping of variable names to replacement values (lower precedence).
94
- env_map : Mapping[str, Any] | None
95
- Environment-backed values that override entries from ``vars_map``.
96
-
97
- Returns
98
- -------
99
- tuple[tuple[str, Any], ...]
100
- Immutable sequence of ``(name, value)`` pairs suitable for token
101
- replacement.
102
- """
103
- if not vars_map and not env_map:
104
- return ()
105
- merged: dict[str, Any] = {**(vars_map or {}), **(env_map or {})}
106
- return tuple(merged.items())
107
-
108
-
109
- def _replace_tokens(
110
- text: str,
111
- substitutions: Iterable[tuple[str, Any]],
112
- ) -> str:
113
- if not substitutions:
114
- return text
115
- out = text
116
- for name, replacement in substitutions:
117
- token = f'${{{name}}}'
118
- if token in out:
119
- out = out.replace(token, str(replacement))
120
- return out