From 43e98aa5bf4d9d6edeafc7185dbb40b7813c4511 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sun, 15 Oct 2023 03:02:37 -0400 Subject: [PATCH 1/5] docs: link code to GitHub Signed-off-by: Jinzhe Zeng --- docs/conf.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/docs/conf.py b/docs/conf.py index b3f478a26..616e2eb39 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -210,3 +210,70 @@ def setup(app): """ app.connect("builder-inited", run_apidoc) app.connect("builder-inited", copy_report) + +def linkcode_resolve(domain, info): + """ + Determine the URL corresponding to Python object + """ + if domain != 'py': + return None + + modname = info['module'] + fullname = info['fullname'] + + submod = sys.modules.get(modname) + if submod is None: + return None + + obj = submod + for part in fullname.split('.'): + try: + obj = getattr(obj, part) + except Exception: + return None + + # Use the original function object if it is wrapped. + while hasattr(obj, "__wrapped__"): + obj = obj.__wrapped__ + # SciPy's distributions are instances of *_gen. Point to this + # class since it contains the implementation of all the methods. + if isinstance(obj, (rv_generic, multi_rv_generic)): + obj = obj.__class__ + try: + fn = inspect.getsourcefile(obj) + except Exception: + fn = None + if not fn: + try: + fn = inspect.getsourcefile(sys.modules[obj.__module__]) + except Exception: + fn = None + if not fn: + return None + + try: + source, lineno = inspect.getsourcelines(obj) + except Exception: + lineno = None + + if lineno: + linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1) + else: + linespec = "" + + import reacnetgenerator as mypkg + + startdir = os.path.abspath(os.path.join(dirname(mypkg.__file__), '..')) + fn = relpath(fn, start=startdir).replace(os.path.sep, '/') + + if fn.startswith('reacnetgenerator/'): + m = re.match(r'^.*dev0\+([a-f0-9]+)$', mypkg.__version__) + base_url = "https://github.com/tongzhugroup/reacnetgenerator/blob" + if m: + return f"{base_url}/{m.group(1)}/{fn}{linespec}" + elif 'dev' in mypkg.__version__: + return f"{base_url}/master/{fn}{linespec}" + else: + return f"{base_url}/v{mypkg.__version__}/{fn}{linespec}" + else: + return None From dd0131fe4b62341e4d6429a23ac7d208999de731 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 07:02:55 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/conf.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 616e2eb39..768233f16 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -211,22 +211,21 @@ def setup(app): app.connect("builder-inited", run_apidoc) app.connect("builder-inited", copy_report) + def linkcode_resolve(domain, info): - """ - Determine the URL corresponding to Python object - """ - if domain != 'py': + """Determine the URL corresponding to Python object.""" + if domain != "py": return None - modname = info['module'] - fullname = info['fullname'] + modname = info["module"] + fullname = info["fullname"] submod = sys.modules.get(modname) if submod is None: return None obj = submod - for part in fullname.split('.'): + for part in fullname.split("."): try: obj = getattr(obj, part) except Exception: @@ -262,16 +261,16 @@ def linkcode_resolve(domain, info): linespec = "" import reacnetgenerator as mypkg - - startdir = os.path.abspath(os.path.join(dirname(mypkg.__file__), '..')) - fn = relpath(fn, start=startdir).replace(os.path.sep, '/') - if fn.startswith('reacnetgenerator/'): - m = re.match(r'^.*dev0\+([a-f0-9]+)$', mypkg.__version__) + startdir = os.path.abspath(os.path.join(dirname(mypkg.__file__), "..")) + fn = relpath(fn, start=startdir).replace(os.path.sep, "/") + + if fn.startswith("reacnetgenerator/"): + m = re.match(r"^.*dev0\+([a-f0-9]+)$", mypkg.__version__) base_url = "https://github.com/tongzhugroup/reacnetgenerator/blob" if m: return f"{base_url}/{m.group(1)}/{fn}{linespec}" - elif 'dev' in mypkg.__version__: + elif "dev" in mypkg.__version__: return f"{base_url}/master/{fn}{linespec}" else: return f"{base_url}/v{mypkg.__version__}/{fn}{linespec}" From 8ef2c26da559be1d2bc949c41e94c20270f4d9be Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sun, 22 Oct 2023 19:49:56 -0400 Subject: [PATCH 3/5] replace viewcode with linkcode Signed-off-by: Jinzhe Zeng --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 768233f16..455c73b40 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -34,7 +34,7 @@ extensions = [ "sphinx.ext.autosummary", "sphinx.ext.mathjax", - "sphinx.ext.viewcode", + "sphinx.ext.linkcode", "sphinx.ext.intersphinx", "sphinxarg.ext", "myst_nb", From 9a69e768ca89abb27358cfed29c44b95059a06cb Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sun, 22 Oct 2023 20:02:51 -0400 Subject: [PATCH 4/5] import modules Signed-off-by: Jinzhe Zeng --- docs/conf.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/conf.py b/docs/conf.py index 455c73b40..5a13c07d5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -214,6 +214,11 @@ def setup(app): def linkcode_resolve(domain, info): """Determine the URL corresponding to Python object.""" + import os + import sys + import re + import inspect + if domain != "py": return None From 9587ca5ee99fc97d26be2886dac42a5dfdfb77b9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:03:02 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/conf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 5a13c07d5..b0133c59b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -214,10 +214,10 @@ def setup(app): def linkcode_resolve(domain, info): """Determine the URL corresponding to Python object.""" + import inspect import os - import sys import re - import inspect + import sys if domain != "py": return None