Skip to content

Commit 871c30c

Browse files
committed
Fix issues with getting the package name
* Able to get package/root directory paths for general linkcode_resolve function, using the info dict provided * For TDKMethLexer, not sure how to do the same - Takes from conf.py for now.... so that must be set to use this
1 parent cd913e6 commit 871c30c

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

README.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.. Author: TDKorn (Adam Korn)
44
55
.. |.get_linkcode_resolve| replace:: ``get_linkcode_resolve()``
6-
.. _.get_linkcode_resolve: https://github.com/TDKorn/sphinx-github-style/blob/v1.0.0/sphinx_github_style/__init__.py#L150-L207
6+
.. _.get_linkcode_resolve: https://github.com/TDKorn/sphinx-github-style/blob/v1.0.0/sphinx_github_style/__init__.py#L153-L210
77
.. |.add_linkcode_node_class| replace:: ``add_linkcode_node_class()``
88
.. _.add_linkcode_node_class: https://github.com/TDKorn/sphinx-github-style/blob/v1.0.0/sphinx_github_style/add_linkcode_class.py#L9-L24
99
.. |.TDKStyle| replace:: ``TDKStyle``
@@ -142,6 +142,16 @@ Configuration Variables
142142

143143
Add any of the following configuration variables to your ``conf.py``
144144

145+
``pkg_name``
146+
^^^^^^^^^^^^^^^^^^^
147+
148+
.. code-block:: python
149+
150+
pkg_name: str
151+
152+
153+
The name of the top-level package. For this repo, it would be ``sphinx_github_style``
154+
145155
``linkcode_blob``
146156
^^^^^^^^^^^^^^^^^^^
147157

README_PyPi.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.. Author: TDKorn (Adam Korn)
44
55
.. |.get_linkcode_resolve| replace:: get_linkcode_resolve()
6-
.. _.get_linkcode_resolve: https://github.com/TDKorn/sphinx-github-style/blob/v1.0.0/sphinx_github_style/__init__.py#L150-L207
6+
.. _.get_linkcode_resolve: https://github.com/TDKorn/sphinx-github-style/blob/v1.0.0/sphinx_github_style/__init__.py#L153-L210
77
.. |.add_linkcode_node_class| replace:: add_linkcode_node_class()
88
.. _.add_linkcode_node_class: https://github.com/TDKorn/sphinx-github-style/blob/v1.0.0/sphinx_github_style/add_linkcode_class.py#L9-L24
99
.. |.TDKStyle| replace:: TDKStyle
@@ -132,6 +132,17 @@ Configuration Variables
132132

133133
Add any of the following configuration variables to your ``conf.py``
134134

135+
``pkg_name``
136+
^^^^^^^^^^^^^^^^^^^
137+
138+
.. code-block:: python
139+
140+
pkg_name: str
141+
142+
143+
The name of the top-level package. For this repo, it would be ``sphinx_github_style``
144+
145+
135146
``linkcode_blob``
136147
^^^^^^^^^^^^^^^^^^^
137148

docs/source/README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ Configuration Variables
117117

118118
Add any of the following configuration variables to your ``conf.py``
119119

120+
``pkg_name``
121+
^^^^^^^^^^^^^^^^^^^
122+
123+
.. code-block:: python
124+
125+
pkg_name: str
126+
127+
128+
The name of the top-level package. For this repo, it would be ``sphinx_github_style``
129+
130+
120131
``linkcode_blob``
121132
^^^^^^^^^^^^^^^^^^^
122133

docs/source/conf.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
# Configure paths
2525
root = os.path.abspath('../../')
26-
modpath = root
27-
2826
sys.path.append(os.path.abspath('.'))
2927
sys.path.insert(0, root)
3028

@@ -39,12 +37,11 @@
3937
repo = project
4038

4139
# Package Info
42-
modname = os.path.basename(modpath)
43-
pkg = pkg_resources.require(modname)[0]
40+
pkg = pkg_resources.require(project)[0]
4441
pkg_name = pkg.get_metadata('top_level.txt').strip()
4542

4643
# Simplify things by using the installed version
47-
version = pkg_resources.require(modname)[0].version
44+
version = pkg.version
4845
release = version
4946

5047
# ======================== General configuration ============================

sphinx_github_style/__init__.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,25 @@
33
import sphinx
44
import inspect
55
import subprocess
6-
import pkg_resources
76
from pathlib import Path
87
from sphinx.application import Sphinx
98
from sphinx.errors import ExtensionError
109
from typing import Dict, Any, Optional, Callable
1110

1211

13-
__version__ = "0.0.1b16"
12+
__version__ = "1.0.1b0"
1413
__author__ = 'Adam Korn <hello@dailykitten.net>'
1514

16-
1715
from .add_linkcode_class import add_linkcode_node_class
1816
from .meth_lexer import TDKMethLexer
1917
from .github_style import TDKStyle
2018

2119

2220
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-
2821
app.connect("builder-inited", add_static_path)
2922

30-
app.add_config_value('pkg_name', pkg_name, 'html')
3123
app.add_config_value('linkcode_blob', 'head', True)
24+
app.add_config_value('pkg_name', None, '')
3225

3326
app.setup_extension('sphinx_github_style.add_linkcode_class')
3427
app.setup_extension('sphinx_github_style.github_style')
@@ -47,9 +40,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
4740
"Function `linkcode_resolve` not found in ``conf.py``; "
4841
"using default function from ``sphinx_github_style``"
4942
)
50-
linkcode_func = get_linkcode_resolve(
51-
linkcode_url, pkg_name, modpath
52-
)
43+
linkcode_func = get_linkcode_resolve(linkcode_url)
5344

5445
app.config.linkcode_resolve = linkcode_func
5546
return {'version': sphinx.__display_version__, 'parallel_read_safe': True}
@@ -150,7 +141,7 @@ def get_linkcode_url(app: Sphinx) -> str:
150141
return url
151142

152143

153-
def get_linkcode_resolve(linkcode_url: str, pkg_name: str, modpath: str) -> Callable:
144+
def get_linkcode_resolve(linkcode_url: str) -> Callable:
154145
"""Defines and returns a ``linkcode_resolve`` function for your package
155146
156147
Used by default if ``linkcode_resolve`` isn't defined in ``conf.py``
@@ -177,11 +168,15 @@ def linkcode_resolve(domain, info):
177168
for part in fullname.split('.'):
178169
try:
179170
obj = getattr(obj, part)
180-
except Exception:
171+
except AttributeError:
181172
return None
182173

174+
pkg_name = modname.split('.')[0]
175+
pkg_dir = sys.modules.get(pkg_name).__file__
176+
repo_dir = Path(pkg_dir).parent.parent
177+
183178
try:
184-
filepath = os.path.relpath(inspect.getsourcefile(obj), modpath)
179+
filepath = os.path.relpath(inspect.getsourcefile(obj), repo_dir)
185180
if filepath is None:
186181
return
187182
except Exception:
@@ -190,7 +185,6 @@ def linkcode_resolve(domain, info):
190185
try:
191186
source, lineno = inspect.getsourcelines(obj)
192187
except OSError:
193-
print(f'failed to get source lines for {obj}')
194188
return None
195189
else:
196190
linestart, linestop = lineno, lineno + len(source) - 1

sphinx_github_style/meth_lexer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import copy
21
import types
32
from typing import Type
43
from sphinx.application import Sphinx
54
from pygments.lexers.python import NumPyLexer
65
from inspect import getmembers, isfunction, ismethod, ismodule, isclass
6+
from sphinx.errors import ExtensionError
77

88

99
def get_pkg_funcs(pkg: types.ModuleType):
@@ -44,6 +44,7 @@ def get_pkg_lexer(cls, pkg_name: str) -> Type["TDKMethLexer"]:
4444

4545

4646
def setup(app: Sphinx):
47-
# Get pkg_name from conf.py; fallback to pkg_name set by __init__.py
48-
pkg_name = app.config._raw_config.get('pkg_name', app.config.pkg_name)
47+
pkg_name = app.config._raw_config.get("pkg_name", getattr(app.config, "pkg_name"))
48+
if pkg_name is None:
49+
raise ExtensionError("`pkg_name` is missing from conf.py")
4950
app.add_lexer('python', TDKMethLexer.get_pkg_lexer(pkg_name))

0 commit comments

Comments
 (0)