|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from enum import Enum |
| 3 | +from os import PathLike |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import yaml |
| 7 | + |
| 8 | +from . import constants |
| 9 | +from .rapids_dependency_file_validator import validate_dependencies |
| 10 | + |
| 11 | + |
| 12 | +class Output(Enum): |
| 13 | + PYPROJECT = "pyproject" |
| 14 | + REQUIREMENTS = "requirements" |
| 15 | + CONDA = "conda" |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class FileExtras: |
| 20 | + table: str |
| 21 | + key: str | None = None |
| 22 | + |
| 23 | + |
| 24 | +@dataclass |
| 25 | +class File: |
| 26 | + output: set[Output] |
| 27 | + includes: list[str] |
| 28 | + extras: FileExtras | None = None |
| 29 | + matrix: dict[str, list[str]] = field(default_factory=dict) |
| 30 | + requirements_dir: Path = Path(constants.default_requirements_dir) |
| 31 | + conda_dir: Path = Path(constants.default_conda_dir) |
| 32 | + pyproject_dir: Path = Path(constants.default_pyproject_dir) |
| 33 | + |
| 34 | + |
| 35 | +@dataclass |
| 36 | +class PipRequirements: |
| 37 | + pip: list[str] |
| 38 | + |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class CommonDependencies: |
| 42 | + output_types: set[Output] |
| 43 | + packages: list[str | PipRequirements] |
| 44 | + |
| 45 | + |
| 46 | +@dataclass |
| 47 | +class MatrixMatcher: |
| 48 | + matrix: dict[str, str] |
| 49 | + packages: list[str | PipRequirements] |
| 50 | + |
| 51 | + |
| 52 | +@dataclass |
| 53 | +class SpecificDependencies: |
| 54 | + output_types: set[Output] |
| 55 | + matrices: list[MatrixMatcher] |
| 56 | + |
| 57 | + |
| 58 | +@dataclass |
| 59 | +class Dependencies: |
| 60 | + common: list[CommonDependencies] = field(default_factory=list) |
| 61 | + specific: list[SpecificDependencies] = field(default_factory=list) |
| 62 | + |
| 63 | + |
| 64 | +@dataclass |
| 65 | +class Config: |
| 66 | + path: Path |
| 67 | + files: dict[str, File] = field(default_factory=dict) |
| 68 | + channels: list[str] = field( |
| 69 | + default_factory=lambda: list(constants.default_channels) |
| 70 | + ) |
| 71 | + dependencies: dict[str, Dependencies] = field(default_factory=dict) |
| 72 | + |
| 73 | + |
| 74 | +def _parse_outputs(outputs: str | list[str]) -> set[Output]: |
| 75 | + if isinstance(outputs, str): |
| 76 | + outputs = [outputs] |
| 77 | + if outputs == ["none"]: |
| 78 | + outputs = [] |
| 79 | + return {Output(o) for o in outputs} |
| 80 | + |
| 81 | + |
| 82 | +def _parse_extras(extras: dict[str, str]) -> FileExtras: |
| 83 | + return FileExtras( |
| 84 | + table=extras["table"], |
| 85 | + key=extras.get("key", None), |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def _parse_file(file_config: dict[str, object]) -> File: |
| 90 | + def get_extras(): |
| 91 | + try: |
| 92 | + extras = file_config["extras"] |
| 93 | + except KeyError: |
| 94 | + return None |
| 95 | + |
| 96 | + return _parse_extras(extras) |
| 97 | + |
| 98 | + return File( |
| 99 | + output=_parse_outputs(file_config["output"]), |
| 100 | + extras=get_extras(), |
| 101 | + includes=list(file_config["includes"]), |
| 102 | + matrix={ |
| 103 | + key: list(value) for key, value in file_config.get("matrix", {}).items() |
| 104 | + }, |
| 105 | + requirements_dir=Path( |
| 106 | + file_config.get("requirements_dir", constants.default_requirements_dir) |
| 107 | + ), |
| 108 | + conda_dir=Path(file_config.get("conda_dir", constants.default_conda_dir)), |
| 109 | + pyproject_dir=Path( |
| 110 | + file_config.get("pyproject_dir", constants.default_pyproject_dir) |
| 111 | + ), |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +def _parse_requirement(requirement: str | dict[str, str]) -> str | PipRequirements: |
| 116 | + if isinstance(requirement, str): |
| 117 | + return requirement |
| 118 | + |
| 119 | + return PipRequirements(pip=requirement["pip"]) |
| 120 | + |
| 121 | + |
| 122 | +def _parse_dependencies(dependencies: dict[str, object]) -> Dependencies: |
| 123 | + return Dependencies( |
| 124 | + common=[ |
| 125 | + CommonDependencies( |
| 126 | + output_types=_parse_outputs(d["output_types"]), |
| 127 | + packages=[_parse_requirement(p) for p in d["packages"]], |
| 128 | + ) |
| 129 | + for d in dependencies.get("common", []) |
| 130 | + ], |
| 131 | + specific=[ |
| 132 | + SpecificDependencies( |
| 133 | + output_types=_parse_outputs(d["output_types"]), |
| 134 | + matrices=[ |
| 135 | + MatrixMatcher( |
| 136 | + matrix=dict(m.get("matrix", {}) or {}), |
| 137 | + packages=[ |
| 138 | + _parse_requirement(p) for p in m.get("packages", []) or [] |
| 139 | + ], |
| 140 | + ) |
| 141 | + for m in d["matrices"] |
| 142 | + ], |
| 143 | + ) |
| 144 | + for d in dependencies.get("specific", []) |
| 145 | + ], |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +def _parse_channels(channels) -> list[str]: |
| 150 | + if isinstance(channels, str): |
| 151 | + return [channels] |
| 152 | + |
| 153 | + return list(channels) |
| 154 | + |
| 155 | + |
| 156 | +def parse_config(config: dict[str, object], path: PathLike) -> Config: |
| 157 | + validate_dependencies(config) |
| 158 | + return Config( |
| 159 | + path=Path(path), |
| 160 | + files={key: _parse_file(value) for key, value in config["files"].items()}, |
| 161 | + channels=_parse_channels(config.get("channels", [])), |
| 162 | + dependencies={ |
| 163 | + key: _parse_dependencies(value) |
| 164 | + for key, value in config["dependencies"].items() |
| 165 | + }, |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +def load_config_from_file(path: PathLike) -> Config: |
| 170 | + with open(path) as f: |
| 171 | + return parse_config(yaml.safe_load(f), path) |
0 commit comments