@@ -42,14 +42,14 @@ class SeverityLevelEnum(Enum):
4242 import subprocess
4343
4444
45- def log (message : str , level : Optional [SeverityLevelEnum ] = None , force_verbose = None ):
45+ def log (message : str , level : Optional [SeverityLevelEnum ] = None , force_verbose : Optional [ bool ] = None ):
4646 """
4747 Log a message based on the VERBOSE flag in the caller's globals().
4848
4949 Args:
5050 message (str): The message to log.
51- level (SeverityLevelEnum): The severity level of the message (e.g., INFO, WARNING, ERROR).
52- force_verbose (bool): If True, log the message regardless of the VERBOSE flag in globals()
51+ level (SeverityLevelEnum, optional ): The severity level of the message (e.g., INFO, WARNING, ERROR).
52+ force_verbose (bool, optional ): If True, log the message regardless of the VERBOSE flag in globals().
5353 """
5454 if force_verbose is True :
5555 should_log = True
@@ -71,11 +71,12 @@ def log(message: str, level: Optional[SeverityLevelEnum] = None, force_verbose=N
7171 print (f"{ level .value } : { message } " )
7272
7373
74- async def install_package_pyodide (pkg : str , verbose = True ):
74+ async def install_package_pyodide (pkg : str , verbose : bool = True ):
7575 """
7676 Install a package in a Pyodide environment.
77+
7778 Args:
78- pkg (string ): The name of the package to install.
79+ pkg (str ): The name of the package to install.
7980 verbose (bool): Whether to print the name of the installed package.
8081 """
8182 is_url = pkg .startswith ("http://" ) or pkg .startswith ("https://" ) or pkg .startswith ("emfs:/" )
@@ -86,24 +87,26 @@ async def install_package_pyodide(pkg: str, verbose=True):
8687 log (f"Installed { pkg_name } " , force_verbose = verbose )
8788
8889
89- def install_package_python (pkg : str , verbose = True ):
90+ def install_package_python (pkg : str , verbose : bool = True ):
9091 """
9192 Install a package in a standard Python environment.
93+
9294 Args:
93- pkg (string ): The name of the package to install.
95+ pkg (str ): The name of the package to install.
9496 verbose (bool): Whether to print the name of the installed package.
9597 """
9698 subprocess .check_call ([sys .executable , "-m" , "pip" , "install" , pkg ])
9799 if verbose :
98100 log (f"Installed { pkg } " , force_verbose = verbose )
99101
100102
101- async def install_packages (notebook_name : str , requirements_path = "config.yml" , verbose = True ):
103+ async def install_packages (notebook_name : str , requirements_path : str = "config.yml" , verbose : bool = True ):
102104 """
103105 Install the packages listed in the requirements file for the notebook with the given name.
106+
104107 Args:
105- notebook_name (string ): The name of the notebook for which to install packages.
106- requirements_path (string ): The path to the requirements file.
108+ notebook_name (str ): The name of the notebook for which to install packages.
109+ requirements_path (str ): The path to the requirements file.
107110 verbose (bool): Whether to print the names of the installed packages and status of installation.
108111 """
109112 if ENVIRONMENT == EnvironmentEnum .PYODIDE :
@@ -167,7 +170,7 @@ def set_data_pyodide(key: str, value: Any):
167170 through a JavaScript function defined in the JupyterLite extension `data_bridge`.
168171
169172 Args:
170- key (string ): The name under which data will be sent.
173+ key (str ): The name under which data will be sent.
171174 value (Any): The value to send to the host environment.
172175 """
173176 serialized_data = json .dumps ({key : value })
@@ -189,8 +192,9 @@ def set_data_pyodide(key: str, value: Any):
189192def set_data_python (key : str , value : Any ):
190193 """
191194 Write data to the `uploads` folder in a JupyterLab environment.
195+
192196 Args:
193- key (string ): The name under which data will be written.
197+ key (str ): The name under which data will be written.
194198 value (Any): The value to write to the `uploads` folder.
195199 """
196200 if not os .path .exists (UPLOADS_FOLDER ):
@@ -206,8 +210,9 @@ def set_data_python(key: str, value: Any):
206210def set_data (key : str , value : Any ):
207211 """
208212 Switch between the two functions `set_data_pyodide` and `set_data_python` based on the environment.
213+
209214 Args:
210- key (string ): The name under which data will be written or sent.
215+ key (str ): The name under which data will be written or sent.
211216 value (Any): The value to write or send.
212217 """
213218 if ENVIRONMENT == EnvironmentEnum .PYODIDE :
@@ -219,20 +224,22 @@ def set_data(key: str, value: Any):
219224def get_data_pyodide (key : str , globals_dict : Optional [Dict ] = None ):
220225 """
221226 Load data from the host environment into globals()[key] variable.
227+
222228 Args:
223- key (string ): global variable name to store the received data.
224- globals_dict (dict): globals() dictionary of the current scope.
229+ key (str ): Global variable name to store the received data.
230+ globals_dict (dict, optional ): globals() dictionary of the current scope.
225231 """
226232 if globals_dict is not None :
227- globals_dict [key ] = globals_dict [ "data_from_host" ]
233+ globals_dict [key ] = globals_dict . get ( "data_from_host" , None )
228234
229235
230236def get_data_python (key : str , globals_dict : Optional [Dict ] = None ):
231237 """
232238 Read data from the `uploads` folder in a JupyterLab environment.
239+
233240 Args:
234- key (string ): The name under which data is expected to be received.
235- globals_dict (dict): A dictionary to store the received data. Defaults to None.
241+ key (str ): The name under which data is expected to be received.
242+ globals_dict (dict, optional ): A dictionary to store the received data. Defaults to None.
236243 """
237244 try :
238245 data_from_host = []
@@ -254,9 +261,10 @@ def get_data_python(key: str, globals_dict: Optional[Dict] = None):
254261def get_data (key : str , globals_dict : Optional [Dict ] = None ):
255262 """
256263 Switch between the two functions `get_data_pyodide` and `get_data_python` based on the environment.
264+
257265 Args:
258- key (string ): The name under which data is expected to be received.
259- globals_dict (dict): A dictionary to store the received data. Defaults to None.
266+ key (str ): The name under which data is expected to be received.
267+ globals_dict (dict, optional ): A dictionary to store the received data. Defaults to None.
260268 """
261269 if ENVIRONMENT == EnvironmentEnum .PYODIDE :
262270 get_data_pyodide (key , globals_dict )
@@ -269,7 +277,7 @@ def get_materials(globals_dict: Optional[Dict] = None) -> List[Any]:
269277 Retrieve materials from the environment and assign them to globals_dict["materials_in"].
270278
271279 Args:
272- globals_dict (dict): The globals dictionary to populate.
280+ globals_dict (dict, optional ): The globals dictionary to populate.
273281
274282 Returns:
275283 List[Material]: A list of Material objects.
@@ -309,14 +317,14 @@ def set_materials(materials: List[Any]):
309317 set_data ("materials" , materials_data )
310318
311319
312- def load_materials_from_folder (folder_path : Optional [str ] = None ) :
320+ def load_materials_from_folder (folder_path : Optional [str ] = None , verbose : bool = True ) -> List [ Any ] :
313321 """
314322 Load materials from the specified folder or from the UPLOADS_FOLDER by default.
315323
316324 Args:
317325 folder_path (Optional[str]): The path to the folder containing material files.
318326 If not provided, defaults to the UPLOADS_FOLDER.
319- verbose: set to be verbose
327+ verbose (bool): Whether to log verbose messages.
320328
321329 Returns:
322330 List[Material]: A list of Material objects loaded from the folder.
@@ -326,7 +334,7 @@ def load_materials_from_folder(folder_path: Optional[str] = None):
326334 folder_path = folder_path or UPLOADS_FOLDER
327335
328336 if not os .path .exists (folder_path ):
329- log (f"Folder '{ folder_path } ' does not exist." , SeverityLevelEnum .ERROR )
337+ log (f"Folder '{ folder_path } ' does not exist." , SeverityLevelEnum .ERROR , force_verbose = verbose )
330338 return []
331339
332340 data_from_host = []
@@ -337,54 +345,67 @@ def load_materials_from_folder(folder_path: Optional[str] = None):
337345 with open (os .path .join (folder_path , filename ), "r" ) as file :
338346 data = json .load (file )
339347 name = os .path .splitext (filename )[0 ]
340- log (f"{ index } : { name } " )
348+ log (f"{ index } : { name } " , SeverityLevelEnum . INFO , force_verbose = verbose )
341349 index += 1
342350 data_from_host .append (data )
343351 except FileNotFoundError :
344- log (f"No data found in the '{ folder_path } ' folder." , SeverityLevelEnum .ERROR )
352+ log (f"No data found in the '{ folder_path } ' folder." , SeverityLevelEnum .ERROR , force_verbose = verbose )
345353 return []
346354
347355 materials = [Material (item ) for item in data_from_host ]
348356
349357 if materials :
350- log (f"Successfully loaded { len (materials )} materials from folder '{ folder_path } '" , SeverityLevelEnum .INFO )
358+ log (
359+ f"Successfully loaded { len (materials )} materials from folder '{ folder_path } '" ,
360+ SeverityLevelEnum .INFO ,
361+ force_verbose = verbose ,
362+ )
351363 else :
352- log (f"No materials found in folder '{ folder_path } '" , SeverityLevelEnum .WARNING )
364+ log (f"No materials found in folder '{ folder_path } '" , SeverityLevelEnum .WARNING , force_verbose = verbose )
353365
354366 return materials
355367
356368
357- def load_material_from_folder (folder_path : str , name : str ) -> Optional [Any ]:
369+ def load_material_from_folder (folder_path : str , name : str , verbose : bool = True ) -> Optional [Any ]:
358370 """
359371 Load a single material from the specified folder by matching a substring of the name.
360372
361373 Args:
362374 folder_path (str): The path to the folder containing material files.
363375 name (str): The substring of the name of the material to load.
376+ verbose (bool): Whether to log verbose messages.
364377
365378 Returns:
366379 Optional[Material]: The first Material object that contains the name substring, or None if not found.
367380 """
368- materials = load_materials_from_folder (folder_path )
381+ # Reuse the existing function to load all materials from the folder
382+ materials = load_materials_from_folder (folder_path , verbose = verbose )
369383 for material in materials :
370384 if name .lower () in material .name .lower ():
371385 log (
372- f"Material containing ' { name } ' found : '{ material .name } ' in folder ' { folder_path } '. " ,
386+ f"Found : '{ material .name } '" ,
373387 SeverityLevelEnum .INFO ,
388+ force_verbose = verbose ,
374389 )
375390 return material
376- log (f"No material containing '{ name } ' found in folder '{ folder_path } '." , SeverityLevelEnum .WARNING )
391+
392+ log (
393+ f"No material containing '{ name } ' found in folder '{ folder_path } '." ,
394+ SeverityLevelEnum .WARNING ,
395+ force_verbose = verbose ,
396+ )
377397 return None
378398
379399
380- def write_materials_to_folder (materials : List [Any ], folder_path : Optional [str ] = None ):
400+ def write_materials_to_folder (materials : List [Any ], folder_path : Optional [str ] = None , verbose : bool = True ):
381401 """
382402 Write materials to the specified folder or to the UPLOADS_FOLDER by default.
383403
384404 Args:
385405 materials (List[Material]): The list of Material objects to write to the folder.
386406 folder_path (Optional[str]): The path to the folder where the materials will be written.
387407 If not provided, defaults to the UPLOADS_FOLDER.
408+ verbose (bool): Whether to log verbose messages.
388409 """
389410 from mat3ra .utils .array import convert_to_array_if_not
390411
@@ -393,10 +414,12 @@ def write_materials_to_folder(materials: List[Any], folder_path: Optional[str] =
393414
394415 if not os .path .exists (folder_path ):
395416 os .makedirs (folder_path )
417+ if verbose :
418+ log (f"Created folder '{ folder_path } '." , SeverityLevelEnum .INFO )
396419
397420 for material in materials :
398421 safe_name = material .name .replace ("%" , "pct" ).replace ("/" , ":" )
399422 file_path = os .path .join (folder_path , f"{ safe_name } .json" )
400423 with open (file_path , "w" ) as file :
401424 json .dump (material .to_json (), file )
402- log (f"Material '{ material .name } ' written to '{ file_path } '" )
425+ log (f"Material '{ material .name } ' written to '{ file_path } '" , SeverityLevelEnum . INFO , force_verbose = verbose )
0 commit comments