Skip to content

Commit

Permalink
resolve branch (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
piskunow authored Nov 25, 2023
1 parent 15f4dd5 commit cf71952
Showing 1 changed file with 20 additions and 17 deletions.
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

0 comments on commit cf71952

Please sign in to comment.