diff --git a/docs/conf.py b/docs/conf.py index 1eb8008..4a9e236 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -9,6 +9,15 @@ from datetime import datetime from importlib.metadata import metadata from pathlib import Path +import importlib.util +import inspect +import os +import re +import subprocess +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Any HERE = Path(__file__).parent sys.path.insert(0, str(HERE / "extensions")) @@ -130,3 +139,79 @@ # you can add an exception to this list. # ("py:class", "igraph.Graph"), ] + + + +# -- Config for linkcode ------------------------------------------- + + +def git(*args): + """Run git command and return output as string.""" + return subprocess.check_output(["git", *args]).strip().decode() + + +# https://github.com/DisnakeDev/disnake/blob/7853da70b13fcd2978c39c0b7efa59b34d298186/docs/conf.py#L192 +# Current git reference. Uses branch/tag name if found, otherwise uses commit hash +git_ref = None +try: + git_ref = git("name-rev", "--name-only", "--no-undefined", "HEAD") + git_ref = re.sub(r"^(remotes/[^/]+|tags)/", "", git_ref) +except Exception: + pass + +# (if no name found or relative ref, use commit hash instead) +if not git_ref or re.search(r"[\^~]", git_ref): + try: + git_ref = git("rev-parse", "HEAD") + except Exception: + git_ref = "main" + +# https://github.com/DisnakeDev/disnake/blob/7853da70b13fcd2978c39c0b7efa59b34d298186/docs/conf.py#L192 +_scvi_tools_module_path = os.path.dirname(importlib.util.find_spec("scvi").origin) # type: ignore + + +def linkcode_resolve(domain, info): + """Determine the URL corresponding to Python object.""" + if domain != "py": + return None + + try: + obj: Any = sys.modules[info["module"]] + for part in info["fullname"].split("."): + obj = getattr(obj, part) + obj = inspect.unwrap(obj) + + if isinstance(obj, property): + obj = inspect.unwrap(obj.fget) # type: ignore + + path = os.path.relpath(inspect.getsourcefile(obj), start=_scvi_tools_module_path) # type: ignore + src, lineno = inspect.getsourcelines(obj) + except Exception: + return None + + path = f"{path}#L{lineno}-L{lineno + len(src) - 1}" + return f"{repository_url}/blob/{git_ref}/src/scvi/{path}" + + +# -- Config for hoverxref ------------------------------------------- + +hoverx_default_type = "tooltip" +hoverxref_domains = ["py"] +hoverxref_role_types = dict.fromkeys( + ["ref", "class", "func", "meth", "attr", "exc", "data", "mod"], + "tooltip", +) +hoverxref_intersphinx = [ + "python", + "numpy", + "scanpy", + "anndata", + "pytorch_lightning", + "scipy", + "pandas", + "ml_collections", + "ray", +] +# use proxied API endpoint on rtd to avoid CORS issues +if os.environ.get("READTHEDOCS"): + hoverxref_api_host = "/_"