From 6f0ffa9e7f7ca95058aca9993a7c16aa2ebbbad5 Mon Sep 17 00:00:00 2001 From: Ankita Katiyar <110245118+ankatiyar@users.noreply.github.com> Date: Mon, 13 Jan 2025 15:15:32 +0000 Subject: [PATCH] docs(datasets): Move to linkcode extension (#985) Move to linkcode extension Signed-off-by: Ankita Katiyar --- kedro-datasets/docs/source/conf.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/kedro-datasets/docs/source/conf.py b/kedro-datasets/docs/source/conf.py index f62e80104..039658936 100644 --- a/kedro-datasets/docs/source/conf.py +++ b/kedro-datasets/docs/source/conf.py @@ -14,6 +14,8 @@ from __future__ import annotations import importlib +import inspect +import os import re import sys from inspect import getmembers, isclass, isfunction @@ -22,6 +24,8 @@ from click import secho, style from kedro import __version__ as release +import kedro_datasets + # -- Project information ----------------------------------------------------- project = "kedro-datasets" @@ -47,7 +51,7 @@ "sphinx_autodoc_typehints", "sphinx.ext.doctest", "sphinx.ext.ifconfig", - "sphinx.ext.viewcode", + "sphinx.ext.linkcode", "sphinxcontrib.jquery", "sphinx_copybutton", "myst_parser", @@ -452,3 +456,25 @@ def setup(app): user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/99.0" myst_heading_anchors = 5 + +def linkcode_resolve(domain, info): + """Resolve a GitHub URL corresponding to a Python object.""" + if domain != 'py': + return None + + try: + mod = sys.modules[info['module']] + obj = mod + for attr in info['fullname'].split('.'): + obj = getattr(obj, attr) + obj = inspect.unwrap(obj) + + filename = inspect.getsourcefile(obj) + source, lineno = inspect.getsourcelines(obj) + relpath = os.path.relpath(filename, start=os.path.dirname( + kedro_datasets.__file__)) + + return f'https://github.com/kedro-org/kedro-plugins/blob/main/kedro-datasets/kedro_datasets/{relpath}#L{lineno}#L{lineno + len(source) - 1}' + + except (KeyError, ImportError, AttributeError, TypeError, OSError, ValueError): + return None