Replace {name} placeholders in `template`, quoting each value with `quoter`. Placeholder names are looked up in `values`. Raises: KeyError: If a placeholder is not found in `values`.
(
template: str,
values: Mapping[str, Any],
quoter: Callable[[str], str],
)
| 45 | |
| 46 | |
| 47 | def _interpolate( |
| 48 | template: str, |
| 49 | values: Mapping[str, Any], |
| 50 | quoter: Callable[[str], str], |
| 51 | ) -> str: |
| 52 | class="st">"""Replace {name} placeholders in `template`, quoting each value with `quoter`. |
| 53 | |
| 54 | Placeholder names are looked up in `values`. |
| 55 | |
| 56 | Raises: |
| 57 | KeyError: If a placeholder is not found in `values`. |
| 58 | class="st">""" |
| 59 | class="cm"># re.split with a capturing group returns alternating |
| 60 | class="cm"># [text, name, text, name, ..., text] elements. |
| 61 | parts = _PLACEHOLDER_RE.split(template) |
| 62 | |
| 63 | for i in range(1, len(parts), 2): |
| 64 | name = parts[i] |
| 65 | if name not in values: |
| 66 | raise KeyError(fclass="st">"a value for placeholder {{{name}}} was not provided") |
| 67 | val = values[name] |
| 68 | if val is None: |
| 69 | parts[i] = class="st">"null" |
| 70 | elif isinstance(val, bool): |
| 71 | parts[i] = class="st">"true" if val else class="st">"false" |
| 72 | else: |
| 73 | parts[i] = quoter(str(values[name])) |
| 74 | |
| 75 | return class="st">"".join(parts) |
| 76 | |
| 77 | |
| 78 | def path_template(template: str, /, **kwargs: Any) -> str: |