Skip to content

Commit 048e5ec

Browse files
committed
Better fix for package name issues
Add ```get_top_level()``` function to retrieve the top level package name using either * conf.py value of ```top_level``` * The grandparent directory of the docs source directory * The ```html_context["github_repo"]``` value If it can't find it after all that, then..... 🙂
1 parent 871c30c commit 048e5ec

File tree

5 files changed

+59
-22
lines changed

5 files changed

+59
-22
lines changed

README.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,23 @@ Add the extension to your ``conf.py``
137137
138138
...
139139

140-
Configuration Variables
141-
=========================
140+
Optional Configuration Variables
141+
===================================
142142

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

145-
``pkg_name``
145+
``top_level``
146146
^^^^^^^^^^^^^^^^^^^
147147

148148
.. code-block:: python
149149
150-
pkg_name: str
150+
top_level: str
151151
152152
153153
The name of the top-level package. For this repo, it would be ``sphinx_github_style``
154154

155+
...
156+
155157
``linkcode_blob``
156158
^^^^^^^^^^^^^^^^^^^
157159

README_PyPi.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,22 @@ Add the extension to your ``conf.py``
127127
128128
...
129129

130-
Configuration Variables
131-
=========================
130+
Optional Configuration Variables
131+
===================================
132132

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

135-
``pkg_name``
135+
``top_level``
136136
^^^^^^^^^^^^^^^^^^^
137137

138138
.. code-block:: python
139139
140-
pkg_name: str
140+
top_level: str
141141
142142
143143
The name of the top-level package. For this repo, it would be ``sphinx_github_style``
144144

145+
...
145146

146147
``linkcode_blob``
147148
^^^^^^^^^^^^^^^^^^^

docs/source/README.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,22 @@ Add the extension to your ``conf.py``
110110
"sphinx_github_style",
111111
]
112112
113-
...
114-
115-
Configuration Variables
116-
=========================
113+
Optional Configuration Variables
114+
===================================
117115

118116
Add any of the following configuration variables to your ``conf.py``
119117

120-
``pkg_name``
118+
``top_level``
121119
^^^^^^^^^^^^^^^^^^^
122120

123121
.. code-block:: python
124122
125-
pkg_name: str
123+
top_level: str
126124
127125
128126
The name of the top-level package. For this repo, it would be ``sphinx_github_style``
129127

128+
...
130129

131130
``linkcode_blob``
132131
^^^^^^^^^^^^^^^^^^^

sphinx_github_style/__init__.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
import sphinx
44
import inspect
55
import subprocess
6+
import pkg_resources
67
from pathlib import Path
78
from sphinx.application import Sphinx
89
from sphinx.errors import ExtensionError
910
from typing import Dict, Any, Optional, Callable
1011

1112

12-
__version__ = "1.0.1b0"
13+
__version__ = "1.0.1b3"
1314
__author__ = 'Adam Korn <hello@dailykitten.net>'
1415

16+
1517
from .add_linkcode_class import add_linkcode_node_class
1618
from .meth_lexer import TDKMethLexer
1719
from .github_style import TDKStyle
@@ -21,7 +23,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
2123
app.connect("builder-inited", add_static_path)
2224

2325
app.add_config_value('linkcode_blob', 'head', True)
24-
app.add_config_value('pkg_name', None, '')
26+
app.add_config_value('top_level', get_top_level(app), '')
2527

2628
app.setup_extension('sphinx_github_style.add_linkcode_class')
2729
app.setup_extension('sphinx_github_style.github_style')
@@ -130,7 +132,7 @@ def get_linkcode_url(app: Sphinx) -> str:
130132
"sphinx-github-style: config value ``linkcode_url`` is missing. "
131133
"Creating link from ``html_context`` values..."
132134
)
133-
blob = context['github_version'] # Added by setup() above
135+
blob = context['github_version'] # Added by setup() above
134136
url = f"https://github.com/{context['github_user']}/{context['github_repo']}/{blob}/"
135137

136138
else:
@@ -146,6 +148,7 @@ def get_linkcode_resolve(linkcode_url: str) -> Callable:
146148
147149
Used by default if ``linkcode_resolve`` isn't defined in ``conf.py``
148150
"""
151+
149152
def linkcode_resolve(domain, info):
150153
"""Returns a link to the source code on GitHub, with appropriate lines highlighted
151154
@@ -204,6 +207,41 @@ def linkcode_resolve(domain, info):
204207
return linkcode_resolve
205208

206209

210+
def get_top_level(app: Sphinx):
211+
# Retrieve conf.py value
212+
top_level = get_conf_val(app, "top_level")
213+
214+
if top_level is None:
215+
# If not defined, try retrieving with pkg_resources
216+
project_dir = Path(app.srcdir).parent.parent
217+
project_name = os.path.basename(project_dir)
218+
pkg = None
219+
220+
try:
221+
pkg = pkg_resources.require(project_name)[0]
222+
223+
except pkg_resources.DistributionNotFound:
224+
# Try `html_context` repo name in case project structure isn't repo/docs/source
225+
project_name = get_conf_val(app, "html_context", {}).get("github_repo")
226+
227+
if project_name is not None:
228+
try:
229+
pkg = pkg_resources.require(project_name)[0]
230+
231+
except pkg_resources.DistributionNotFound:
232+
pass
233+
234+
finally:
235+
if pkg is None:
236+
raise ExtensionError(
237+
"sphinx_github_style: Unable to determine top-level package")
238+
239+
top_level = pkg.get_metadata('top_level.txt').strip()
240+
app.config._raw_config['top_level'] = top_level
241+
242+
return top_level
243+
244+
207245
def get_conf_val(app: Sphinx, attr: str, default: Any = None) -> Any:
208246
"""Retrieve values from ``conf.py``
209247

sphinx_github_style/meth_lexer.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from sphinx.application import Sphinx
44
from pygments.lexers.python import NumPyLexer
55
from inspect import getmembers, isfunction, ismethod, ismodule, isclass
6-
from sphinx.errors import ExtensionError
76

87

98
def get_pkg_funcs(pkg: types.ModuleType):
@@ -44,7 +43,5 @@ def get_pkg_lexer(cls, pkg_name: str) -> Type["TDKMethLexer"]:
4443

4544

4645
def setup(app: Sphinx):
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")
50-
app.add_lexer('python', TDKMethLexer.get_pkg_lexer(pkg_name))
46+
top_level = app.config._raw_config['top_level'] # Set by __init__.setup()
47+
app.add_lexer('python', TDKMethLexer.get_pkg_lexer(top_level))

0 commit comments

Comments
 (0)