ears.py¶
clew/wiki/ears.py
EARS: which Easy Approach to Requirements Syntax pattern a requirement statement follows (S9).
ears_badge
str clew.wiki.ears.ears_badge(str statement)
The badge a requirement page carries when EARS is on.
Source · 7 lines
def ears_badge(statement: str) -> str:
"""@brief Pattern, or "not EARS". @return HTML."""
pattern = ears_pattern(statement)
label = f"EARS: {pattern}" if pattern else "not EARS"
return f'<span class="ears ears-{pattern or "none"}">{label}</span>'
Activity · 3 steps
Requirements
Flows
ears_notes
list[str] clew.wiki.ears.ears_notes(db, bool enabled)
clew wiki check 's EARS notes: each indexed requirement whose statement follows none.
Source · 18 lines
def ears_notes(db, enabled: bool) -> list[str]: # noqa: ANN001
"""NOTES, NOT PROBLEMS: the caller prints them and leaves the exit code alone, because S9 is
never a gate. The full statement is printed, so a writer can see what to rephrase.
@brief List the requirements in no EARS pattern.
@return Notes.
"""
if not enabled:
return []
from ..query._common import connect
with connect(db) as conn:
rows = conn.execute("SELECT id, acceptance, title FROM requirements ORDER BY id").fetchall()
return [
f"note: {req} is in no EARS pattern: {statement_of(acc, title)!r}"
for req, acc, title in rows
if ears_pattern(statement_of(acc, title)) is None
]1 more lines, not shown
Activity · 6 steps
Requirements
ears_pattern
str | None clew.wiki.ears.ears_pattern(str statement)
The EARS pattern a statement follows, or None when it follows none.
Source · 20 lines
def ears_pattern(statement: str) -> str | None:
"""`If` needs its `then`: EARS's unwanted-behaviour form is "If …, then the … shall …", and a
bare "If …, the … shall" is a common slip the badge should not bless.
@brief Classify one statement.
@return Pattern name or None.
"""
text = " ".join(str(statement or "").split())
found: list[str] = []
while match := _CLAUSE.match(text):
found.append(match.group(1).lower())
text = text[match.end() :]
if "if" in found and not text.lower().startswith("then "):
return None
if not _SHALL.match(text):
return None
names = dict(_KEYWORDS)
return "complex" if len(found) > 1 else names[found[0]] if found else "ubiquitous"
Activity · 11 steps
Requirements
Flows
statement_of
str clew.wiki.ears.statement_of(object acceptance, object title)
The statement EARS is judged on: the acceptance text, else the title.
Source · 10 lines
def statement_of(acceptance: object, title: object) -> str:
"""A catalogue's title is usually a label ("Load the data"); the acceptance text is where a
requirement is stated, so it is read first.
@brief Pick the statement.
@return Text.
"""
return str(acceptance or title or "")