Skip to content

Commit fe4d2c2

Browse files
add option to dynamically override configuration (#36)
* add option to dynamically override configuration Signed-off-by: Martin Kloesch <martin.kloesch@gmail.com> * fix formatting errors in documentation Signed-off-by: Martin Kloesch <martin.kloesch@gmail.com> --------- Signed-off-by: Martin Kloesch <martin.kloesch@gmail.com>
1 parent 3a42ca4 commit fe4d2c2

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

README.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ the recommended approach is to explicitly assign the name:
178178
179179
This will prevent warnings from linters etc., but is not necessary for Sphinx to see the configuration.
180180

181+
Additionally the ``SphinxConfig`` class takes an optional parameter ``config_overrides`` that
182+
can be used to dynamically update values from ``pyproject.toml``. This can be helpful for setting
183+
dynamic values like ``version``.
184+
185+
.. code-block:: python3
186+
187+
# conf.py
188+
from sphinx_pyproject import SphinxConfig
189+
190+
from myproject import __version__ as myproject_version
191+
192+
config = SphinxConfig("../pyproject.toml", globalns=globals(), config_overrides = {"version": myproject_version})
193+
181194
182195
Configuration
183196
^^^^^^^^^^^^^^^

sphinx_pyproject/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ class SphinxConfig(Mapping[str, Any]):
5858
:no-default globalns:
5959
:param style: Either ``pep621`` (default), or ``poetry`` to read configuration from the ``[tool.poetry]`` table.
6060
:no-default style:
61-
61+
:param config_overrides: Custom configuration overrides.
62+
This parameter can be used to dynamically update values from ``pyproject.toml``.
63+
This can be used to patch dynamic values like ``version``.
64+
By default, or if explicitly :py:obj:`None`, no config updates are performed.
65+
:no-default config_overrides:
6266
6367
.. autosummary-widths:: 1/4
6468
"""
@@ -127,6 +131,7 @@ def __init__(
127131
*,
128132
globalns: Optional[MutableMapping] = None,
129133
style: str = "pep621",
134+
config_overrides: Optional[MutableMapping] = None,
130135
):
131136

132137
pyproject_file = PathPlus(pyproject_file).abspath()
@@ -140,6 +145,9 @@ def __init__(
140145
namespace = parser_cls.get_namespace(pyproject_file, config)
141146
pep621_config = parser_cls().parse(namespace)
142147

148+
for key, value in (config_overrides or {}).items():
149+
pep621_config[key] = value
150+
143151
for key in ("name", "version", "description"):
144152
if key not in pep621_config:
145153
raise BadConfigError(

tests/test_sphinx_pyproject.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,15 @@ def test_invalid_style(tmp_pathplus: PathPlus):
208208
err = "'style' argument must be one of: pep621, poetry"
209209
with pytest.raises(ValueError, match=err):
210210
SphinxConfig(tmp_pathplus / "pyproject.toml", style="other")
211+
212+
213+
def test_config_overrides(tmp_pathplus: PathPlus):
214+
(tmp_pathplus / "pyproject.toml").write_text(MINIMUM)
215+
config_overrides: Dict[str, Any] = {"version": "3.2.1"}
216+
217+
config = SphinxConfig(tmp_pathplus / "pyproject.toml", config_overrides=config_overrides)
218+
219+
assert config.name == "foo"
220+
assert config.author == "Dominic Davis-Foster"
221+
assert config.version == "3.2.1"
222+
assert config.description == "Description"

0 commit comments

Comments
 (0)