dispatch.py

clew/dispatch.py

The indirect-dispatch DECLARATION — a repo's own map of its indirections.

empty_manifest

DispatchManifest clew.dispatch.empty_manifest()

An empty manifest, for the (universal, today) undeclared case.

clew/dispatch.py:258

Source · 11 lines
def empty_manifest() -> DispatchManifest:
    """Constructed rather than shared as a module constant: the sections are
    mutable lists, and a shared instance is one accidental `.append` away from
    leaking one build's declaration into the next.

    @brief Build a fresh empty manifest.
    @version 1
    """
    return DispatchManifest([], [], [], ())

Activity · 1 steps
lines 266–266return DispatchManifest([], [], [], ())

Requirements

REQ-DDB-SCHEMA-004

is_empty

bool clew.dispatch.DispatchManifest.is_empty(self)

True when the declaration would produce nothing.

clew/dispatch.py:246

Show on the map →

Called by none

Calls none

Source · 8 lines
    def is_empty(self) -> bool:
        """@brief True when nothing was declared.

        @version 1
        """
        return not (self.interfaces or self.tables or self.wrappers)

Activity · 1 steps
lines 251–251return not (self.interfaces or self.tables or self.wrappers)

Requirements

REQ-DDB-SCHEMA-004

load_dispatch_manifest

DispatchManifest clew.dispatch.load_dispatch_manifest(Path|dict|None source)

Parse a repo's declared indirect-dispatch conventions.

clew/dispatch.py:414

Show on the map →

Called by _build_stages, _wrapper_manifest

Tested by

25 teststest_a_declared_boundary_is_recorded_even_from_a_fuzzy_call_site, test_a_manifest_file_that_is_not_a_mapping_refuses_the_build, test_a_missing_required_endpoint_refuses_the_build, test_a_misspelled_entry_field_refuses_the_build, test_a_misspelled_section_refuses_the_build, test_a_section_that_is_not_a_list_of_mappings_refuses_the_build, test_a_standalone_manifest_file_and_a_declaration_section_share_one_parser, test_a_typod_direction_refuses_instead_of_filing_the_opposite_role, test_ambiguous_handler_name_records_NOTHING_rather_than_guessing, test_ambiguous_or_absent_class_is_skipped_not_guessed, test_boundary_flag_records_the_second_terminus_kind, test_declared_dispatch_table_connects_the_dispatcher_to_its_handlers
13 moretest_declared_interface_binding_continues_the_chain_to_the_implementor, test_declared_key_wrapper_produces_the_dataflow_edge, test_declared_wrapper_direction_selects_the_matcher_list, test_dispatch_is_validated_by_its_own_loader_not_a_second_copy, test_inherited_members_do_not_pair_the_interface_with_itself, test_method_filter_narrows_the_binding, test_no_declaration_writes_nothing_at_all, test_registration_site_is_harvested_but_is_not_itself_a_call_edge, test_shared_key_document_is_none_when_nothing_contributes, test_synthetic_edge_inherits_the_weakest_link_from_a_fuzzy_premise, test_the_declaration_route_reaches_the_stage_and_the_flag_wins, test_the_dispatch_manifest_participates_in_the_stage_cache_key, test_unresolvable_dispatch_via_leaves_the_handlers_unconnected

Calls _document, _entries, _interface_entry, _reject_unknown, _table_entry, _wrapper_entry, declaration_origin, empty_manifest

Source · 34 lines
def load_dispatch_manifest(source: Path | dict | None) -> DispatchManifest:
    """Undeclared is the norm: no repo has written a dispatch declaration yet, so
    this returns an empty manifest and the pipeline emits nothing. Anything
    malformed RAISES rather than degrading, because a silently-ignored
    declaration is indistinguishable from never having written one — including a
    misspelled SECTION name, which is the shape that would otherwise slip
    through (see `_reject_unknown`).

    @brief Load and validate the dispatch declaration.
    @version 2
    """
    doc = _document(source)
    if doc is None:
        return empty_manifest()
    origin = declaration_origin(source, SECTION_DISPATCH)
    _reject_unknown(doc, _DOCUMENT_KEYS, origin)
    manifest = DispatchManifest(
        [_interface_entry(e, origin) for e in _entries(doc, KEY_INTERFACES, origin)],
        [_table_entry(e, origin) for e in _entries(doc, KEY_TABLES, origin)],
        [_wrapper_entry(e, origin) for e in _entries(doc, KEY_WRAPPERS, origin)],
        tuple(str(p) for p in (doc.get(KEY_ALIAS_PREFIXES) or []) if str(p)),
    )
    if not manifest.is_empty():
        logger.info(
            "dispatch: %s declares %d interface binding(s), %d dispatch table(s), "
            "%d key wrapper(s)",
            origin,
            len(manifest.interfaces),
            len(manifest.tables),
            len(manifest.wrappers),
        )
    return manifest

Activity · 9 steps
lines 425–425doc = _document(source)lines 426–426◇ if doc is Nonelines 427–427return empty_manifest()lines 428–428origin = declaration_origin(source, SECTION_DISPATCH)lines 429–429_reject_unknown(doc, _DOCUMENT_KEYS, origin)lines 430–435manifest = DispatchManifest( [_interface_entry(e, origin) for e in _entries(doc, KEY_INTERFACES, origin)], [_table_entry(e, origin) for e in _entries(doc, KEY_TABLES, origin)], [_wrapper_entry(e, origin) for e in _entries(doc, KEY_WRAPPERS, origin)], tuple(str(p) for p in (doc.get(KEY_ALIAS_PREFIXES) or []) if str(p)), )lines 436–436◇ if not manifest.is_empty()lines 437–444logger.info( "dispatch: %s declares %d interface binding(s), %d dispatch table(s), " "%d key wrapper(s)", origin, len(manifest.interfaces), len(manifest.tables), len(manifest.wrappers), )lines 445–445return manifest

Requirements

REQ-DDB-SCHEMA-004

shared_key_document

dict | None clew.dispatch.shared_key_document(DispatchManifest manifest)

Render the declared key wrappers as a shared-key patterns document.

clew/dispatch.py:453

Source · 20 lines
def shared_key_document(manifest: DispatchManifest) -> dict | None:
    """`shared_key_wrappers` IS the argument-keyed half of the shared-key
    manifest, so it is translated into that exact entry shape rather than given a
    second parser. One format, one matcher, two places an author may write it.

    Returns None when there is nothing to contribute, so the caller can leave the
    shared-key stage's inputs — and therefore its cache key — untouched.

    @brief Translate declared wrappers into shared-key writer/reader entries.
    @version 1
    """
    if not manifest.wrappers and not manifest.key_alias_prefixes:
        return None
    doc: dict[str, Any] = {"writers": [], "readers": []}
    for wrapper in manifest.wrappers:
        bucket = "writers" if wrapper.direction == "write" else "readers"
        doc[bucket].append({"pattern": wrapper.pattern, "key_arg_index": wrapper.key_arg_index})
    if manifest.key_alias_prefixes:
        doc[KEY_ALIAS_PREFIXES] = list(manifest.key_alias_prefixes)
    return doc

1 more lines, not shown

Activity · 9 steps
lines 464–464◇ if not manifest.wrappers and not manifest.key_alias_prefixeslines 465–465return Nonelines 466–466doc: dict[str, Any] = {"writers": [], "readers": []}lines 467–467◇ for wrapper in manifest.wrapperslines 468–468bucket = "writers" if wrapper.direction == "write" else "readers"lines 469–469doc[bucket].append({"pattern": wrapper.pattern, "key_arg_index": wrapper.key_arg_index})lines 470–470◇ if manifest.key_alias_prefixeslines 471–471doc[KEY_ALIAS_PREFIXES] = list(manifest.key_alias_prefixes)lines 472–472return doc

Requirements

REQ-DDB-SCHEMA-004