MartinPdeS-docs 0.4.0__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: MartinPdeS-docs
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Shared Sphinx configuration for MartinPdeS scientific Python packages.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -54,6 +54,15 @@ over the `tag` environment variable. The package must be importable and its
54
54
  order used for documentation imports. Omitting `version` retains the existing
55
55
  environment and distribution-metadata fallback.
56
56
 
57
+ Pass `docs_root` when assets or gallery data live outside the current working
58
+ directory. The optional `profile` argument accepts `"default"`,
59
+ `"scientific-gallery"`, or `"minimal"`; the minimal profile disables
60
+ Sphinx-Gallery while retaining the shared theme and autodoc defaults.
61
+
62
+ Theme options and autodoc options are merged recursively, so a project can
63
+ override one nested value without replacing shared defaults. Repository values
64
+ must use the `owner/name` GitHub form.
65
+
57
66
  For a multi-section Sphinx-Gallery, pass the section names instead of
58
67
  repeating the directory mappings:
59
68
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: MartinPdeS-docs
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Shared Sphinx configuration for MartinPdeS scientific Python packages.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -54,6 +54,15 @@ over the `tag` environment variable. The package must be importable and its
54
54
  order used for documentation imports. Omitting `version` retains the existing
55
55
  environment and distribution-metadata fallback.
56
56
 
57
+ Pass `docs_root` when assets or gallery data live outside the current working
58
+ directory. The optional `profile` argument accepts `"default"`,
59
+ `"scientific-gallery"`, or `"minimal"`; the minimal profile disables
60
+ Sphinx-Gallery while retaining the shared theme and autodoc defaults.
61
+
62
+ Theme options and autodoc options are merged recursively, so a project can
63
+ override one nested value without replacing shared defaults. Repository values
64
+ must use the `owner/name` GitHub form.
65
+
57
66
  For a multi-section Sphinx-Gallery, pass the section names instead of
58
67
  repeating the directory mappings:
59
68
 
@@ -37,6 +37,15 @@ over the `tag` environment variable. The package must be importable and its
37
37
  order used for documentation imports. Omitting `version` retains the existing
38
38
  environment and distribution-metadata fallback.
39
39
 
40
+ Pass `docs_root` when assets or gallery data live outside the current working
41
+ directory. The optional `profile` argument accepts `"default"`,
42
+ `"scientific-gallery"`, or `"minimal"`; the minimal profile disables
43
+ Sphinx-Gallery while retaining the shared theme and autodoc defaults.
44
+
45
+ Theme options and autodoc options are merged recursively, so a project can
46
+ override one nested value without replacing shared defaults. Repository values
47
+ must use the `owner/name` GitHub form.
48
+
40
49
  For a multi-section Sphinx-Gallery, pass the section names instead of
41
50
  repeating the directory mappings:
42
51
 
@@ -11,9 +11,29 @@ from importlib.metadata import (
11
11
  version as distribution_version,
12
12
  )
13
13
  from pathlib import Path
14
+ import warnings
14
15
 
15
16
 
16
- def _raster_thumbnail_path(logo: str, favicon: str) -> str | None:
17
+ def _deep_merge(base: dict, override: Mapping) -> dict:
18
+ """Merge nested mappings without mutating either input mapping."""
19
+ merged = dict(base)
20
+ for key, value in override.items():
21
+ if isinstance(merged.get(key), Mapping) and isinstance(value, Mapping):
22
+ merged[key] = _deep_merge(dict(merged[key]), value)
23
+ else:
24
+ merged[key] = value
25
+ return merged
26
+
27
+
28
+ def _parse_repository(repository: str) -> tuple[str, str]:
29
+ """Validate and split a ``owner/repository`` GitHub identifier."""
30
+ parts = repository.split("/")
31
+ if len(parts) != 2 or not all(parts) or any(part.strip() != part for part in parts):
32
+ raise ValueError("repository must be a GitHub identifier in the form 'owner/name'.")
33
+ return parts[0], parts[1]
34
+
35
+
36
+ def _raster_thumbnail_path(logo: str, favicon: str, docs_root: Path | None = None) -> str | None:
17
37
  """Choose a Sphinx-Gallery-compatible fallback thumbnail.
18
38
 
19
39
  Sphinx-Gallery requires a raster image for ``default_thumb_file``. SVG
@@ -23,20 +43,17 @@ def _raster_thumbnail_path(logo: str, favicon: str) -> str | None:
23
43
  raster_suffixes = {".png", ".jpg", ".jpeg", ".gif"}
24
44
  for candidate in (logo, favicon):
25
45
  if Path(candidate).suffix.lower() in raster_suffixes:
26
- return _resolve_doc_asset(candidate)
46
+ return _resolve_doc_asset(candidate, docs_root)
27
47
  return None
28
48
 
29
49
 
30
- def _resolve_doc_asset(path: str) -> str:
50
+ def _resolve_doc_asset(path: str, docs_root: Path | None = None) -> str:
31
51
  """Resolve a documentation asset relative to common Sphinx build roots."""
32
52
  candidate = Path(path)
33
53
  if candidate.is_absolute():
34
54
  return str(candidate)
35
55
 
36
- roots = (
37
- Path.cwd(),
38
- Path.cwd() / "docs" / "source",
39
- )
56
+ roots = tuple(root for root in (docs_root, Path.cwd(), Path.cwd() / "docs" / "source") if root is not None)
40
57
  for root in roots:
41
58
  resolved = root / candidate
42
59
  if resolved.is_file():
@@ -69,6 +86,12 @@ def _resolve_version(package: str, version: str | None) -> str:
69
86
  try:
70
87
  return distribution_version(package)
71
88
  except PackageNotFoundError:
89
+ warnings.warn(
90
+ f"Could not resolve an installed distribution for {package!r}; using 'latest'. "
91
+ "Pass version= explicitly or use version='auto' for published documentation.",
92
+ RuntimeWarning,
93
+ stacklevel=2,
94
+ )
72
95
  return "latest"
73
96
 
74
97
 
@@ -77,6 +100,12 @@ def _prefer_installed_package(package: str) -> None:
77
100
  try:
78
101
  installed_site = str(distribution(package).locate_file(""))
79
102
  except PackageNotFoundError:
103
+ warnings.warn(
104
+ f"prefer_installed_package=True but distribution {package!r} is not installed; "
105
+ "documentation imports may resolve to the source checkout.",
106
+ RuntimeWarning,
107
+ stacklevel=2,
108
+ )
80
109
  return
81
110
 
82
111
  sys.path[:] = [entry for entry in sys.path if entry != installed_site]
@@ -106,6 +135,8 @@ def configure(
106
135
  prefer_installed_package: bool = False,
107
136
  include_gallery: bool = True,
108
137
  version: str | None = None,
138
+ docs_root: str | Path | None = None,
139
+ profile: str = "default",
109
140
  source_suffix: str = ".rst",
110
141
  master_doc: str = "index",
111
142
  ) -> dict:
@@ -121,6 +152,13 @@ def configure(
121
152
  to read ``__version__`` from the importable package's ``_version.py`` and
122
153
  use it for both Sphinx version fields and the version switcher.
123
154
  """
155
+ if profile not in {"default", "scientific-gallery", "minimal"}:
156
+ raise ValueError("profile must be one of 'default', 'scientific-gallery', or 'minimal'.")
157
+ if profile == "minimal":
158
+ include_gallery = False
159
+
160
+ resolved_docs_root = Path(docs_root).resolve() if docs_root is not None else None
161
+
124
162
  if prefer_installed_package:
125
163
  _prefer_installed_package(package)
126
164
 
@@ -144,7 +182,7 @@ def configure(
144
182
  if logo_alt_text is not None:
145
183
  options["logo"]["alt_text"] = logo_alt_text
146
184
  if repository:
147
- owner, repo = repository.split("/", 1)
185
+ owner, repo = _parse_repository(repository)
148
186
  options["icon_links"] = [
149
187
  {
150
188
  "name": "GitHub",
@@ -158,7 +196,7 @@ def configure(
158
196
  },
159
197
  {
160
198
  "name": "Anaconda",
161
- "url": f"https://anaconda.org/{repository.split('/')[0]}/{package}",
199
+ "url": f"https://anaconda.org/{owner}/{package}",
162
200
  "icon": "fa-brands fa-python",
163
201
  },
164
202
  ]
@@ -172,7 +210,7 @@ def configure(
172
210
  "default_mode": "dark",
173
211
  }
174
212
  if repository:
175
- owner, repo = repository.split("/", 1)
213
+ owner, repo = _parse_repository(repository)
176
214
  ref_name = os.getenv("GITHUB_REF_NAME", "master")
177
215
  context.update(
178
216
  {
@@ -184,7 +222,7 @@ def configure(
184
222
  if html_context:
185
223
  context.update(html_context)
186
224
  if html_theme_options:
187
- options.update(html_theme_options)
225
+ options = _deep_merge(options, html_theme_options)
188
226
 
189
227
  default_extensions = [
190
228
  "sphinx.ext.autodoc",
@@ -218,7 +256,7 @@ def configure(
218
256
  # Use the project's favicon for examples that do not create a figure.
219
257
  # The path is interpreted relative to the documentation source tree,
220
258
  # just like ``html_favicon``.
221
- "default_thumb_file": _raster_thumbnail_path(logo, favicon),
259
+ "default_thumb_file": _raster_thumbnail_path(logo, favicon, resolved_docs_root),
222
260
  "line_numbers": False,
223
261
  "remove_config_comments": True,
224
262
  "capture_repr": ("_repr_html_", "__repr__"),
@@ -232,7 +270,7 @@ def configure(
232
270
  gallery["gallery_dirs"] = [f"gallery/{section}" for section in sections]
233
271
  if gallery is not None:
234
272
  if gallery_default_thumb is not None:
235
- gallery["default_thumb_file"] = _resolve_doc_asset(gallery_default_thumb)
273
+ gallery["default_thumb_file"] = _resolve_doc_asset(gallery_default_thumb, resolved_docs_root)
236
274
  if gallery_options:
237
275
  gallery.update(gallery_options)
238
276
  if sphinx_gallery_conf:
@@ -252,6 +290,8 @@ def configure(
252
290
  configuration = {
253
291
  "project": project,
254
292
  "package": package,
293
+ "version": resolved_version,
294
+ "release": resolved_version,
255
295
  "author": author,
256
296
  "copyright": author,
257
297
  "extensions": configured_extensions,
@@ -292,7 +332,4 @@ def configure(
292
332
  }
293
333
  if gallery is not None:
294
334
  configuration["sphinx_gallery_conf"] = gallery
295
- if version == "auto":
296
- configuration["version"] = resolved_version
297
- configuration["release"] = resolved_version
298
335
  return configuration
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "MartinPdeS-docs"
7
- version = "0.4.0"
7
+ version = "0.5.0"
8
8
  description = "Shared Sphinx configuration for MartinPdeS scientific Python packages."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -93,6 +93,16 @@ def test_custom_extensions_and_theme_options_are_preserved():
93
93
  "image": "_static/logo.svg",
94
94
  "alt_text": "Example logo",
95
95
  }
96
+
97
+ nested = configure(
98
+ project="Example",
99
+ package="example",
100
+ html_theme_options={"logo": {"text": "Custom"}},
101
+ )
102
+ assert nested["html_theme_options"]["logo"] == {
103
+ "text": "Custom",
104
+ "image": "_static/logo.svg",
105
+ }
96
106
  assert additions["autodoc_default_options"]["members"] is True
97
107
 
98
108
  no_gallery = configure(
@@ -129,6 +139,44 @@ def test_gallery_thumbnail_paths_are_resolved_from_docs_source(tmp_path, monkeyp
129
139
  assert config["sphinx_gallery_conf"]["default_thumb_file"] == str(asset)
130
140
 
131
141
 
142
+ def test_docs_root_resolves_gallery_assets(tmp_path):
143
+ asset = tmp_path / "gallery-default.png"
144
+ asset.write_bytes(b"png")
145
+
146
+ config = configure(
147
+ project="Example",
148
+ package="example",
149
+ docs_root=tmp_path,
150
+ gallery_default_thumb="gallery-default.png",
151
+ )
152
+
153
+ assert config["sphinx_gallery_conf"]["default_thumb_file"] == str(asset)
154
+
155
+
156
+ def test_repository_identifier_is_validated():
157
+ with pytest.raises(ValueError, match="owner/name"):
158
+ configure(project="Example", package="example", repository="github.com/owner/name")
159
+
160
+
161
+ def test_minimal_profile_disables_gallery():
162
+ config = configure(project="Example", package="example", profile="minimal")
163
+
164
+ assert "sphinx_gallery.gen_gallery" not in config["extensions"]
165
+ assert "sphinx_gallery_conf" not in config
166
+
167
+
168
+ def test_missing_distribution_warns_when_preferred():
169
+ with pytest.warns(RuntimeWarning, match="not installed"):
170
+ configure(project="Example", package="missing_example", prefer_installed_package=True)
171
+
172
+
173
+ def test_missing_distribution_version_warns():
174
+ with pytest.warns(RuntimeWarning, match="using 'latest'"):
175
+ config = configure(project="Example", package="missing_example")
176
+
177
+ assert config["version"] == "latest"
178
+
179
+
132
180
  def test_version_can_be_resolved_from_the_environment(monkeypatch):
133
181
  monkeypatch.setenv("tag", "v2.3.4")
134
182
 
File without changes