Visitar URL original
Cache gdot script execution results in Sphinx environment (#49) · sdpython/sphinx-runpython@f3fadce · GitHub
Skip to content

Commit f3fadce

Browse files
Copilotxadupre
andauthored
Cache gdot script execution results in Sphinx environment (#49)
* Initial plan * speedup runpython execution for graphviz: cache script results in Sphinx env Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com> * fix * fix * gdot: prefix cache key with last segment of docname Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com> * change * changes --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com> Co-authored-by: Xavier Dupré <xadupre@microsoft.com>
1 parent ed8d015 commit f3fadce

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎CHANGELOGS.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Change Logs
44
0.4.2
55
+++++
66

7+
* :pr:`53`: Disable gdot call to graphviz if ``UNITTEST_GOING=1``
8+
* :pr:`51`: Uses GitHub actions for CI
9+
* :pr:`49`: Cache gdot script execution results in Sphinx environment`
710
* :pr:`47`: use svg by default with gdot
811

912
0.4.1

‎_unittests/ut_gdot/test_gdot_extension.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,35 @@ def test_gdot4_png(self):
163163
self.assertIn("png", content)
164164

165165
@ignore_warnings(PendingDeprecationWarning)
166+
def test_gdot_script_cache(self):
167+
"""Test that identical scripts are cached and produce the same output."""
168+
script = "print('digraph foo { HbarH -> HbazH; }')".replace("H", '"')
169+
content = f"""
170+
before
171+
172+
.. gdot::
173+
:script:
174+
175+
{script}
176+
177+
middle
178+
179+
.. gdot::
180+
:script:
181+
182+
{script}
183+
184+
after
185+
"""
186+
content = rst2html(
187+
content, writer_name="rst", new_extensions=["sphinx_runpython.gdot"]
188+
)
189+
# Both gdot directives should produce the same DOT output
190+
count = content.count('digraph foo { "bar" -> "baz"; }')
191+
self.assertEqual(
192+
count, 2, f"Expected the DOT code to appear twice, got {count}"
193+
)
194+
166195
def test_gdot_unittest_going_svg(self):
167196
"""When UNITTEST_GOING=1, a dummy SVG containing 'DISABLED FOR TESTS' is rendered."""
168197
content = """

‎sphinx_runpython/gdot/sphinx_gdot_extension.py‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hashlib
12
import os
23
import logging
34
from docutils import nodes
@@ -153,7 +154,25 @@ def run(self):
153154
# executes script if any
154155
content = "\n".join(self.content)
155156
if script or script == "":
156-
stdout, stderr, _ = run_python_script(content, process=process)
157+
env = info.get("env")
158+
doc_prefix = docname.split("/")[-1] if docname else ""
159+
cache_key = (
160+
f"{doc_prefix}:"
161+
+ hashlib.sha256(f"{content}:{process}".encode()).hexdigest()
162+
)
163+
if env is not None:
164+
if not hasattr(env, "gdot_script_cache"):
165+
env.gdot_script_cache = {}
166+
cached = env.gdot_script_cache.get(cache_key, None)
167+
else:
168+
cached = None
169+
170+
if cached is not None:
171+
stdout, stderr = cached
172+
else:
173+
stdout, stderr, _ = run_python_script(content, process=process)
174+
if env is not None:
175+
env.gdot_script_cache[cache_key] = (stdout, stderr)
157176

158177
if stderr:
159178
out = [

0 commit comments

Comments
 (0)