Skip to main content

write.add_consumer

write.add_consumer(spec, unit, ref, *, kind=None, note='', dry_run=False)

Append one [[consumers]] block to a unit record: the writer behind babregen add-consumer.

Parameters

Name Type Description Default
spec RegenSpec The loaded spec. required
unit str A unit name or a unique suffix of it. required
ref str A repository-relative path in its plain form, or, for kind external, free text such as <repo>:<path>. required
kind str | None code, test, doc or external. By default the rule of new-unit: a file that is not Python is a doc, a Python file below the source directory is code, one below tests is a test, any other is code. None
note str What the consumer relies on (prose). Without it the block has no note. ''
dry_run bool Check everything and return the text, but write nothing. False

Returns

Name Type Description
Written Written The record, the appended text and the findings of check_unit for it.

Raises

Name Type Description
SpecError If the unit is unknown, ref is already declared (as a consumer, an evaluation file or a unit test), a ref that is not external does not exist (a similar path is suggested when its directory holds one), the record keeps its consumers as an inline array or the result would not load.

Examples

from pathlib import Path
from tempfile import TemporaryDirectory

from babregen import add_consumer, load_spec, make_sample_repo

with TemporaryDirectory() as tmp:
    root = make_sample_repo(Path(tmp) / "demo")
    (root / "scripts").mkdir()
    (root / "scripts/use_it.py").write_text("from samplepkg.core import calc\n")
    done = add_consumer(
        load_spec(root), "core.calc", "scripts/use_it.py", dry_run=True
    )
    print(done.text)

[[consumers]]
ref = "scripts/use_it.py"
kind = "code"