Skip to content

Commit 2fee341

Browse files
MNT: update vendored docs script github_link.py
Copied from scikit-learn 1.3.2. File initially copied in #1258.
1 parent 41d0eeb commit 2fee341

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

docs/sphinxext/github_link.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
"""
2-
This script comes from scikit-learn:
3-
https://github.com/scikit-learn/scikit-learn/blob/master/doc/sphinxext/github_link.py
4-
"""
5-
from operator import attrgetter
61
import inspect
7-
import subprocess
82
import os
3+
import subprocess
94
import sys
105
from functools import partial
6+
from operator import attrgetter
117

12-
REVISION_CMD = 'git rev-parse --short HEAD'
8+
REVISION_CMD = "git rev-parse --short HEAD"
139

1410

1511
def _get_git_revision():
1612
try:
1713
revision = subprocess.check_output(REVISION_CMD.split()).strip()
1814
except (subprocess.CalledProcessError, OSError):
19-
print('Failed to execute git to get revision')
15+
print("Failed to execute git to get revision")
2016
return None
21-
return revision.decode('utf-8')
17+
return revision.decode("utf-8")
2218

2319

2420
def _linkcode_resolve(domain, info, package, url_fmt, revision):
@@ -30,25 +26,26 @@ def _linkcode_resolve(domain, info, package, url_fmt, revision):
3026
>>> _linkcode_resolve('py', {'module': 'tty',
3127
... 'fullname': 'setraw'},
3228
... package='tty',
33-
... url_fmt='https://hg.python.org/cpython/file/'
29+
... url_fmt='http://hg.python.org/cpython/file/'
3430
... '{revision}/Lib/{package}/{path}#L{lineno}',
3531
... revision='xxxx')
36-
'https://hg.python.org/cpython/file/xxxx/Lib/tty/tty.py#L18'
32+
'http://hg.python.org/cpython/file/xxxx/Lib/tty/tty.py#L18'
3733
"""
3834

3935
if revision is None:
4036
return
41-
if domain not in ('py', 'pyx'):
37+
if domain not in ("py", "pyx"):
4238
return
43-
if not info.get('module') or not info.get('fullname'):
39+
if not info.get("module") or not info.get("fullname"):
4440
return
4541

46-
class_name = info['fullname'].split('.')[0]
47-
if type(class_name) != str:
48-
# Python 2 only
49-
class_name = class_name.encode('utf-8')
50-
module = __import__(info['module'], fromlist=[class_name])
51-
obj = attrgetter(info['fullname'])(module)
42+
class_name = info["fullname"].split(".")[0]
43+
module = __import__(info["module"], fromlist=[class_name])
44+
obj = attrgetter(info["fullname"])(module)
45+
46+
# Unwrap the object to get the correct source
47+
# file in case that is wrapped by a decorator
48+
obj = inspect.unwrap(obj)
5249

5350
try:
5451
fn = inspect.getsourcefile(obj)
@@ -62,14 +59,12 @@ def _linkcode_resolve(domain, info, package, url_fmt, revision):
6259
if not fn:
6360
return
6461

65-
fn = os.path.relpath(fn,
66-
start=os.path.dirname(__import__(package).__file__))
62+
fn = os.path.relpath(fn, start=os.path.dirname(__import__(package).__file__))
6763
try:
6864
lineno = inspect.getsourcelines(obj)[1]
6965
except Exception:
70-
lineno = ''
71-
return url_fmt.format(revision=revision, package=package,
72-
path=fn, lineno=lineno)
66+
lineno = ""
67+
return url_fmt.format(revision=revision, package=package, path=fn, lineno=lineno)
7368

7469

7570
def make_linkcode_resolve(package, url_fmt):
@@ -84,5 +79,6 @@ def make_linkcode_resolve(package, url_fmt):
8479
'{path}#L{lineno}')
8580
"""
8681
revision = _get_git_revision()
87-
return partial(_linkcode_resolve, revision=revision, package=package,
88-
url_fmt=url_fmt)
82+
return partial(
83+
_linkcode_resolve, revision=revision, package=package, url_fmt=url_fmt
84+
)

0 commit comments

Comments
 (0)