oc_chat
Loading...
Searching...
No Matches
oc_chat.scripts.py_link_check Namespace Reference

Classes

Functions

 _iter_markdown_files (Path root)
str|None _clean_target (str raw)
list[BrokenLink] find_broken_links (Path root)
argparse.ArgumentParser _build_parser ()
int main (list[str]|None argv=None)

Variables

 _LINK_RE = re.compile(r"\]\‍(([^)]+)\‍)")
tuple _SKIP_PREFIXES = ("http://", "https://", "mailto:", "ftp://", "tel:", "#", "//")
 _LINE_LOCATOR_RE = re.compile(r"(?::\d+)+(?:-\d+(?::\d+)?)?$")

Detailed Description

Relative-link checker for NON-``.md`` link targets (the class ``tools/link_check.py`` is blind to).

Purpose
-------
``tools/link_check.py`` only validates markdown links whose target ends in ``.md``
(``_is_md_ref``), so it is BLIND to dead relative links pointing at ``.py`` (or any
other non-``.md``) files — exactly the class a code move breaks (e.g. the many
``.py``-target links in ``CONTRACTS.md`` / ``TOOL_CONTRACT.md``). This companion
sweep resolves every relative, non-URL, non-anchor markdown link whose target is a
file with a non-``.md`` extension and reports the ones that do not exist on disk.
Usage: ``python oc_chat/scripts/py_link_check.py [--root DIR] [--json]``. ``--root``
defaults to the repository root inferred from this file's location, so the script
scans the tree it travels with (post-move it is invoked from the monorepo root as
``python oc_chat/oc_chat/scripts/py_link_check.py``). Exit ``0`` when no broken
non-``.md`` relative file links are found; ``1`` otherwise. The §2.2 gate is
baseline-relative: fail only on NEW entries vs the recorded pre-move baseline.

Responsibilities
----------------
- Enumerate ``*.md`` files under a scan root (skipping VCS internals).
- Extract inline-link targets and normalise them (strip titles, fragments,
  queries, ``:NN`` line locators, percent-encoding).
- Resolve each non-``.md`` relative file target and report the unresolved ones.

Diagnostics
-----------
Domain: OC_CHAT.MAIN
Levels:
    L2 - CLI lifecycle (scan start / result)
    L3 - per-root resolution details
    L4 - per-file / per-link tracing

Contracts
---------
- Imports ONLY ``oc_diagnostics`` (structured logging) — never ``oc_chat`` — so it
  is immune to the ``sys.path[0] = scripts/`` ``import oc_chat`` editable-install
  shadowing trap (plan 151 §2.3); the scan logic is driven entirely by the ``--root``
  filesystem path, not by imports, so it runs identically pre- and post-move.
- Read-only: never writes to the scanned tree.
- Deterministic: results are sorted; identical tree -> identical output.

Dependencies
------------
- oc_diagnostics (structured debug logging via _dbg)

Function Documentation

◆ _build_parser()

argparse.ArgumentParser oc_chat.scripts.py_link_check._build_parser ( )
protected
Construct the CLI argument parser.

Definition at line 155 of file py_link_check.py.

Here is the caller graph for this function:

◆ _clean_target()

str | None oc_chat.scripts.py_link_check._clean_target ( str raw)
protected
Normalise a link body to a bare relative file path, or ``None`` to skip.

Strips ``<...>`` wrappers, an optional ``"title"`` suffix, ``#anchor`` /
``?query`` fragments, a trailing ``:NN`` source-line locator, and percent
encoding. Returns ``None`` for URLs, pure anchors, absolute paths, extension-less
targets (directory/section references), and ``.md`` targets (owned by
``tools/link_check.py``).

Definition at line 94 of file py_link_check.py.

Here is the caller graph for this function:

◆ _iter_markdown_files()

oc_chat.scripts.py_link_check._iter_markdown_files ( Path root)
protected
Yield every ``*.md`` file under *root*, skipping VCS internals.

Definition at line 85 of file py_link_check.py.

Here is the caller graph for this function:

◆ find_broken_links()

list[BrokenLink] oc_chat.scripts.py_link_check.find_broken_links ( Path root)
Return every broken non-``.md`` relative file link under *root*, sorted.

Definition at line 127 of file py_link_check.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int oc_chat.scripts.py_link_check.main ( list[str] | None argv = None)
CLI entry point: scan ``--root`` and report broken non-``.md`` file links.

Definition at line 172 of file py_link_check.py.

Here is the call graph for this function:

Variable Documentation

◆ _LINE_LOCATOR_RE

oc_chat.scripts.py_link_check._LINE_LOCATOR_RE = re.compile(r"(?::\d+)+(?:-\d+(?::\d+)?)?$")
protected

Definition at line 73 of file py_link_check.py.

◆ _LINK_RE

oc_chat.scripts.py_link_check._LINK_RE = re.compile(r"\]\‍(([^)]+)\‍)")
protected

Definition at line 68 of file py_link_check.py.

◆ _SKIP_PREFIXES

tuple oc_chat.scripts.py_link_check._SKIP_PREFIXES = ("http://", "https://", "mailto:", "ftp://", "tel:", "#", "//")
protected

Definition at line 70 of file py_link_check.py.