Skip to content

Commit

Permalink
0.12 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 authored Jan 9, 2024
1 parent ef5df93 commit 88c9438
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ This code will be executed
```

# Changelog
#### 0.12 (2024-01-09)
- Error when providing invalid options

#### 0.11 (2024-01-09)
- Updated CI and ruff fixes

Expand Down
2 changes: 1 addition & 1 deletion src/sphinx_exec_code/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.11'
__version__ = '0.12'
6 changes: 3 additions & 3 deletions src/sphinx_exec_code/sphinx_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sphinx_exec_code.code_exec import CodeExceptionError, execute_code
from sphinx_exec_code.code_format import VisibilityMarkerError, get_show_exec_code
from sphinx_exec_code.configuration import EXAMPLE_DIR
from sphinx_exec_code.sphinx_spec import SpecCode, SpecOutput, SphinxSpecBase, build_spec
from sphinx_exec_code.sphinx_spec import SphinxSpecBase, build_spec, get_specs


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

code_spec = SpecCode.from_options(self.options)
code_spec, output_spec = get_specs(self.options)

# Read from example files
if code_spec.filename:
Expand Down Expand Up @@ -115,5 +115,5 @@ def _run(self) -> list:
raise ExtensionError(msg) from None

# Show the output from the code execution
create_literal_block(output, code_results, spec=SpecOutput.from_options(self.options))
create_literal_block(output, code_results, spec=output_spec)
return output
17 changes: 16 additions & 1 deletion src/sphinx_exec_code/sphinx_spec.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, ClassVar, Dict
from typing import Any, Callable, ClassVar, Dict, Tuple

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

Expand Down Expand Up @@ -28,6 +28,7 @@ def from_options(cls, options: Dict[str, Any]) -> 'SphinxSpecBase':
if not val:
val = cls.defaults[name]
opts[name] = val

return cls(**opts)

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


def get_specs(options: Dict[str, Any]) -> Tuple['SpecCode', 'SpecOutput']:
supported = set(SpecCode.aliases) | set(SpecOutput.aliases)
invalid = set(options) - supported

if invalid:
msg = (
f'Invalid option{"s" if len(invalid) != 1 else ""}: '
f'{", ".join(sorted(map(str, invalid)))}! Supported: {", ".join(sorted(map(str, supported)))}'
)
raise ValueError(msg)

return SpecCode.from_options(options), SpecOutput.from_options(options)


class SpecCode(SphinxSpecBase):
aliases: ClassVar = {
'hide_code': 'hide',
Expand Down
23 changes: 20 additions & 3 deletions tests/test_sphinx_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from docutils.parsers.rst import directives

from sphinx_exec_code.sphinx_spec import SpecCode, SpecOutput, SphinxSpecBase, build_spec
from sphinx_exec_code.sphinx_spec import SpecCode, SpecOutput, SphinxSpecBase, build_spec, get_specs


def test_aliases_unique():
Expand Down Expand Up @@ -42,12 +42,12 @@ def test_build_spec_code():


def test_spec_code():
obj = SpecCode.from_options({'linenos': None, 'caption': 'my_header'})
obj = SpecCode.from_options({'linenos': None, 'caption': 'my_header', 'filename': 'filename'})
assert obj.caption == 'my_header'
assert obj.language == 'python'
assert obj.linenos is True
assert obj.hide is False
assert obj.filename == ''
assert obj.filename == 'filename'


def test_spec_output():
Expand All @@ -56,3 +56,20 @@ def test_spec_output():
assert obj.language == 'none'
assert obj.linenos is False
assert obj.hide is True


def test_invalid_options():
with pytest.raises(ValueError) as e: # noqa: PT011
get_specs({'hide-output': None})

assert str(e.value) == ('Invalid option: hide-output! '
'Supported: caption, caption_output, filename, hide_code, hide_output, '
'language, language_output, linenos, linenos_output')


with pytest.raises(ValueError) as e: # noqa: PT011
get_specs({'hide-output': None, 'language_output': 'asdf', 'caption-output': 'test'})

assert str(e.value) == ('Invalid options: caption-output, hide-output! '
'Supported: caption, caption_output, filename, hide_code, hide_output, '
'language, language_output, linenos, linenos_output')

0 comments on commit 88c9438

Please sign in to comment.