Skip to content

input4mips_validation.validation#

Sub-package Description
Conventions Validation of the Conventions attribute
allowed_characters Support for defining rules around the characters allowed in metadata
cf_checker Validation with the cf-checker
comment Validation of the comment attribute
creation_date Validation of the creation_date attribute
database Database validation
datasets_to_write_to_disk Validation of datasets that we are writing to disk
email Validation of emails
error_catching Tools for catching errors in validation without stopping
exceptions Exceptions related to validation that don't obviously fit elsewhere
external_variables Validation of the external_variables attribute
file Validation of an individual file in isolation
frequency Validation of the frequency attribute
orcid Validation of ORCID's
regexp Validation of values against regular expressions
tracking_id Validation of the tracking_id attribute
tree Validation of a tree of files
variable_id Validation of the variable_id attribute

input4mips_validation.validation #

Validation module

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)