44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from email.utils import formataddr
|
|
from enum import StrEnum
|
|
|
|
|
|
class RecipientType(StrEnum):
|
|
TO = "to"
|
|
CC = "cc"
|
|
BCC = "bcc"
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Recipient:
|
|
address: str
|
|
name: str | None = None
|
|
type: RecipientType = RecipientType.TO
|
|
|
|
def formatted(self) -> str:
|
|
return formataddr((self.name or self.address, self.address))
|
|
|
|
|
|
@dataclass
|
|
class RecipientList:
|
|
recipients: list[Recipient] = field(default_factory=list)
|
|
|
|
def add_recipient(self, recipient: Recipient) -> "RecipientList":
|
|
if recipient not in self.recipients:
|
|
self.recipients.append(recipient)
|
|
return self
|
|
|
|
def add_multiple_recipients(self, recipients: list[Recipient] | tuple[Recipient, ...]) -> "RecipientList":
|
|
for recipient in recipients:
|
|
self.add_recipient(recipient)
|
|
return self
|
|
|
|
def clear_all_recipients(self) -> "RecipientList":
|
|
self.recipients.clear()
|
|
return self
|
|
|
|
def by_type(self, recipient_type: RecipientType) -> list[Recipient]:
|
|
return [r for r in self.recipients if r.type == recipient_type]
|