config_defaults.py

clew/config_defaults.py

macro → on or off as the repository ships it.

defaults_harvester

Harvester clew.config_defaults.defaults_harvester()

The config-default stage's harvester.

clew/config_defaults.py:291

Source · 10 lines
def defaults_harvester() -> Harvester:
    """ONE CONSTRUCTION SITE, so the shared parse pass's cache key and this stage's key are
    equal by construction rather than by two edits staying in step.

    @brief Build this stage's harvester.
    @version 1
    """
    return _DefaultsHarvester()

Activity · 1 steps
lines 298–298return _DefaultsHarvester()

Requirements

REQ-DDB-PIPE-015

ensure_config_defaults_table

None clew.config_defaults.ensure_config_defaults_table(sqlite3.Connection conn)

Create the configuration-default table.

clew/config_defaults.py:159

Show on the map →

Called by _gate_db, conn, import_config_defaults, schema_db

Tested by

1 teststest_the_table_is_created_even_at_zero_rows

Calls none

Source · 26 lines
def ensure_config_defaults_table(conn: sqlite3.Connection) -> None:
    """CREATED EVEN AT ZERO ROWS, like `kconfig_gates`, and for the same reason: an empty table
    means "we looked and this repo decides nothing here", an absent one means "this layer never
    ran", and a consumer separating a correct negative from a detector failure needs both.

    @brief Create config_defaults.
    @version 1
    """
    conn.execute(
        f"""
        CREATE TABLE IF NOT EXISTS config_defaults (
            macro     TEXT NOT NULL,
            state     TEXT NOT NULL {check("config_defaults", "state")},
            value     TEXT NOT NULL DEFAULT '',
            file_path TEXT NOT NULL,
            line      INTEGER NOT NULL,
            doc       TEXT NOT NULL DEFAULT '',
            origin    TEXT NOT NULL,
            UNIQUE(macro, file_path, line)
        )
        """
    )
    conn.execute("CREATE INDEX IF NOT EXISTS idx_config_defaults_macro ON config_defaults(macro)")
    conn.commit()

Activity · 3 steps
lines 167–180conn.execute( f""" CREATE TABLE IF NOT EXISTS config_defaults ( macro TEXT NOT NULL, state TEXT NOT NULL {check("config_defaults", "state")}, value TEXT NOT NULL DEFAULT '', file_path TEXT NOT NULL, line INTEGER NOT NULL, doc TEXT NOT NULL DEFAULT '', origin TEXT NOT NULL, UNIQUE(macro, file_path, line) ) """ )lines 181–181conn.execute("CREATE INDEX IF NOT EXISTS idx_config_defaults_macro ON config_defaults(macro)")lines 182–182conn.commit()

Requirements

REQ-DDB-PIPE-015

harvest

Any clew.config_defaults._DefaultsHarvester.harvest(self, Any tree, bytes src_bytes)

Harvest one file's option defaults.

clew/config_defaults.py:208

Show on the map →

Called by none

Calls _active_define, _commented_define, _define_name, _guard_name

Source · 39 lines
    def harvest(self, tree: Any, src_bytes: bytes) -> Any:
        """Returns LISTS, not tuples, so a cache hit and a fresh parse are structurally
        identical — the payload round-trips through JSON, where a tuple returns as a list.

        @brief Walk one file's defines and commented-out defines.
        @return Default entries for this file.
        @version 1
        """
        found: list[list[Any]] = []
        stack = [tree.root_node]
        while stack:
            node = stack.pop()
            if node.type == _COMMENT_NODE:
                entry = _commented_define(node)
                if entry:
                    found.append(entry)
            elif node.type == _IFDEF_NODE:
                ## THE GUARD'S OWN DEFINE IS SKIPPED, NOT THE BLOCK IT WRAPS. Nearly every
                ## configuration header is wrapped in one, so descending is mandatory — what
                ## is dropped is the single `#define FOO_H` matching the `#ifndef FOO_H`.
                ##
                ## COMPARED BY NAME, NOT BY NODE IDENTITY. The parser hands out a fresh Python
                ## wrapper on every child access, so `child is guard_node` is never true and an
                ## identity filter silently keeps the guard it was written to drop.
                guard = _guard_name(node)
                stack.extend(
                    child
                    for child in node.children
                    if not (guard and child.type == _DEFINE_NODE and _define_name(child) == guard)
                )
                continue
            elif node.type == _DEFINE_NODE:
                entry = _active_define(node)
                if entry:
                    found.append(entry)
            stack.extend(node.children)
        return found

Activity · 18 steps
lines 216–216found: list[list[Any]] = []lines 217–217stack = [tree.root_node]lines 218–218◇ while stacklines 219–219node = stack.pop()lines 220–220◇ if node.type == _COMMENT_NODElines 221–221entry = _commented_define(node)lines 222–222◇ if entrylines 223–223found.append(entry)lines 224–224◇ elif node.type == _IFDEF_NODElines 232–232guard = _guard_name(node)lines 233–237stack.extend( child for child in node.children if not (guard and child.type == _DEFINE_NODE and _define_name(child) == guard) )lines 238–238continuelines 239–239◇ elif node.type == _DEFINE_NODElines 240–240entry = _active_define(node)lines 241–241◇ if entrylines 242–242found.append(entry)lines 243–243stack.extend(node.children)lines 244–244return found

Requirements

REQ-DDB-PIPE-015

import_config_defaults

int clew.config_defaults.import_config_defaults(Path db_path, Path repo_root, IndexCache|None cache=None, frozenset[str] declared=frozenset())

Fill config_defaults from the indexed source tree.

clew/config_defaults.py:339

Show on the map →

Called by _build, _build_stages, run_real_stages

Tested by

1 teststest_an_include_guard_is_not_an_option

Calls _rows_from_harvest, defaults_harvester, ensure_config_defaults_table, run_harvest

Source · 32 lines
def import_config_defaults(
    db_path: Path,
    repo_root: Path,
    cache: IndexCache | None = None,
    declared: frozenset[str] = frozenset(),
) -> int:
    """@brief Harvest and store configuration-option defaults.
    @return Row count.
    @version 1
    """
    conn = sqlite3.connect(str(db_path))
    try:
        ensure_config_defaults_table(conn)
        harvested = run_harvest(conn, repo_root, defaults_harvester(), cache)
        rows = _rows_from_harvest(conn, harvested, declared)
        conn.executemany(
            "INSERT OR IGNORE INTO config_defaults"
            "(macro, state, value, file_path, line, doc, origin) VALUES(?,?,?,?,?,?,?)",
            rows,
        )
        conn.commit()
    finally:
        conn.close()
    off = sum(1 for row in rows if row[1] == CONFIG_DEFAULT_OFF)
    logger.info(
        "config defaults: %d option(s) over %d header(s) — %d off by default, %d on",
        len(rows),
        len({row[3] for row in rows}),
        off,
        len(rows) - off,
    )
    return len(rows)

1 more lines, not shown

Activity · 10 steps
lines 349–349conn = sqlite3.connect(str(db_path))lines 350–350trylines 351–351ensure_config_defaults_table(conn)lines 352–352harvested = run_harvest(conn, repo_root, defaults_harvester(), cache)lines 353–353rows = _rows_from_harvest(conn, harvested, declared)lines 354–358conn.executemany( "INSERT OR IGNORE INTO config_defaults" "(macro, state, value, file_path, line, doc, origin) VALUES(?,?,?,?,?,?,?)", rows, )lines 359–359conn.commit()lines 362–362off = sum(1 for row in rows if row[1] == CONFIG_DEFAULT_OFF)lines 363–369logger.info( "config defaults: %d option(s) over %d header(s) — %d off by default, %d on", len(rows), len({row[3] for row in rows}), off, len(rows) - off, )lines 370–370return len(rows)

Requirements

REQ-DDB-PIPE-015