Repository navigation
Expand file tree
/
Copy pathflags.py
More file actions
103 lines (78 loc) · 3.24 KB
/
Copy pathflags.py
File metadata and controls
103 lines (78 loc) · 3.24 KB
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
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
92
93
94
95
96
97
98
99
100
101
102
103
"""Central registry for tester/feature flags.
A flag is an opt-in behavior override for testing (for example, pointing OAuth
login at a pre-release console). Every flag is declared once here and gets, for
free, a ``--<name>`` CLI argument and a ``<env>`` environment variable — the CLI
argument simply writes through to the environment, which is the single runtime
source of truth (modules read flags per request via :func:`value`, so tests can
toggle them with ``mock.patch.dict(os.environ, ...)``).
To add a flag:
1. Add a ``Flag`` entry to ``FLAGS`` below.
2. Read it where needed with ``flags.value(flags.MY_FLAG)``.
3. Document how to enable and test it in ``docs/flags.md``.
Flags are for testing overrides only — permanent configuration belongs in
``constants.py`` or a plain environment variable.
"""
from __future__ import annotations
import argparse
import os
from dataclasses import dataclass
@dataclass(frozen=True)
class Flag:
name: str
"""Kebab-case CLI name, exposed as ``--<name>``."""
env: str
"""Environment variable backing the flag; the CLI argument writes to it."""
help: str
"""One-line description shown in ``--help`` and docs."""
CONSOLE_URL = Flag(
name="console-url",
env="MCP_CONSOLE_URL",
help=(
"Base URL of an alternative Appwrite Console to use for OAuth "
"login/consent (e.g. https://new.appwrite.io). HTTP transport only."
),
)
EVENTS = Flag(
name="events",
env="MCP_EVENTS",
help=(
"Set to 1 to advertise MCP Events and serve the events/* methods. "
"HTTP transport only."
),
)
EVENTS_PUSH = Flag(
name="events-push",
env="MCP_EVENTS_PUSH",
help=(
"Set to 1 to also offer push delivery (events/stream) for MCP Events. "
"Needs the events flag and MCP_EVENTS_REDIS_URL."
),
)
FLAGS: tuple[Flag, ...] = (CONSOLE_URL, EVENTS, EVENTS_PUSH)
ENABLED_VALUES = frozenset({"1", "true", "yes", "on"})
"""Values that switch an on/off flag on (case-insensitive)."""
def value(flag: Flag) -> str | None:
"""The flag's current value (normalized), or ``None`` when unset."""
return os.getenv(flag.env, "").strip().rstrip("/") or None
def enabled(flag: Flag) -> bool:
"""Whether an on/off flag is switched on. Anything outside
:data:`ENABLED_VALUES`, including unset, means off."""
return (value(flag) or "").lower() in ENABLED_VALUES
def register_cli_args(parser: argparse.ArgumentParser) -> None:
"""Add a ``--<name>`` argument per flag. The default is ``None`` (not the
environment variable) so an explicit ``--<name> ""`` is distinguishable
from "not provided" and can clear a flag exported in the shell."""
for flag in FLAGS:
parser.add_argument(
f"--{flag.name}",
default=None,
help=f"Testing flag: {flag.help} (default ${flag.env}).",
)
def apply_cli_args(args: argparse.Namespace) -> None:
"""Write parsed CLI flag values back to their environment variables.
A flag not provided on the CLI leaves its environment variable untouched;
an explicit empty value (``--<name> ""``) clears it."""
for flag in FLAGS:
raw = getattr(args, flag.name.replace("-", "_"), None)
if raw is not None:
os.environ[flag.env] = raw