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
6
1
import inspect
7
- import subprocess
8
2
import os
3
+ import subprocess
9
4
import sys
10
5
from functools import partial
6
+ from operator import attrgetter
11
7
12
- REVISION_CMD = ' git rev-parse --short HEAD'
8
+ REVISION_CMD = " git rev-parse --short HEAD"
13
9
14
10
15
11
def _get_git_revision ():
16
12
try :
17
13
revision = subprocess .check_output (REVISION_CMD .split ()).strip ()
18
14
except (subprocess .CalledProcessError , OSError ):
19
- print (' Failed to execute git to get revision' )
15
+ print (" Failed to execute git to get revision" )
20
16
return None
21
- return revision .decode (' utf-8' )
17
+ return revision .decode (" utf-8" )
22
18
23
19
24
20
def _linkcode_resolve (domain , info , package , url_fmt , revision ):
@@ -30,25 +26,26 @@ def _linkcode_resolve(domain, info, package, url_fmt, revision):
30
26
>>> _linkcode_resolve('py', {'module': 'tty',
31
27
... 'fullname': 'setraw'},
32
28
... package='tty',
33
- ... url_fmt='https ://hg.python.org/cpython/file/'
29
+ ... url_fmt='http ://hg.python.org/cpython/file/'
34
30
... '{revision}/Lib/{package}/{path}#L{lineno}',
35
31
... 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'
37
33
"""
38
34
39
35
if revision is None :
40
36
return
41
- if domain not in ('py' , ' pyx' ):
37
+ if domain not in ("py" , " pyx" ):
42
38
return
43
- if not info .get (' module' ) or not info .get (' fullname' ):
39
+ if not info .get (" module" ) or not info .get (" fullname" ):
44
40
return
45
41
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 )
52
49
53
50
try :
54
51
fn = inspect .getsourcefile (obj )
@@ -62,14 +59,12 @@ def _linkcode_resolve(domain, info, package, url_fmt, revision):
62
59
if not fn :
63
60
return
64
61
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__ ))
67
63
try :
68
64
lineno = inspect .getsourcelines (obj )[1 ]
69
65
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 )
73
68
74
69
75
70
def make_linkcode_resolve (package , url_fmt ):
@@ -84,5 +79,6 @@ def make_linkcode_resolve(package, url_fmt):
84
79
'{path}#L{lineno}')
85
80
"""
86
81
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