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

resolve branch #38

Merged
merged 1 commit into from
Nov 25, 2023
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
37 changes: 20 additions & 17 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sphinx configuration."""

import inspect
import os
import sys


Expand Down Expand Up @@ -46,37 +47,39 @@


def linkcode_resolve(domain, info):
"""Parse links to source."""
"""Resolve and link to source."""
if domain != "py":
return None
if not info["module"]:
return None

# Determine the branch based on RTD version
rtd_version = os.getenv("READTHEDOCS_VERSION", "latest")
github_branch = "develop" if rtd_version == "latest" else "main"

github_repo = "https://github.com/piskunow/kpm-tools"

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

try:
filename = inspect.getsourcefile(module)
if filename is None:
return None
filename = inspect.getsourcefile(module)
if filename is None:
return None

# Assuming your package is installed in 'site-packages'
# and the source is in 'src/kpm_tools' in the repo
src_path = filename.split("site-packages")[1]
rel_fn = "src" + src_path
# Adjust the file path for the repository structure
package_dir = "src/kpm_tools"
rel_fn = os.path.relpath(filename, start=os.path.dirname(package_dir))

obj = module
for part in info["fullname"].split("."):
obj = getattr(obj, part, None)
obj = module
for part in info["fullname"].split("."):
obj = getattr(obj, part, None)

if obj is None:
return None
if obj is None:
return None

try:
line = inspect.getsourcelines(obj)[1]
return f"{github_repo}/blob/develop/{rel_fn}#L{line}"
except Exception as e:
print(f"Error generating linkcode URL for {info['module']}: {e}")
return f"{github_repo}/blob/{github_branch}/{rel_fn}#L{line}"
except Exception:
return None
Loading