Skip to content

Commit 5ae15e6

Browse files
committed
docs: Use linkcode to point to source code from docs
1 parent 3ba722f commit 5ae15e6

4 files changed

Lines changed: 142 additions & 5 deletions

File tree

.readthedocs.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# https://docs.readthedocs.io/en/stable/config-file/v2.html#supported-settings
2+
3+
version: 2
4+
5+
sphinx:
6+
# The config file overrides the UI settings:
7+
# https://github.com/pyca/cryptography/issues/5863#issuecomment-817828152
8+
builder: dirhtml
9+
10+
build:
11+
# readdocs master now includes a rust toolchain
12+
os: "ubuntu-20.04"
13+
tools:
14+
python: "3.9"
15+
16+
python:
17+
install:
18+
- method: pip
19+
path: .
20+
extra_requirements:
21+
- docs

docs/_ext/linkcode_res.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import importlib
2+
import inspect
3+
import os
4+
import sys
5+
6+
import xbox.webapi
7+
8+
# -- Linkcode resolver -----------------------------------------------------
9+
10+
# This is HEAVILY inspired by numpy's
11+
# https://github.com/numpy/numpy/blob/73fe877ff967f279d470b81ad447b9f3056c1335/doc/source/conf.py#L390
12+
13+
# Copyright (c) 2005-2020, NumPy Developers.
14+
# All rights reserved.
15+
#
16+
# Redistribution and use in source and binary forms, with or without
17+
# modification, are permitted provided that the following conditions are
18+
# met:
19+
#
20+
# * Redistributions of source code must retain the above copyright
21+
# notice, this list of conditions and the following disclaimer.
22+
#
23+
# * Redistributions in binary form must reproduce the above
24+
# copyright notice, this list of conditions and the following
25+
# disclaimer in the documentation and/or other materials provided
26+
# with the distribution.
27+
#
28+
# * Neither the name of the NumPy Developers nor the names of any
29+
# contributors may be used to endorse or promote products derived
30+
# from this software without specific prior written permission.
31+
#
32+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43+
44+
45+
def linkcode_resolve(domain, info):
46+
"""
47+
Determine the url corresponding to Python object
48+
"""
49+
if domain != "py":
50+
return None
51+
52+
modname = info["module"]
53+
fullname = info["fullname"]
54+
55+
try:
56+
importlib.import_module(modname)
57+
except Exception:
58+
return None
59+
submod = sys.modules.get(modname)
60+
if submod is None:
61+
return None
62+
63+
obj = submod
64+
for part in fullname.split("."):
65+
try:
66+
obj = getattr(obj, part)
67+
except Exception:
68+
return None
69+
70+
# strip decorators, which would resolve to the source of the decorator
71+
# possibly an upstream bug in getsourcefile, bpo-1764286
72+
try:
73+
unwrap = inspect.unwrap
74+
except AttributeError:
75+
pass
76+
else:
77+
obj = unwrap(obj)
78+
79+
fn = None
80+
lineno = None
81+
82+
try:
83+
fn = inspect.getsourcefile(obj)
84+
except Exception:
85+
fn = None
86+
if not fn:
87+
return None
88+
89+
try:
90+
source, lineno = inspect.getsourcelines(obj)
91+
except Exception:
92+
lineno = None
93+
94+
fn = os.path.relpath(fn, start=os.path.dirname(xbox.webapi.__file__))
95+
96+
if lineno:
97+
linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1)
98+
else:
99+
linespec = ""
100+
101+
url = "https://github.com/OpenXbox/xbox-webapi-python/blob/%s/xbox/webapi/%s%s"
102+
# url = "https://github.com/pyca/cryptography/blob/%s/src/cryptography/%s%s"
103+
if "dev" in xbox.webapi.__version__:
104+
return url % ("master", fn, linespec)
105+
else:
106+
version = f"v{xbox.webapi.__version__}"
107+
return url % (version, fn, linespec)

docs/conf.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import sys
1919

2020
sys.path.insert(0, os.path.abspath('../'))
21+
sys.path.insert(0, os.path.abspath('_ext'))
2122

2223
# -- Project information -----------------------------------------------------
2324

@@ -44,9 +45,13 @@
4445
'sphinx.ext.autodoc',
4546
'sphinx.ext.intersphinx',
4647
'sphinx.ext.napoleon',
47-
'recommonmark'
48+
'sphinx.ext.linkcode',
49+
'sphinx_mdinclude'
4850
]
4951

52+
# Linkcode resolver
53+
from linkcode_res import linkcode_resolve # noqa: E402, F401
54+
5055
# Add any paths that contain templates here, relative to this directory.
5156
templates_path = ['_templates']
5257

@@ -166,7 +171,7 @@
166171
# -- Options for intersphinx extension ---------------------------------------
167172

168173
# Example configuration for intersphinx: refer to the Python standard library.
169-
intersphinx_mapping = {'https://docs.python.org/': None}
174+
intersphinx_mapping = {'https://docs.python.org/3': None}
170175

171176
# -- Options for napoleon extension ------------------------------------------
172177
napoleon_google_docstring = True

setup.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@
4848
"flake8",
4949
"tox",
5050
"coverage",
51-
"Sphinx",
52-
"sphinx_rtd_theme",
53-
"recommonmark",
51+
# PyPi
5452
"twine",
53+
# Tests
5554
"pytest",
5655
"pytest-cov",
5756
"pytest-asyncio",
5857
"pytest-runner",
5958
],
59+
"docs": [
60+
"Sphinx",
61+
"sphinx_rtd_theme",
62+
"sphinx-mdinclude",
63+
],
6064
},
6165
entry_points={
6266
"console_scripts": [

0 commit comments

Comments
 (0)