|
6 | 6 | """
|
7 | 7 | from __future__ import annotations
|
8 | 8 |
|
| 9 | +import inspect |
| 10 | +import os |
| 11 | +import sys |
| 12 | +import warnings |
9 | 13 | from importlib.metadata import version
|
| 14 | +from pathlib import Path |
10 | 15 | from typing import TYPE_CHECKING
|
11 | 16 |
|
| 17 | +import pytask |
| 18 | + |
12 | 19 | if TYPE_CHECKING:
|
13 | 20 | import sphinx
|
14 | 21 |
|
|
36 | 43 | "sphinx.ext.autodoc",
|
37 | 44 | "sphinx.ext.extlinks",
|
38 | 45 | "sphinx.ext.intersphinx",
|
| 46 | + "sphinx.ext.linkcode", |
39 | 47 | "sphinx.ext.napoleon",
|
40 | 48 | "sphinxext.opengraph",
|
41 |
| - "sphinx.ext.viewcode", |
42 | 49 | "sphinx_copybutton",
|
43 | 50 | "sphinx_click",
|
44 | 51 | "sphinx_toolbox.more_autodoc.autoprotocol",
|
|
90 | 97 | ogp_social_cards = {"image": "_static/images/pytask_w_text.png"}
|
91 | 98 |
|
92 | 99 |
|
| 100 | +# Linkcode, based on numpy doc/source/conf.py |
| 101 | +def linkcode_resolve(domain: str, info: dict[str, str]) -> str: # noqa: C901, PLR0912 |
| 102 | + """Determine the URL corresponding to Python object.""" |
| 103 | + if domain != "py": |
| 104 | + return None |
| 105 | + |
| 106 | + modname = info["module"] |
| 107 | + fullname = info["fullname"] |
| 108 | + |
| 109 | + submod = sys.modules.get(modname) |
| 110 | + if submod is None: |
| 111 | + return None |
| 112 | + |
| 113 | + obj = submod |
| 114 | + for part in fullname.split("."): |
| 115 | + try: |
| 116 | + with warnings.catch_warnings(): |
| 117 | + # Accessing deprecated objects will generate noisy warnings |
| 118 | + warnings.simplefilter("ignore", FutureWarning) |
| 119 | + obj = getattr(obj, part) |
| 120 | + except AttributeError: # noqa: PERF203 |
| 121 | + return None |
| 122 | + |
| 123 | + try: |
| 124 | + fn = inspect.getsourcefile(inspect.unwrap(obj)) |
| 125 | + except TypeError: |
| 126 | + try: # property |
| 127 | + fn = inspect.getsourcefile(inspect.unwrap(obj.fget)) |
| 128 | + except (AttributeError, TypeError): |
| 129 | + fn = None |
| 130 | + if not fn: |
| 131 | + return None |
| 132 | + |
| 133 | + try: |
| 134 | + source, lineno = inspect.getsourcelines(obj) |
| 135 | + except TypeError: |
| 136 | + try: # property |
| 137 | + source, lineno = inspect.getsourcelines(obj.fget) |
| 138 | + except (AttributeError, TypeError): |
| 139 | + lineno = None |
| 140 | + except OSError: |
| 141 | + lineno = None |
| 142 | + |
| 143 | + linespec = f"#L{lineno}-L{lineno + len(source) - 1}" if lineno else "" |
| 144 | + |
| 145 | + fn = os.path.relpath(fn, start=Path(pytask.__file__).parent) |
| 146 | + |
| 147 | + if "+" in pytask.__version__: |
| 148 | + return ( |
| 149 | + f"https://github.com/pytask-dev/pytask/blob/main/src/pytask/{fn}{linespec}" |
| 150 | + ) |
| 151 | + return ( |
| 152 | + f"https://github.com/pytask-dev/pytask/blob/" |
| 153 | + f"v{pytask.__version__}/src/pytask/{fn}{linespec}" |
| 154 | + ) |
| 155 | + |
| 156 | + |
93 | 157 | # -- Options for HTML output -----------------------------------------------------------
|
94 | 158 |
|
95 | 159 | # The theme to use for HTML and HTML Help pages. See the documentation for a list of
|
|
0 commit comments