From 3386c4cdf031937fed42be3aa6f8cb739f4cf268 Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:50:00 -0400 Subject: [PATCH] Use pathlib for relativizing file name --- docs/conf.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 39bbe5c4ab85..fce2d805bfc8 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -21,7 +21,7 @@ import importlib import inspect import os -import re +from pathlib import Path project = "Qiskit" @@ -166,6 +166,9 @@ # Source code links # ---------------------------------------------------------------------------------- +REPO_ROOT = Path(__file__).resolve().parents[1] + + def linkcode_resolve(domain, info): if domain != "py": return None @@ -173,7 +176,7 @@ def linkcode_resolve(domain, info): module_name = info["module"] if "qiskit" not in module_name: return None - + try: module = importlib.import_module(module_name) except ModuleNotFoundError: @@ -192,7 +195,10 @@ def linkcode_resolve(domain, info): return None if full_file_name is None: return None - file_name = full_file_name.split("/qiskit/")[-1] + try: + file_name = Path(full_file_name).resolve().relative_to(REPO_ROOT / "qiskit") + except ValueError: + return None try: source, lineno = inspect.getsourcelines(obj)