Source code for mckit_meshes.utils._io

"""Output utilities."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

import sys

from pathlib import Path

if TYPE_CHECKING:
    # noinspection PyCompatibility
    from collections.abc import Callable, Iterable

    from _typeshed import SupportsWrite


[docs] def ignore_existing_file_strategy(_: str | Path) -> None: """Do nothing if file exists."""
[docs] def raise_error_when_file_exists_strategy(path: str | Path) -> None: """Strategy to use when file exists. Args: path: path to check Raises: FileExistsError: if `path` exits. """ path = Path(path) if path.exists(): errmsg = f"""\ Cannot override existing file \"{path}\". Please remove the file or specify --override option""" raise FileExistsError(errmsg)
[docs] def check_if_path_exists(*, override: bool) -> Callable[[str | Path], None]: """Select strategy to handle existing files, depending on option `override`. Args: override: if True ignore the case if file exists, otherwise rise Error Returns: The selected strategy. """ return ignore_existing_file_strategy if override else raise_error_when_file_exists_strategy