Skip to content

Helpers

psengine.classic_alerts.helpers

save_image

save_image(
    image_bytes: bytes,
    file_name: str,
    output_directory: Union[
        str, Path
    ] = DEFAULT_CA_OUTPUT_DIR,
) -> Path

Save an image to disk as a PNG file.

PARAMETER DESCRIPTION
image_bytes

The image to save.

TYPE: bytes

file_name

The file name to save the image as, without extension.

TYPE: str

output_directory

The directory to save the image to.

TYPE: Union[str, Path] DEFAULT: DEFAULT_CA_OUTPUT_DIR

RAISES DESCRIPTION
ValidationError

If any supplied parameter is of incorrect type.

WriteFileError

In any of the following situations:

  • If the path provided is not a directory
  • If the path provided cannot be created.
  • If the write operation fails.
RETURNS DESCRIPTION
Path

The path to the file written.

Source code in psengine/classic_alerts/helpers.py
@validate_call
def save_image(
    image_bytes: Annotated[bytes, Doc('The image to save.')],
    file_name: Annotated[str, Doc('The file name to save the image as, without extension.')],
    output_directory: Annotated[
        Union[str, Path], Doc('The directory to save the image to.')
    ] = DEFAULT_CA_OUTPUT_DIR,
) -> Annotated[Path, Doc('The path to the file written.')]:
    """Save an image to disk as a PNG file.

    Raises:
        ValidationError: If any supplied parameter is of incorrect type.
        WriteFileError: In any of the following situations:

            - If the path provided is not a directory
            - If the path provided cannot be created.
            - If the write operation fails.
    """
    try:
        LOG.info(f"Saving image '{file_name}' to disk")
        dir_path = OSHelpers.mkdir(output_directory)
        image_filepath = Path(dir_path) / f'{file_name}.png'
        with Path.open(image_filepath, 'wb') as file:
            file.write(image_bytes)
    except OSError as err:
        raise WriteFileError(
            f'Failed to save classic alert image to disk. Cause: {err.args}',
        ) from err

    return image_filepath

save_images

save_images(
    alert: ClassicAlert,
    output_directory: Union[
        str, Path
    ] = DEFAULT_CA_OUTPUT_DIR,
) -> dict

Save all images from a ClassicAlert to disk.

PARAMETER DESCRIPTION
alert

The alert to save images from.

TYPE: ClassicAlert

output_directory

The directory to save the images to.

TYPE: Union[str, Path] DEFAULT: DEFAULT_CA_OUTPUT_DIR

RAISES DESCRIPTION
ValidationError

If any supplied parameter is of incorrect type.

WriteFileError

In any of the following situations:

  • If the path provided is not a directory
  • If the path provided cannot be created.
  • If the write operation fails.
RETURNS DESCRIPTION
dict

A dictionary of image file paths with the image ID as the key.

Source code in psengine/classic_alerts/helpers.py
@validate_call
def save_images(
    alert: Annotated[ClassicAlert, Doc('The alert to save images from.')],
    output_directory: Annotated[
        Union[str, Path], Doc('The directory to save the images to.')
    ] = DEFAULT_CA_OUTPUT_DIR,
) -> Annotated[dict, Doc('A dictionary of image file paths with the image ID as the key.')]:
    """Save all images from a `ClassicAlert` to disk.

    Raises:
        ValidationError: If any supplied parameter is of incorrect type.
        WriteFileError: In any of the following situations:

            - If the path provided is not a directory
            - If the path provided cannot be created.
            - If the write operation fails.
    """
    image_file_paths = {}
    for id_, bytes_ in alert.images.items():
        image_file_paths[id_] = save_image(
            image_bytes=bytes_, output_directory=output_directory, file_name=id_
        )

    return image_file_paths