feat: enhance profile path resolution and add example profiles for better project adaptability
This commit is contained in:
72
scripts/aiw/profile.py
Normal file
72
scripts/aiw/profile.py
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Profile path resolution for AI Workspace scripts.
|
||||
|
||||
Profiles own their configuration. Reusable scripts should call this module
|
||||
instead of hardcoding root-level project paths.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
DEFAULT_WORKSPACE = {
|
||||
"knowledge_dir": "project-knowledge",
|
||||
"inbox_dir": "ai/inbox",
|
||||
"index_dir": ".aiw/indexes/{profile}",
|
||||
}
|
||||
|
||||
|
||||
def workspace_config_path(profile: str, root: Path | None = None) -> Path:
|
||||
base = root or ROOT
|
||||
return base / "profiles" / profile / "workspace.json"
|
||||
|
||||
|
||||
def load_workspace_config(profile: str, root: Path | None = None) -> dict[str, Any]:
|
||||
base = root or ROOT
|
||||
config = dict(DEFAULT_WORKSPACE)
|
||||
config["profile"] = profile
|
||||
path = workspace_config_path(profile, root=base)
|
||||
if path.is_file():
|
||||
try:
|
||||
loaded = json.loads(path.read_text(encoding="utf-8"))
|
||||
if isinstance(loaded, dict):
|
||||
config.update(loaded)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
return config
|
||||
|
||||
|
||||
def resolve_path(raw: str | None, *, profile: str, root: Path | None = None, fallback: str) -> Path:
|
||||
base = root or ROOT
|
||||
value = (raw or fallback).format(profile=profile)
|
||||
path = Path(value).expanduser()
|
||||
return path if path.is_absolute() else base / path
|
||||
|
||||
|
||||
def knowledge_dir(profile: str, root: Path | None = None) -> Path:
|
||||
config = load_workspace_config(profile, root=root)
|
||||
return resolve_path(config.get("knowledge_dir"), profile=profile, root=root, fallback="project-knowledge")
|
||||
|
||||
|
||||
def inbox_dir(profile: str, root: Path | None = None) -> Path:
|
||||
config = load_workspace_config(profile, root=root)
|
||||
return resolve_path(config.get("inbox_dir"), profile=profile, root=root, fallback="ai/inbox")
|
||||
|
||||
|
||||
def index_dir(profile: str, root: Path | None = None) -> Path:
|
||||
config = load_workspace_config(profile, root=root)
|
||||
return resolve_path(config.get("index_dir"), profile=profile, root=root, fallback=".aiw/indexes/{profile}")
|
||||
|
||||
|
||||
def relative_to_root(path: Path, root: Path | None = None) -> str:
|
||||
base = root or ROOT
|
||||
try:
|
||||
return str(path.relative_to(base))
|
||||
except ValueError:
|
||||
return str(path)
|
||||
Reference in New Issue
Block a user