event_edges.py¶
clew/event_edges.py
Author-DECLARED event edges, recovered from doxygen xrefitem aliases.
_edges_for
list[tuple[int, int, str]] clew.event_edges._edges_for(dict[str, dict[str, set[int]]] topics)
Expand topic buckets into (emitter_rowid, handler_rowid, topic) triples.
Source · 17 lines
def _edges_for(topics: dict[str, dict[str, set[int]]]) -> list[tuple[int, int, str]]:
"""Every emitter of a topic is paired with every handler of it. This is the
same honesty level as `shared_key_edges`: sharing a topic proves a POSSIBLE
causal path, not that a specific emit reached a specific handler — no
static reading of a decoupled bus can prove the latter. A topic with only
emitters (or only handlers) yields no edge; those one-sided topics are
reported by the caller as a diagnostic, since an event emitted but never
handled is usually a real defect.
"""
edges: set[tuple[int, int, str]] = set()
for topic, bucket in topics.items():
for erow in bucket[PRODUCER]:
for hrow in bucket[CONSUMER]:
edges.add((erow, hrow, topic))
return sorted(edges)
Activity · 6 steps
Requirements
_topic_roles
dict[str, dict[str, set[int]]] clew.event_edges._topic_roles(list[tuple[str, str, str, int, bool]] tagged)
Collapse decl/def duplicates into one rowid set per topic and role.
Source · 21 lines
def _topic_roles(
tagged: list[tuple[str, str, str, int, bool]],
) -> dict[str, dict[str, set[int]]]:
"""Doxygen documents one function twice when it is declared in a header and
defined in a .cpp, so the SAME logical emitter can carry the tag on two
rowids. Collapse per (topic, role, name), preferring the definition row —
but only ever among rows that carried the tag, never widening to untagged
same-named functions.
"""
grouped: dict[tuple[str, str, str], list[tuple[int, bool]]] = {}
for topic, role, name, rowid, is_def in tagged:
grouped.setdefault((topic, role, name), []).append((rowid, is_def))
topics: dict[str, dict[str, set[int]]] = {}
for (topic, role, _name), entries in grouped.items():
defs = [r for r, is_def in entries if is_def]
chosen = defs if defs else [r for r, _ in entries]
bucket = topics.setdefault(topic, {PRODUCER: set(), CONSUMER: set()})
bucket[role].update(chosen)
return topics
Activity · 10 steps
Requirements
classify_aliases
tuple[dict[str, str], tuple[str, ...]] clew.event_edges.classify_aliases(Path doxyfile, dict[str, str]|None event_tags)
Split a target's xrefitem ALIASES into claimed event keys and unclaimed tags.
Source · 37 lines
def classify_aliases(
doxyfile: Path, event_tags: dict[str, str] | None
) -> tuple[dict[str, str], tuple[str, ...]]:
"""BOTH HALVES RETURNED, because both are answers. The claimed keys drive the
import; the unclaimed tags are the only evidence a reader gets that this repo
documents a bus in words the active vocabulary does not know.
Split out of `_declared_event_keys` for gh#320 so the diagnostic can be reported
as DATA and not only logged. It is pure apart from reading one file, which is what
lets the stamp site re-derive it instead of threading a metadata value back out
through the importer — the choice `cli` already documents for `key_alias_prefixes`.
Re-deriving is only safe because nothing in the database can disagree with it: the
unclaimed tags produce no rows, by definition.
An unreadable Doxyfile is a correct negative, not an error — a synthesised Doxyfile
(#33) legitimately has no ALIASES, and reports zero of each.
@brief Classify a repo's ALIASES against the active event vocabulary.
@return Claimed keys and unclaimed tag names.
@version 1
"""
vocabulary = event_tags if event_tags is not None else DEFAULT_EVENT_TAGS
try:
text = doxyfile.read_text(encoding="utf-8", errors="replace")
except OSError:
return {}, ()
keys: dict[str, str] = {}
unrecognised: set[str] = set()
for tag, key in _ALIAS_RE.findall(text):
role = vocabulary.get(tag.lower())
if role in (PRODUCER, CONSUMER):
keys[key] = role
else:
unrecognised.add(tag)
return keys, tuple(sorted(unrecognised))
Activity · 14 steps
Requirements
import_event_edges
None clew.event_edges.import_event_edges(Path db_path, Path doxyfile, dict[str, str]|None event_tags=None)
Import author-declared @emits/@handles event edges into shared_key_edges.
Called by _build_stages
Tested by
Calls _declared_event_keys, _edges_for, _harvest_tagged, _report_one_sided, _topic_roles
Source · 42 lines
def import_event_edges(
db_path: Path,
doxyfile: Path,
event_tags: dict[str, str] | None = None,
) -> None:
"""Recover the event graph the author already declared.
Rows land as `edge_kind='event'`, `declared=1`,
`source='shared_key_declared'`, `dispatch_mode='keyed'` (a topic-keyed bus)
and `edge_triggered=1` (an event is edge-triggered by definition — the
first time anything populates that column). Runs before
`annotate_thread_boundaries`, so these edges also acquire
crosses_thread/to_thread_id.
@brief Import declared event edges.
@version 2
"""
keys = _declared_event_keys(Path(doxyfile), event_tags)
if not keys:
logger.info("event edges: no event xrefitem alias declared — skipping (correct negative)")
return
conn = sqlite3.connect(db_path)
try:
topics = _topic_roles(_harvest_tagged(conn, keys))
edges = _edges_for(topics)
conn.executemany(
"INSERT OR IGNORE INTO shared_key_edges "
"(writer_rowid, reader_rowid, key_name, edge_kind, declared, source, "
" confidence, dispatch_mode, edge_triggered) "
"VALUES (?, ?, ?, 'event', 1, 'shared_key_declared', 'high', 'keyed', 1)",
edges,
)
conn.commit()
_report_one_sided(topics)
logger.info(
"event edges: %d topics from %d declared alias(es) → %d edges",
len(topics),
len(keys),
len(edges),
)
finally:
conn.close()1 more lines, not shown