Skip to content

Commit 99dd0cb

Browse files
committed
Refactor sphinx.theming to prepare for iterative loading
1 parent 256c8f9 commit 99dd0cb

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

sphinx/theming.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,6 @@
3535
_THEME_CONF = 'theme.conf'
3636

3737

38-
def _extract_zip(filename: str, target_dir: str, /) -> None:
39-
"""Extract zip file to target directory."""
40-
ensuredir(target_dir)
41-
42-
with ZipFile(filename) as archive:
43-
for name in archive.namelist():
44-
if name.endswith('/'):
45-
continue
46-
entry = path.join(target_dir, name)
47-
ensuredir(path.dirname(entry))
48-
with open(path.join(entry), 'wb') as fp:
49-
fp.write(archive.read(name))
50-
51-
52-
def _load_theme_conf(theme_dir: os.PathLike[str] | str) -> configparser.RawConfigParser:
53-
c = configparser.RawConfigParser()
54-
config_file_path = path.join(theme_dir, _THEME_CONF)
55-
if not os.path.isfile(config_file_path):
56-
raise ThemeError(__('theme configuration file %r not found') % config_file_path)
57-
c.read(config_file_path, encoding='utf-8')
58-
return c
59-
60-
6138
class Theme:
6239
"""A Theme is a set of HTML templates and configurations.
6340
@@ -169,15 +146,6 @@ def _cleanup(self) -> None:
169146
self._base._cleanup()
170147

171148

172-
def _is_archived_theme(filename: str, /) -> bool:
173-
"""Check whether the specified file is an archived theme file or not."""
174-
try:
175-
with ZipFile(filename) as f:
176-
return _THEME_CONF in f.namelist()
177-
except Exception:
178-
return False
179-
180-
181149
class HTMLThemeFactory:
182150
"""A factory class for HTML Themes."""
183151

@@ -214,9 +182,10 @@ def _load_extra_theme(self, name: str) -> None:
214182
pass
215183
else:
216184
self._app.registry.load_extension(self._app, entry_point.module)
217-
_config_post_init(None, self._app.config)
185+
_config_post_init(self._app, self._app.config)
218186

219-
def _find_themes(self, theme_path: str) -> dict[str, str]:
187+
@staticmethod
188+
def _find_themes(theme_path: str) -> dict[str, str]:
220189
"""Search themes from specified directory."""
221190
themes: dict[str, str] = {}
222191
if not path.isdir(theme_path):
@@ -246,3 +215,35 @@ def create(self, name: str) -> Theme:
246215
raise ThemeError(__('no theme named %r found (missing theme.conf?)') % name)
247216

248217
return Theme(name, self._themes[name], self)
218+
219+
220+
def _is_archived_theme(filename: str, /) -> bool:
221+
"""Check whether the specified file is an archived theme file or not."""
222+
try:
223+
with ZipFile(filename) as f:
224+
return _THEME_CONF in f.namelist()
225+
except Exception:
226+
return False
227+
228+
229+
def _extract_zip(filename: str, target_dir: str, /) -> None:
230+
"""Extract zip file to target directory."""
231+
ensuredir(target_dir)
232+
233+
with ZipFile(filename) as archive:
234+
for name in archive.namelist():
235+
if name.endswith('/'):
236+
continue
237+
entry = path.join(target_dir, name)
238+
ensuredir(path.dirname(entry))
239+
with open(path.join(entry), 'wb') as fp:
240+
fp.write(archive.read(name))
241+
242+
243+
def _load_theme_conf(theme_dir: os.PathLike[str] | str, /) -> configparser.RawConfigParser:
244+
c = configparser.RawConfigParser()
245+
config_file_path = path.join(theme_dir, _THEME_CONF)
246+
if not os.path.isfile(config_file_path):
247+
raise ThemeError(__('theme configuration file %r not found') % config_file_path)
248+
c.read(config_file_path, encoding='utf-8')
249+
return c

tests/test_theming/test_theming.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import pytest
66

77
import sphinx.builders.html
8-
from sphinx.theming import Theme, ThemeError
8+
from sphinx.errors import ThemeError
9+
from sphinx.theming import Theme
910

1011

1112
@pytest.mark.sphinx(
@@ -26,7 +27,7 @@ def test_theme_api(app, status, warning):
2627
# test Theme instance API
2728
theme = app.builder.theme
2829
assert theme.name == 'ziptheme'
29-
theme_dir = theme._theme_dir
30+
tmp_dirs = (theme._theme_dir,)
3031
assert theme._base.name == 'basic'
3132
assert len(theme.get_theme_dirs()) == 2
3233

@@ -50,7 +51,7 @@ def test_theme_api(app, status, warning):
5051

5152
# cleanup temp directories
5253
theme._cleanup()
53-
assert not os.path.exists(theme_dir)
54+
assert not any(map(os.path.exists, tmp_dirs))
5455

5556

5657
def test_nonexistent_theme_conf(tmp_path):

0 commit comments

Comments
 (0)