Skip to content

Credentials

email_profile.core.credentials.Credentials dataclass

Resolved (server, user, password, port, ssl) tuple.

Source code in email_profile/core/credentials.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@dataclass(frozen=True)
class Credentials:
    """Resolved (server, user, password, port, ssl) tuple."""

    server: str
    user: str
    password: str
    port: int = 993
    ssl: bool = True

    def __repr__(self) -> str:
        return f"Credentials(server={self.server!r}, user={self.user!r}, password='***')"

    def __str__(self) -> str:
        return self.__repr__()

password instance-attribute

port = 993 class-attribute instance-attribute

server instance-attribute

ssl = True class-attribute instance-attribute

user instance-attribute

__init__(server, user, password, port=993, ssl=True)

__repr__()

Source code in email_profile/core/credentials.py
22
23
def __repr__(self) -> str:
    return f"Credentials(server={self.server!r}, user={self.user!r}, password='***')"

__str__()

Source code in email_profile/core/credentials.py
25
26
def __str__(self) -> str:
    return self.__repr__()

email_profile.core.credentials.EmailFactories

Build :class:Credentials without the user spelling out a hostname.

Source code in email_profile/core/credentials.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class EmailFactories:
    """Build :class:`Credentials` without the user spelling out a hostname."""

    @classmethod
    def from_address(
        cls,
        address: str,
        password: str,
        *,
        port: Optional[int] = None,
        ssl: Optional[bool] = None,
    ) -> Credentials:
        """Auto-discover the IMAP host from the email address.

        ``port`` and ``ssl`` override the discovered values when given.
        """
        host = resolve_imap_host(address)
        return Credentials(
            server=host.host,
            user=address,
            password=password,
            port=host.port if port is None else port,
            ssl=host.ssl if ssl is None else ssl,
        )

    @classmethod
    def from_env(
        cls,
        server_var: str = "EMAIL_SERVER",
        user_var: str = "EMAIL_USERNAME",
        password_var: str = "EMAIL_PASSWORD",
        load_dotenv: bool = True,
        *,
        port: Optional[int] = None,
        ssl: Optional[bool] = None,
    ) -> Credentials:
        """Read credentials from env vars (or `.env`)."""
        if load_dotenv:
            try:
                from dotenv import load_dotenv as _ld

                _ld()
            except ImportError:
                pass

        user = os.environ.get(user_var)
        password = os.environ.get(password_var)
        if not user or not password:
            raise KeyError(
                f"Missing {user_var!r} or {password_var!r} in environment."
            )

        server = os.environ.get(server_var)
        if server:
            return Credentials(
                server=server,
                user=user,
                password=password,
                port=993 if port is None else port,
                ssl=True if ssl is None else ssl,
            )

        return cls.from_address(user, password, port=port, ssl=ssl)

from_address(address, password, *, port=None, ssl=None) classmethod

Auto-discover the IMAP host from the email address.

port and ssl override the discovered values when given.

Source code in email_profile/core/credentials.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@classmethod
def from_address(
    cls,
    address: str,
    password: str,
    *,
    port: Optional[int] = None,
    ssl: Optional[bool] = None,
) -> Credentials:
    """Auto-discover the IMAP host from the email address.

    ``port`` and ``ssl`` override the discovered values when given.
    """
    host = resolve_imap_host(address)
    return Credentials(
        server=host.host,
        user=address,
        password=password,
        port=host.port if port is None else port,
        ssl=host.ssl if ssl is None else ssl,
    )

from_env(server_var='EMAIL_SERVER', user_var='EMAIL_USERNAME', password_var='EMAIL_PASSWORD', load_dotenv=True, *, port=None, ssl=None) classmethod

Read credentials from env vars (or .env).

Source code in email_profile/core/credentials.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@classmethod
def from_env(
    cls,
    server_var: str = "EMAIL_SERVER",
    user_var: str = "EMAIL_USERNAME",
    password_var: str = "EMAIL_PASSWORD",
    load_dotenv: bool = True,
    *,
    port: Optional[int] = None,
    ssl: Optional[bool] = None,
) -> Credentials:
    """Read credentials from env vars (or `.env`)."""
    if load_dotenv:
        try:
            from dotenv import load_dotenv as _ld

            _ld()
        except ImportError:
            pass

    user = os.environ.get(user_var)
    password = os.environ.get(password_var)
    if not user or not password:
        raise KeyError(
            f"Missing {user_var!r} or {password_var!r} in environment."
        )

    server = os.environ.get(server_var)
    if server:
        return Credentials(
            server=server,
            user=user,
            password=password,
            port=993 if port is None else port,
            ssl=True if ssl is None else ssl,
        )

    return cls.from_address(user, password, port=port, ssl=ssl)