Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
image_reducer_error_class.py
Go to the documentation of this file.
1r"""
2# +==== BEGIN CatFeeder =================+
3# LOGO:
4# ..............(..../\
5# ...............)..(.')
6# ..............(../..)
7# ...............\‍(__)|
8# Inspired by Joan Stark
9# source https://www.asciiart.eu/
10# animals/cats
11# /STOP
12# PROJECT: CatFeeder
13# FILE: image_reducer_error_class.py
14# CREATION DATE: 05-01-2026
15# LAST Modified: 0:21:25 08-01-20266
16# DESCRIPTION:
17# This is the backend server in charge of making the actual website work.
18# /STOP
19# COPYRIGHT: (c) Cat Feeder
20# PURPOSE:
21# This is the file in charge of providing custom classes that can be thrown as error.
22# This module defines a small hierarchy of exceptions raised by the image reducer code. Use these specific exceptions to distinguish between different error conditions when opening, validating or saving images.
23# /STOP
24# // AR
25# +==== END CatFeeder =================+
26"""
27
28from typing import Union, List, Optional
29
30
31class ImageReducer(ValueError):
32 """Base exception for image reducer errors.
33
34 All custom image-reducer exceptions inherit from this class. Catching this
35 base class will handle any error produced by the image reducer module.
36 """
37
38
40 """Raised when image bytes cannot be identified as a valid image.
41
42 This exception is typically raised when PIL.Image.open fails with an
43 UnidentifiedImageError and the input does not represent a known image format.
44
45 Attributes:
46 image_file: The file type that was provided.
47 allowed_files: The expected file format(s).
48 """
49
50 def __init__(self, image_file: str, allowed_files: str):
51 """Initialize ImageReducerInvalidImageFile exception.
52
53 Arguments:
54 image_file: The file type or name that was provided.
55 allowed_files: The expected file format(s).
56 """
57 self.image_file = image_file
58 self.allowed_files = allowed_files
59 super().__init__(
60 f"Invalid Image file/data, you provided {self.image_file} but the program expected {self.allowed_files}"
61 )
62
63
65 """Raised when image format is not in the allowed formats list.
66
67 This exception signals that the image's format (as reported by Pillow) is not
68 accepted by the current configuration.
69
70 Attributes:
71 image_file: The image format that was detected.
72 allowed_files: The allowed image format(s).
73 """
74
75 def __init__(self, image_file: Optional[str], allowed_files: Union[List[str], str]):
76 """Initialize ImageReducerUnsupportedFormat exception.
77
78 Arguments:
79 image_file: The image format that was detected.
80 allowed_files: The allowed image format(s) as list or comma-separated string.
81 """
82 self.image_file = image_file
83 self.allowed_files = allowed_files
84 if isinstance(self.allowed_files, List):
85 self.allowed_files = ", ".join(self.allowed_files)
86 super().__init__(
87 f"Unsupported image format, you provided '{self.image_file}' but the program expected '{self.allowed_files}'"
88 )
89
90
92 """Raised when image dimensions exceed the configured maximum.
93
94 This exception indicates that either the width or height of the image is
95 larger than an allowed max_dimension. The caller should request a resized
96 image before retrying the operation.
97
98 Attributes:
99 width: The image width in pixels.
100 height: The image height in pixels.
101 max_dimension: The maximum allowed dimension in pixels.
102 """
103
104 def __init__(self, width: int, height: int, max_dimension: int):
105 """Initialize ImageReducerTooLarge exception.
106
107 Arguments:
108 width: The image width in pixels.
109 height: The image height in pixels.
110 max_dimension: The maximum allowed dimension in pixels.
111 """
112 self.width = width
113 self.height = height
114 self.max_dimension = max_dimension
115 super().__init__(
116 f"Image too large ({width}x{height}), max allowed is {max_dimension}px"
117 )
__init__(self, Optional[str] image_file, Union[List[str], str] allowed_files)