Skip to content

Commit 8bd4a59

Browse files
committed
refactor matlab helper
1 parent 2f2fdeb commit 8bd4a59

2 files changed

Lines changed: 27 additions & 20 deletions

File tree

rascal2/config.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ def get_matlab_engine(engine_ready, engine_output):
162162
class MatlabHelper:
163163
"""Helper to start MATLAB on another process."""
164164

165+
class ConfigError(Exception):
166+
"""Error when setting up MATLAB for RasCAL usage."""
167+
165168
_instance = None
166169

167170
def __new__(cls):
@@ -170,6 +173,7 @@ def __new__(cls):
170173
cls._instance.ready_event = mp.Event()
171174
cls._instance.close_event = mp.Event()
172175
cls._instance.engine_output = None
176+
cls._instance.matlab_dir = ""
173177
cls._instance.__engine = None
174178
cls._instance.async_start()
175179

@@ -221,19 +225,31 @@ def get_matlab_path(self):
221225
Return MATLAB install directory.
222226
"""
223227
install_dir = ""
224-
error = ""
225228

226229
try:
227230
with open(MATLAB_ARCH_FILE) as path_file:
228231
lines = path_file.readlines()
229232
if len(lines) == 4:
233+
arch = {"x86_64": "maci64", "arm64": "maca64"}
234+
if platform.system() == "Darwin" and arch.get(platform.mac_ver()[-1]) != lines[0].strip():
235+
# installed intel Matlab on ARM or vice versa
236+
raise MatlabHelper.ConfigError(
237+
"The installed MATLAB is incompatible, "
238+
f"ensure the {platform.mac_ver()[-1]} version of "
239+
"MATLAB is installed instead."
240+
)
241+
230242
install_dir = pathlib.Path(lines[1]).parent.parent
231243
else:
232-
error = "Matlab not found, specify MATLAB location in settings i.e. 'File > Settings' menu"
233-
except FileNotFoundError:
234-
error = "Matlab engine could not be found, ensure it is installed properly"
235-
if error:
244+
raise MatlabHelper.ConfigError(
245+
"Matlab not found, specify MATLAB location in settings i.e. 'File > Settings' menu."
246+
)
247+
except (FileNotFoundError, MatlabHelper.ConfigError) as ex:
248+
if isinstance(ex, FileNotFoundError):
249+
ex.add_note("Matlab engine could not be found, ensure it is installed properly.")
236250
self.engine_output[:] = []
237-
self.engine_output.append(Exception(error))
238-
LOGGER.error(f"{error}. Attempt to read MATLAB _arch file failed {MATLAB_ARCH_FILE}.")
239-
return str(install_dir)
251+
self.engine_output.append(ex)
252+
LOGGER.error(f"Attempt to read MATLAB _arch file failed {MATLAB_ARCH_FILE}.\n {ex}.")
253+
254+
self.matlab_dir = str(install_dir)
255+
return self.matlab_dir

rascal2/dialogs/settings_dialog.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def __init__(self):
136136
label_layout.addWidget(QtWidgets.QLabel("Current Matlab Directory:"))
137137
label_layout.addStretch(1)
138138
self.matlab_path = QtWidgets.QLineEdit(self)
139-
self.matlab_path.setText(MatlabHelper().get_matlab_path())
139+
self.matlab_path.setText(MatlabHelper().matlab_dir)
140140
self.matlab_path.setReadOnly(True)
141141
self.matlab_path.setPlaceholderText("Select MATLAB directory")
142142
self.matlab_path.setFocusPolicy(QtCore.Qt.FocusPolicy.NoFocus)
@@ -180,28 +180,19 @@ def set_matlab_paths(self):
180180
if not self.changed:
181181
return
182182

183-
# should_init = False
184183
with suppress(FileNotFoundError), open(MATLAB_ARCH_FILE, "r+") as path_file:
185184
try:
186185
install_dir = pathlib.Path(self.matlab_path.text())
187-
LOGGER.info(f"install_dir: {install_dir}")
188186
if not getattr(sys, "frozen", False):
189187
return
190188

191-
# if len(path_file.readlines()) == 0:
192-
# should_init = True
193-
194189
path_file.seek(0)
195190
if platform.system() == "Windows":
196191
arch = "win64"
197192
elif platform.system() == "Darwin":
198-
if platform.mac_ver()[-1] == "arm64" and (install_dir / "bin/maca64").exists():
199-
arch = "maca64"
200-
else:
201-
arch = "maci64"
193+
arch = "maca64" if platform.mac_ver()[-1] == "arm64" else "maci64"
202194
else:
203195
arch = "glnxa64"
204-
LOGGER.info(f"arch: {arch}")
205196
path_file.writelines(
206197
[
207198
f"{arch}\n",
@@ -213,5 +204,5 @@ def set_matlab_paths(self):
213204
path_file.truncate()
214205
except Exception as ex:
215206
LOGGER.error("exception occurred", exc_info=ex)
216-
# if should_init:
207+
217208
MatlabHelper().async_start()

0 commit comments

Comments
 (0)