MartinPdeS-docs 0.1.4__tar.gz → 0.2.1__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.1.4
3
+ Version: 0.2.1
4
4
  Summary: Shared Sphinx configuration for MartinPdeS scientific Python packages.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -39,3 +39,25 @@ globals().update(
39
39
 
40
40
  Project-specific values can be assigned below this call to override any
41
41
  shared default.
42
+
43
+ For a multi-section Sphinx-Gallery, pass the section names instead of
44
+ repeating the directory mappings:
45
+
46
+ ```python
47
+ globals().update(
48
+ configure(
49
+ project="FlowCyPy",
50
+ package="FlowCyPy",
51
+ repository="MartinPdeS/FlowCyPy",
52
+ gallery_sections=["tutorials", "studies", "validation"],
53
+ gallery_default_thumb="_static/gallery-default.png",
54
+ )
55
+ )
56
+ ```
57
+
58
+ Use `gallery_options` for project-specific Sphinx-Gallery settings such as an
59
+ additional image scraper or an example error policy.
60
+
61
+ For packages with compiled extensions, set `prefer_installed_package=True` so
62
+ documentation builds import the installed wheel instead of the source
63
+ checkout.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: MartinPdeS-docs
3
- Version: 0.1.4
3
+ Version: 0.2.1
4
4
  Summary: Shared Sphinx configuration for MartinPdeS scientific Python packages.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -39,3 +39,25 @@ globals().update(
39
39
 
40
40
  Project-specific values can be assigned below this call to override any
41
41
  shared default.
42
+
43
+ For a multi-section Sphinx-Gallery, pass the section names instead of
44
+ repeating the directory mappings:
45
+
46
+ ```python
47
+ globals().update(
48
+ configure(
49
+ project="FlowCyPy",
50
+ package="FlowCyPy",
51
+ repository="MartinPdeS/FlowCyPy",
52
+ gallery_sections=["tutorials", "studies", "validation"],
53
+ gallery_default_thumb="_static/gallery-default.png",
54
+ )
55
+ )
56
+ ```
57
+
58
+ Use `gallery_options` for project-specific Sphinx-Gallery settings such as an
59
+ additional image scraper or an example error policy.
60
+
61
+ For packages with compiled extensions, set `prefer_installed_package=True` so
62
+ documentation builds import the installed wheel instead of the source
63
+ checkout.
@@ -27,3 +27,25 @@ globals().update(
27
27
 
28
28
  Project-specific values can be assigned below this call to override any
29
29
  shared default.
30
+
31
+ For a multi-section Sphinx-Gallery, pass the section names instead of
32
+ repeating the directory mappings:
33
+
34
+ ```python
35
+ globals().update(
36
+ configure(
37
+ project="FlowCyPy",
38
+ package="FlowCyPy",
39
+ repository="MartinPdeS/FlowCyPy",
40
+ gallery_sections=["tutorials", "studies", "validation"],
41
+ gallery_default_thumb="_static/gallery-default.png",
42
+ )
43
+ )
44
+ ```
45
+
46
+ Use `gallery_options` for project-specific Sphinx-Gallery settings such as an
47
+ additional image scraper or an example error policy.
48
+
49
+ For packages with compiled extensions, set `prefer_installed_package=True` so
50
+ documentation builds import the installed wheel instead of the source
51
+ checkout.
@@ -1,6 +1,13 @@
1
1
  """Configurable defaults for the shared project documentation sites."""
2
2
 
3
3
  import os
4
+ from collections.abc import Mapping, Sequence
5
+ import sys
6
+ from importlib.metadata import (
7
+ PackageNotFoundError,
8
+ distribution,
9
+ version as distribution_version,
10
+ )
4
11
  from pathlib import Path
5
12
 
6
13
 
@@ -18,6 +25,32 @@ def _raster_thumbnail_path(logo: str, favicon: str) -> str | None:
18
25
  return None
19
26
 
20
27
 
28
+ def _resolve_version(package: str, version: str | None) -> str:
29
+ """Resolve the documentation version without importing the package."""
30
+ if version is not None:
31
+ return version
32
+
33
+ environment_version = os.getenv("tag")
34
+ if environment_version:
35
+ return environment_version
36
+
37
+ try:
38
+ return distribution_version(package)
39
+ except PackageNotFoundError:
40
+ return "latest"
41
+
42
+
43
+ def _prefer_installed_package(package: str) -> None:
44
+ """Put an installed distribution before the documentation checkout."""
45
+ try:
46
+ installed_site = str(distribution(package).locate_file(""))
47
+ except PackageNotFoundError:
48
+ return
49
+
50
+ sys.path[:] = [entry for entry in sys.path if entry != installed_site]
51
+ sys.path.insert(0, installed_site)
52
+
53
+
21
54
  def configure(
22
55
  *,
23
56
  project: str,
@@ -29,7 +62,11 @@ def configure(
29
62
  extensions: list[str] | None = None,
30
63
  html_theme_options: dict | None = None,
31
64
  sphinx_gallery_conf: dict | None = None,
32
- version: str = "latest",
65
+ gallery_sections: Sequence[str] | None = None,
66
+ gallery_default_thumb: str | None = None,
67
+ gallery_options: Mapping[str, object] | None = None,
68
+ prefer_installed_package: bool = False,
69
+ version: str | None = None,
33
70
  source_suffix: str = ".rst",
34
71
  master_doc: str = "index",
35
72
  ) -> dict:
@@ -37,13 +74,24 @@ def configure(
37
74
 
38
75
  In a project's ``conf.py`` use ``globals().update(configure(...))`` and
39
76
  keep project-specific settings below that call so they remain easy to
40
- override.
77
+ override. Pass ``gallery_sections`` when examples are split into named
78
+ directories; use ``gallery_default_thumb`` for a project-specific raster
79
+ fallback; use ``gallery_options`` for less common Sphinx-Gallery settings;
80
+ and set ``prefer_installed_package`` for projects whose documentation must
81
+ import a built wheel instead of the source checkout.
41
82
  """
83
+ if prefer_installed_package:
84
+ _prefer_installed_package(package)
85
+
86
+ resolved_version = _resolve_version(package, version)
87
+
42
88
  default_extensions = [
43
89
  "sphinx.ext.autodoc",
44
90
  "sphinx.ext.autosummary",
45
91
  "sphinx.ext.intersphinx",
92
+ "sphinx.ext.mathjax",
46
93
  "sphinx.ext.napoleon",
94
+ "sphinx.ext.autosectionlabel",
47
95
  "sphinx.ext.viewcode",
48
96
  "sphinx.ext.todo",
49
97
  "sphinx_design",
@@ -85,7 +133,7 @@ def configure(
85
133
  ]
86
134
  options["switcher"] = {
87
135
  "json_url": f"https://raw.githubusercontent.com/{repository}/documentation_page/version_switcher.json",
88
- "version_match": version,
136
+ "version_match": resolved_version,
89
137
  }
90
138
 
91
139
  html_context = {}
@@ -111,6 +159,8 @@ def configure(
111
159
  "plot_gallery": True,
112
160
  "thumbnail_size": [600, 600],
113
161
  "download_all_examples": False,
162
+ "reset_modules_order": "after",
163
+ "within_subsection_order": "FileNameSortKey",
114
164
  # Use the project's favicon for examples that do not create a figure.
115
165
  # The path is interpreted relative to the documentation source tree,
116
166
  # just like ``html_favicon``.
@@ -120,6 +170,14 @@ def configure(
120
170
  "capture_repr": ("_repr_html_", "__repr__"),
121
171
  "nested_sections": True,
122
172
  }
173
+ if gallery_sections:
174
+ sections = list(gallery_sections)
175
+ gallery["examples_dirs"] = [f"../examples/{section}" for section in sections]
176
+ gallery["gallery_dirs"] = [f"gallery/{section}" for section in sections]
177
+ if gallery_default_thumb is not None:
178
+ gallery["default_thumb_file"] = gallery_default_thumb
179
+ if gallery_options:
180
+ gallery.update(gallery_options)
123
181
  if sphinx_gallery_conf:
124
182
  gallery.update(sphinx_gallery_conf)
125
183
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "MartinPdeS-docs"
7
- version = "0.1.4"
7
+ version = "0.2.1"
8
8
  description = "Shared Sphinx configuration for MartinPdeS scientific Python packages."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -0,0 +1,98 @@
1
+ from importlib.metadata import distribution
2
+ import sys
3
+
4
+ from martinpdes_docs import configure
5
+
6
+
7
+ def test_configuration_has_project_overrides():
8
+ config = configure(project="Example", package="example", repository="MartinPdeS/Example")
9
+
10
+ assert config["project"] == "Example"
11
+ assert config["package"] == "example"
12
+ assert config["html_logo"] == "_static/logo.svg"
13
+ assert config["html_static_path"][0] == "_static"
14
+ assert config["html_static_path"][1].endswith("martinpdes_docs/static")
15
+ assert config["templates_path"] == ["_templates"]
16
+ assert config["html_css_files"] == ["default.css", "martinpdes_layout.css"]
17
+ assert config["epub_exclude_files"] == ["search.html"]
18
+ assert config["sphinx_gallery_conf"]["image_scrapers"] == ("matplotlib",)
19
+ assert config["sphinx_gallery_conf"]["thumbnail_size"] == [600, 600]
20
+ assert config["sphinx_gallery_conf"]["default_thumb_file"] is None
21
+ assert config["sphinx_gallery_conf"]["reset_modules_order"] == "after"
22
+ assert config["sphinx_gallery_conf"]["within_subsection_order"] == "FileNameSortKey"
23
+
24
+ sections = configure(
25
+ project="Example",
26
+ package="example",
27
+ gallery_sections=["tutorials", "validation"],
28
+ gallery_default_thumb="_static/gallery-default.png",
29
+ )
30
+ assert sections["sphinx_gallery_conf"]["examples_dirs"] == [
31
+ "../examples/tutorials",
32
+ "../examples/validation",
33
+ ]
34
+ assert sections["sphinx_gallery_conf"]["gallery_dirs"] == [
35
+ "gallery/tutorials",
36
+ "gallery/validation",
37
+ ]
38
+ assert sections["sphinx_gallery_conf"]["default_thumb_file"] == (
39
+ "_static/gallery-default.png"
40
+ )
41
+
42
+ raster = configure(
43
+ project="Example",
44
+ package="example",
45
+ logo="../images/logo.png",
46
+ favicon="_static/favicon.svg",
47
+ )
48
+ assert raster["sphinx_gallery_conf"]["default_thumb_file"] == "../images/logo.png"
49
+ assert "github_url" not in config["html_theme_options"]
50
+ assert config["html_context"]["github_repo"] == "Example"
51
+
52
+
53
+ def test_custom_extensions_and_theme_options_are_preserved():
54
+ config = configure(
55
+ project="Example",
56
+ package="example",
57
+ extensions=["custom.extension"],
58
+ html_theme_options={"announcement": "Hello"},
59
+ )
60
+
61
+ assert config["extensions"] == ["custom.extension"]
62
+ assert config["html_theme_options"]["announcement"] == "Hello"
63
+
64
+
65
+ def test_gallery_options_override_shared_defaults():
66
+ config = configure(
67
+ project="Example",
68
+ package="example",
69
+ gallery_options={"image_scrapers": ("matplotlib", "pyvista")},
70
+ )
71
+
72
+ assert config["sphinx_gallery_conf"]["image_scrapers"] == ("matplotlib", "pyvista")
73
+
74
+
75
+ def test_version_can_be_resolved_from_the_environment(monkeypatch):
76
+ monkeypatch.setenv("tag", "v2.3.4")
77
+
78
+ config = configure(
79
+ project="Example",
80
+ package="example",
81
+ repository="MartinPdeS/Example",
82
+ )
83
+
84
+ assert config["html_theme_options"]["switcher"]["version_match"] == "v2.3.4"
85
+
86
+
87
+ def test_installed_package_can_be_preferred(monkeypatch):
88
+ installed_site = str(distribution("MartinPdeS-docs").locate_file(""))
89
+ monkeypatch.setattr(sys, "path", ["checkout", installed_site, "other"])
90
+
91
+ configure(
92
+ project="Example",
93
+ package="MartinPdeS-docs",
94
+ prefer_installed_package=True,
95
+ )
96
+
97
+ assert sys.path[0] == installed_site
98
+ assert sys.path.count(installed_site) == 1
@@ -1,39 +0,0 @@
1
- from martinpdes_docs import configure
2
-
3
-
4
- def test_configuration_has_project_overrides():
5
- config = configure(project="Example", package="example", repository="MartinPdeS/Example")
6
-
7
- assert config["project"] == "Example"
8
- assert config["package"] == "example"
9
- assert config["html_logo"] == "_static/logo.svg"
10
- assert config["html_static_path"][0] == "_static"
11
- assert config["html_static_path"][1].endswith("martinpdes_docs/static")
12
- assert config["templates_path"] == ["_templates"]
13
- assert config["html_css_files"] == ["default.css", "martinpdes_layout.css"]
14
- assert config["epub_exclude_files"] == ["search.html"]
15
- assert config["sphinx_gallery_conf"]["image_scrapers"] == ("matplotlib",)
16
- assert config["sphinx_gallery_conf"]["thumbnail_size"] == [600, 600]
17
- assert config["sphinx_gallery_conf"]["default_thumb_file"] is None
18
-
19
- raster = configure(
20
- project="Example",
21
- package="example",
22
- logo="../images/logo.png",
23
- favicon="_static/favicon.svg",
24
- )
25
- assert raster["sphinx_gallery_conf"]["default_thumb_file"] == "../images/logo.png"
26
- assert "github_url" not in config["html_theme_options"]
27
- assert config["html_context"]["github_repo"] == "Example"
28
-
29
-
30
- def test_custom_extensions_and_theme_options_are_preserved():
31
- config = configure(
32
- project="Example",
33
- package="example",
34
- extensions=["custom.extension"],
35
- html_theme_options={"announcement": "Hello"},
36
- )
37
-
38
- assert config["extensions"] == ["custom.extension"]
39
- assert config["html_theme_options"]["announcement"] == "Hello"