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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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
21
22
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
24
25
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
28
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
class EmailFactories:
    """Build :class:`Credentials` without the user spelling out a hostname."""

    @classmethod
    def from_address(cls, address: str, password: str) -> Credentials:
        """Auto-discover the IMAP host from the email address."""
        host = resolve_imap_host(address)
        return Credentials(
            server=host.host,
            user=address,
            password=password,
            port=host.port,
            ssl=host.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,
    ) -> 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)

        return cls.from_address(user, password)

from_address(address, password) classmethod

Auto-discover the IMAP host from the email address.

Source code in email_profile/core/credentials.py
31
32
33
34
35
36
37
38
39
40
41
@classmethod
def from_address(cls, address: str, password: str) -> Credentials:
    """Auto-discover the IMAP host from the email address."""
    host = resolve_imap_host(address)
    return Credentials(
        server=host.host,
        user=address,
        password=password,
        port=host.port,
        ssl=host.ssl,
    )

from_env(server_var='EMAIL_SERVER', user_var='EMAIL_USERNAME', password_var='EMAIL_PASSWORD', load_dotenv=True) classmethod

Read credentials from env vars (or .env).

Source code in email_profile/core/credentials.py
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
@classmethod
def from_env(
    cls,
    server_var: str = "EMAIL_SERVER",
    user_var: str = "EMAIL_USERNAME",
    password_var: str = "EMAIL_PASSWORD",
    load_dotenv: bool = True,
) -> 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)

    return cls.from_address(user, password)