Files
claude-config/bin/do_epic/config.py
T

73 lines
4.5 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, only https. `env-file` is the file of Jira credentials, a path that starts with / or ~/ and lies outside `~/.claude-projects` and the clone folder, so that no git remote receives them. `repoless-model` is the model that works an epic item with no repo. `clone-folder` is the folder a missing repo is cloned into; `~/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."""
import dataclasses
import pathlib
import re
from failure import Failure
from project_config.settings import anywhere, CONFIG, DO_EPIC, ENV_FILE, language_fault, LANGUAGE, lies_in, parsed, PROJECTS, SITE
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)
# A model id as Claude Code takes it, such as claude-opus-5-5 or claude-sonnet-4-5-20250929.
MODEL_ID = re.compile(r"claude-[a-z]+-[0-9]+(?:-[0-9]+)*")
@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) -> DoEpicConfig | Failure:
"""The settings and the repo rule under home; a Failure naming each missing or misspelt one, so that do-epic refuses to start."""
folder = home / PROJECTS / DO_EPIC
values = parsed(folder / CONFIG)
if isinstance(values, Failure):
return values
try:
rule = (folder / REPO_RULE).read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as failure:
return Failure(f"cannot read {folder / REPO_RULE}: {failure}")
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 []),
# lies_in resolves both sides, so a symlink can't lead the file into a place it must stay out of.
*([f"`{ENV_FILE}` is {values[ENV_FILE]!r}, inside ~/{PROJECTS} or the clone folder, which a git remote may receive"] if env_file and lies_in(env_file, home / PROJECTS, *([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 id such as \"claude-opus-5-5\""] if REPOLESS_MODEL in values and not (isinstance(model, str) and MODEL_ID.fullmatch(model)) else []),
*([f"`{REPO_RULE}` is blank"] if not rule.strip() else []),
]
if faults:
return Failure(f"{folder}: {'; '.join(faults)}. The settings are {', '.join(SETTINGS)}, and optionally {' and '.join(OPTIONAL)}.")
# faults names a missing or misspelt value of each type asserted here.
assert clone_folder and env_file and isinstance(url, str) and isinstance(model, str) and isinstance(language, str)
return DoEpicConfig(folder, url, env_file, clone_folder, language, model, rule)