Symbol links

Resolve and linkify backticked Python symbols across fastship and legacy nbdev entry points and live package indexes.

DocLookup maps qualified or stripped symbol names to documentation URLs and GitHub source URLs, and linkifies backticked symbols in Markdown text while skipping code blocks and existing links. Use mod_syms, write_modidx, and write_pkg_docs to generate and persist pkg/_modidx.py.

lk = DocLookup(extra={'demo.core': {'demo.core.foo': ('api/core.html#foo', 'demo/core.py')}})
assert lk.doc('foo') == '/api/core.html#foo'
assert lk.linkify('Call `foo` now.') == 'Call [`foo`](/api/core.html#foo) now.'

DocLookup

def DocLookup(
    strip_libs=None, # Package names whose module prefixes are stripped for bare symbol lookups (defaults to current project)
    incl_libs=None, # Optional package names to restrict entry-point loading to
    skip_mods=None, # Module names excluded when stripping prefixes
    ns:dict=None, # Optional namespace dict to resolve module import aliases (e.g. `np.array`)
    extra:dict=None, # Optional live `{mod: {sym: (doc_path, py_path)}}` mapping overriding installed entry points
    local_libs:tuple=(), # Package names whose doc links emit root-relative `/...` URLs for the site `Linker`
):

Mapping from symbol names to documentation and source URLs.

DocLookup.doc

def doc(
    sym:str
)->str | None:

Documentation URL for sym, or None if unknown.

DocLookup.code

def code(
    sym:str
)->str | None:

GitHub source URL for sym (appending #L<line> when source line is found).

def link_line(
    l:str
)->str:

Linkify backticked symbols in a single line of Markdown.

DocLookup.linkify

def linkify(
    md:str
)->str:

Linkify backticked symbols in Markdown md, skipping fenced/indented code blocks and existing links.

Building and writing _modidx.py

Index public module symbols into d = {'settings': ..., 'syms': ...} and persist resolved docs = ... metadata in pkg/_modidx.py.

mod_syms

def mod_syms(
    mod_pages:dict, # `{mod_nm: (page_html_rel, py_rel)}` mapping modules to their HTML page and source path
)->dict: # `{mod_nm: {qual_sym: (page_html#anchor, py_rel)}}`

Build the syms index mapping public modules, classes, callables, and class members to doc anchors and source paths.

write_modidx

def write_modidx(
    pkg_dir:str | pathlib._local.Path, # Package directory where `_modidx.py` is written
    syms:dict, # `{mod_nm: {qual_sym: (page_html#anchor, py_rel)}}` from `mod_syms`
    settings:dict, # Dict with `doc_host`, `doc_baseurl`, `lib_path`, `git_url`, `branch`
    header:str='# Autogenerated by fastship\n', # File header when creating `_modidx.py`
)->pathlib._local.Path | None:

Write d = {'settings': ..., 'syms': ...} in pkg/_modidx.py via set_py_var.

write_pkg_docs

def write_pkg_docs(
    pkg_dir:str | pathlib._local.Path, # Package directory where `_modidx.py` is written
    docs:dict | None, # `{section: items | {desc?, items?, Sub: ...}}`
)->pathlib._local.Path | None:

Write docs = ... in pkg/_modidx.py when docs is non-empty; remove it otherwise.

nbglob

def nbglob(
    path=None, # path to start searching
    skip_folder_re:str='^[_.]', # Skip folders matching regex,
    file_glob:str='*.ipynb', # Only include files matching glob
    skip_file_re:str='^[_.]', # Skip files matching regex
    key:str='ship_path', as_path:bool=False, *, recursive:bool=True, # search subfolders
    maxdepth:int=None, # max depth to descend (1=just immediate contents; None=unlimited)
    symlinks:bool=True, # follow symlinks?
    file_re:str=None, # Only include files matching regex
    path_glob:str=None, # Only include files whose full paths match glob (wildcards match separators)
    path_re:str=None, # Only include files whose full paths match regex
    folder_re:str=None, # Only enter folders matching regex
    skip_file_glob:str=None, # Skip files matching glob
    func:<built-in function callable>=walk_join, # function to apply to each matched file
    ret_folders:bool=False, # return folders, not just files
    sort:bool=True, # sort files by name within each folder
    types:str | list=None, # list or comma-separated str of ext types from: py, js, java, c, cpp, rb, r, ex, sh, web, doc, cfg
    exts:str | list=None, # list or comma-separated str of exts to include
): # Paths to matched files

Find all files in a directory matching an extension given a config key.

nbglob_cli

def nbglob_cli(
    path:str=None, # Path to notebooks
    symlinks:bool=False, # Follow symlinks?
    file_glob:str='*.ipynb', # Only include files matching glob
    file_re:str=None, # Only include files matching regex
    path_glob:str=None, # Only include files whose full paths match glob (wildcards match separators)
    path_re:str=None, # Only include files whose full paths match regex
    folder_re:str=None, # Only enter folders matching regex
    skip_file_glob:str=None, # Skip files matching glob
    skip_file_re:str='^[_.]', # Skip files matching regex
    skip_folder_re:str='^[_.]', # Skip folders matching regex
):

Find all files in a directory matching an extension given a config key.

patch_name

def patch_name(
    o
):

If o is decorated with patch or patch_to, return its class-prefix name.

```{python}
def _test_patch(code): return patch_name(parse_py(code).defs[0])
assert _test_patch("@patch

def _f(self:_T): …") == '_T._f' assert _test_patch("@patch_to(_T) def _g(self): …") == '_T._g' assert _test_patch("@patch def _f(self:_T|_U|_V): …") == ['_T._f', '_U._f', '_V._f'] ```