Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions source/_ext/snippets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Sphinx directive for building code from snippets."""

import warnings
from contextlib import redirect_stdout
from io import StringIO

Expand Down Expand Up @@ -45,8 +44,7 @@ def setup(app: Sphinx) -> ExtensionMetadata:

def setup_envs(*ignore):
"""Initialise Python/MATLAB environments."""
app.env.snippets_env = {"RAT": RATapi}
print("Starting up MATLAB Engine...")
app.env.snippets_env = {"RAT": RATapi}
app.env.matlab_engine = setup_matlab()
app.env.matlab_engine.eval(
"cd('API'); addPaths; cd('..'); ratVars = who;", nargout=0
Expand Down Expand Up @@ -74,8 +72,7 @@ class FallbackMatlabEngine:
"""A fallback class that intercepts calls to MATLAB engine when the engine is not available."""

def eval(self, *args, **kwargs):
print("Could not create output as MATLAB engine not available!")
warnings.warn("Could not create output as MATLAB engine was not available.")
pass

def quit(self):
pass
Expand All @@ -88,6 +85,7 @@ def setup_matlab():
except ImportError:
return FallbackMatlabEngine()

print("Starting up MATLAB Engine for snippets...")
return start_matlab()


Expand Down
23 changes: 16 additions & 7 deletions source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from urllib.parse import urljoin
from urllib.request import urlretrieve
from pathlib import Path


# -- Project information -----------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
Expand Down Expand Up @@ -52,26 +54,33 @@
# -- Setup example files -----------------------------------------------------
PYTHON_RAT_RELEASE = metadata.version("RATapi")

MATLAB_AVAILABLE = True
try:
from matlab.engine import start_matlab
except ImportError:
print("MATLAB Engine not found. Some examples and code blocks may be missing.")
MATLAB_AVAILABLE = False

if not os.path.isdir("./python_examples/data"):
zip_dir, headers = urlretrieve(f"https://github.com/RascalSoftware/python-RAT/archive/refs/tags/{PYTHON_RAT_RELEASE}.zip")
with zipfile.ZipFile(zip_dir) as zf:
zf.extractall()
print("Copying Jupyter notebooks...")
for directory in ['normal_reflectivity', 'domains', 'absorption', 'convert_rascal_project']:
for directory in ['normal_reflectivity', 'domains', 'absorption']:
for file in Path(f"./python-RAT-{PYTHON_RAT_RELEASE}/RATapi/examples/{directory}/").glob('*'):
shutil.copy(file, "./python_examples/notebooks/")
if MATLAB_AVAILABLE: # convert_rascal example requires matlab engine
for file in Path(f"./python-RAT-{PYTHON_RAT_RELEASE}/RATapi/examples/convert_rascal_project/").glob('*'):
shutil.copy(file, "./python_examples/notebooks/")
Comment thread
alexhroom marked this conversation as resolved.


shutil.copytree(f"./python-RAT-{PYTHON_RAT_RELEASE}/RATapi/examples/data", "./python_examples/data", dirs_exist_ok=True)

shutil.rmtree(f"./python-RAT-{PYTHON_RAT_RELEASE}")

if not os.path.isfile("./matlab_examples/standardLayersDSPCSheet.html"):
try:
from matlab.engine import start_matlab
except ImportError:
print("Could not copy MATLAB live scripts as MATLAB is not installed.")
else:
print("Starting MATLAB Engine...")
if MATLAB_AVAILABLE:
print("Starting MATLAB Engine for live scripts...")
eng = start_matlab()
matlab_examples_path = Path("./matlab_examples").resolve()
eng.eval("cd('../API'); addPaths;", nargout=0)
Expand Down