vocabulary.py¶
clew/vocabulary.py
The schema's enumerated vocabularies — ONE definition per value set.
check
str clew.vocabulary.Vocabulary.check(self, str column)
SQL CHECK clause constraining a column to this vocabulary.
Source · 11 lines
def check(self, column: str) -> str:
"""Explicit quote-and-join, never `{tuple!r}` — a 1-tuple repr carries a
trailing comma that SQLite rejects, and repr quotes an apostrophe-bearing
value with double quotes, which SQLite reads as an identifier.
@brief Generate this vocabulary's CHECK clause for one column.
@version 1
"""
inner = ", ".join("'" + v.replace("'", "''") + "'" for v in self.values)
return f"CHECK({column} IN ({inner}))"
Activity · 2 steps
check(table: str, column: str)
Source · 11 lines
def check(table: str, column: str) -> str:
"""Raises rather than returning an empty string for an unregistered column:
a silently-absent CHECK is the exact hole this module exists to close.
@brief Look up a column's vocabulary and generate its CHECK clause.
@version 1
"""
vocab = COLUMNS.get((table, column))
if vocab is None:
raise KeyError(f"no vocabulary registered for {table}.{column} — add it to COLUMNS")
return vocab.check(column)Activity · 4 steps
Requirements
declaration_origin
str clew.vocabulary.declaration_origin(object source, str section)
Name the manifest a declared value came from, for an error message.
Called by load_dispatch_manifest, load_lock_patterns, load_shared_key_patterns, load_thread_patterns
Calls none
Source · 13 lines
def declaration_origin(source: object, section: str) -> str:
"""A `DeclarationError` is only actionable if it names the file to edit, and
the loaders receive either a standalone manifest path or an already-parsed
`.clew.yaml` section — this collapses both into one label.
@brief Label a declaration's origin for a fail-closed error message.
@version 2
"""
if isinstance(source, (str, Path)):
return str(source)
return f".clew.yaml [{section}]"
Activity · 3 steps
Requirements
validated
str clew.vocabulary.Vocabulary.validated(self, str value, *str owner, str field)
Return a declared token when allowed, else raise DeclarationError.
Source · 18 lines
def validated(self, value: str, *, owner: str, field: str) -> str:
"""The single choke point every declared-enum path routes through. It
raises rather than falling back because there is no honest fallback: see
the module docstring on lock identity collapse.
@brief Validate one declared token, failing closed.
@version 1
"""
if value in self.values:
return value
raise DeclarationError(
f"{owner}: invalid {field} {value!r} — allowed: {', '.join(self.values)}"
)
# ── The registry ─────────────────────────────────────────────────────────────
# One entry per distinct MEANING, even where two entries carry identical tuples.