Skip to content

Commit

Permalink
fix version handling
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Dec 9, 2023
1 parent db52404 commit cefa60f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 50 deletions.
26 changes: 16 additions & 10 deletions jupyter_sphinx/_version.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
version_info = (0, 4, 0, "final")
"""
store the current version info of the project.
_specifier_ = {"alpha": "a", "beta": "b", "candidate": "rc", "final": ""}
"""
import re
from typing import List

__version__ = "{}.{}.{}{}".format(
version_info[0],
version_info[1],
version_info[2],
""
if version_info[3] == "final"
else _specifier_[version_info[3]] + str(version_info[4]),
)
# Version string must appear intact for automatic versioning
__version__ = "0.4.0"

# Build up version_info tuple for backwards compatibility
pattern = r"(?P<major>\d+).(?P<minor>\d+).(?P<patch>\d+)(?P<rest>.*)"
match = re.match(pattern, __version__)
assert match is not None
parts: List[object] = [int(match[part]) for part in ["major", "minor", "patch"]]
if match["rest"]:
parts.append(match["rest"])
version_info = tuple(parts)
2 changes: 1 addition & 1 deletion jupyter_sphinx/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ class CombineCellInputOutput(SphinxTransform):
def apply(self):
moved_outputs = set()

for cell_node in self.document.traverse(JupyterCellNode):
for cell_node in self.document.findall(JupyterCellNode):
if not cell_node.attributes["execute"]:
if not cell_node.attributes["hide_code"]:
# Cell came from jupyter-input
Expand Down
6 changes: 3 additions & 3 deletions jupyter_sphinx/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ def apply(self):
linenos_config = self.config.jupyter_sphinx_linenos
continue_linenos = self.config.jupyter_sphinx_continue_linenos
# Check if we have anything to execute.
if not doctree.traverse(JupyterCellNode):
if not doctree.findall(JupyterCellNode):
return

if thebe_config:
# Add the button at the bottom if it is not present
if not doctree.traverse(ThebeButtonNode):
if not doctree.findall(ThebeButtonNode):
doctree.append(ThebeButtonNode())

add_thebelab_library(doctree, self.env)
Expand All @@ -140,7 +140,7 @@ def apply(self):
jupyter_nodes = (JupyterCellNode, JupyterKernelNode)
nodes_by_notebook = split_on(
lambda n: isinstance(n, JupyterKernelNode),
doctree.traverse(lambda n: isinstance(n, jupyter_nodes)),
doctree.findall(lambda n: isinstance(n, jupyter_nodes)),
)

for first, *nodes in nodes_by_notebook:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ doc = [
]

[tool.hatch.version]
path = "jupyter_sphinx/__init__.py"
path = "jupyter_sphinx/_version.py"

[tool.hatch.build.targets.sdist]
include = [
Expand All @@ -61,7 +61,7 @@ build = "cd doc; make html-strict"
[tool.hatch.envs.test]
features = ["test"]
[tool.hatch.envs.test.scripts]
test = "python -m bash_kernel.install && python -m pytest -vv {args}"
test = "python -m bash_kernel.install && JUPYTER_PLATFORM_DIRS=1 python -m pytest -vv {args}"
nowarn = "test -W default {args}"

[tool.pytest.ini_options]
Expand Down
68 changes: 34 additions & 34 deletions tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_basic(doctree, buildername):
2 + 2
"""
tree = doctree(source, buildername=buildername)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, celloutput) = cell.children
assert not cell.attributes["code_below"]
assert not cell.attributes["hide_code"]
Expand All @@ -105,7 +105,7 @@ def test_hide_output(doctree):
2 + 2
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, celloutput) = cell.children
assert cell.attributes["hide_output"]
assert len(celloutput.children) == 0
Expand All @@ -120,7 +120,7 @@ def test_hide_code(doctree):
2 + 2
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(celloutput,) = cell.children
assert cell.attributes["hide_code"]
assert len(cell.children) == 1
Expand All @@ -135,7 +135,7 @@ def test_code_below(doctree):
2 + 2
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(celloutput, cellinput) = cell.children
assert cell.attributes["code_below"]
assert cellinput.children[0].astext().strip() == "2 + 2"
Expand All @@ -150,7 +150,7 @@ def test_linenos(doctree):
2 + 2
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, celloutput) = cell.children
assert cellinput.children[0]["linenos"]
assert len(cell.children) == 2
Expand All @@ -164,7 +164,7 @@ def test_linenos(doctree):
2 + 2
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(celloutput, cellinput) = cell.children
assert cellinput.children[0]["linenos"]

Expand All @@ -176,7 +176,7 @@ def test_linenos_conf_option(doctree):
2 + 2
"""
tree = doctree(source, config="jupyter_sphinx_linenos = True")
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, celloutput) = cell.children
assert cellinput.children[0].attributes["linenos"]
assert "highlight_args" not in cellinput.children[0].attributes
Expand All @@ -194,7 +194,7 @@ def test_continue_linenos_conf_option(doctree):
"""

tree = doctree(source, config="jupyter_sphinx_continue_linenos = True")
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, celloutput) = cell.children
assert not cellinput.children[0].attributes["linenos"]
assert cellinput.children[0].astext().strip() == "2 + 2"
Expand All @@ -218,7 +218,7 @@ def test_continue_linenos_conf_option(doctree):
"jupyter_sphinx_continue_linenos = True",
)

cell0, cell1 = tree.traverse(JupyterCellNode)
cell0, cell1 = tree.findall(JupyterCellNode)
(cellinput0, celloutput0) = cell0.children
(cellinput1, celloutput1) = cell1.children
assert cellinput0.children[0].attributes["linenos"]
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_continue_linenos_conf_option(doctree):
config="jupyter_sphinx_linenos = True\n"
"jupyter_sphinx_continue_linenos = True",
)
cell0, cell1 = tree.traverse(JupyterCellNode)
cell0, cell1 = tree.findall(JupyterCellNode)
(cellinput0, celloutput0) = cell0.children
(cellinput1, celloutput1) = cell1.children
assert cellinput0.children[0].attributes["highlight_args"]["linenostart"] == 7
Expand Down Expand Up @@ -282,7 +282,7 @@ def test_emphasize_lines(doctree):
5 + 5
"""
tree = doctree(source)
cell0, cell1 = tree.traverse(JupyterCellNode)
cell0, cell1 = tree.findall(JupyterCellNode)

assert cell0.attributes["emphasize_lines"] == [1, 3, 4, 5]
assert cell1.attributes["emphasize_lines"] == [2, 4]
Expand All @@ -300,7 +300,7 @@ def test_execution_environment_carries_over(doctree):
a
"""
tree = doctree(source)
_, cell1 = tree.traverse(JupyterCellNode)
_, cell1 = tree.findall(JupyterCellNode)
(_, celloutput1) = cell1.children
assert celloutput1.children[0].astext().strip() == "2"

Expand All @@ -321,7 +321,7 @@ def test_kernel_restart(doctree):
a
"""
tree = doctree(source)
_, cell1 = tree.traverse(JupyterCellNode)
_, cell1 = tree.findall(JupyterCellNode)
(_, celloutput1) = cell1.children
assert "NameError" in celloutput1.children[0].astext()

Expand All @@ -342,7 +342,7 @@ def test_raises(doctree):
raise ValueError()
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(_, celloutput) = cell.children
assert "ValueError" in celloutput.children[0].astext()

Expand All @@ -353,7 +353,7 @@ def test_raises(doctree):
raise ValueError()
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(_, celloutput) = cell.children
assert "ValueError" in celloutput.children[0].astext()

Expand All @@ -366,8 +366,8 @@ def test_widgets(doctree):
ipywidgets.Button()
"""
tree = doctree(source)
assert len(list(tree.traverse(JupyterWidgetViewNode))) == 1
assert len(list(tree.traverse(JupyterWidgetStateNode))) == 1
assert len(list(tree.findall(JupyterWidgetViewNode))) == 1
assert len(list(tree.findall(JupyterWidgetStateNode))) == 1


def test_javascript(doctree):
Expand All @@ -378,7 +378,7 @@ def test_javascript(doctree):
Javascript('window.alert("Hello world!")')
"""
tree = doctree(source)
(node,) = list(tree.traverse(raw))
(node,) = list(tree.findall(raw))
(text,) = node.children
assert "world" in text

Expand All @@ -390,7 +390,7 @@ def test_stdout(doctree):
print('hello world')
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(_, celloutput) = cell.children
assert len(cell.children) == 2
assert celloutput.children[0].astext().strip() == "hello world"
Expand All @@ -406,7 +406,7 @@ def test_stderr(doctree):

tree, _, warnings = doctree(source, return_all=True)
assert "hello world" in warnings
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(_, celloutput) = cell.children
assert len(celloutput) == 0 # no output

Expand All @@ -418,7 +418,7 @@ def test_stderr(doctree):
print('hello world', file=sys.stderr)
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(_, celloutput) = cell.children
assert len(cell.children) == 2
assert "stderr" in celloutput.children[0].attributes["classes"]
Expand All @@ -436,7 +436,7 @@ def test_thebe_hide_output(doctree):
2 + 2
"""
tree = doctree(source, thebe_config)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, celloutput) = cell.children
assert cell.attributes["hide_output"]
assert len(celloutput.children) == 0
Expand All @@ -455,7 +455,7 @@ def test_thebe_hide_code(doctree):
2 + 2
"""
tree = doctree(source, thebe_config)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, celloutput) = cell.children
assert cell.attributes["hide_code"]
assert len(cell.children) == 2
Expand All @@ -480,7 +480,7 @@ def test_thebe_code_below(doctree):
2 + 2
"""
tree = doctree(source, thebe_config)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, celloutput) = cell.children
assert cell.attributes["code_below"]

Expand All @@ -504,7 +504,7 @@ def test_thebe_button_auto(doctree):
1 + 1
"""
tree = doctree(source, config=config)
assert len(tree.traverse(ThebeButtonNode)) == 1
assert len(list(tree.findall(ThebeButtonNode))) == 1


def test_thebe_button_manual(doctree):
Expand All @@ -517,14 +517,14 @@ def test_thebe_button_manual(doctree):
.. thebe-button::
"""
tree = doctree(source, config)
assert len(tree.traverse(ThebeButtonNode)) == 1
assert len(list(tree.findall(ThebeButtonNode))) == 1


def test_thebe_button_none(doctree):
config = 'jupyter_sphinx_thebelab_config = {"dummy": True}'
source = "No Jupyter cells"
tree = doctree(source, config)
assert len(tree.traverse(ThebeButtonNode)) == 0
assert len(list(tree.findall(ThebeButtonNode))) == 0


def test_latex(doctree):
Expand All @@ -539,9 +539,9 @@ def test_latex(doctree):

for start, end in delimiter_pairs:
tree = doctree(source.format(start, end))
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(_, celloutput) = cell.children
assert next(iter(celloutput.traverse(math_block))).astext() == r"\int"
assert next(iter(celloutput.findall(math_block))).astext() == r"\int"


def test_cell_output_to_nodes(doctree):
Expand Down Expand Up @@ -582,7 +582,7 @@ def test_cell_output_to_nodes(doctree):
for index, cell in enumerate(cells):
cell = from_dict(cell)
(output_node,) = cell_output_to_nodes(cell["outputs"], True, output_dir, None)
(image_node,) = output_node.traverse(image)
(image_node,) = output_node.findall(image)
assert image_node.attributes["uri"] == img_locs[index]

# Testing inline functionality
Expand Down Expand Up @@ -680,7 +680,7 @@ def test_input_cell(doctree):
2 + 2
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, empty) = cell.children
assert cell.attributes["hide_output"] is True
assert cellinput.children[0].attributes["linenos"] is False
Expand All @@ -696,7 +696,7 @@ def test_input_cell_linenos(doctree):
2 + 2
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(cellinput, empty) = cell.children
assert cell.attributes["hide_output"] is True
assert cellinput.children[0].attributes["linenos"] is True
Expand All @@ -715,7 +715,7 @@ def test_output_cell(doctree):
4
"""
tree = doctree(source)
(cell,) = tree.traverse(JupyterCellNode)
(cell,) = tree.findall(JupyterCellNode)
(
cellinput,
celloutput,
Expand Down Expand Up @@ -749,7 +749,7 @@ def test_multiple_directives(doctree):
5
"""
tree = doctree(source)
(ex, jin) = tree.traverse(JupyterCellNode)
(ex, jin) = tree.findall(JupyterCellNode)
(ex_in, ex_out) = ex.children
(jin_in, jin_out) = jin.children
assert ex_in.children[0].astext().strip() == "2 + 2"
Expand Down

0 comments on commit cefa60f

Please sign in to comment.