Skip to content

Commit

Permalink
0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Jan 9, 2024
1 parent cc8e2ec commit 93190ac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
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
23 changes: 15 additions & 8 deletions 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 @@ -29,13 +29,6 @@ def from_options(cls, options: Dict[str, Any]) -> 'SphinxSpecBase':
val = cls.defaults[name]
opts[name] = val

if left := set(options) - set(cls.aliases):
msg = (
f'Invalid option{"s" if len(left) != 1 else ""}: '
f'{", ".join(sorted(map(str, left)))}! Supported: {", ".join(sorted(map(str, cls.aliases)))}'
)
raise ValueError(msg)

return cls(**opts)

@classmethod
Expand All @@ -52,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
12 changes: 7 additions & 5 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 @@ -60,14 +60,16 @@ def test_spec_output():

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

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


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

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

0 comments on commit 93190ac

Please sign in to comment.