Skip to content

input4mips_validation.validation.exceptions#

input4mips_validation.validation.exceptions #

Exceptions related to validation that don't obviously fit elsewhere

FileAssociatedWithDatabaseEntryError #

Bases: ValueError

Raised when the file associated with a database entry has a problem

Source code in src/input4mips_validation/validation/exceptions.py
class FileAssociatedWithDatabaseEntryError(ValueError):
    """
    Raised when the file associated with a database entry has a problem
    """

    def __init__(
        self,
        entry: Input4MIPsDatabaseEntryFile,
        error_container: list[tuple[str, Exception]],
    ) -> None:
        """
        Initialise the error

        Parameters
        ----------
        entry
            The entry we tried to validate

        error_container
            The thing which was being done
            and the error which was caught
            while validating the file.
        """
        error_msgs: list[str] = []
        for error in error_container:
            process, exc = error
            formatted_exc = f"{type(exc).__name__}: {exc}"
            error_msgs.append(f"{process} failed. Exception: {formatted_exc}")

        error_msgs_str = "\n\n".join(error_msgs)

        error_msg = (
            f"Failed to validate {entry=}\n"
            "Caught error messages:\n\n"
            f"{error_msgs_str}"
        )

        super().__init__(error_msg)

__init__(entry, error_container) #

Initialise the error

Parameters:

Name Type Description Default
entry Input4MIPsDatabaseEntryFile

The entry we tried to validate

required
error_container list[tuple[str, Exception]]

The thing which was being done and the error which was caught while validating the file.

required
Source code in src/input4mips_validation/validation/exceptions.py
def __init__(
    self,
    entry: Input4MIPsDatabaseEntryFile,
    error_container: list[tuple[str, Exception]],
) -> None:
    """
    Initialise the error

    Parameters
    ----------
    entry
        The entry we tried to validate

    error_container
        The thing which was being done
        and the error which was caught
        while validating the file.
    """
    error_msgs: list[str] = []
    for error in error_container:
        process, exc = error
        formatted_exc = f"{type(exc).__name__}: {exc}"
        error_msgs.append(f"{process} failed. Exception: {formatted_exc}")

    error_msgs_str = "\n\n".join(error_msgs)

    error_msg = (
        f"Failed to validate {entry=}\n"
        "Caught error messages:\n\n"
        f"{error_msgs_str}"
    )

    super().__init__(error_msg)

InvalidFileError #

Bases: ValueError

Raised when a file does not pass all of the validation

Source code in src/input4mips_validation/validation/exceptions.py
class InvalidFileError(ValueError):
    """
    Raised when a file does not pass all of the validation
    """

    def __init__(
        self,
        filepath: Path | str,
        error_container: list[tuple[str, Exception]],
    ) -> None:
        """
        Initialise the error

        Parameters
        ----------
        filepath
            The filepath we tried to validate

        error_container
            The thing which was being done
            and the error which was caught
            while validating the file.
        """
        # Not clear how input could be further validated hence noqa
        ncdump_loc = shutil.which("ncdump")
        if ncdump_loc is None:
            msg = "Could not find ncdump executable"
            raise AssertionError(msg)

        # Not clear how input could be further validated hence noqa
        file_ncdump_h = subprocess.check_output(
            [ncdump_loc, "-h", str(filepath)]  # noqa: S603
        ).decode()

        error_msgs: list[str] = []
        for error in error_container:
            process, exc = error
            formatted_exc = f"{type(exc).__name__}: {exc}"
            error_msgs.append(f"{process} failed. Exception: {formatted_exc}")

        error_msgs_str = "\n\n".join(error_msgs)

        error_msg = (
            f"Failed to validate {filepath=}\n"
            f"File's `ncdump -h` output:\n\n{file_ncdump_h}\n\n"
            "Caught error messages:\n\n"
            f"{error_msgs_str}"
        )

        super().__init__(error_msg)

__init__(filepath, error_container) #

Initialise the error

Parameters:

Name Type Description Default
filepath Path | str

The filepath we tried to validate

required
error_container list[tuple[str, Exception]]

The thing which was being done and the error which was caught while validating the file.

required
Source code in src/input4mips_validation/validation/exceptions.py
def __init__(
    self,
    filepath: Path | str,
    error_container: list[tuple[str, Exception]],
) -> None:
    """
    Initialise the error

    Parameters
    ----------
    filepath
        The filepath we tried to validate

    error_container
        The thing which was being done
        and the error which was caught
        while validating the file.
    """
    # Not clear how input could be further validated hence noqa
    ncdump_loc = shutil.which("ncdump")
    if ncdump_loc is None:
        msg = "Could not find ncdump executable"
        raise AssertionError(msg)

    # Not clear how input could be further validated hence noqa
    file_ncdump_h = subprocess.check_output(
        [ncdump_loc, "-h", str(filepath)]  # noqa: S603
    ).decode()

    error_msgs: list[str] = []
    for error in error_container:
        process, exc = error
        formatted_exc = f"{type(exc).__name__}: {exc}"
        error_msgs.append(f"{process} failed. Exception: {formatted_exc}")

    error_msgs_str = "\n\n".join(error_msgs)

    error_msg = (
        f"Failed to validate {filepath=}\n"
        f"File's `ncdump -h` output:\n\n{file_ncdump_h}\n\n"
        "Caught error messages:\n\n"
        f"{error_msgs_str}"
    )

    super().__init__(error_msg)

InvalidTreeError #

Bases: ValueError

Raised when a tree does not pass all of the validation

Source code in src/input4mips_validation/validation/exceptions.py
class InvalidTreeError(ValueError):
    """
    Raised when a tree does not pass all of the validation
    """

    def __init__(
        self,
        root: Path | str,
        error_container: list[tuple[str, Exception]],
    ) -> None:
        """
        Initialise the error

        Parameters
        ----------
        root
            The root of the tree we tried to validate

        error_container
            The thing which was being done
            and the error which was caught
            while validating the file.
        """
        error_msgs: list[str] = []
        for error in error_container:
            process, exc = error
            formatted_exc = f"{type(exc).__name__}: {exc}"
            error_msgs.append(f"{process} failed. Exception: {formatted_exc}")

        error_msgs_str = "\n\n".join(error_msgs)

        error_msg = (
            f"Failed to validate {root=}\n"
            "Caught error messages:\n\n"
            f"{error_msgs_str}"
        )

        super().__init__(error_msg)

__init__(root, error_container) #

Initialise the error

Parameters:

Name Type Description Default
root Path | str

The root of the tree we tried to validate

required
error_container list[tuple[str, Exception]]

The thing which was being done and the error which was caught while validating the file.

required
Source code in src/input4mips_validation/validation/exceptions.py
def __init__(
    self,
    root: Path | str,
    error_container: list[tuple[str, Exception]],
) -> None:
    """
    Initialise the error

    Parameters
    ----------
    root
        The root of the tree we tried to validate

    error_container
        The thing which was being done
        and the error which was caught
        while validating the file.
    """
    error_msgs: list[str] = []
    for error in error_container:
        process, exc = error
        formatted_exc = f"{type(exc).__name__}: {exc}"
        error_msgs.append(f"{process} failed. Exception: {formatted_exc}")

    error_msgs_str = "\n\n".join(error_msgs)

    error_msg = (
        f"Failed to validate {root=}\n"
        "Caught error messages:\n\n"
        f"{error_msgs_str}"
    )

    super().__init__(error_msg)

MissingAttributeError #

Bases: KeyError

Raised to signal that an attribute is missing

Obviously, this is only raised when the attribute should be there, but isn't.

Source code in src/input4mips_validation/validation/exceptions.py
class MissingAttributeError(KeyError):
    """
    Raised to signal that an attribute is missing

    Obviously, this is only raised when the attribute should be there, but isn't.
    """

    def __init__(self, missing_attribute: str) -> None:
        """
        Initialise the error

        Parameters
        ----------
        missing_attribute
            The attribute which is missing.
        """
        super().__init__(missing_attribute)

__init__(missing_attribute) #

Initialise the error

Parameters:

Name Type Description Default
missing_attribute str

The attribute which is missing.

required
Source code in src/input4mips_validation/validation/exceptions.py
def __init__(self, missing_attribute: str) -> None:
    """
    Initialise the error

    Parameters
    ----------
    missing_attribute
        The attribute which is missing.
    """
    super().__init__(missing_attribute)