|
7 | 7 | # documentation root, use os.path.abspath to make it absolute, like shown here. |
8 | 8 | import os |
9 | 9 | import sys |
| 10 | +import shutil |
10 | 11 | import datetime |
| 12 | +import zipfile |
| 13 | +from importlib import metadata |
11 | 14 | from urllib.parse import urljoin |
| 15 | +from urllib.request import urlretrieve |
| 16 | +from pathlib import Path |
12 | 17 | # -- Project information ----------------------------------------------------- |
13 | 18 | # Add any Sphinx extension module names here, as strings. They can be |
14 | 19 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom |
|
33 | 38 | sys.path.insert(0, os.path.dirname(os.path.abspath(".."))) |
34 | 39 | from version import get_doc_version |
35 | 40 | doc_version = get_doc_version() |
36 | | - |
| 41 | + |
37 | 42 | # -- General configuration --------------------------------------------------- |
38 | 43 |
|
39 | 44 | # add extensions path for snippets |
40 | 45 | sys.path.append(os.path.abspath("./_ext")) |
41 | 46 |
|
42 | | -extensions = ['sphinxcontrib.matlab', 'sphinx.ext.napoleon', 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', 'sphinxcontrib.autodoc_pydantic', 'sphinx_design', 'sphinx_copybutton', 'snippets', 'enum_tools.autoenum'] |
| 47 | +extensions = ['sphinxcontrib.matlab', 'sphinx.ext.napoleon', 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', 'sphinxcontrib.autodoc_pydantic', 'sphinx_design', 'sphinx_copybutton', 'snippets', 'enum_tools.autoenum', 'nbsphinx'] |
43 | 48 |
|
44 | 49 | # Add any paths that contain templates here, relative to this directory. |
45 | 50 | templates_path = ['_templates'] |
46 | 51 |
|
| 52 | +# -- Setup example files ----------------------------------------------------- |
| 53 | +PYTHON_RAT_RELEASE = metadata.version("RATapi") |
| 54 | + |
| 55 | +if not os.path.isdir("./python_examples/data"): |
| 56 | + zip_dir, headers = urlretrieve(f"https://github.com/RascalSoftware/python-RAT/archive/refs/tags/{PYTHON_RAT_RELEASE}.zip") |
| 57 | + with zipfile.ZipFile(zip_dir) as zf: |
| 58 | + zf.extractall() |
| 59 | + print("Copying Jupyter notebooks...") |
| 60 | + for directory in ['normal_reflectivity', 'domains', 'absorption']: |
| 61 | + for file in Path(f"./python-RAT-{PYTHON_RAT_RELEASE}/RATapi/examples/{directory}/").glob('*'): |
| 62 | + shutil.copy(file, "./python_examples/notebooks/") |
| 63 | + |
| 64 | + shutil.copytree(f"./python-RAT-{PYTHON_RAT_RELEASE}/RATapi/examples/data", "./python_examples/data", dirs_exist_ok=True) |
| 65 | + |
| 66 | + shutil.rmtree(f"./python-RAT-{PYTHON_RAT_RELEASE}") |
| 67 | + |
| 68 | +if not os.path.isfile("./matlab_examples/standardLayersDSPCSheet.html"): |
| 69 | + try: |
| 70 | + from matlab.engine import start_matlab |
| 71 | + except ImportError: |
| 72 | + print("Could not copy MATLAB live scripts as MATLAB is not installed.") |
| 73 | + else: |
| 74 | + print("Starting MATLAB Engine...") |
| 75 | + eng = start_matlab() |
| 76 | + matlab_examples_path = Path("./matlab_examples").resolve() |
| 77 | + eng.eval("cd('../API'); addPaths;", nargout=0) |
| 78 | + for sheet in ['normalReflectivity/standardLayers/standardLayersDSPCSheet', |
| 79 | + 'normalReflectivity/customLayers/customLayersDSPCSheet', |
| 80 | + 'normalReflectivity/customXY/customXYDSPCSheet', |
| 81 | + 'domains/standardLayers/domainsStandardLayersSheet', |
| 82 | + 'domains/customLayers/domainsCustomLayersSheet', |
| 83 | + 'domains/customXY/domainsCustomXYSheet', |
| 84 | + 'miscellaneous/convertRascal1Project/convertRascal', |
| 85 | + 'miscellaneous/alternativeLanguages/customModelLanguagesSheet',]: |
| 86 | + filename = Path(sheet).name |
| 87 | + folder = str(Path(sheet).parent) |
| 88 | + print(f"exporting {sheet}") |
| 89 | + eng.cd(f"examples/{folder}", nargout=0) |
| 90 | + eng.matlab.internal.liveeditor.executeAndSave(str(Path(f"../API/examples/{sheet}.mlx").resolve()), nargout=0) |
| 91 | + eng.export(f"{filename}.mlx", str(matlab_examples_path / f"{filename}.html"), nargout=0) |
| 92 | + eng.cd("../../../", nargout=0) |
| 93 | + |
| 94 | + |
47 | 95 | # -- Options for HTML output ------------------------------------------------- |
48 | 96 | #set primary_domain = 'matlab' |
49 | 97 | primary_domain = None |
|
84 | 132 |
|
85 | 133 | autodoc_typehints = "description" |
86 | 134 |
|
| 135 | +nbsphinx_prolog = r""" |
| 136 | +{% set docname = 'doc/' + env.doc2path(env.docname, base=None)|string %} |
| 137 | +
|
| 138 | +.. raw:: html |
| 139 | +
|
| 140 | + <div class="admonition note"> |
| 141 | + This page was generated from the notebook {{ env.docname.split('/')|last|e + '.ipynb' }} found in |
| 142 | + <a class="reference external" href="https://github.com/RascalSoftware/python-RAT/blob/"""+PYTHON_RAT_RELEASE+r"""/RATapi/examples/">the Python-RAT repository</a>. |
| 143 | + <a href="{{ env.docname.split('/')|last|e + '.ipynb' }}" class="reference download internal" download>Download notebook</a>. |
| 144 | + </div> |
| 145 | +
|
| 146 | +.. note:: |
| 147 | +
|
| 148 | + To get the output project and results from this example in your Python session, run: |
| 149 | +
|
| 150 | + .. code-block:: python |
| 151 | +
|
| 152 | + from RATapi.examples import {{ env.docname.split('/')|last|e }} |
| 153 | + project, results = {{ env.docname.split('/')|last|e }}() |
| 154 | +
|
| 155 | +------------------------------------------------------------------------------------- |
| 156 | +
|
| 157 | +""" |
| 158 | + |
87 | 159 | ### autodoc_pydantic settings |
88 | 160 | # hide JSON schemas by default |
89 | 161 | autodoc_pydantic_model_show_json = False |
|
0 commit comments