|
| 1 | +import importlib |
| 2 | +import inspect |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | +from typing import Dict, Optional |
| 6 | + |
| 7 | +import pandera as pa |
| 8 | +from pydantic import BaseModel, root_validator, validator |
| 9 | + |
| 10 | +from dac._file_helper import temporarily_copied_file |
| 11 | +from dac._input.pyproject import PyProjectConfig |
| 12 | + |
| 13 | + |
| 14 | +class PackConfig(BaseModel): |
| 15 | + data_path: Optional[Path] = None |
| 16 | + load_path: Path |
| 17 | + schema_path: Path |
| 18 | + wheel_dir: Path |
| 19 | + pyproject: PyProjectConfig |
| 20 | + |
| 21 | + @validator("data_path", "load_path", "schema_path", "wheel_dir") |
| 22 | + def path_exists(cls, path: Path) -> Path: # pylint: disable=no-self-argument,no-self-use |
| 23 | + if path is not None and not path.exists(): |
| 24 | + raise ValueError((f"Path {path.as_posix()} is not valid")) |
| 25 | + return path |
| 26 | + |
| 27 | + @validator("load_path") |
| 28 | + def load_contains_expected_function(cls, path: Path) -> Path: # pylint: disable=no-self-argument,no-self-use |
| 29 | + try: |
| 30 | + sys.path.append(path.parent.as_posix()) |
| 31 | + pkg = importlib.import_module(name=path.stem) |
| 32 | + except Exception as e: |
| 33 | + raise ValueError( |
| 34 | + ( |
| 35 | + f"{path.as_posix()} is not a path to a python module that can be imported, " |
| 36 | + "because of the following error:" |
| 37 | + "\n" |
| 38 | + f"{e}" |
| 39 | + ) |
| 40 | + ) from e |
| 41 | + |
| 42 | + try: |
| 43 | + signature = inspect.getfullargspec(pkg.load) |
| 44 | + assert signature.args == [] |
| 45 | + except Exception as e: |
| 46 | + raise ValueError((f"{path.as_posix()} does not contain the required `def load()`")) from e |
| 47 | + return path |
| 48 | + |
| 49 | + @validator("schema_path") |
| 50 | + def schema_contains_expected_class(cls, path: Path) -> Path: # pylint: disable=no-self-argument,no-self-use |
| 51 | + try: |
| 52 | + sys.path.append(path.parent.as_posix()) |
| 53 | + pkg = importlib.import_module(name=path.stem) |
| 54 | + except Exception as e: |
| 55 | + raise ValueError( |
| 56 | + ( |
| 57 | + f"{path.as_posix()} is not a path to a python module that can be imported, " |
| 58 | + "because of the following error:" |
| 59 | + "\n" |
| 60 | + f"{e}" |
| 61 | + ) |
| 62 | + ) from e |
| 63 | + |
| 64 | + try: |
| 65 | + issubclass(pkg.Schema, pa.SchemaModel) |
| 66 | + except Exception as e: |
| 67 | + raise ValueError((f"{path.as_posix()} does not contain the required `class Schema(pa.SchemaModel)`")) from e |
| 68 | + return path |
| 69 | + |
| 70 | + @root_validator |
| 71 | + def schema_match_data( # pylint: disable=no-self-argument,no-self-use |
| 72 | + cls, values: Dict[str, Path] |
| 73 | + ) -> Dict[str, Path]: |
| 74 | + try: |
| 75 | + sys.path.append(values["load_path"].parent.as_posix()) |
| 76 | + load_module = importlib.import_module(name=values["load_path"].stem) |
| 77 | + except Exception as e: |
| 78 | + raise ValueError( |
| 79 | + "Validation of the schema against the data has failed because the load module could not be imported" |
| 80 | + ) from e |
| 81 | + |
| 82 | + try: |
| 83 | + if values.get("data_path", None) is not None: |
| 84 | + with temporarily_copied_file( |
| 85 | + src=values["data_path"], dst=values["load_path"].parent / values["data_path"].name |
| 86 | + ): |
| 87 | + data = load_module.load() |
| 88 | + else: |
| 89 | + data = load_module.load() |
| 90 | + except Exception as e: |
| 91 | + raise ValueError("`load()` failed due to the following error:" "\n" f"{e}") from e |
| 92 | + |
| 93 | + try: |
| 94 | + sys.path.append(values["schema_path"].parent.as_posix()) |
| 95 | + schema_module = importlib.import_module(name=values["schema_path"].stem) |
| 96 | + except Exception as e: |
| 97 | + raise ValueError( |
| 98 | + "Validation of the schema against the data has failed because the schema module could not be imported" |
| 99 | + ) from e |
| 100 | + |
| 101 | + try: |
| 102 | + schema_module.Schema.validate(data, lazy=True) |
| 103 | + except pa.errors.SchemaErrors as e: |
| 104 | + raise ValueError("Validation of the schema against the data has failed:" "\n" f"{e.failure_cases}") from e |
| 105 | + except Exception as e: |
| 106 | + raise ValueError( |
| 107 | + "Validation of the schema against the data has failed for unexpected reasons:" "\n" f"{e}" |
| 108 | + ) from e |
| 109 | + |
| 110 | + return values |
0 commit comments