entrypoints.py

clew/wiki/entrypoints.py

The repository's entry points, as flows the site derives without being told.

_calls_something

bool clew.wiki.entrypoints._calls_something(object conn, str name, str rel)

Whether an entry calls anything the index resolved.

clew/wiki/entrypoints.py:197

Source · 13 lines
def _calls_something(conn: object, name: str, rel: str) -> bool:
    """@brief Has callees? @return Bool."""
    if not table_exists(conn, "call_edges"):
        return False
    row = conn.execute(
        "SELECT 1 FROM call_edges c JOIN memberdef m ON m.rowid = c.caller_rowid "
        "JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) WHERE m.name = ? AND p.name = ? "
        "AND c.source != 'binding' LIMIT 1",
        (name, rel),
    ).fetchone()
    return row is not None

Activity · 4 steps
lines 199–199◇ if not table_exists(conn, "call_edges")lines 200–200return Falselines 201–206row = conn.execute( "SELECT 1 FROM call_edges c JOIN memberdef m ON m.rowid = c.caller_rowid " "JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) WHERE m.name = ? AND p.name = ? " "AND c.source != 'binding' LIMIT 1", (name, rel), ).fetchone()lines 207–207return row is not None

Flows

_declared_entry_patterns

list[str] clew.wiki.entrypoints._declared_entry_patterns(object conn, Path repo_root)

The entry patterns the repository or its operator stated.

clew/wiki/entrypoints.py:127

Source · 25 lines
def _declared_entry_patterns(conn: object, repo_root: Path) -> list[str]:
    """THE BUILD'S OWN TIERS: tier 1 (`--entry-patterns`) is recorded in `build_meta`, tier 2 is
    the `.clew.yaml`; the built-in heuristics are neither.

    @brief Read stated entry patterns.
    @return Patterns.
    """
    from ..declaration import SECTION_ENTRY_PATTERNS, load_declaration
    from ..tiers import EXPLICIT_KEY, OPTIONS_META_PREFIX, VALUE_SEPARATOR

    row = None
    if table_exists(conn, "build_meta"):
        row = conn.execute(
            "SELECT value FROM build_meta WHERE key = ?",
            (f"{OPTIONS_META_PREFIX}.{SECTION_ENTRY_PATTERNS}.{EXPLICIT_KEY}",),
        ).fetchone()
    if row and row[0]:
        return [p for p in str(row[0]).split(VALUE_SEPARATOR) if p]
    try:
        declared = load_declaration(repo_root).get(SECTION_ENTRY_PATTERNS) or []
    except Exception:  # a malformed declaration is the build's to refuse, not the site's
        declared = []
    return [str(p) for p in declared]

Activity · 12 steps
lines 134–134from ..declaration import SECTION_ENTRY_PATTERNS, load_declarationlines 135–135from ..tiers import EXPLICIT_KEY, OPTIONS_META_PREFIX, VALUE_SEPARATORlines 137–137row = Nonelines 138–138◇ if table_exists(conn, "build_meta")lines 139–142row = conn.execute( "SELECT value FROM build_meta WHERE key = ?", (f"{OPTIONS_META_PREFIX}.{SECTION_ENTRY_PATTERNS}.{EXPLICIT_KEY}",), ).fetchone()lines 143–143◇ if row and row[0]lines 144–144return [p for p in str(row[0]).split(VALUE_SEPARATOR) if p]lines 145–145trylines 146–146declared = load_declaration(repo_root).get(SECTION_ENTRY_PATTERNS) or []lines 147–147except Exception: # a malformed declaration is the build's to refuse, not the site'slines 148–148declared = []lines 149–149return [str(p) for p in declared]

Flows

_defined

bool clew.wiki.entrypoints._defined(object conn, str name, str rel)

Whether the index holds a function of this name in this file.

clew/wiki/entrypoints.py:179

Show on the map →

Called by _scripts

Calls none

Source · 10 lines
def _defined(conn: object, name: str, rel: str) -> bool:
    """@brief Look up. @return Defined?"""
    row = conn.execute(
        "SELECT 1 FROM memberdef m JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) "
        "WHERE m.kind = 'function' AND m.name = ? AND p.name = ? LIMIT 1",
        (name, rel),
    ).fetchone()
    return row is not None

Activity · 2 steps
lines 181–185row = conn.execute( "SELECT 1 FROM memberdef m JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) " "WHERE m.kind = 'function' AND m.name = ? AND p.name = ? LIMIT 1", (name, rel), ).fetchone()lines 186–186return row is not None

Flows

_mains

list[tuple[str, str, str]] clew.wiki.entrypoints._mains(object conn, Path repo_root)

Entry functions outside test code: the platform facts and the declared patterns.

clew/wiki/entrypoints.py:99

Source · 22 lines
def _mains(conn: object, repo_root: Path) -> list[tuple[str, str, str]]:
    """THE FACTS AND THE DECLARATION, NOT THE GUESSES. Liveness seeds on the name heuristics too
    (`%_cb`, `%callback%`), where a false positive only keeps a function alive; as flows they
    would draw every callback as an entry point.

    @brief Find entry functions.
    @return Rows.
    """
    from ..reachability import ENTRY_PATTERN_FACTS

    patterns = list(
        dict.fromkeys([*ENTRY_PATTERN_FACTS, *_declared_entry_patterns(conn, repo_root)])
    )
    clause = " OR ".join(["m.name LIKE ?"] * len(patterns))
    rows = conn.execute(
        "SELECT DISTINCT m.name, p.name FROM memberdef m JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) "  # noqa: S608
        f"WHERE m.kind = 'function' AND ({clause}) {_not_test(conn)} ORDER BY p.name, m.name",
        patterns,
    )
    return [(str(n), str(rel), f"`{n}` ({rel})") for n, rel in rows]

Activity · 5 steps
lines 107–107from ..reachability import ENTRY_PATTERN_FACTSlines 109–111patterns = list( dict.fromkeys([*ENTRY_PATTERN_FACTS, *_declared_entry_patterns(conn, repo_root)]) )lines 112–112clause = " OR ".join(["m.name LIKE ?"] * len(patterns))lines 113–117rows = conn.execute( "SELECT DISTINCT m.name, p.name FROM memberdef m JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) " # noqa: S608 f"WHERE m.kind = 'function' AND ({clause}) {_not_test(conn)} ORDER BY p.name, m.name", patterns, )lines 118–118return [(str(n), str(rel), f"`{n}` ({rel})") for n, rel in rows]

Flows

_not_test

str clew.wiki.entrypoints._not_test(object conn)

A clause excluding test code, when the index has a test-scope layer.

clew/wiki/entrypoints.py:215

Source · 5 lines
def _not_test(conn: object) -> str:
    """@brief Clause. @return SQL."""
    if not table_exists(conn, "test_scope"):
        return ""
    return "AND p.rowid NOT IN (SELECT path_rowid FROM test_scope)"

1 more lines, not shown

Activity · 3 steps
lines 217–217◇ if not table_exists(conn, "test_scope")lines 218–218return ""lines 219–219return "AND p.rowid NOT IN (SELECT path_rowid FROM test_scope)"

Flows

_scripts

list[tuple[str, str, str]] clew.wiki.entrypoints._scripts(object conn, Path repo_root)

Console scripts declared in pyproject.toml, resolved to indexed functions.

clew/wiki/entrypoints.py:63

Source · 27 lines
def _scripts(conn: object, repo_root: Path) -> list[tuple[str, str, str]]:
    """`pkg.mod:func` IS LOOKED FOR at `pkg/mod.py`, `pkg/mod/__init__.py` and their `src/` forms;
    a script whose function the index does not hold is skipped.

    @brief Read scripts.
    @return Rows.
    """
    from ..py_entrypoints import console_scripts

    scripts = console_scripts(repo_root)
    out = []
    for script, target in scripts.items():
        module, _, func = str(target).partition(":")
        func = func.split(".")[-1]
        base = module.replace(".", "/")
        for rel in (
            f"{base}.py",
            f"{base}/__init__.py",
            f"src/{base}.py",
            f"src/{base}/__init__.py",
        ):
            if _defined(conn, func, rel):
                out.append((func, rel, f"{script} — `{func}` ({rel})"))
                break
    return out

Activity · 12 steps
lines 70–70from ..py_entrypoints import console_scriptslines 72–72scripts = console_scripts(repo_root)lines 73–73out = []lines 74–74◇ for script, target in scripts.items()lines 75–75module, _, func = str(target).partition(":")lines 76–76func = func.split(".")[-1]lines 77–77base = module.replace(".", "/")lines 78–78◇ for rel in (lines 84–84◇ if _defined(conn, func, rel)lines 85–85out.append((func, rel, f"{script} — `{func}` ({rel})"))lines 86–86breaklines 87–87return out

Flows

_thread_entries

list[tuple[str, str, str]] clew.wiki.entrypoints._thread_entries(object conn)

Thread entry functions outside test code.

clew/wiki/entrypoints.py:158

Source · 13 lines
def _thread_entries(conn: object) -> list[tuple[str, str, str]]:
    """@brief Find thread entries. @return Rows."""
    if not table_exists(conn, "threads"):
        return []
    rows = conn.execute(
        "SELECT DISTINCT m.name, p.name, t.name FROM threads t "
        "JOIN memberdef m ON m.rowid = t.entry_memberdef_rowid "
        f"JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) "
        f"WHERE 1 = 1 {_not_test(conn)} ORDER BY p.name, m.name"
    )
    return [(str(n), str(rel), f"thread `{t}` — `{n}` ({rel})") for n, rel, t in rows]

Activity · 4 steps
lines 160–160◇ if not table_exists(conn, "threads")lines 161–161return []lines 162–167rows = conn.execute( "SELECT DISTINCT m.name, p.name, t.name FROM threads t " "JOIN memberdef m ON m.rowid = t.entry_memberdef_rowid " f"JOIN path p ON p.rowid = COALESCE(NULLIF(m.bodyfile_id, 0), m.file_id) " f"WHERE 1 = 1 {_not_test(conn)} ORDER BY p.name, m.name" )lines 168–168return [(str(n), str(rel), f"thread `{t}` — `{n}` ({rel})") for n, rel, t in rows]

Flows

entry_point_flows

list[FlowSpec] clew.wiki.entrypoints.entry_point_flows(Path db, Path repo_root, int depth)

The flows of a repository's entry points.

clew/wiki/entrypoints.py:39

Source · 17 lines
def entry_point_flows(db: Path, repo_root: Path, depth: int) -> list[FlowSpec]:
    """@brief Discover entry points. @return Specs."""
    found: dict[tuple[str, str], tuple[str, str]] = {}
    with connect(db) as conn:
        for (name, rel, title), role in (
            *((s, "") for s in _scripts(conn, Path(repo_root))),
            *((m, "") for m in _mains(conn, Path(repo_root))),
            *((e, "threads") for e in _thread_entries(conn)),
        ):
            if (name, rel) not in found and _calls_something(conn, name, rel):
                found[(name, rel)] = (title, role)
    return [
        FlowSpec(entry=name, file=rel, depth=depth, title=title, role=role)
        for (name, rel), (title, role) in found.items()
    ]

Activity · 6 steps
lines 41–41found: dict[tuple[str, str], tuple[str, str]] = {}lines 42–42with connect(db) as connlines 43–43◇ for (name, rel, title), role in (lines 48–48◇ if (name, rel) not in found and _calls_something(conn, name, rel)lines 49–49found[(name, rel)] = (title, role)lines 50–53return [ FlowSpec(entry=name, file=rel, depth=depth, title=title, role=role) for (name, rel), (title, role) in found.items() ]

Requirements

REQ-DDB-WIKI-018

Flows