From de238a3acb93650297f19dcebf601b06c1ed5ccf Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
<66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Fri, 10 Nov 2023 20:12:32 +0000
Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---
capsul/sphinxext/layoutdocgen.py | 75 +++++++-----
capsul/sphinxext/pipelinedocgen.py | 109 +++++++++---------
.../test/test_process_pipeline_doc.py | 77 ++++++-------
capsul/sphinxext/test/test_usercases_doc.py | 24 ++--
capsul/sphinxext/usecasesdocgen.py | 44 +++----
5 files changed, 173 insertions(+), 156 deletions(-)
diff --git a/capsul/sphinxext/layoutdocgen.py b/capsul/sphinxext/layoutdocgen.py
index 961033a0f..165e327ee 100644
--- a/capsul/sphinxext/layoutdocgen.py
+++ b/capsul/sphinxext/layoutdocgen.py
@@ -17,10 +17,10 @@
class LayoutHelperWriter:
- """ A basic class to create sphinx layout and associated index.
- """
+ """A basic class to create sphinx layout and associated index."""
+
def __init__(self, module_names, root_module_name, rst_extension=".rst"):
- """ Initialize the LayoutHelperWriter class
+ """Initialize the LayoutHelperWriter class
Parameters
----------
@@ -34,10 +34,10 @@ def __init__(self, module_names, root_module_name, rst_extension=".rst"):
self.module_names = module_names
self.rst_extension = rst_extension
self.root_module_name = root_module_name
- self.rst_section_levels = ['*', '=', '-', '~', '^']
+ self.rst_section_levels = ["*", "=", "-", "~", "^"]
def generate_index_entry(self, module_name, indent=4):
- """ Make autodoc documentation of pilots
+ """Make autodoc documentation of pilots
Parameters
----------
@@ -58,8 +58,7 @@ def generate_index_entry(self, module_name, indent=4):
except ImportError:
exc_info = sys.exc_info()
logging.error("".join(traceback.format_exception(*exc_info)))
- logging.error(
- "Can't load module {0}".format(full_module_name))
+ logging.error("Can't load module {0}".format(full_module_name))
module = sys.modules[full_module_name]
description = module.__doc__
@@ -71,15 +70,16 @@ def generate_index_entry(self, module_name, indent=4):
ad += spacer + "\n"
ad += spacer + "
\n\n")
title = "Documentation of the {0} Pipelines\n".format(
- self.root_module_name.upper())
+ self.root_module_name.upper()
+ )
w(title)
w(self.rst_section_levels[1] * len(title) + "\n\n")
diff --git a/capsul/sphinxext/pipelinedocgen.py b/capsul/sphinxext/pipelinedocgen.py
index c8903aa56..e51a2bfda 100644
--- a/capsul/sphinxext/pipelinedocgen.py
+++ b/capsul/sphinxext/pipelinedocgen.py
@@ -4,15 +4,15 @@
class PipelineHelpWriter:
- """ Class for automatic generation of pipeline API documentations
+ """Class for automatic generation of pipeline API documentations
in Sphinx-parsable reST format.
"""
# Only separating first two levels
- rst_section_levels = ['*', '=', '-', '~', '^']
+ rst_section_levels = ["*", "=", "-", "~", "^"]
def __init__(self, pipelines, rst_extension=".rst", short_names={}):
- """ Initialize package for parsing
+ """Initialize package for parsing
Parameters
----------
@@ -28,7 +28,7 @@ def __init__(self, pipelines, rst_extension=".rst", short_names={}):
self.short_names = short_names
def generate_api_doc(self, pipeline, schema):
- """ Make autodoc documentation for a pipeline python module
+ """Make autodoc documentation for a pipeline python module
Parameters
----------
@@ -70,12 +70,15 @@ def generate_api_doc(self, pipeline, schema):
ad += "\n.. _{0}\n\n".format(label)
chap_title = pipeline
- ad += (chap_title + "\n" +
- self.rst_section_levels[1] * len(chap_title) + "\n\n")
+ ad += chap_title + "\n" + self.rst_section_levels[1] * len(chap_title) + "\n\n"
# Add a subtitle
- ad += (pipeline_name + "\n" +
- self.rst_section_levels[2] * len(pipeline_name) + "\n\n")
+ ad += (
+ pipeline_name
+ + "\n"
+ + self.rst_section_levels[2] * len(pipeline_name)
+ + "\n\n"
+ )
# Then add the trait description
# It will generate two sections: input and output
@@ -84,8 +87,7 @@ def generate_api_doc(self, pipeline, schema):
# Add schema if generated
if schema:
schama_title = "Pipeline schema"
- ad += ("\n" + schama_title + "\n" +
- "~" * len(schama_title) + "\n\n")
+ ad += "\n" + schama_title + "\n" + "~" * len(schama_title) + "\n\n"
ad += ".. image:: {0}\n".format(schema)
ad += " :height: 400px\n"
ad += " :align: center\n\n"
@@ -93,7 +95,7 @@ def generate_api_doc(self, pipeline, schema):
return ad, title
def write_api_docs(self, outdir=None, returnrst=False):
- """ Generate API reST files.
+ """Generate API reST files.
Parameters
----------
@@ -111,8 +113,9 @@ def write_api_docs(self, outdir=None, returnrst=False):
# Check output directory
if returnrst is False:
if not isinstance(outdir, str):
- raise Exception("If 'returnrst' is False, need a valid output "
- "directory.")
+ raise Exception(
+ "If 'returnrst' is False, need a valid output " "directory."
+ )
if not os.path.exists(outdir):
os.makedirs(outdir)
else:
@@ -121,12 +124,10 @@ def write_api_docs(self, outdir=None, returnrst=False):
# Generate reST API
written_modules = []
for pipeline in self.pipelines:
-
pipeline_short = self.get_short_name(pipeline)
# Check if an image representation of the pipeline exists
if returnrst is False:
- schema = os.path.join(os.pardir, "schema",
- pipeline_short + ".png")
+ schema = os.path.join(os.pardir, "schema", pipeline_short + ".png")
if not os.path.isfile(os.path.join(outdir, schema)):
schema = None
else:
@@ -139,8 +140,7 @@ def write_api_docs(self, outdir=None, returnrst=False):
# Write to file
if returnrst is False:
- outfile = os.path.join(outdir,
- pipeline_short + self.rst_extension)
+ outfile = os.path.join(outdir, pipeline_short + self.rst_extension)
fileobj = open(outfile, "wt")
fileobj.write(api_str)
fileobj.close()
@@ -166,17 +166,18 @@ def get_short_name(self, name):
return short_name
# look for a shorter name for the longest module prefix
modules = name.split(".")
- for i in range(len(modules)-1, 0, -1):
- path = '.'.join(modules[:i])
+ for i in range(len(modules) - 1, 0, -1):
+ path = ".".join(modules[:i])
short_path = self.short_names.get(path)
if short_path:
- return '.'.join([short_path] + modules[i+1:])
+ return ".".join([short_path] + modules[i + 1 :])
# not found
return name
- def write_index(self, outdir, froot="index", relative_to=None,
- rst_extension=".rst"):
- """ Make a reST API index file from the list of written files
+ def write_index(
+ self, outdir, froot="index", relative_to=None, rst_extension=".rst"
+ ):
+ """Make a reST API index file from the list of written files
Parameters
----------
@@ -195,7 +196,7 @@ def write_index(self, outdir, froot="index", relative_to=None,
"""
# Check if some modules have been written
if self.written_modules is None:
- raise ValueError('No modules written')
+ raise ValueError("No modules written")
# Get full index filename path
path = os.path.join(outdir, froot + rst_extension)
@@ -205,7 +206,7 @@ def write_index(self, outdir, froot="index", relative_to=None,
relpath = outdir.replace(relative_to + os.path.sep, "")
else:
relpath = outdir
- print('relpath:', relpath)
+ print("relpath:", relpath)
# Edit the index file
idx = open(path, "wt")
@@ -221,22 +222,22 @@ def write_index(self, outdir, froot="index", relative_to=None,
# Table definition
table = [""]
table.append("
")
- table.append(""
- "")
+ table.append("" "")
table.append("")
# Add all modules
for title_str, f in self.written_modules:
pipeline_short = self.get_short_name(f)
- print('title_str:', title_str, ', f:', f)
+ print("title_str:", title_str, ", f:", f)
relative_pipeline = ".".join(f.split(".")[2:])
- print('relative_pipeline:', relative_pipeline)
+ print("relative_pipeline:", relative_pipeline)
ref = os.path.join(relpath, pipeline_short + ".html")
- print('ref:', ref)
+ print("ref:", ref)
table.append("")
table.append(
""
- "{1} | \n".format(ref, relative_pipeline))
+ "{1}\n".format(ref, relative_pipeline)
+ )
table.append("{0} | ".format(title_str))
table.append("
")
@@ -251,10 +252,16 @@ def write_index(self, outdir, froot="index", relative_to=None,
# Close the file
idx.close()
- def write_main_index(self, outdir, module_name, root_module_name,
- froot="index", rst_extension=".rst",
- have_usecases=True):
- """ Make a reST API index file for the module
+ def write_main_index(
+ self,
+ outdir,
+ module_name,
+ root_module_name,
+ froot="index",
+ rst_extension=".rst",
+ have_usecases=True,
+ ):
+ """Make a reST API index file for the module
Parameters
----------
@@ -285,8 +292,7 @@ def write_main_index(self, outdir, module_name, root_module_name,
# Generate a title
chap_title = " ".join([x.capitalize() for x in module_name.split("_")])
- w(chap_title + "\n" +
- self.rst_section_levels[0] * len(chap_title) + "\n\n")
+ w(chap_title + "\n" + self.rst_section_levels[0] * len(chap_title) + "\n\n")
# Generate a markup
label = module_name
@@ -294,10 +300,8 @@ def write_main_index(self, outdir, module_name, root_module_name,
# Page use cases
# # Generate a title
- chap_title = ":mod:`{0}.{1}`: User Guide".format(
- root_module_name, module_name)
- w(chap_title + "\n" +
- self.rst_section_levels[1] * len(chap_title) + "\n\n")
+ chap_title = ":mod:`{0}.{1}`: User Guide".format(root_module_name, module_name)
+ w(chap_title + "\n" + self.rst_section_levels[1] * len(chap_title) + "\n\n")
if have_usecases:
# # Generate a markup
@@ -310,31 +314,28 @@ def write_main_index(self, outdir, module_name, root_module_name,
# API page
# # Generate a title
- chap_title = ":mod:`{0}.{1}`: API".format(
- root_module_name, module_name)
- w(chap_title + "\n" +
- self.rst_section_levels[1] * len(chap_title) + "\n\n")
+ chap_title = ":mod:`{0}.{1}`: API".format(root_module_name, module_name)
+ w(chap_title + "\n" + self.rst_section_levels[1] * len(chap_title) + "\n\n")
# # Generate a markup
label = module_name + "_api"
w(".. _{0}:\n\n".format(label))
# # Some text description
- w("The API of functions and classes, as given by the "
- "docstrings.")
+ w("The API of functions and classes, as given by the " "docstrings.")
if have_usecases:
- w(" For the *user guide* see the {0}_ug_ "
- "section for further details.\n\n".format(module_name))
+ w(
+ " For the *user guide* see the {0}_ug_ "
+ "section for further details.\n\n".format(module_name)
+ )
else:
w("\n\n")
# # Include pipeline and buildingblock indexes
# ## Pipeline
chap_title = "Pipelines"
- w(chap_title + "\n" +
- self.rst_section_levels[2] * len(chap_title) + "\n\n")
+ w(chap_title + "\n" + self.rst_section_levels[2] * len(chap_title) + "\n\n")
w(".. include:: pipeline/index%s\n\n" % rst_extension)
# ## Buildingblocks
chap_title = "Buildingblocks"
- w(chap_title + "\n" +
- self.rst_section_levels[2] * len(chap_title) + "\n\n")
+ w(chap_title + "\n" + self.rst_section_levels[2] * len(chap_title) + "\n\n")
w(".. include:: process/index%s\n\n" % rst_extension)
# Close file
diff --git a/capsul/sphinxext/test/test_process_pipeline_doc.py b/capsul/sphinxext/test/test_process_pipeline_doc.py
index fb4557bab..0f5317a8d 100644
--- a/capsul/sphinxext/test/test_process_pipeline_doc.py
+++ b/capsul/sphinxext/test/test_process_pipeline_doc.py
@@ -7,9 +7,10 @@
from soma.controller import File, field
+
class MyProcess(Process):
- """ My dummy process.
- """
+ """My dummy process."""
+
# Some inputs
input_image: field(type_=File, optional=False, doc="an image.")
input_float: field(type_=float, optional=True, doc="a float.")
@@ -20,40 +21,42 @@ class MyProcess(Process):
class MyPipeline(Pipeline):
- """ My dummy pipeline with a switch.
- """
+ """My dummy pipeline with a switch."""
+
def pipeline_definition(self):
- """ Create a simple pipeline with a switch and some of its output plugs
+ """Create a simple pipeline with a switch and some of its output plugs
exported to the pipeline output.
"""
# Create processes
self.add_process(
- "way1",
- "capsul.sphinxext.test.test_process_pipeline_doc.MyProcess")
+ "way1", "capsul.sphinxext.test.test_process_pipeline_doc.MyProcess"
+ )
self.add_process(
- "way2",
- "capsul.sphinxext.test.test_process_pipeline_doc.MyProcess")
+ "way2", "capsul.sphinxext.test.test_process_pipeline_doc.MyProcess"
+ )
self.add_process(
- "node",
- "capsul.sphinxext.test.test_process_pipeline_doc.MyProcess")
+ "node", "capsul.sphinxext.test.test_process_pipeline_doc.MyProcess"
+ )
# Create Switch
- self.create_switch("switch", {
- "one": {
- "image": "way1.output_image",
- "float": "way1.output_float",
+ self.create_switch(
+ "switch",
+ {
+ "one": {
+ "image": "way1.output_image",
+ "float": "way1.output_float",
+ },
+ "two": {
+ "image": "way2.output_image",
+ "float": "way2.output_float",
+ },
},
- "two": {
- "image": "way2.output_image",
- "float": "way2.output_float",
- }
- })
+ )
# Link input
self.export_parameter("way1", "input_image")
self.export_parameter("way1", "input_float")
-
# Link way2
self.add_link("input_image->way2.input_image")
self.add_link("input_float->way2.input_float")
@@ -63,40 +66,34 @@ def pipeline_definition(self):
self.add_link("switch.float->node.input_float")
# Outputs
- self.export_parameter("node", "output_image",
- pipeline_parameter="node_image")
- self.export_parameter("node", "output_float",
- pipeline_parameter="node_float")
- self.export_parameter("switch", "float",
- pipeline_parameter="switch_float")
- self.export_parameter("way1", "output_image",
- pipeline_parameter="way1_image")
+ self.export_parameter("node", "output_image", pipeline_parameter="node_image")
+ self.export_parameter("node", "output_float", pipeline_parameter="node_float")
+ self.export_parameter("switch", "float", pipeline_parameter="switch_float")
+ self.export_parameter("way1", "output_image", pipeline_parameter="way1_image")
class TestSphinxExt(unittest.TestCase):
- """ Class to test that we can properly generate the process and pipeline
+ """Class to test that we can properly generate the process and pipeline
rst documentation.
"""
+
def setUp(self):
- """ In the setup construct a process and a pipeline
- """
+ """In the setup construct a process and a pipeline"""
# Construct the processe and pipeline
- self.process_id = ("capsul.sphinxext.test.test_process_pipeline_doc."
- "MyProcess")
- self.pipeline_id = ("capsul.sphinxext.test.test_process_pipeline_doc."
- "MyPipeline")
+ self.process_id = "capsul.sphinxext.test.test_process_pipeline_doc." "MyProcess"
+ self.pipeline_id = (
+ "capsul.sphinxext.test.test_process_pipeline_doc." "MyPipeline"
+ )
def test_process_doc(self):
- """ Method to test the process rst documentation.
- """
+ """Method to test the process rst documentation."""
# Generate the writer object
docwriter = PipelineHelpWriter([self.process_id])
rstdoc = docwriter.write_api_docs(returnrst=True)
self.assertTrue(self.process_id in rstdoc)
def test_pipeline_doc(self):
- """ Method to test the pipeline rst documentation.
- """
+ """Method to test the pipeline rst documentation."""
# Generate the writer object
docwriter = PipelineHelpWriter([self.pipeline_id])
rstdoc = docwriter.write_api_docs(returnrst=True)
diff --git a/capsul/sphinxext/test/test_usercases_doc.py b/capsul/sphinxext/test/test_usercases_doc.py
index e5912612b..6992ece54 100644
--- a/capsul/sphinxext/test/test_usercases_doc.py
+++ b/capsul/sphinxext/test/test_usercases_doc.py
@@ -7,12 +7,13 @@
# Capsul import
import capsul.sphinxext.test as my_module
-#from capsul.utils.pilot import pilotfunction
+
+# from capsul.utils.pilot import pilotfunction
from capsul.sphinxext.usecasesdocgen import UseCasesHelperWriter
from capsul.sphinxext.load_pilots import load_pilots
-#@pilotfunction
+# @pilotfunction
def pilot_dummy_test():
"""
Test pilot doc generation.
@@ -38,25 +39,24 @@ def pilot_dummy_test():
class TestUseCases(unittest.TestCase):
- """ Class to test that we can properly generate the use cases rst
+ """Class to test that we can properly generate the use cases rst
documentation.
"""
+
def test_use_cases_doc(self):
- """ Method to test the use cases rst documentation.
- """
+ """Method to test the use cases rst documentation."""
# Get all the use cases
module_path = my_module.__path__[0]
- pilots = load_pilots(module_path, module_path,
- "capsul.sphinxext.test")
+ pilots = load_pilots(module_path, module_path, "capsul.sphinxext.test")
self.assertTrue(len(pilots) == 1)
self.assertTrue("capsul.sphinxext.test.test_usercases_doc" in pilots)
- self.assertTrue(
- len(pilots["capsul.sphinxext.test.test_usercases_doc"]) == 1)
+ self.assertTrue(len(pilots["capsul.sphinxext.test.test_usercases_doc"]) == 1)
# Generate the writer object
docwriter = UseCasesHelperWriter(
- pilots["capsul.sphinxext.test.test_usercases_doc"])
+ pilots["capsul.sphinxext.test.test_usercases_doc"]
+ )
rstdoc = docwriter.write_usecases_docs(returnrst=True)
self.assertTrue(
- "capsul.sphinxext.test.test_usercases_doc.pilot_dummy_test" in
- rstdoc)
+ "capsul.sphinxext.test.test_usercases_doc.pilot_dummy_test" in rstdoc
+ )
diff --git a/capsul/sphinxext/usecasesdocgen.py b/capsul/sphinxext/usecasesdocgen.py
index 72e6eb92f..e8fa65cc8 100644
--- a/capsul/sphinxext/usecasesdocgen.py
+++ b/capsul/sphinxext/usecasesdocgen.py
@@ -20,10 +20,10 @@
class UseCasesHelperWriter:
- """ A basic class to convert the pilot codes to rst use cases
- """
+ """A basic class to convert the pilot codes to rst use cases"""
+
def __init__(self, pilots, rst_extension=".rst"):
- """ Initialize the UseCasesHelper class
+ """Initialize the UseCasesHelper class
Parameters
----------
@@ -36,7 +36,7 @@ def __init__(self, pilots, rst_extension=".rst"):
self.rst_extension = rst_extension
def getsource(self, function):
- """ Method that returns the source code of a function
+ """Method that returns the source code of a function
Parameters
----------
@@ -51,7 +51,7 @@ def getsource(self, function):
return inspect.getsource(function)
def generate_usecases_doc(self, src_code, module_name):
- """ Make autodoc documentation of pilots
+ """Make autodoc documentation of pilots
Parameters
----------
@@ -79,8 +79,7 @@ def generate_usecases_doc(self, src_code, module_name):
is_header = True
full_code = "# The full use case code: {0}\n".format(module_name)
for code_item in pilot_tree:
- if (isinstance(code_item, ast.Expr) and
- isinstance(code_item.value, ast.Str)):
+ if isinstance(code_item, ast.Expr) and isinstance(code_item.value, ast.Str):
# Find End code line
code_value = lines[line_start_code:line_end_code]
for line_index in range(line_end_code, nb_lines):
@@ -133,7 +132,7 @@ def generate_usecases_doc(self, src_code, module_name):
return ad
def write_usecases_docs(self, outdir=None, returnrst=False):
- """ Generate Use Cases reST files.
+ """Generate Use Cases reST files.
Parameters
----------
@@ -147,8 +146,9 @@ def write_usecases_docs(self, outdir=None, returnrst=False):
# Check output directory
if returnrst is False:
if not isinstance(outdir, six.string_types):
- raise Exception("If 'returnrst' is False, need a valid output "
- "directory.")
+ raise Exception(
+ "If 'returnrst' is False, need a valid output " "directory."
+ )
if not os.path.exists(outdir):
os.makedirs(outdir)
else:
@@ -157,10 +157,12 @@ def write_usecases_docs(self, outdir=None, returnrst=False):
# Write reST use cases documentation
written_usecases = []
for pilot in self.pilots:
-
# Information message
- logger.info("Processing pilot '{0}' in module '{1}'...".format(
- pilot.__name__, pilot.__module__))
+ logger.info(
+ "Processing pilot '{0}' in module '{1}'...".format(
+ pilot.__name__, pilot.__module__
+ )
+ )
# Generate reST
uid = pilot.__module__ + "." + pilot.__name__
@@ -188,9 +190,10 @@ def write_usecases_docs(self, outdir=None, returnrst=False):
if returnrst is True:
return rstdoc
- def write_index(self, outdir, froot="index", relative_to=None,
- rst_extension=".rst"):
- """ Make a reST API index file from written files
+ def write_index(
+ self, outdir, froot="index", relative_to=None, rst_extension=".rst"
+ ):
+ """Make a reST API index file from written files
Parameters
----------
@@ -208,7 +211,7 @@ def write_index(self, outdir, froot="index", relative_to=None,
Extension for reST files, default '.rst'
"""
if self.written_usecases is None:
- raise ValueError('No modules written')
+ raise ValueError("No modules written")
# Get full filename path
path = os.path.join(outdir, froot + rst_extension)
# Path written into index is relative to rootpath
@@ -223,12 +226,10 @@ def write_index(self, outdir, froot="index", relative_to=None,
table = [""]
table.append("")
- table.append(""
- "")
+ table.append("" "")
table.append("")
for title_str, f in self.written_usecases:
-
# generate the relative pipeline name
relative_uid = ".".join(f.split(".")[2:])
@@ -236,7 +237,8 @@ def write_index(self, outdir, froot="index", relative_to=None,
table.append("")
table.append(
""
- "{1} | \n".format(ref, relative_uid))
+ "{1}\n".format(ref, relative_uid)
+ )
table.append("{0} | ".format(title_str))
table.append("
")