11"""Wrappers for the interface between RATapi and MATLAB custom files."""
2+
23import atexit
34import os
45import pathlib
56import platform
67import shutil
78import subprocess
89from contextlib import contextmanager , suppress
9- from typing import Callable , Tuple
10+ from typing import Callable
1011
1112from numpy .typing import ArrayLike
1213
1718
1819
1920def get_matlab_engine ():
21+ """Return MATLAB engine object if available else None.
22+
23+ Returns
24+ -------
25+ engine : Optional[RATapi.rat_core.MatlabEngine]
26+ A matlab engine object
27+ """
2028 return __MATLAB_ENGINE
2129
2230
@@ -37,7 +45,7 @@ def cd(new_dir: str):
3745 os .chdir (prev_dir )
3846
3947
40- def get_matlab_paths (matlab_exe_path : str ) -> Tuple [str , str ]:
48+ def get_matlab_paths (matlab_exe_path : str ) -> tuple [str , str ]:
4149 """Return paths for loading MATLAB engine C interface dynamic libraries.
4250
4351 Parameters
@@ -71,14 +79,14 @@ def get_matlab_paths(matlab_exe_path: str) -> Tuple[str, str]:
7179 return f"{ bin_path .as_posix ()} /" , f"{ dll_path .as_posix ()} /"
7280
7381
74- def start_matlab (matlab_exe_path : str = "" ):
82+ def start_matlab (matlab_exe_path : str = "" ):
7583 """Load MATLAB engine dynamic libraries and creates wrapper object.
76-
84+
7785 Parameters
7886 ----------
7987 matlab_exe_path : str, default ""
8088 The full path of the MATLAB executable
81-
89+
8290 Returns
8391 -------
8492 engine : Optional[RATapi.rat_core.MatlabEngine]
@@ -96,7 +104,7 @@ def start_matlab(matlab_exe_path: str =""):
96104 engine = RATapi .rat_core .MatlabEngine ()
97105 # Ensure MATLAB is closed when Python shuts down.
98106 atexit .register (engine .close )
99-
107+
100108 return engine
101109
102110
@@ -112,14 +120,13 @@ def set_matlab_path(matlab_exe_path: str) -> None:
112120 """
113121 if not matlab_exe_path :
114122 return
115-
123+
116124 global __MATLAB_ENGINE
117125 if __MATLAB_ENGINE is not None :
118126 __MATLAB_ENGINE .close ()
119127 atexit .unregister (__MATLAB_ENGINE .close )
120128 __MATLAB_ENGINE = start_matlab (matlab_exe_path )
121129
122-
123130 if platform .system () == "Windows" :
124131 process = subprocess .Popen (f'"{ matlab_exe_path } " -batch "comserver(\' register\' )"' )
125132 process .wait ()
@@ -148,7 +155,7 @@ def __init__(self, filename) -> None:
148155 engine .cd (str (path .parent ))
149156 engine .setFunction (path .stem )
150157
151- def getHandle (self ) -> Callable [[ArrayLike , ArrayLike , ArrayLike , int , int ], tuple [ArrayLike , float ]]:
158+ def get_handle (self ) -> Callable [[ArrayLike , ArrayLike , ArrayLike , int , int ], tuple [ArrayLike , float ]]:
152159 """Return a wrapper for the custom dynamic library function.
153160
154161 Returns
@@ -179,7 +186,7 @@ class DylibWrapper:
179186 def __init__ (self , filename , function_name ) -> None :
180187 self .engine = RATapi .rat_core .DylibEngine (filename , function_name )
181188
182- def getHandle (self ) -> Callable [[ArrayLike , ArrayLike , ArrayLike , int , int ], tuple [ArrayLike , float ]]:
189+ def get_handle (self ) -> Callable [[ArrayLike , ArrayLike , ArrayLike , int , int ], tuple [ArrayLike , float ]]:
183190 """Return a wrapper for the custom dynamic library function.
184191
185192 Returns
@@ -196,6 +203,18 @@ def handle(*args):
196203
197204
198205def find_existing_matlab () -> str :
206+ """Find existing MATLAB from cache file or checking if the MATLAB command is available.
207+
208+ Parameters
209+ ----------
210+ matlab_exe_path : str, default ""
211+ The full path of the MATLAB executable
212+
213+ Returns
214+ -------
215+ engine : Optional[RATapi.rat_core.MatlabEngine]
216+ A matlab engine object
217+ """
199218 matlab_exe_path = ""
200219
201220 with suppress (FileNotFoundError ), open (MATLAB_PATH_FILE ) as path_file :
@@ -210,7 +229,8 @@ def find_existing_matlab() -> str:
210229 if temp .is_symlink ():
211230 matlab_exe_path = temp .readlink ().as_posix ()
212231 set_matlab_path (matlab_exe_path )
213-
214- return matlab_exe_path
232+
233+ return matlab_exe_path
234+
215235
216236__MATLAB_ENGINE = start_matlab ()
0 commit comments