Skip to content

Commit

Permalink
Split the code of Configurable.build_doc into several methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MBartkowiakSTFC committed Apr 18, 2024
1 parent 2e9ce82 commit 3fb1077
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 73 deletions.
113 changes: 41 additions & 72 deletions MDANSE/Src/MDANSE/Framework/Configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,39 +210,7 @@ def __str__(self):
return "\n".join(self._info)

@classmethod
def build_doc(cls):
"""
Return the documentation about a configurable class based on its configurators contents.
:param cls: the configurable class for which documentation should be built
:type cls: an instance of MDANSE.Framework.Configurable.Configurable derived class
:return: the documentation about the configurable class
:rtype: str
"""
from MDANSE.Framework.Configurators.IConfigurator import IConfigurator

settings = getattr(cls, "settings", {})

if not isinstance(settings, dict):
raise ConfigurationError(
"Invalid type for settings: must be a mapping-like object"
)

doclist = []

for name, (typ, kwds) in list(settings.items()):
cfg = IConfigurator.create(typ, name, **kwds)
descr = kwds.get("description", "")
descr += "\n" + str(cfg.__doc__)
doclist.append(
{
"Configurator": name,
"Default value": repr(cfg.default),
"Description": descr,
}
)

def build_doc_example(cls):
docstring = ":Example:\n\n"
docstring += ">>> \n"
docstring += ">>> \n"
Expand All @@ -253,8 +221,11 @@ def build_doc(cls):
docstring += ">>> job = IJob.create(%r)\n" % cls.__name__
docstring += ">>> job.setup(parameters)\n"
docstring += ">>> job.run()\n"
return docstring

docstring += "\n**Job input configurators:** \n\n"
@classmethod
def build_doc_texttable(cls, doclist):
docstring = "\n**Job input configurators:** \n\n"

columns = ["Configurator", "Default value", "Description"]

Expand Down Expand Up @@ -314,11 +285,41 @@ def build_doc(cls):
)

docstring += "\n"
return docstring

@classmethod
def build_doc_htmltable(cls, doclist):
docstring = "\n**Job input configurators:**"

columns = ["Configurator", "Default value", "Description"]

for v in doclist:
# Case of Description field: has to be splitted and parsed for inserting sphinx "|" keyword for multiline
v["Description"] = v["Description"].strip()
v["Description"] = v["Description"].split("\n")
v["Description"] = ["" + vv.strip() for vv in v["Description"]]

docstring += "<table>\n"
docstring += "<tr>"
for col in columns:
docstring += f"<th>{col}</th>"
docstring += "</tr>\n"

for v in doclist:
docstring += "<tr>"
for item in [
v["Configurator"],
v["Default value"],
v["Description"][0],
]:
docstring += f"<td>{item}</td>"
docstring += "</tr>\n"

docstring += "</table>\n"
return docstring

@classmethod
def build_html_doc(cls):
def build_doc(cls, use_html_table=False):
"""
Return the documentation about a configurable class based on its configurators contents.
Expand Down Expand Up @@ -351,44 +352,12 @@ def build_html_doc(cls):
}
)

docstring = ":Example:\n\n"
docstring += ">>> \n"
docstring += ">>> \n"
docstring += ">>> parameters = {}\n"
for k, v in list(cls.get_default_parameters().items()):
docstring += ">>> parameters[%r]=%r\n" % (k, v)
docstring += ">>> \n"
docstring += ">>> job = IJob.create(%r)\n" % cls.__name__
docstring += ">>> job.setup(parameters)\n"
docstring += ">>> job.run()\n"

docstring += "\n**Job input configurators:** \n\n"

columns = ["Configurator", "Default value", "Description"]
docstring = cls.build_doc_example()

for v in doclist:
# Case of Description field: has to be splitted and parsed for inserting sphinx "|" keyword for multiline
v["Description"] = v["Description"].strip()
v["Description"] = v["Description"].split("\n")
v["Description"] = ["" + vv.strip() for vv in v["Description"]]

docstring += "<table>\n"
docstring += "<tr>"
for col in columns:
docstring += f"<th>{col}</th>"
docstring += "</tr>\n"

for v in doclist:
docstring += "<tr>"
for item in [
v["Configurator"],
v["Default value"],
v["Description"][0],
]:
docstring += f"<td>{item}</td>"
docstring += "</tr>\n"

docstring += "</table>\n"
if use_html_table:
docstring += cls.build_doc_htmltable(doclist)
else:
docstring += cls.build_doc_texttable(doclist)

return docstring

Expand Down
2 changes: 1 addition & 1 deletion MDANSE_GUI/Src/MDANSE_GUI/Tabs/Models/JobTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def createNode(self, name: str, thing, filter: str = ""):
self._values[new_number] = thing
self._docstrings[new_number] = thing.__doc__
try:
self._docstrings[new_number] += "\n" + thing.build_html_doc()
self._docstrings[new_number] += "\n" + thing.build_doc(use_html_table=True)
except AttributeError:
pass
except TypeError:
Expand Down

0 comments on commit 3fb1077

Please sign in to comment.