From 9ec2a5efce0b8ca9e22a75bc20c5942a4b05accb Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Wed, 9 Oct 2024 17:45:04 -0400 Subject: [PATCH] Link to github for flytekit docs Signed-off-by: Thomas J. Fan --- docs/conf.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index ebd887f4a5..9a8bbcaf0d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -16,6 +16,7 @@ from pathlib import Path import logging import sys +import inspect import sphinx.application import sphinx.errors @@ -58,8 +59,8 @@ "sphinx.ext.graphviz", "sphinx.ext.todo", "sphinx.ext.coverage", + "sphinx.ext.linkcode", "sphinx.ext.ifconfig", - "sphinx.ext.viewcode", "sphinx.ext.extlinks", "sphinx-prompt", "sphinx_copybutton", @@ -725,6 +726,67 @@ def filter(self, record: logging.LogRecord) -> bool: return True +def linkcode_resolve(domain, info): + """ + Determine the URL corresponding to Python object + """ + if domain != "py": + return None + + import flytekit + + 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: + obj = getattr(obj, part) + except AttributeError: + return None + + if inspect.isfunction(obj): + obj = inspect.unwrap(obj) + try: + fn = inspect.getsourcefile(obj) + except TypeError: + fn = None + if not fn or fn.endswith("__init__.py"): + try: + fn = inspect.getsourcefile(sys.modules[obj.__module__]) + except (TypeError, AttributeError, KeyError): + fn = None + if not fn: + return None + + try: + source, lineno = inspect.getsourcelines(obj) + except (OSError, TypeError): + lineno = None + + linespec = f"#L{lineno:d}-L{lineno + len(source) - 1:d}" if lineno else "" + + startdir = Path(flytekit.__file__).parent.parent + try: + fn = os.path.relpath(fn, start=startdir).replace(os.path.sep, "/") + except ValueError: + return None + + if not fn.startswith("flytekit/"): + return None + + if flytekit.__version__ == "dev": + tag = "master" + else: + tag = f"v{flytekit.__version__}" + + return f"https://github.com/flyteorg/flytekit/blob/{tag}/{fn}{linespec}" + + def setup(app: sphinx.application.Sphinx) -> None: """Setup root logger for Sphinx""" logger = logging.getLogger("sphinx")