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: Protocol

Protocol 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: object

Mixin with safe IMAP operations.

The implementing class must have an _mail_client that is an imaplib.IMAP4 or imaplib.IMAP4_SSL and 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 to core.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:

None

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:
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:
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]]

safe_login(*args, **kwargs)[source]
Return type:

TypeVar(IMAP4ActionResponse, bound= TypeAliasType) | None

Parameters:
safe_select(*args, **kwargs)[source]
Return type:

TypeVar(IMAP4ActionResponse, bound= TypeAliasType) | None

Parameters:
safe_unselect(*args, **kwargs)[source]
Return type:

TypeVar(IMAP4ActionResponse, bound= TypeAliasType) | None

Parameters:
safe_list(*args, **kwargs)[source]
Return type:

TypeVar(IMAP4ActionResponse, bound= TypeAliasType) | None

Parameters:
safe_uid(*args, **kwargs)[source]
Return type:

TypeVar(IMAP4ActionResponse, bound= TypeAliasType) | None

Parameters:
safe_noop(*args, **kwargs)[source]
Return type:

TypeVar(IMAP4ActionResponse, bound= TypeAliasType) | None

Parameters:
safe_check(*args, **kwargs)[source]
Return type:

TypeVar(IMAP4ActionResponse, bound= TypeAliasType) | None

Parameters:
safe_append(*args, **kwargs)[source]
Return type:

TypeVar(IMAP4ActionResponse, bound= TypeAliasType) | None

Parameters:
safe_logout(*args, **kwargs)[source]
Return type:

TypeVar(IMAP4ActionResponse, bound= TypeAliasType) | None

Parameters: