Fix compatibility with Sphinx 7.3 and Sphinx Gallery 0.16

This commit is contained in:
Ray Speth 2024-06-01 16:57:10 -04:00 committed by Ray Speth
parent 5254a6bff6
commit 7c0acf4a8b
2 changed files with 59 additions and 55 deletions

View File

@ -14,7 +14,6 @@
import sys, os, re
from pathlib import Path
from sphinx_gallery.sorting import ExplicitOrder
from sphinx_gallery.scrapers import figure_rst
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
@ -24,6 +23,8 @@ sys.path.insert(0, os.path.abspath('../../python'))
sys.path.append(os.path.abspath('.'))
sys.path.append(os.path.abspath('./exts'))
import ctutils
# -- General configuration -----------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
@ -48,39 +49,6 @@ extensions = [
]
class GraphvizScraper():
"""
Capture Graphviz objects that are assigned to variables in the global namespace.
"""
def __init__(self):
# IDs of graphviz objects that have already been seen and processed
self.processed = set()
def __repr__(self):
return 'GraphvizScraper'
def __call__(self, block, block_vars, gallery_conf):
import graphviz
# We use a list to collect references to image names
image_names = list()
# The `image_path_iterator` is created by Sphinx-Gallery, it will yield
# a path to a file name that adheres to Sphinx-Gallery naming convention.
image_path_iterator = block_vars['image_path_iterator']
# Define a list of our already-created figure objects.
for obj in block_vars["example_globals"].values():
if isinstance(obj, graphviz.Source) and id(obj) not in self.processed:
self.processed.add(id(obj))
image_path = Path(next(image_path_iterator)).with_suffix(".svg")
obj.format = "svg"
obj.render(image_path.with_suffix(""))
image_names.append(image_path)
# Use the `figure_rst` helper function to generate the reST for this
# code block's figures.
return figure_rst(image_names, gallery_conf['src_dir'])
sphinx_gallery_conf = {
'filename_pattern': '\.py',
@ -90,7 +58,7 @@ sphinx_gallery_conf = {
'image_srcset': ["2x"],
'remove_config_comments': True,
'ignore_repr_types': r'matplotlib\.(text|axes|legend)',
'image_scrapers': ('matplotlib', GraphvizScraper()),
'image_scrapers': ('matplotlib', ctutils.GraphvizScraper()),
'examples_dirs': [
'../samples/python/',
'../samples/cxx/',
@ -165,26 +133,7 @@ header_prefix = """
sphinx_gallery.gen_rst.EXAMPLE_HEADER = header_prefix + sphinx_gallery.gen_rst.EXAMPLE_HEADER
# Provide options to examples that only generate plots if an option is specified
class ResetArgv:
wants_plot = {
"adiabatic.py",
"premixed_counterflow_twin_flame.py",
"piston.py",
"reactor1.py",
"reactor2.py",
"sensitivity1.py",
}
def __repr__(self):
return 'ResetArgv'
def __call__(self, sphinx_gallery_conf, script_vars):
if Path(script_vars['src_file']).name in self.wants_plot:
return ['--plot']
else:
return []
sphinx_gallery_conf["reset_argv"] = ResetArgv()
sphinx_gallery_conf["reset_argv"] = ctutils.ResetArgv()
# Options for sphinx_tags extension
tags_create_tags = True

55
doc/sphinx/ctutils.py Normal file
View File

@ -0,0 +1,55 @@
from pathlib import Path
from sphinx_gallery.scrapers import figure_rst
# Provide options to examples that only generate plots if an option is specified
class ResetArgv:
wants_plot = {
"adiabatic.py",
"premixed_counterflow_twin_flame.py",
"piston.py",
"reactor1.py",
"reactor2.py",
"sensitivity1.py",
}
def __repr__(self):
return 'ResetArgv'
def __call__(self, sphinx_gallery_conf, script_vars):
if Path(script_vars['src_file']).name in self.wants_plot:
return ['--plot']
else:
return []
class GraphvizScraper():
"""
Capture Graphviz objects that are assigned to variables in the global namespace.
"""
def __init__(self):
# IDs of graphviz objects that have already been seen and processed
self.processed = set()
def __repr__(self):
return 'GraphvizScraper'
def __call__(self, block, block_vars, gallery_conf):
import graphviz
# We use a list to collect references to image names
image_names = list()
# The `image_path_iterator` is created by Sphinx-Gallery, it will yield
# a path to a file name that adheres to Sphinx-Gallery naming convention.
image_path_iterator = block_vars['image_path_iterator']
# Define a list of our already-created figure objects.
for obj in block_vars["example_globals"].values():
if isinstance(obj, graphviz.Source) and id(obj) not in self.processed:
self.processed.add(id(obj))
image_path = Path(next(image_path_iterator)).with_suffix(".svg")
obj.format = "svg"
obj.render(image_path.with_suffix(""))
image_names.append(image_path)
# Use the `figure_rst` helper function to generate the reST for this
# code block's figures.
return figure_rst(image_names, gallery_conf['src_dir'])