From 7c0acf4a8b7c5cc476a98267c0c3491930080d73 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Sat, 1 Jun 2024 16:57:10 -0400 Subject: [PATCH] Fix compatibility with Sphinx 7.3 and Sphinx Gallery 0.16 --- doc/sphinx/conf.py | 59 +++---------------------------------------- doc/sphinx/ctutils.py | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 55 deletions(-) create mode 100644 doc/sphinx/ctutils.py diff --git a/doc/sphinx/conf.py b/doc/sphinx/conf.py index d03da1084..3f9ac7557 100644 --- a/doc/sphinx/conf.py +++ b/doc/sphinx/conf.py @@ -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 diff --git a/doc/sphinx/ctutils.py b/doc/sphinx/ctutils.py new file mode 100644 index 000000000..e5d9b9a99 --- /dev/null +++ b/doc/sphinx/ctutils.py @@ -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'])