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.
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
Requirements
ensure_config_defaults_table
None clew.config_defaults.ensure_config_defaults_table(sqlite3.Connection conn)
Create the configuration-default table.
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
Requirements
harvest
Any clew.config_defaults._DefaultsHarvester.harvest(self, Any tree, bytes src_bytes)
Harvest one file's option defaults.
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
Requirements
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.
Called by _build, _build_stages, run_real_stages
Tested by
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