"""Explicit, framework-independent project profile records. The bundled profiles describe supported contexts, not ownership or access defaults. """ import json from pathlib import Path FIELDS = ( "profile", "development_context", "ownership", "branding", "contributor_access", "testing_expectations", "deployment_expectations", ) PROFILES = ("business", "personal") def read_flat_record(path): """Read the repository's flat string-only YAML subset without extra dependencies.""" values = {} for number, raw in enumerate(Path(path).read_text(encoding="utf-8").splitlines(), 1): line = raw.strip() if not line or line.startswith("#"): continue if ":" not in line: raise ValueError(f"{path}:{number}: expected key: value") key, value = (part.strip() for part in line.split(":", 1)) if key not in FIELDS or key in values or not value: raise ValueError(f"{path}:{number}: invalid or duplicate field {key!r}") if value.startswith('"'): try: value = json.loads(value) except json.JSONDecodeError as exc: raise ValueError(f"{path}:{number}: invalid quoted value") from exc if not isinstance(value, str) or not value.strip(): raise ValueError(f"{path}:{number}: value must be nonempty text") values[key] = value if set(values) != set(FIELDS): raise ValueError(f"{path}: expected exactly {', '.join(FIELDS)}") return values def build_record(template_path, *, profile, ownership, branding, contributor_access, testing_expectations, deployment_expectations): if profile not in PROFILES: raise ValueError("profile must be business or personal") template = read_flat_record(template_path) if template["profile"] != profile or template["development_context"] != profile: raise ValueError("profile template identity does not match selection") values = dict(profile=profile, development_context=profile, ownership=ownership, branding=branding, contributor_access=contributor_access, testing_expectations=testing_expectations, deployment_expectations=deployment_expectations) for key in FIELDS[2:]: value = values[key] if not isinstance(value, str) or not value.strip() or value.strip() == "project-specific": raise ValueError(f"{key} requires an explicit project value") values[key] = value.strip() return values def write_record(project, values): project = Path(project) if not project.is_dir() or project.is_symlink(): raise ValueError("project must be an existing, non-symlink directory") config_dir = project / ".bortstack" if config_dir.is_symlink(): raise ValueError("profile directory must not be a symlink") target = config_dir / "profile.yaml" if target.exists() or target.is_symlink(): raise FileExistsError(f"profile already exists: {target}") config_dir.mkdir(exist_ok=True) content = "# Explicit project context; values describe policy but grant no access.\n" content += "".join(f"{key}: {json.dumps(values[key], ensure_ascii=False)}\n" for key in FIELDS) with target.open("x", encoding="utf-8") as handle: handle.write(content) return target