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

Add linkcode extension to refer to GitHub #70

Merged
merged 1 commit into from
Aug 11, 2024
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
74 changes: 74 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import inspect
import sys
import warnings
from contextlib import suppress
from pathlib import Path
from typing import Optional, List, Tuple, Any

import torch_geopooling


# Project information
project = "Torch Geopooling"
author = "Yakau Bubnou"
Expand All @@ -12,6 +22,7 @@
"sphinx.ext.doctest",
"sphinx.ext.intersphinx",
"sphinx.ext.napoleon",
"sphinx.ext.linkcode",
"nbsphinx",
]

Expand All @@ -26,3 +37,66 @@
# Options for HTML output
html_theme = "furo"
templates_path = ["_templates"]


def linkcode_sourcefile(obj: Any) -> Optional[str]:
fn: Optional[str] = None
try:
fn = inspect.getsourcefile(inspect.unwrap(obj)) # type: ignore
except TypeError:
# Consider object as a property.
with suppress(AttributeError, TypeError):
fn = inspect.getsourcefile(inspect.unwrap(obj.fget)) # type: ignore
return fn


def linkcode_sourcelines(obj: Any) -> Tuple[Optional[List[str]], Optional[int]]:
lines: Optional[List[str]] = None
lineno: Optional[int] = None
try:
lines, lineno = inspect.getsourcelines(obj) # type: ignore
except TypeError:
# Consider object as a property.
with suppress(AttributeError, TypeError):
lines, lineno = inspect.getsourcelines(obj.fget) # type: ignore
except OSError:
pass
return lines, lineno


# Based on numpy doc/source/conf.py
def linkcode_resolve(domain, info) -> Optional[str]:
"""Determine the GitHub URL corresponding to the Python object."""
if domain != "py":
return None

submodule = sys.modules.get(info["module"])
if submodule is None:
return None

obj = submodule
for part in info["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

fn = linkcode_sourcefile(obj)
if not fn:
return None

lines, lineno = linkcode_sourcelines(obj)

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

fnspec = Path(fn).relative_to(Path(torch_geopooling.__file__).parent)

return (
f"https://github.com/ybubnov/torch_geopooling/blob/"
f"v{torch_geopooling.__version__}/torch_geopooling/{fnspec}{linespec}"
)