Skip to content

Commit 88c9438

Browse files
0.12 (#11)
1 parent ef5df93 commit 88c9438

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ This code will be executed
3636
```
3737

3838
# Changelog
39+
#### 0.12 (2024-01-09)
40+
- Error when providing invalid options
41+
3942
#### 0.11 (2024-01-09)
4043
- Updated CI and ruff fixes
4144

src/sphinx_exec_code/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.11'
1+
__version__ = '0.12'

src/sphinx_exec_code/sphinx_exec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sphinx_exec_code.code_exec import CodeExceptionError, execute_code
1111
from sphinx_exec_code.code_format import VisibilityMarkerError, get_show_exec_code
1212
from sphinx_exec_code.configuration import EXAMPLE_DIR
13-
from sphinx_exec_code.sphinx_spec import SpecCode, SpecOutput, SphinxSpecBase, build_spec
13+
from sphinx_exec_code.sphinx_spec import SphinxSpecBase, build_spec, get_specs
1414

1515

1616
def create_literal_block(objs: list, code: str, spec: SphinxSpecBase):
@@ -83,7 +83,7 @@ def _run(self) -> list:
8383
file = Path(raw_file)
8484
line = self._get_code_line(raw_line, content)
8585

86-
code_spec = SpecCode.from_options(self.options)
86+
code_spec, output_spec = get_specs(self.options)
8787

8888
# Read from example files
8989
if code_spec.filename:
@@ -115,5 +115,5 @@ def _run(self) -> list:
115115
raise ExtensionError(msg) from None
116116

117117
# Show the output from the code execution
118-
create_literal_block(output, code_results, spec=SpecOutput.from_options(self.options))
118+
create_literal_block(output, code_results, spec=output_spec)
119119
return output

src/sphinx_exec_code/sphinx_spec.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable, ClassVar, Dict
1+
from typing import Any, Callable, ClassVar, Dict, Tuple
22

33
from docutils.parsers.rst import directives # type: ignore
44

@@ -28,6 +28,7 @@ def from_options(cls, options: Dict[str, Any]) -> 'SphinxSpecBase':
2828
if not val:
2929
val = cls.defaults[name]
3030
opts[name] = val
31+
3132
return cls(**opts)
3233

3334
@classmethod
@@ -44,6 +45,20 @@ def build_spec() -> Dict[str, Callable[[Any], Any]]:
4445
return spec
4546

4647

48+
def get_specs(options: Dict[str, Any]) -> Tuple['SpecCode', 'SpecOutput']:
49+
supported = set(SpecCode.aliases) | set(SpecOutput.aliases)
50+
invalid = set(options) - supported
51+
52+
if invalid:
53+
msg = (
54+
f'Invalid option{"s" if len(invalid) != 1 else ""}: '
55+
f'{", ".join(sorted(map(str, invalid)))}! Supported: {", ".join(sorted(map(str, supported)))}'
56+
)
57+
raise ValueError(msg)
58+
59+
return SpecCode.from_options(options), SpecOutput.from_options(options)
60+
61+
4762
class SpecCode(SphinxSpecBase):
4863
aliases: ClassVar = {
4964
'hide_code': 'hide',

tests/test_sphinx_spec.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from docutils.parsers.rst import directives
66

7-
from sphinx_exec_code.sphinx_spec import SpecCode, SpecOutput, SphinxSpecBase, build_spec
7+
from sphinx_exec_code.sphinx_spec import SpecCode, SpecOutput, SphinxSpecBase, build_spec, get_specs
88

99

1010
def test_aliases_unique():
@@ -42,12 +42,12 @@ def test_build_spec_code():
4242

4343

4444
def test_spec_code():
45-
obj = SpecCode.from_options({'linenos': None, 'caption': 'my_header'})
45+
obj = SpecCode.from_options({'linenos': None, 'caption': 'my_header', 'filename': 'filename'})
4646
assert obj.caption == 'my_header'
4747
assert obj.language == 'python'
4848
assert obj.linenos is True
4949
assert obj.hide is False
50-
assert obj.filename == ''
50+
assert obj.filename == 'filename'
5151

5252

5353
def test_spec_output():
@@ -56,3 +56,20 @@ def test_spec_output():
5656
assert obj.language == 'none'
5757
assert obj.linenos is False
5858
assert obj.hide is True
59+
60+
61+
def test_invalid_options():
62+
with pytest.raises(ValueError) as e: # noqa: PT011
63+
get_specs({'hide-output': None})
64+
65+
assert str(e.value) == ('Invalid option: hide-output! '
66+
'Supported: caption, caption_output, filename, hide_code, hide_output, '
67+
'language, language_output, linenos, linenos_output')
68+
69+
70+
with pytest.raises(ValueError) as e: # noqa: PT011
71+
get_specs({'hide-output': None, 'language_output': 'asdf', 'caption-output': 'test'})
72+
73+
assert str(e.value) == ('Invalid options: caption-output, hide-output! '
74+
'Supported: caption, caption_output, filename, hide_code, hide_output, '
75+
'language, language_output, linenos, linenos_output')

0 commit comments

Comments
 (0)