Skip to main content

write.write_unit

write.write_unit(spec, module, pace)

Write the draft of scaffold_unit to disk, reload it and check it.

This is the sequence behind babregen new-unit without --dry-run: draft the record, load the draft with the strict loader, create the file in regen/units/ without overwriting one that exists, load the file that now exists and run the unit check on it, so the caller sees at once what is still open.

Parameters

Name Type Description Default
spec RegenSpec The loaded spec. required
module str Dotted module name, with or without the package prefix. required
pace str fast, medium or slow. required

Returns

Name Type Description
Written Written The new record (path, text), with findings the findings of babregen.check_unit on it (a fresh draft always has INTENT01) and the next step; report formats them as the command prints them.

Raises

Name Type Description
SpecError If the module already has a unit record, pace is unknown, the draft does not load or the file cannot be written.
FileNotFoundError If the module has no file below the source directory.

Examples

from pathlib import Path
from tempfile import TemporaryDirectory

from babregen import load_spec, make_sample_repo, write_unit

with TemporaryDirectory() as tmp:
    root = make_sample_repo(Path(tmp) / "demo")
    (root / "regen/units/core.calc.toml").unlink()
    done = write_unit(load_spec(root), "core.calc", "slow")
    print(done.path.relative_to(root))
    print([f.id for f in done.findings])
regen/units/core.calc.toml
['INTENT01', 'COMPACT05']