|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import TYPE_CHECKING, Dict, Iterator, List, Mapping, Set, TypeVar, cast |
| 5 | + |
| 6 | +from tox.config.loader.api import Loader, Override |
| 7 | +from tox.config.types import Command, EnvList |
| 8 | + |
| 9 | +from ._api import TomlTypes |
| 10 | +from ._validate import validate |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from tox.config.loader.section import Section |
| 14 | + from tox.config.main import Config |
| 15 | + |
| 16 | +_T = TypeVar("_T") |
| 17 | +_V = TypeVar("_V") |
| 18 | + |
| 19 | + |
| 20 | +class TomlLoader(Loader[TomlTypes]): |
| 21 | + """Load configuration from a pyproject.toml file.""" |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + section: Section, |
| 26 | + overrides: list[Override], |
| 27 | + content: Mapping[str, TomlTypes], |
| 28 | + unused_exclude: set[str], |
| 29 | + ) -> None: |
| 30 | + self.content = content |
| 31 | + self._unused_exclude = unused_exclude |
| 32 | + super().__init__(section, overrides) |
| 33 | + |
| 34 | + def __repr__(self) -> str: |
| 35 | + return f"{self.__class__.__name__}({self.section.name}, {self.content!r})" |
| 36 | + |
| 37 | + def load_raw(self, key: str, conf: Config | None, env_name: str | None) -> TomlTypes: # noqa: ARG002 |
| 38 | + return self.content[key] |
| 39 | + |
| 40 | + def found_keys(self) -> set[str]: |
| 41 | + return set(self.content.keys()) - self._unused_exclude |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def to_str(value: TomlTypes) -> str: |
| 45 | + return validate(value, str) # type: ignore[return-value] # no mypy support |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def to_bool(value: TomlTypes) -> bool: |
| 49 | + return validate(value, bool) |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def to_list(value: TomlTypes, of_type: type[_T]) -> Iterator[_T]: |
| 53 | + of = List[of_type] # type: ignore[valid-type] # no mypy support |
| 54 | + return iter(validate(value, of)) # type: ignore[call-overload,no-any-return] |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def to_set(value: TomlTypes, of_type: type[_T]) -> Iterator[_T]: |
| 58 | + of = Set[of_type] # type: ignore[valid-type] # no mypy support |
| 59 | + return iter(validate(value, of)) # type: ignore[call-overload,no-any-return] |
| 60 | + |
| 61 | + @staticmethod |
| 62 | + def to_dict(value: TomlTypes, of_type: tuple[type[_T], type[_V]]) -> Iterator[tuple[_T, _V]]: |
| 63 | + of = Dict[of_type[0], of_type[1]] # type: ignore[valid-type] # no mypy support |
| 64 | + return validate(value, of).items() # type: ignore[attr-defined,no-any-return] |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def to_path(value: TomlTypes) -> Path: |
| 68 | + return Path(TomlLoader.to_str(value)) |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def to_command(value: TomlTypes) -> Command: |
| 72 | + return Command(args=cast(List[str], value)) # validated during load in _ensure_type_correct |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + def to_env_list(value: TomlTypes) -> EnvList: |
| 76 | + return EnvList(envs=list(TomlLoader.to_list(value, str))) |
| 77 | + |
| 78 | + |
| 79 | +__all__ = [ |
| 80 | + "TomlLoader", |
| 81 | +] |
0 commit comments