Skip to content

ontocast.onto.iri_policy

as_sparql_iriref(value)

Render an IRI for a SPARQL IRIREF slot, or None if it cannot be one.

Callers should log-and-skip on None rather than raising: a single malformed IRI must not fail a whole query.

Parameters:

Name Type Description Default
value str

IRI text, optionally wrapped in Turtle-style angle brackets.

required

Returns:

Type Description
str | None

str | None: <iri> ready for interpolation, or None when value

str | None

is empty or contains a codepoint IRIREF forbids.

Source code in ontocast/onto/iri_policy.py
def as_sparql_iriref(value: str) -> str | None:
    """Render an IRI for a SPARQL ``IRIREF`` slot, or ``None`` if it cannot be one.

    Callers should log-and-skip on ``None`` rather than raising: a single
    malformed IRI must not fail a whole query.

    Args:
        value: IRI text, optionally wrapped in Turtle-style angle brackets.

    Returns:
        str | None: ``<iri>`` ready for interpolation, or ``None`` when ``value``
        is empty or contains a codepoint ``IRIREF`` forbids.
    """
    text = strip_iri_brackets(value).strip()
    if not text:
        return None
    if any(char in _IRIREF_FORBIDDEN or ord(char) <= 0x20 for char in text):
        return None
    return f"<{text}>"

canonicalize_namespace_iri(namespace)

Map a delimiter-normalized namespace IRI to its canonical alias.

Source code in ontocast/onto/iri_policy.py
def canonicalize_namespace_iri(namespace: str) -> str:
    """Map a delimiter-normalized namespace IRI to its canonical alias."""
    return _NAMESPACE_ALIASES.get(namespace, namespace)

normalize_namespace_iri(namespace, *, context='auto')

Return a namespace IRI with a deterministic terminal delimiter.

Existing trailing # or / are preserved. When absent, we append # for ontology contexts and / for facts/default contexts. Known namespace aliases (e.g. http://schema.org/) are mapped to their canonical form.

Source code in ontocast/onto/iri_policy.py
def normalize_namespace_iri(namespace: str, *, context: URIContext = "auto") -> str:
    """Return a namespace IRI with a deterministic terminal delimiter.

    Existing trailing ``#`` or ``/`` are preserved. When absent, we append ``#``
    for ontology contexts and ``/`` for facts/default contexts. Known namespace
    aliases (e.g. ``http://schema.org/``) are mapped to their canonical form.
    """
    text = strip_iri_brackets(namespace)
    if text.endswith("#") or text.endswith("/"):
        return canonicalize_namespace_iri(text)
    resolved_context = _resolve_context(text, context)
    suffix = "#" if resolved_context == "ontology" else "/"
    return canonicalize_namespace_iri(f"{text}{suffix}")

strip_iri_brackets(value)

Strip optional Turtle-style <...> wrappers from an IRI string.

Source code in ontocast/onto/iri_policy.py
def strip_iri_brackets(value: str) -> str:
    """Strip optional Turtle-style ``<...>`` wrappers from an IRI string."""
    text = value.strip()
    if text.startswith("<") and text.endswith(">") and len(text) > 2:
        return text[1:-1].strip()
    return text