Files
fengqun/swarm_minimal/local_env.py
T
gongzhiyongandOmX 10a980b0bf Establish agent swarm quality evidence
Define Agent and swarm-specific acceptance evidence, move the reports under docs, and make the homepage point to the current standard, live run, model I/O, and handoff evidence.

Constraint: Agent quality standards are configured from industry AI and agent risk references because there is no single accepted swarm-Agent certification standard.

Rejected: Treating py_compile or unittest as the primary quality standard | they are evidence collection tools, not the Agent quality standard itself.

Confidence: high

Scope-risk: moderate

Directive: Keep future standard reports under docs/ and keep secrets in ignored local .env files only.

Tested: git diff --cached --check; python -B -m py_compile swarm_minimal/*.py examples/*.py tests/*.py; python -B -m unittest discover -s tests; python -u -B examples/run_academic_standard_evaluation.py

Not-tested: Did not rerun the full live Azure/NewAPI S07 scenario after moving docs; previous live run 3e8e58ae4e084bc8b90cf5c46f8992f3 passed before the docs relocation.

Co-authored-by: OmX <omx@oh-my-codex.dev>
2026-05-16 14:36:47 +08:00

64 lines
1.8 KiB
Python

"""Load local environment files without adding a dependency.
The preferred file is ``.env`` under the project root. For local test runs,
``examples/.env`` is also accepted so users can keep live-test credentials next
to the example entrypoints. Both paths are ignored by git and should never be
committed.
"""
from __future__ import annotations
from pathlib import Path
import os
def find_project_env(root: Path) -> Path | None:
"""Return the first supported private env file path for a project."""
for path in (root / ".env", root / "examples" / ".env"):
if path.exists():
return path
return None
def load_project_env(root: Path, *, override: bool = False) -> int:
"""Load the supported project env file if one exists."""
path = find_project_env(root)
if path is None:
return 0
return load_env_file(path, override=override)
def load_env_file(path: Path, *, override: bool = False) -> int:
"""Load KEY=VALUE pairs from a local env file.
Returns the number of values loaded. Lines beginning with ``#`` and empty
lines are ignored. Existing environment variables are preserved unless
``override`` is true.
"""
if not path.exists():
return 0
loaded = 0
for raw_line in path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
key = key.strip()
value = _strip_quotes(value.strip())
if not key:
continue
if override or key not in os.environ:
os.environ[key] = value
loaded += 1
return loaded
def _strip_quotes(value: str) -> str:
if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"'}:
return value[1:-1]
return value