File size: 1,600 Bytes
6a65890
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
"""The small public interface for one site-isolated backend."""

from __future__ import annotations

from collections.abc import Callable, Mapping
from pathlib import Path
from typing import Any

import sqlite3

from .database import SiteDatabaseLifecycle
from .mail import SiteMail
from .payments import SitePayments
from .runtime import RuntimeConfig, load_runtime


class SiteBackend:
    """One runtime contract, one site binding, and three deep capabilities."""

    def __init__(
        self,
        runtime: RuntimeConfig,
        lifecycle: SiteDatabaseLifecycle,
    ) -> None:
        self.config = runtime
        self.lifecycle = lifecycle
        self.mail = SiteMail(runtime, lifecycle)
        self.payments = SitePayments(runtime, lifecycle)

    @classmethod
    def open(
        cls,
        config: Path | str | Mapping[str, Any],
        *,
        data_root: Path | None = None,
        migration_hook: Callable[[sqlite3.Connection], None] | None = None,
        seed_hook: Callable[[sqlite3.Connection], None] | None = None,
    ) -> "SiteBackend":
        runtime = load_runtime(config)
        lifecycle = SiteDatabaseLifecycle(
            runtime,
            data_root=data_root,
            migration_hook=migration_hook,
            seed_hook=seed_hook,
        )
        return cls(runtime, lifecycle)

    @property
    def session_cookie(self) -> dict[str, Any]:
        """Framework-neutral Host-only cookie facts; intentionally no Domain."""

        return {
            "name": self.config.cookie_name,
            **dict(self.config.cookie_options),
        }