Skip to content

ontocast.config.section_labels

Load versioned section-label schemas from YAML in this package.

SectionLabelDef

Bases: BaseModel

One canonical section label and heading regex patterns.

Source code in ontocast/config/section_labels/__init__.py
class SectionLabelDef(BaseModel):
    """One canonical section label and heading regex patterns."""

    id: str
    heading_patterns: list[str] = Field(default_factory=list)

SectionLabelSchema

Bases: BaseModel

Domain-specific section label vocabulary.

Source code in ontocast/config/section_labels/__init__.py
class SectionLabelSchema(BaseModel):
    """Domain-specific section label vocabulary."""

    schema_version: str
    id: str
    description: str = ""
    labels: list[SectionLabelDef]

    @property
    def compiled_patterns(self) -> tuple[tuple[str, re.Pattern[str]], ...]:
        compiled: list[tuple[str, re.Pattern[str]]] = []
        for label_def in self.labels:
            for pattern in label_def.heading_patterns:
                compiled.append((label_def.id, re.compile(pattern, re.I)))
        return tuple(compiled)

clear_section_label_caches()

Clear loader caches (for tests).

Source code in ontocast/config/section_labels/__init__.py
def clear_section_label_caches() -> None:
    """Clear loader caches (for tests)."""
    load_manifest.cache_clear()
    load_section_label_schema.cache_clear()
    all_known_label_ids.cache_clear()

normalise_user_section_label(raw, *, schema_id=None)

Map user-supplied section name to a canonical label.

Source code in ontocast/config/section_labels/__init__.py
def normalise_user_section_label(
    raw: str,
    *,
    schema_id: str | None = None,
) -> str | None:
    """Map user-supplied section name to a canonical label."""
    if raw.strip() == "*":
        return "*"

    cleaned = raw.strip().lower().replace(" ", "_").replace("-", "_")
    if cleaned in all_known_label_ids():
        return cleaned

    resolved_id = resolve_section_schema_id(
        section_schema_id=schema_id,
        document_type_hint=None,
    )
    schema = load_section_label_schema(resolved_id)
    if cleaned in canonical_labels(schema):
        return cleaned

    matched = match_heading_line(raw, schema)
    if matched is not None:
        return matched

    for entry in load_manifest().schemas:
        other = load_section_label_schema(entry.id)
        matched = match_heading_line(raw, other)
        if matched is not None:
            return matched

    return None

resolve_section_schema_id(*, section_schema_id=None, document_type_hint=None)

Pick schema: explicit id, then hint substring match, then manifest default.

Source code in ontocast/config/section_labels/__init__.py
def resolve_section_schema_id(
    *,
    section_schema_id: str | None = None,
    document_type_hint: str | None = None,
) -> str:
    """Pick schema: explicit id, then hint substring match, then manifest default."""
    manifest = load_manifest()
    if section_schema_id and section_schema_id.strip():
        schema_id = section_schema_id.strip().lower()
        load_section_label_schema(schema_id)
        return schema_id

    if document_type_hint and document_type_hint.strip():
        hint_lower = document_type_hint.strip().lower()
        for needle, schema_id in manifest.document_type_hints.items():
            if needle.lower() in hint_lower:
                return schema_id

    return manifest.default_schema