Skip to main content

spec.locate_name

spec.locate_name(path, key, name, item_id=None)

Find the line of a name inside the array that holds it.

A finding about a name in evaluations = [...] is more useful at the line of that name than at the line of the key, when the array stands on several lines. The search is over the array of key at the top of the file (no item_id) or in the [[intent]] item whose id is item_id. A name that is not found in the array (an escaped one, say) gives the line of the key.

Parameters

Name Type Description Default
path Path | str The TOML file. required
key str The key of the array, for example "evaluations" or "decisions". required
name str The string to find, without quotes. required
item_id str | None The id of the intent item, or None for the top of the file. None

Returns

Name Type Description
int | None int | None: The 1-based line, or None if the file cannot be read, the item or the key does not exist or the key is not unique in its scope.

Examples

from pathlib import Path
from tempfile import TemporaryDirectory

from babregen import make_sample_repo
from babregen.spec import locate_name

with TemporaryDirectory() as tmp:
    root = make_sample_repo(Path(tmp) / "demo")
    unit = root / "regen" / "units" / "core.calc.toml"
    print(locate_name(unit, "evaluations", "test_add", item_id="C-1"))
    print(locate_name(unit, "public", "add"))
12
3