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.

clew/wiki/ears.py:88

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
lines 90–90pattern = ears_pattern(statement)lines 91–91label = f"EARS: {pattern}" if pattern else "not EARS"lines 92–92return f'<span class="ears ears-{pattern or "none"}">{label}</span>'

Requirements

REQ-DDB-WIKI-042

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.

clew/wiki/ears.py:101

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
lines 108–108◇ if not enabledlines 109–109return []lines 110–110from ..query._common import connectlines 112–112with connect(db) as connlines 113–113rows = conn.execute("SELECT id, acceptance, title FROM requirements ORDER BY id").fetchall()lines 114–118return [ 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 ]

Requirements

REQ-DDB-WIKI-042

ears_pattern

str | None clew.wiki.ears.ears_pattern(str statement)

The EARS pattern a statement follows, or None when it follows none.

clew/wiki/ears.py:47

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
lines 54–54text = " ".join(str(statement or "").split())lines 55–55found: list[str] = []lines 56–56◇ while match := _CLAUSE.match(text)lines 57–57found.append(match.group(1).lower())lines 58–58text = text[match.end() :]lines 59–59◇ if "if" in found and not text.lower().startswith("then ")lines 60–60return Nonelines 61–61◇ if not _SHALL.match(text)lines 62–62return Nonelines 63–63names = dict(_KEYWORDS)lines 64–64return "complex" if len(found) > 1 else names[found[0]] if found else "ubiquitous"

Requirements

REQ-DDB-WIKI-042

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.

clew/wiki/ears.py:73

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 "")

Activity · 1 steps
lines 80–80return str(acceptance or title or "")

Requirements

REQ-DDB-WIKI-042

Flows