83 lines
4.9 KiB
Python
83 lines
4.9 KiB
Python
"""do-epic's machine-wide settings: `~/.claude-projects/do-epic/config.toml`, and `repo-rule.md` beside it.
|
|
|
|
One pair per machine, the same for every epic (docs/decisions.md → `do-epic: drives one Jira epic …`). The config is TOML:
|
|
|
|
jira-url = "https://acme.atlassian.net"
|
|
env-file = "~/.secrets/acme-jira.env"
|
|
repoless-model = "claude-opus-5-5"
|
|
clone-folder = "~/code"
|
|
language = "Swedish"
|
|
|
|
- `jira-url` is the Jira site's address: https, a host, and no path.
|
|
- `env-file` is the file of Jira credentials: a path that starts with / or ~/, outside every folder of `~/.claude-projects` and outside the clone folder, which hold git checkouts. `~/.claude-projects/<key>.env` is fine.
|
|
- `repoless-model` is the model that works an epic item with no repo: a model on the routing ladder, `model_routing/routing-ladder.json`.
|
|
- `clone-folder` is the folder a missing repo is cloned into: a path that starts with / or ~/. `~/code` where left out.
|
|
- `language` is the default language of Jira text, by its English name. English where left out.
|
|
- `repo-rule.md` is prose that says how to tell an item's repo, and mustn't be blank."""
|
|
import dataclasses
|
|
import pathlib
|
|
import re
|
|
|
|
from project_config.settings import anywhere, CONFIG, DO_EPIC, ENV_FILE, LANGUAGE, language_fault, lies_in, parsed, PROJECTS, SITE
|
|
from failure import Failure
|
|
from model_routing.ladder_file import read_ladder, SHIPPED
|
|
|
|
REPO_RULE = "repo-rule.md"
|
|
# Three readers, seated as the human who writes this config, picked these spellings from four candidates each, 3-0: `jira-url` over `site`, `clone-folder` over `repos-folder` (which reads as the folder of repos that already exist) and `repoless-model` over `no-repo-model`. `env-file` and `language` keep the names a project's config gives them.
|
|
JIRA_URL = "jira-url"
|
|
REPOLESS_MODEL = "repoless-model"
|
|
CLONE_FOLDER = "clone-folder"
|
|
SETTINGS = (JIRA_URL, ENV_FILE, REPOLESS_MODEL)
|
|
OPTIONAL = (CLONE_FOLDER, LANGUAGE)
|
|
LISTED = f"The settings are {', '.join(SETTINGS)}, and optionally {' and '.join(OPTIONAL)}."
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class DoEpicConfig:
|
|
"""folder is `~/.claude-projects/do-epic`; repo_rule is `repo-rule.md`'s text."""
|
|
folder: pathlib.Path
|
|
jira_url: str
|
|
env_file: pathlib.Path
|
|
clone_folder: pathlib.Path
|
|
language: str
|
|
repoless_model: str
|
|
repo_rule: str
|
|
|
|
|
|
def load(home: pathlib.Path, ladder_file: pathlib.Path = SHIPPED) -> DoEpicConfig | Failure:
|
|
"""The settings and the repo rule under home; a Failure naming the file it can't read, else each missing or misspelt setting it finds, so that do-epic refuses to start."""
|
|
folder = home / PROJECTS / DO_EPIC
|
|
values = parsed(folder / CONFIG)
|
|
if isinstance(values, Failure):
|
|
return Failure(f"{values.reason}. {LISTED}")
|
|
ladder = read_ladder(ladder_file)
|
|
if isinstance(ladder, Failure):
|
|
return ladder
|
|
models = sorted({rung.model for rung in ladder.rungs})
|
|
rule: str | Failure
|
|
try:
|
|
rule = (folder / REPO_RULE).read_text(encoding="utf-8")
|
|
except (OSError, UnicodeDecodeError) as failure:
|
|
rule = Failure(f"cannot read {folder / REPO_RULE}: {failure}")
|
|
config_folders = [path for path in (home / PROJECTS).iterdir() if path.is_dir()]
|
|
clone_folder = anywhere(values.get(CLONE_FOLDER, "~/code"), home)
|
|
env_file = anywhere(values.get(ENV_FILE), home)
|
|
language = values.get(LANGUAGE, "English")
|
|
url, model = values.get(JIRA_URL), values.get(REPOLESS_MODEL)
|
|
faults = [
|
|
*(f"`{name}` is missing" for name in SETTINGS if name not in values),
|
|
*(f"`{name}` is not a setting" for name in values if name not in (*SETTINGS, *OPTIONAL)),
|
|
*([f"`{JIRA_URL}` is {url!r}, not a site's https address without a path, such as \"https://acme.atlassian.net\""] if JIRA_URL in values and not (isinstance(url, str) and re.fullmatch(SITE, url)) else []),
|
|
*([f"`{CLONE_FOLDER}` is {values[CLONE_FOLDER]!r}, not a path that starts with / or ~/"] if clone_folder is None else []),
|
|
*([f"`{ENV_FILE}` is {values[ENV_FILE]!r}, not a path that starts with / or ~/"] if ENV_FILE in values and env_file is None else []),
|
|
*([f"`{ENV_FILE}` is {values[ENV_FILE]!r}, inside a folder of ~/{PROJECTS} or the clone folder, which hold git checkouts"] if env_file and lies_in(env_file, *config_folders, *([clone_folder] if clone_folder else [])) else []),
|
|
*([f"`{LANGUAGE}` is {language!r}, {fault}"] if LANGUAGE in values and (fault := language_fault(language)) else []),
|
|
*([f"`{REPOLESS_MODEL}` is {model!r}, not a model on the routing ladder: {', '.join(models)}"] if REPOLESS_MODEL in values and model not in models else []),
|
|
*([rule.reason] if isinstance(rule, Failure) else [f"`{REPO_RULE}` is blank"] if not rule.strip() else []),
|
|
]
|
|
if faults:
|
|
return Failure(f"{folder}: {'; '.join(faults)}. {LISTED}")
|
|
# Without a fault, each value above is present and of the type asserted here.
|
|
assert clone_folder and env_file and isinstance(url, str) and isinstance(model, str) and isinstance(language, str) and isinstance(rule, str)
|
|
return DoEpicConfig(folder, url, env_file, clone_folder, language, model, rule)
|