|
1 | 1 | import sys |
| 2 | +from enum import Enum |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any, Dict, Optional, Sequence, Tuple, Union, cast |
2 | 5 |
|
3 | 6 | from robotcode.core.dataclasses import from_dict |
4 | 7 |
|
|
10 | 13 |
|
11 | 14 | from .model import MainProfile |
12 | 15 |
|
| 16 | +PYPROJECT_TOML = "pyproject.toml" |
| 17 | +ROBOT_TOML = "robot.toml" |
| 18 | +LOCAL_ROBOT_TOML = ".robot.toml" |
13 | 19 |
|
14 | | -def create_from_toml(__s: str) -> MainProfile: |
| 20 | + |
| 21 | +class DiscoverdBy(str, Enum): |
| 22 | + GIT = ".git directory" |
| 23 | + HG = "hg" |
| 24 | + PYPROJECT_TOML = PYPROJECT_TOML |
| 25 | + ROBOT_TOML = ROBOT_TOML |
| 26 | + LOCAL_ROBOT_TOML = LOCAL_ROBOT_TOML |
| 27 | + NOT_FOUND = "not found" |
| 28 | + |
| 29 | + |
| 30 | +class ConfigType(str, Enum): |
| 31 | + PYPROJECT_TOML = PYPROJECT_TOML |
| 32 | + ROBOT_TOML = ROBOT_TOML |
| 33 | + LOCAL_ROBOT_TOML = LOCAL_ROBOT_TOML |
| 34 | + |
| 35 | + |
| 36 | +def loads_config_from_robot_toml(__s: str) -> MainProfile: |
15 | 37 | dict_data = tomllib.loads(__s) |
16 | 38 | return from_dict(dict_data, MainProfile) |
| 39 | + |
| 40 | + |
| 41 | +def loads_config_from_pyproject_toml(__s: str) -> MainProfile: |
| 42 | + dict_data = tomllib.loads(__s) |
| 43 | + |
| 44 | + return from_dict(dict_data.get("tool", {}).get("robot", {}), MainProfile) |
| 45 | + |
| 46 | + |
| 47 | +def _load_config_data_from_path(__path: Path) -> Dict[str, Any]: |
| 48 | + if __path.name == PYPROJECT_TOML: |
| 49 | + return cast(Dict[str, Any], tomllib.loads(__path.read_text("utf-8")).get("tool", {}).get("robot", {})) |
| 50 | + |
| 51 | + if __path.name == ROBOT_TOML or __path.name == LOCAL_ROBOT_TOML: |
| 52 | + return tomllib.loads(__path.read_text("utf-8")) |
| 53 | + |
| 54 | + raise ValueError(f"Unknown config file name: {__path.name}") |
| 55 | + |
| 56 | + |
| 57 | +def load_config_from_path(*__paths: Union[Path, Tuple[Path, ConfigType]]) -> MainProfile: |
| 58 | + config_data = {} |
| 59 | + for __path in __paths: |
| 60 | + config_data.update(_load_config_data_from_path(__path if isinstance(__path, Path) else __path[0])) |
| 61 | + |
| 62 | + return from_dict(config_data, MainProfile) |
| 63 | + |
| 64 | + |
| 65 | +def find_project_root(*sources: Union[str, Path]) -> Tuple[Optional[Path], DiscoverdBy]: |
| 66 | + if not sources: |
| 67 | + sources = (str(Path.cwd().resolve()),) |
| 68 | + |
| 69 | + path_srcs = [Path(Path.cwd(), src).resolve() for src in sources] |
| 70 | + |
| 71 | + src_parents = [list(path.parents) + ([path] if path.is_dir() else []) for path in path_srcs] |
| 72 | + |
| 73 | + common_base = max( |
| 74 | + set.intersection(*(set(parents) for parents in src_parents)), |
| 75 | + key=lambda path: path.parts, |
| 76 | + ) |
| 77 | + |
| 78 | + for directory in (common_base, *common_base.parents): |
| 79 | + if (directory / LOCAL_ROBOT_TOML).is_file(): |
| 80 | + return directory, DiscoverdBy.LOCAL_ROBOT_TOML |
| 81 | + |
| 82 | + if (directory / ROBOT_TOML).is_file(): |
| 83 | + return directory, DiscoverdBy.ROBOT_TOML |
| 84 | + |
| 85 | + if (directory / PYPROJECT_TOML).is_file(): |
| 86 | + return directory, DiscoverdBy.PYPROJECT_TOML |
| 87 | + |
| 88 | + if (directory / ".git").exists(): |
| 89 | + return directory, DiscoverdBy.GIT |
| 90 | + |
| 91 | + if (directory / ".hg").is_dir(): |
| 92 | + return directory, DiscoverdBy.HG |
| 93 | + |
| 94 | + return None, DiscoverdBy.NOT_FOUND |
| 95 | + |
| 96 | + |
| 97 | +def get_config_files_from_folder(folder: Path) -> Sequence[Tuple[Path, ConfigType]]: |
| 98 | + result = [] |
| 99 | + |
| 100 | + pyproject_toml = folder / PYPROJECT_TOML |
| 101 | + if pyproject_toml.is_file(): |
| 102 | + result.append((pyproject_toml, ConfigType.PYPROJECT_TOML)) |
| 103 | + |
| 104 | + robot_toml = folder / ROBOT_TOML |
| 105 | + if robot_toml.is_file(): |
| 106 | + result.append((robot_toml, ConfigType.ROBOT_TOML)) |
| 107 | + |
| 108 | + local_robot_toml = folder / LOCAL_ROBOT_TOML |
| 109 | + if local_robot_toml.is_file(): |
| 110 | + result.append((local_robot_toml, ConfigType.LOCAL_ROBOT_TOML)) |
| 111 | + |
| 112 | + return result |
0 commit comments