Skip to content

Commit a4ffd34

Browse files
abrichrclaude
andauthored
feat: disable telemetry via pyproject.toml [tool.openadapt] (#5)
Enterprises can commit one file to disable telemetry for all devs: [tool.openadapt] telemetry = false Walks up from cwd to find nearest pyproject.toml. Uses tomllib (stdlib 3.11+) or tomli fallback. Only checks the first pyproject.toml found (nearest to cwd). Priority: DO_NOT_TRACK > OPENADAPT_TELEMETRY_ENABLED > pyproject.toml > CI detection. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9b2811c commit a4ffd34

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/openadapt_telemetry/posthog.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,56 @@ def _is_truthy(raw: str | None) -> bool:
3737
return str(raw or "").strip().lower() in {"1", "true", "yes", "on"}
3838

3939

40+
def _pyproject_telemetry_disabled() -> bool:
41+
"""Check if telemetry is disabled via pyproject.toml [tool.openadapt].
42+
43+
Walks up from cwd looking for pyproject.toml with::
44+
45+
[tool.openadapt]
46+
telemetry = false
47+
48+
This lets enterprises commit one file to their repo to disable
49+
telemetry for all developers — no per-user .env needed.
50+
"""
51+
try:
52+
import tomllib
53+
except ImportError:
54+
try:
55+
import tomli as tomllib # type: ignore[no-redef]
56+
except ImportError:
57+
return False
58+
59+
from pathlib import Path
60+
61+
for parent in [Path.cwd(), *Path.cwd().parents]:
62+
pyproject = parent / "pyproject.toml"
63+
if pyproject.is_file():
64+
try:
65+
data = tomllib.loads(pyproject.read_text())
66+
val = data.get("tool", {}).get("openadapt", {}).get("telemetry")
67+
if val is not None:
68+
return not _is_truthy(str(val))
69+
except Exception:
70+
pass
71+
break # only check the nearest pyproject.toml
72+
return False
73+
74+
4075
def _usage_enabled() -> bool:
76+
# 1. Standard DO_NOT_TRACK env var (consoledonottrack.com)
4177
if _is_truthy(os.getenv("DO_NOT_TRACK")):
4278
return False
4379

80+
# 2. Explicit env var
4481
explicit = os.getenv("OPENADAPT_TELEMETRY_ENABLED")
4582
if explicit is not None:
4683
return _is_truthy(explicit)
4784

85+
# 3. pyproject.toml [tool.openadapt] telemetry = false
86+
if _pyproject_telemetry_disabled():
87+
return False
88+
89+
# 4. CI environments default to off
4890
if is_ci_environment() and not _is_truthy(os.getenv("OPENADAPT_TELEMETRY_IN_CI")):
4991
return False
5092

0 commit comments

Comments
 (0)