|
| 1 | +"""Support for TOML config sources. |
| 2 | +
|
| 3 | +This is experimental API! Expect things to be broken. |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from collections import defaultdict |
| 9 | +from itertools import chain |
| 10 | +from typing import TYPE_CHECKING, Iterable, Iterator |
| 11 | + |
| 12 | +import tomllib |
| 13 | + |
| 14 | +from tox.config.loader.ini.factor import find_envs |
| 15 | +from tox.config.loader.section import Section |
| 16 | +from tox.config.loader.toml import TomlLoader |
| 17 | + |
| 18 | +from .api import Source |
| 19 | +from .ini_section import CORE, PKG_ENV_PREFIX, TEST_ENV_PREFIX, IniSection |
| 20 | + |
| 21 | +if TYPE_CHECKING: |
| 22 | + from pathlib import Path |
| 23 | + |
| 24 | + from tox.config.loader.api import OverrideMap |
| 25 | + from tox.config.sets import ConfigSet |
| 26 | + |
| 27 | + |
| 28 | +class TomlSource(Source): |
| 29 | + """Configuration sourced from a toml file (such as tox.toml). |
| 30 | +
|
| 31 | + This is experimental API! Expect things to be broken. |
| 32 | + """ |
| 33 | + |
| 34 | + CORE_SECTION = CORE |
| 35 | + |
| 36 | + def __init__(self, path: Path, content: str | None = None) -> None: |
| 37 | + super().__init__(path) |
| 38 | + if content is None: |
| 39 | + if not path.exists(): |
| 40 | + raise ValueError |
| 41 | + content = path.read_text() |
| 42 | + self._raw = tomllib.loads(content) |
| 43 | + self._section_mapping: defaultdict[str, list[str]] = defaultdict(list) |
| 44 | + |
| 45 | + def __repr__(self) -> str: |
| 46 | + return f"{type(self).__name__}(path={self.path})" |
| 47 | + |
| 48 | + def transform_section(self, section: Section) -> Section: # noqa: PLR6301 |
| 49 | + return IniSection(section.prefix, section.name) |
| 50 | + |
| 51 | + def get_loader(self, section: Section, override_map: OverrideMap) -> TomlLoader | None: |
| 52 | + # look up requested section name in the generative testenv mapping to find the real config source |
| 53 | + for key in self._section_mapping.get(section.name) or []: |
| 54 | + if section.prefix is None or Section.from_key(key).prefix == section.prefix: |
| 55 | + break |
| 56 | + else: |
| 57 | + # if no matching section/prefix is found, use the requested section key as-is (for custom prefixes) |
| 58 | + key = section.key |
| 59 | + if key in self._raw: |
| 60 | + return TomlLoader( |
| 61 | + section=section, |
| 62 | + raw=self._raw, |
| 63 | + overrides=override_map.get(section.key, []), |
| 64 | + core_section=self.CORE_SECTION, |
| 65 | + section_key=key, |
| 66 | + ) |
| 67 | + return None |
| 68 | + |
| 69 | + def get_base_sections(self, base: list[str], in_section: Section) -> Iterator[Section]: # noqa: PLR6301 |
| 70 | + for a_base in base: |
| 71 | + section = IniSection.from_key(a_base) |
| 72 | + yield section # the base specifier is explicit |
| 73 | + if in_section.prefix is not None: # no prefix specified, so this could imply our own prefix |
| 74 | + yield IniSection(in_section.prefix, a_base) |
| 75 | + |
| 76 | + def sections(self) -> Iterator[IniSection]: |
| 77 | + for key in self._raw: |
| 78 | + yield IniSection.from_key(key) |
| 79 | + |
| 80 | + def envs(self, core_config: ConfigSet) -> Iterator[str]: |
| 81 | + seen = set() |
| 82 | + for name in self._discover_tox_envs(core_config): |
| 83 | + if name not in seen: |
| 84 | + seen.add(name) |
| 85 | + yield name |
| 86 | + |
| 87 | + def _discover_tox_envs(self, core_config: ConfigSet) -> Iterator[str]: |
| 88 | + def register_factors(envs: Iterable[str]) -> None: |
| 89 | + known_factors.update(chain.from_iterable(e.split("-") for e in envs)) |
| 90 | + |
| 91 | + explicit = list(core_config["env_list"]) |
| 92 | + yield from explicit |
| 93 | + known_factors: set[str] = set() |
| 94 | + register_factors(explicit) |
| 95 | + |
| 96 | + # discover all additional defined environments, including generative section headers |
| 97 | + for section in self.sections(): |
| 98 | + if section.is_test_env: |
| 99 | + register_factors(section.names) |
| 100 | + for name in section.names: |
| 101 | + self._section_mapping[name].append(section.key) |
| 102 | + yield name |
| 103 | + # add all conditional markers that are not part of the explicitly defined sections |
| 104 | + for section in self.sections(): |
| 105 | + yield from self._discover_from_section(section, known_factors) |
| 106 | + |
| 107 | + def _discover_from_section(self, section: IniSection, known_factors: set[str]) -> Iterator[str]: |
| 108 | + for value in self._raw[section.key].values(): |
| 109 | + if isinstance(value, bool): |
| 110 | + # It's not a value with env definition. |
| 111 | + continue |
| 112 | + # XXX: We munch the value to multiline string to parse it by the library utils. |
| 113 | + merged_value = "\n".join(str(v) for v in tuple(value)) |
| 114 | + for env in find_envs(merged_value): |
| 115 | + if set(env.split("-")) - known_factors: |
| 116 | + yield env |
| 117 | + |
| 118 | + def get_tox_env_section(self, item: str) -> tuple[Section, list[str], list[str]]: # noqa: PLR6301 |
| 119 | + return IniSection.test_env(item), [TEST_ENV_PREFIX], [PKG_ENV_PREFIX] |
| 120 | + |
| 121 | + def get_core_section(self) -> Section: |
| 122 | + return self.CORE_SECTION |
| 123 | + |
| 124 | + |
| 125 | +class ToxToml(TomlSource): |
| 126 | + """Configuration sourced from a tox.toml file. |
| 127 | +
|
| 128 | + This is experimental API! Expect things to be broken. |
| 129 | + """ |
| 130 | + |
| 131 | + FILENAME = "tox.toml" |
| 132 | + |
| 133 | + |
| 134 | +# TODO: Section model is way too configparser precific for this to work easily. |
| 135 | +# class PyProjectToml(TomlSource): |
| 136 | +# """Configuration sourced from a pyproject.toml file. |
| 137 | + |
| 138 | +# This is experimental API! Expect things to be broken. |
| 139 | +# """ |
| 140 | + |
| 141 | +# FILENAME = "pyproject.toml" |
| 142 | +# CORE_SECTION = IniSection("tool", "tox") |
0 commit comments