3
3
import sphinx
4
4
import inspect
5
5
import subprocess
6
- import pkg_resources
7
6
from pathlib import Path
8
7
from sphinx .application import Sphinx
9
8
from sphinx .errors import ExtensionError
10
9
from typing import Dict , Any , Optional , Callable
11
10
12
11
13
- __version__ = "0 .0.1b16 "
12
+ __version__ = "1 .0.1b0 "
14
13
__author__ = 'Adam Korn <hello@dailykitten.net>'
15
14
16
-
17
15
from .add_linkcode_class import add_linkcode_node_class
18
16
from .meth_lexer import TDKMethLexer
19
17
from .github_style import TDKStyle
20
18
21
19
22
20
def setup (app : Sphinx ) -> Dict [str , Any ]:
23
- modpath = os .path .abspath ('../' )
24
- modname = os .path .basename (modpath )
25
- pkg = pkg_resources .require (modname )[0 ]
26
- pkg_name = pkg .get_metadata ('top_level.txt' ).strip ()
27
-
28
21
app .connect ("builder-inited" , add_static_path )
29
22
30
- app .add_config_value ('pkg_name' , pkg_name , 'html' )
31
23
app .add_config_value ('linkcode_blob' , 'head' , True )
24
+ app .add_config_value ('pkg_name' , None , '' )
32
25
33
26
app .setup_extension ('sphinx_github_style.add_linkcode_class' )
34
27
app .setup_extension ('sphinx_github_style.github_style' )
@@ -47,9 +40,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
47
40
"Function `linkcode_resolve` not found in ``conf.py``; "
48
41
"using default function from ``sphinx_github_style``"
49
42
)
50
- linkcode_func = get_linkcode_resolve (
51
- linkcode_url , pkg_name , modpath
52
- )
43
+ linkcode_func = get_linkcode_resolve (linkcode_url )
53
44
54
45
app .config .linkcode_resolve = linkcode_func
55
46
return {'version' : sphinx .__display_version__ , 'parallel_read_safe' : True }
@@ -150,7 +141,7 @@ def get_linkcode_url(app: Sphinx) -> str:
150
141
return url
151
142
152
143
153
- def get_linkcode_resolve (linkcode_url : str , pkg_name : str , modpath : str ) -> Callable :
144
+ def get_linkcode_resolve (linkcode_url : str ) -> Callable :
154
145
"""Defines and returns a ``linkcode_resolve`` function for your package
155
146
156
147
Used by default if ``linkcode_resolve`` isn't defined in ``conf.py``
@@ -177,11 +168,15 @@ def linkcode_resolve(domain, info):
177
168
for part in fullname .split ('.' ):
178
169
try :
179
170
obj = getattr (obj , part )
180
- except Exception :
171
+ except AttributeError :
181
172
return None
182
173
174
+ pkg_name = modname .split ('.' )[0 ]
175
+ pkg_dir = sys .modules .get (pkg_name ).__file__
176
+ repo_dir = Path (pkg_dir ).parent .parent
177
+
183
178
try :
184
- filepath = os .path .relpath (inspect .getsourcefile (obj ), modpath )
179
+ filepath = os .path .relpath (inspect .getsourcefile (obj ), repo_dir )
185
180
if filepath is None :
186
181
return
187
182
except Exception :
@@ -190,7 +185,6 @@ def linkcode_resolve(domain, info):
190
185
try :
191
186
source , lineno = inspect .getsourcelines (obj )
192
187
except OSError :
193
- print (f'failed to get source lines for { obj } ' )
194
188
return None
195
189
else :
196
190
linestart , linestop = lineno , lineno + len (source ) - 1
0 commit comments