Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: add linkcode to docs #15860

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions docs/cudf/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import datetime
import filecmp
import glob
import inspect
import os
import re
import sys
import tempfile
import warnings
import xml.etree.ElementTree as ET

from docutils.nodes import Text
Expand Down Expand Up @@ -69,6 +71,7 @@ class PseudoLexer(RegexLexer):
"sphinx.ext.autosummary",
"sphinx_copybutton",
"sphinx_remove_toctrees",
"sphinx.ext.linkcode",
"numpydoc",
"IPython.sphinxext.ipython_console_highlighting",
"IPython.sphinxext.ipython_directive",
Expand Down Expand Up @@ -557,6 +560,64 @@ def on_missing_reference(app, env, node, contnode):
]


# Needed for the [source] button on the API docs to link to the github code
# based on pandas doc/source/conf.py
def linkcode_resolve(domain, info) -> str | None:
"""
Determine the URL corresponding to Python object
"""
if domain != "py":
return None

modname = info["module"]
fullname = info["fullname"]

submod = sys.modules.get(modname)
if submod is None:
return None

obj = submod
for part in fullname.split("."):
try:
with warnings.catch_warnings():
# Accessing deprecated objects will generate noisy warnings
warnings.simplefilter("ignore", FutureWarning)
obj = getattr(obj, part)
except AttributeError:
return None

try:
fn = inspect.getsourcefile(inspect.unwrap(obj))
except TypeError:
try: # property
fn = inspect.getsourcefile(inspect.unwrap(obj.fget))
except (AttributeError, TypeError):
fn = None
if not fn:
return None

try:
source, lineno = inspect.getsourcelines(obj)
except TypeError:
try: # property
source, lineno = inspect.getsourcelines(obj.fget)
except (AttributeError, TypeError):
lineno = None
except OSError:
lineno = None

if lineno:
linespec = f"#L{lineno}-L{lineno + len(source) - 1}"
else:
linespec = ""

fn = os.path.relpath(fn, start=os.path.dirname(cudf.__file__))
return (
f"https://github.com/rapidsai/cudf/blob/"
f"branch-{version}/python/cudf/cudf/{fn}{linespec}"
)


def setup(app):
app.add_css_file("https://docs.rapids.ai/assets/css/custom.css")
app.add_js_file(
Expand Down
Loading