Skip to content

Commit 0d0fe2d

Browse files
committed
add collapse runpython
1 parent 2e80968 commit 0d0fe2d

23 files changed

+3153
-12
lines changed

CHANGELOGS.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Change Logs
44
0.1.0
55
+++++
66

7-
* :pr:`5`: ***
7+
* :pr:`2`: add sphinx extension runpython and collapse

README.rst

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,48 @@ sphinx-runpython: run python code in sphinx
2828

2929
**sphinx-runpython** implements sphinx extensions including one
3030
to execute code and add the output to the documentation.
31-
3231
The library is released on
3332
`pypi/sphinx-runpython <https://pypi.org/project/sphinx-runpython/>`_
3433
and its documentation is published at
3534
`sphinx-runpython
3635
<http://www.xavierdupre.fr/app/sphinx-runpython/helpsphinx/index.html>`_.
36+
37+
epkg
38+
++++
39+
40+
It implements a list of recurring urls in documentation.
41+
42+
**conf.py**
43+
44+
::
45+
46+
epkg_dictionary = {'title': 'url' }
47+
48+
**rst**
49+
50+
::
51+
52+
:epkg:`title` -> `title <url>`_
53+
54+
55+
runpython
56+
+++++++++
57+
58+
Executes code in the documentation and adds it to documentation.
59+
60+
::
61+
62+
.. runpython::
63+
:showcode:
64+
65+
print("python code")
66+
67+
::
68+
69+
<<<
70+
71+
print("python code")
72+
73+
>>>
74+
75+
python code

_doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"sphinx_gallery.gen_gallery",
1616
"matplotlib.sphinxext.plot_directive",
1717
"sphinx_runpython.epkg",
18-
# "sphinx_runpython.runpython",
18+
"sphinx_runpython.runpython",
1919
]
2020

2121
templates_path = ["_templates"]
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import unittest
2+
from pyquickhelper.helpgen import rst2html
3+
from sphinx_runpython.ext_test_case import ExtTestCase, ignore_warnings
4+
from sphinx_runpython.collapse.sphinx_collapse_extension import (
5+
CollapseDirective,
6+
collapse_node,
7+
visit_collapse_node_rst,
8+
depart_collapse_node_rst,
9+
)
10+
11+
tives = [
12+
(
13+
"collapse",
14+
CollapseDirective,
15+
collapse_node,
16+
visit_collapse_node_rst,
17+
depart_collapse_node_rst,
18+
)
19+
]
20+
21+
22+
class TestCollapseExtension(ExtTestCase):
23+
@ignore_warnings(PendingDeprecationWarning)
24+
def test_collapse(self):
25+
content = """
26+
before
27+
28+
.. collapse::
29+
30+
this code shoud appear___
31+
32+
after
33+
""".replace(
34+
" ", ""
35+
)
36+
content = content.replace('u"', '"')
37+
38+
# RST
39+
html = rst2html(content, writer="rst", keep_warnings=True, directives=tives)
40+
41+
t1 = " this code shoud appear___"
42+
if t1 not in html:
43+
raise AssertionError(html)
44+
45+
t1 = ".. collapse::"
46+
if t1 not in html:
47+
raise AssertionError(html)
48+
49+
t1 = " :legend:"
50+
if t1 not in html:
51+
raise AssertionError(html)
52+
53+
t1 = "after"
54+
if t1 not in html:
55+
raise AssertionError(html)
56+
57+
t1 = ".. collapse:: :legend:"
58+
if t1 in html:
59+
raise AssertionError(html)
60+
61+
# HTML
62+
html = rst2html(content, writer="custom", keep_warnings=True, directives=tives)
63+
64+
t1 = "this code shoud appear"
65+
if t1 not in html:
66+
raise AssertionError(html)
67+
68+
t1 = "after"
69+
if t1 not in html:
70+
raise AssertionError(html)
71+
72+
t1 = 'if (x.style.display === "none")'
73+
if t1 not in html:
74+
raise AssertionError(html)
75+
76+
t1 = "b.innerText = 'unhide';"
77+
if t1 not in html:
78+
raise AssertionError(html)
79+
80+
@ignore_warnings(PendingDeprecationWarning)
81+
def test_collapse_legend(self):
82+
content = """
83+
before
84+
85+
.. collapse::
86+
:legend: ABC/abcd
87+
88+
this code shoud appear___
89+
90+
after
91+
""".replace(
92+
" ", ""
93+
)
94+
content = content.replace('u"', '"')
95+
96+
# RST
97+
html = rst2html(content, writer="rst", keep_warnings=True, directives=tives)
98+
99+
t1 = ":legend: ABC/abcd"
100+
if t1 not in html:
101+
raise AssertionError(html)
102+
103+
t1 = ":hide:"
104+
if t1 in html:
105+
raise AssertionError(html)
106+
107+
# HTML
108+
html = rst2html(content, writer="custom", keep_warnings=True, directives=tives)
109+
110+
t1 = "b.innerText = 'abcd';"
111+
if t1 not in html:
112+
raise AssertionError(html)
113+
114+
@ignore_warnings(PendingDeprecationWarning)
115+
def test_collapse_show(self):
116+
content = """
117+
before
118+
119+
.. collapse::
120+
:legend: ABC/abcd
121+
:hide:
122+
123+
this code shoud appear___
124+
125+
after
126+
""".replace(
127+
" ", ""
128+
)
129+
content = content.replace('u"', '"')
130+
131+
# RST
132+
html = rst2html(content, writer="rst", keep_warnings=True, directives=tives)
133+
134+
t1 = ":hide:"
135+
if t1 not in html:
136+
raise AssertionError(html)
137+
138+
# HTML
139+
html = rst2html(content, writer="custom", keep_warnings=True, directives=tives)
140+
141+
t1 = ">abcd<"
142+
if t1 not in html:
143+
raise AssertionError(html)
144+
145+
t1 = '"display:none;"'
146+
if t1 not in html:
147+
raise AssertionError(html)
148+
149+
150+
if __name__ == "__main__":
151+
unittest.main()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sys
2+
import os
3+
import unittest
4+
from sphinx_runpython.runpython.run_cmd import run_cmd, skip_run_cmd
5+
from sphinx_runpython.ext_test_case import ExtTestCase
6+
7+
8+
class TestRunCmd(ExtTestCase):
9+
def test_run_cmd_1(self):
10+
cmd = "set" if sys.platform.startswith("win") else "env"
11+
out1, _ = run_cmd(cmd, wait=True)
12+
out2, _ = run_cmd(cmd, wait=True, communicate=False)
13+
self.maxDiff = None
14+
self.assertEqual(out1.strip(), out2.strip())
15+
16+
def test_run_cmd_2(self):
17+
cmd = "set" if sys.platform.startswith("win") else "env"
18+
19+
out3, err = run_cmd(cmd, wait=True, communicate=False, tell_if_no_output=600)
20+
out, err = skip_run_cmd(cmd, wait=True)
21+
assert len(out) == 0
22+
assert len(err) == 0
23+
counts = dict(out=[], err=[])
24+
25+
def stop_running_if(out, err, counts=counts):
26+
if out:
27+
counts["out"].append(out)
28+
if err:
29+
counts["err"].append(err)
30+
31+
out4, err = run_cmd(
32+
cmd,
33+
wait=True,
34+
communicate=False,
35+
tell_if_no_output=600,
36+
stop_running_if=stop_running_if,
37+
)
38+
39+
out, err = skip_run_cmd(cmd, wait=True)
40+
self.assertEqual(len(out), 0)
41+
self.assertEqual(len(err), 0)
42+
self.maxDiff = None
43+
self.assertEqual(out3.strip(), out4.strip())
44+
assert len(counts["out"]) > 0
45+
if len(counts["err"]) > 0:
46+
raise AssertionError(counts["err"])
47+
48+
def test_run_cmd_more(self):
49+
cmd = "more " + os.path.abspath(__file__)
50+
51+
try:
52+
run_cmd(
53+
cmd,
54+
wait=True,
55+
communicate=False,
56+
tell_if_no_output=600,
57+
sin="\n\n\n" * 100,
58+
)
59+
except Exception as e:
60+
self.assertIn("ERROR", str(e))
61+
62+
out, err = run_cmd(cmd, wait=True, communicate=True, sin="\n\n\n" * 100)
63+
self.assertGreater(len(out), 10)
64+
self.assertEqual(len(err), 0)
65+
66+
67+
if __name__ == "__main__":
68+
unittest.main(verbosity=2)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sys
2+
import unittest
3+
from sphinx_runpython.runpython.run_cmd import run_cmd, parse_exception_message
4+
from sphinx_runpython.ext_test_case import ExtTestCase
5+
6+
7+
class TestRunCmdException(ExtTestCase):
8+
def test_run_cmd_exc(self):
9+
cmd = "unexpectedcommand"
10+
ex = "not affected"
11+
try:
12+
out, err = run_cmd(
13+
cmd,
14+
wait=True,
15+
log_error=False,
16+
catch_exit=True,
17+
communicate=False,
18+
tell_if_no_output=120,
19+
)
20+
no_exception = True
21+
ex = None
22+
except Exception as e:
23+
no_exception = False
24+
out, err = parse_exception_message(e)
25+
ex = e
26+
self.assertTrue(not no_exception)
27+
if sys.platform.startswith("win"):
28+
if out is None or err is None:
29+
raise AssertionError("A\n" + str(ex))
30+
if len(out) > 0:
31+
raise AssertionError("B\n" + str(ex))
32+
if len(err) == 0:
33+
raise AssertionError("C\n" + str(ex))
34+
else:
35+
self.assertTrue(out is None)
36+
self.assertTrue(err is None)
37+
self.assertTrue(isinstance(ex, Exception))
38+
39+
40+
if __name__ == "__main__":
41+
unittest.main()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from sphinx_runpython.runpython.run_cmd import run_cmd
3+
from sphinx_runpython.ext_test_case import ExtTestCase
4+
5+
6+
class TestRunCmdTimeout(ExtTestCase):
7+
def test_run_cmd_timeout(self):
8+
if __name__ == "__main__":
9+
cmd = "more"
10+
out, err = run_cmd(
11+
cmd, wait=True, tell_if_no_output=1, communicate=False, timeout=3
12+
)
13+
self.assertIn("Process killed", err)
14+
# kill does not work
15+
16+
17+
if __name__ == "__main__":
18+
unittest.main()

0 commit comments

Comments
 (0)