core.utils.fetchers.SafeIMAPMixin module¶
Module with the SafeIMAPMixin mixin.
- type core.utils.fetchers.SafeIMAPMixin.IMAP4Response = tuple[str, str | list[bytes] | list[None] | list[Any] | list[bytes | None] | list[bytes | tuple[bytes, bytes]]]¶
- class core.utils.fetchers.SafeIMAPMixin.IMAP4FetcherClass(*args, **kwargs)[source]¶
Bases:
ProtocolProtocol defining the required attributes of a class implementing this mixin.
- _mail_client: imaplib.IMAP4¶
- logger: logging.Logger¶
- __init__(*args, **kwargs)¶
- _is_protocol = True¶
- class core.utils.fetchers.SafeIMAPMixin.SafeIMAPMixin[source]¶
Bases:
objectMixin with safe IMAP operations.
The implementing class must have an
_mail_clientthat is animaplib.IMAP4orimaplib.IMAP4_SSLand an instance logger.Only errors leading up to and during the fetching process raise outside of the class. Otherwise issues with logout etc would destroy the work done with fetching, which makes no sense.
- check_response(response, command_name, exception_class, expected_status='OK')[source]¶
Checks the status response of an IMAP action.
If it doesn’t match the expectation raises an exception.
Todo
Safely! extract error message from response for logging and exception.
- Parameters:
response (
TypeAliasType) – The complete response to the IMAP action.command_name (
str) – The name of the action.exception_class (
type[FetcherError] |None) – The exception to raise if the status doesn’t match the expectation. Defaults tocore.utils.fetchers.exceptions.FetcherError.expected_status (
str) – The expected status response. Defaults to “OK”.
- Raises:
exception_class – If the response status doesn’t match the expectation.
- Return type:
- static safe(exception_class, expected_status='OK')[source]¶
- Overloads:
exception_class (type[FetcherError]), expected_status (str) → Callable[[Callable[…, IMAP4ActionResponse]], Callable[…, IMAP4ActionResponse]]
exception_class (None), expected_status (str) → Callable[[Callable[…, IMAP4ActionResponse]], Callable[…, IMAP4ActionResponse | None]]
- Parameters:
exception_class (type[FetcherError] | None)
expected_status (str)
- Return type:
Callable[[Callable[[…], IMAP4ActionResponse]], Callable[[…], IMAP4ActionResponse | None]]
Wrapper for IMAP actions.
Catches expected errors, checks for correct responses and raises an FetcherError in case.
Todo
Find a better way to disable the exception and getting rid of the typing mess.
- Parameters:
exception_class (
type[FetcherError] |None) – The exception class to raise if an error occurs or the status doesn’t match the expectation. Must be a subclass ofcore.utils.fetchers.exceptions.FetcherError. Defaults tocore.utils.fetchers.exceptions.FetcherError. If None is passed, no exception is raised, all errors are logged nonetheless.expected_status (
str) – The expected status response. Defaults to “OK”.
- Returns:
The return value of the wrapped action. None if an error occurs and exception is None.
- Raises:
exception_class – If an error occurs or the status doesn’t match the expectation.
- Return type:
Callable[[Callable[[…], IMAP4ActionResponse]], Callable[[…], IMAP4ActionResponse | None]]