Skip to content

Commit

Permalink
refactor; allow to skip validation
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino committed Jun 14, 2024
1 parent 05e1e32 commit 91b541b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
11 changes: 7 additions & 4 deletions cognite_toolkit/_cdf_tk/data_classes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def load_from_directory(
build_env_name: str,
warn: Callable[[ToolkitWarning], None] | None = None,
command: str | None = None,
skip_validation: bool = False,
) -> T_BuildConfig:
file_name = cls._file_name(build_env_name)
filepath = source_path / file_name
Expand All @@ -57,18 +58,20 @@ def load_from_directory(
hint = f"{command} {hint}"
raise ToolkitFileNotFoundError(f"{file_name!r} does not exist. Did you mean {hint!r}?")

return cls.load(read_yaml_file(filepath), build_env_name, filepath)
return cls.load(read_yaml_file(filepath), build_env_name, filepath, skip_validation)

@classmethod
@abstractmethod
def load(cls: type[T_BuildConfig], data: dict[str, Any], build_env: str, filepath: Path) -> T_BuildConfig:
def load(
cls: type[T_BuildConfig], data: dict[str, Any], build_env: str, filepath: Path, skip_validation: bool
) -> T_BuildConfig:
raise NotImplementedError


T_BuildConfig = TypeVar("T_BuildConfig", bound=ConfigCore)


def _load_version_variable(data: dict[str, Any], file_name: str) -> str:
def _load_version_variable(data: dict[str, Any], file_name: str, skip_validation: bool = False) -> str:
try:
cdf_tk_version: str = data["cdf_toolkit_version"]
except KeyError:
Expand All @@ -81,7 +84,7 @@ def _load_version_variable(data: dict[str, Any], file_name: str) -> str:
err_msg.format("Run `cdf-tk init --upgrade` to initialize the modules again to create a correct file.")
)

if cdf_tk_version != _version.__version__:
if not skip_validation and cdf_tk_version != _version.__version__:
raise ToolkitVersionError(
f"The version of the modules ({cdf_tk_version}) does not match the version of the installed CLI "
f"({_version.__version__}). Please either run `cdf-tk init --upgrade` to upgrade the modules OR "
Expand Down
2 changes: 1 addition & 1 deletion cognite_toolkit/_cdf_tk/data_classes/_config_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def validate_environment(self) -> None:
return None

@classmethod
def load(cls, data: dict[str, Any], build_env_name: str, filepath: Path) -> BuildConfigYAML:
def load(cls, data: dict[str, Any], build_env_name: str, filepath: Path, skip_validation: bool) -> BuildConfigYAML:
if missing := {"environment", "variables"}.difference(data):
err_msg = f"Expected {list(missing)} section(s) in {filepath!s}."
if "modules" in data and "variables" in missing:
Expand Down
4 changes: 2 additions & 2 deletions cognite_toolkit/_cdf_tk/data_classes/_system_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def _file_name(cls, build_env_name: str) -> str:
return cls.file_name

@classmethod
def load(cls, data: dict[str, Any], build_env_name: str, filepath: Path) -> SystemYAML:
version = _load_version_variable(data, filepath.name)
def load(cls, data: dict[str, Any], build_env_name: str, filepath: Path, skip_validation: bool) -> SystemYAML:
version = _load_version_variable(data, filepath.name, skip_validation)
packages = data.get("packages", {})
if not packages:
print(f" [bold yellow]Warning:[/] No packages defined in {cls.file_name}.")
Expand Down
2 changes: 1 addition & 1 deletion cognite_toolkit/_cdf_tk/prototypes/commands/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def upgrade(self, project_dir: Optional[str] = None) -> None:
project_path = Path(project_dir or ".")

# Validation.
system_yaml = SystemYAML.load_from_directory(project_path, build_env_name="dev")
system_yaml = SystemYAML.load_from_directory(project_path, build_env_name="dev", skip_validation=True)
SystemYAML.validate_module_dir(project_path)

cli_version = parse_version(__version__)
Expand Down

0 comments on commit 91b541b

Please sign in to comment.