-
Notifications
You must be signed in to change notification settings - Fork 55
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
Changes from 5 commits
2cd3a1c
b3a1f4c
b59d98c
fce3a1b
5312b14
ff41416
7d643a2
e9dca16
e0e254b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,10 @@ | |||||
|
||||||
# -- Path setup -------------------------------------------------------------- | ||||||
|
||||||
import inspect | ||||||
import os | ||||||
from operator import attrgetter | ||||||
|
||||||
from packaging.version import parse | ||||||
|
||||||
# If extensions (or modules to document with autodoc) are in another directory, | ||||||
|
@@ -36,6 +40,7 @@ | |||||
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom | ||||||
# ones. | ||||||
extensions = [ | ||||||
"sphinx.ext.linkcode", | ||||||
"sphinx.ext.autodoc", | ||||||
"numpydoc", | ||||||
"sphinx_gallery.gen_gallery", | ||||||
|
@@ -65,6 +70,46 @@ | |||||
# (note that `group` is the GitHub user or organization) | ||||||
issues_github_path = "skops-dev/skops" | ||||||
|
||||||
|
||||||
def linkcode_resolve(domain, info): | ||||||
if domain != "py": | ||||||
return None | ||||||
if not info["module"]: | ||||||
return None | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is checked bellow again ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now it works, I updated the PR. Could you please take a look at it? Thank you so much for everything! |
||||||
filename = info["module"].replace(".", "/") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Done |
||||||
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 | ||||||
|
||||||
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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Done |
||||||
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 = "" | ||||||
return ( | ||||||
"https://github.com/skops-dev/skops/blob/main/skops/" + fn + "#L" + str(lineno) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added these lines from scikit-learn:
However, the source code links became something like this (https://github.com/skops-dev/skops/blob/%7Brevision%7D/skops/hub_utils/_hf_hub.py#L328) and of course, they don't work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you try this patch: diff --git a/docs/conf.py b/docs/conf.py
index 3ed773f..4ed5d3d 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -8,6 +8,7 @@
import inspect
import os
+import subprocess
from operator import attrgetter
from packaging.version import parse
@@ -71,6 +72,19 @@ autosummary_generate = True
issues_github_path = "skops-dev/skops"
+# -- linkcode configuration --------------------------------------------------
+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 != "py":
return None
@@ -93,7 +107,7 @@ def linkcode_resolve(domain, info):
try:
fn = inspect.getsourcefile(inspect.unwrap(obj))
except TypeError:
- try:
+ try:
fn = inspect.getsourcefile(inspect.unwrap(obj.fget))
except (AttributeError, TypeError):
fn = None
@@ -105,9 +119,16 @@ def linkcode_resolve(domain, info):
lineno = inspect.getsourcelines(obj)[1]
except Exception:
lineno = ""
- return (
- "https://github.com/skops-dev/skops/blob/main/skops/" + fn + "#L" + str(lineno)
+ url_fmt = (
+ "https://github.com/scikit-learn/"
+ "scikit-learn/blob/{revision}/"
+ "{package}/{path}#L{lineno}"
)
+ revision = _get_git_revision()
+ return url_fmt.format(revision=revision, package=package, path=fn, lineno=lineno)
+
+
+# -- linkcode configuration --------------------------------------------------
# -- Options for HTML output ------------------------------------------------- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns something like this: https://github.com/scikit-learn/scikit-learn/blob/a6db88f/skops/hub_utils/_hf_hub.py#L594 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two problems why this doesn't work. First of all it links to sklearn but we want to link to skops (I guess Adrin forgot to adjust the copied code): url_fmt = (
"https://github.com/skops-dev/"
"skops/blob/{revision}/"
"{package}/{path}#L{lineno}"
) But even after that, the link wouldn't lead anywhere. However, this is probably because the git revision's hash does not lead anywhere (yet). If we replace it with the hash of an actual commit (current HEAD on main branch), it will work, e.g.: https://github.com/skops-dev/skops/blob/69621cd/skops/hub_utils/_hf_hub.py#L594 where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahahahahhahahhahah I feel like an idiot. Thank you so much! |
||||||
) | ||||||
|
||||||
|
||||||
# -- Options for HTML output ------------------------------------------------- | ||||||
|
||||||
# The theme to use for HTML and HTML Help pages. See the documentation for | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adrin already mentioned this, but this line and the one below
if domain not in ("py", "pyx"):
do redundant work. Only the latter is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Done