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

Added source code link #172

Merged
merged 9 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ v0.3
entries contained line breaks. :pr:`156` by `Benjamin Bossan`_.
- Use ``huggingface_hub`` v0.10.1 for model cards, drop ``modelcards``
dependency. :pr:`162` by `Benjamin Bossan`_.
- Add source links to API documentation. :pr:`172` by `Ayyuce Demirbas`_.


v0.2
----
Expand Down Expand Up @@ -57,4 +59,4 @@ Contributors
~~~~~~~~~~~~

:user:`Adrin Jalali <adrinjalali>`, :user:`Merve Noyan <merveenoyan>`,
:user:`Benjamin Bossan <BenjaminBossan>`
:user:`Benjamin Bossan <BenjaminBossan>`, :user:`Ayyuce Demirbas <ayyucedemirbas>`
58 changes: 58 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

# -- Path setup --------------------------------------------------------------

import inspect
import os
import subprocess
from operator import attrgetter

from packaging.version import parse

# If extensions (or modules to document with autodoc) are in another directory,
Expand Down Expand Up @@ -36,6 +41,7 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
"sphinx.ext.linkcode",
"sphinx.ext.autodoc",
"numpydoc",
"sphinx_gallery.gen_gallery",
Expand Down Expand Up @@ -65,6 +71,58 @@
# (note that `group` is the GitHub user or organization)
issues_github_path = "skops-dev/skops"

REVISION_CMD = "git rev-parse --short HEAD"


def _get_git_revision():
try:
revision = subprocess.check_output(REVISION_CMD.split()).strip()
except (subprocess.CalledProcessError, OSError):
print("Failed to execute git to get revision")
return None
return revision.decode("utf-8")


def linkcode_resolve(domain, info):
if domain not in ("py", "pyx"):
return
ayyucedemirbas marked this conversation as resolved.
Show resolved Hide resolved
if not info.get("module") or not info.get("fullname"):
return
revision = _get_git_revision()

if revision is None:
return

class_name = info["fullname"].split(".")[0]
module = __import__(info["module"], fromlist=[class_name])
obj = attrgetter(info["fullname"])(module)

# Unwrap the object to get the correct source
# file in case that is wrapped by a decorator
obj = inspect.unwrap(obj)

try:
fn = inspect.getsourcefile(inspect.unwrap(obj))
except TypeError:
try:
fn = inspect.getsourcefile(inspect.unwrap(obj.fget))
except (AttributeError, TypeError):
fn = None
if not fn:
return None
package = "skops"
fn = os.path.relpath(fn, start=os.path.dirname(__import__(package).__file__))
try:
lineno = inspect.getsourcelines(obj)[1]
except Exception:
lineno = ""
url_fmt = (
"https://github.com/skops-dev/skops/blob/{revision}/{package}/{path}#L{lineno}"
)
revision = _get_git_revision()
return url_fmt.format(revision=revision, package=package, path=fn, lineno=lineno)


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
Expand Down