33from typing import Optional
44from dataclasses import dataclass , asdict
55import zipfile
6+ import logging
67
78
89@dataclass
@@ -11,7 +12,7 @@ class SPMConvertInputParameters:
1112 eln : str | Path
1213 expriement_type : str
1314 reader : str = "spm"
14- output : str = None
15+ output : Optional [ str | Path ] = None
1516 nxdl : Optional [str ] = None
1617 create_zip : Optional [bool ] = True
1718 zip_file_path : Optional [str | Path ] = None
@@ -22,6 +23,9 @@ class SPMConvertInputParameters:
2223
2324def convert_spm_experiments (
2425 input_params : SPMConvertInputParameters ,
26+ converter_logger : Optional [logging .Logger ],
27+ converter_handeler : Optional [logging .Handler ] = None ,
28+
2529):
2630 """Convert SPM (STS, STM and AFM) experirment data files to NeXus format.
2731 Later, the input files and generated output file are zipped together to
@@ -38,19 +42,19 @@ def convert_spm_experiments(
3842 """
3943
4044 if not isinstance (input_params , SPMConvertInputParameters ):
41- raise ValueError (
45+ converter_logger . error (
4246 "Input parameters must be an instance of SPMConvertInputParameters"
4347 )
4448
4549 if not input_params .input_file :
46- raise ValueError ("Input files are required to run an SPM experiment" )
50+ converter_logger . error ("Input files are required to run an SPM experiment" )
4751 if not input_params .eln :
48- raise ValueError (
49- f"ELN is required to run an { input_params .expriement_type } experiment."
50- )
52+ converter_logger .error (f"ELN file is requred to run an { input_params .expriement_type } experiment" )
5153
5254 if not input_params .expriement_type :
53- raise ValueError ("Experiment type is required to run an SPM experiment" )
55+ converter_logger .error (
56+ "Experiment type is required to run an SPM experiment"
57+ )
5458
5559 input_params .input_file = (* input_params .input_file , input_params .eln )
5660 input_params .input_file = tuple (
@@ -67,6 +71,7 @@ def convert_spm_experiments(
6771 for file in input_params .input_file :
6872 if file .suffix in (
6973 input_params .raw_extension ,
74+ # TODO remoce the following line
7075 f".{ input_params .raw_extension } " ,
7176 ):
7277 if input_params .output is None :
@@ -78,27 +83,37 @@ def convert_spm_experiments(
7883 "Valid raw files and extension is required to run an SPM experiment"
7984 )
8085 # TODO Try with input_file as tuple of Path objects
81- input_params .input_file = [str (file ) for file in input_params .input_file ]
82- input_params .output = str (input_params .output )
86+ # Use handler only for conver function. Do not close the handler
87+ # after the function call as it will be used again and again
88+ if converter_handeler not in converter_logger .handlers :
89+ converter_logger .addHandler (converter_handeler )
8390 try :
84- convert (** asdict (input_params ))
85-
91+ kwargs = asdict (input_params )
92+ print ("#### kwargs:" , kwargs )
93+ kwargs ["input_file" ] = tuple (map (str , input_params .input_file ))
94+ kwargs ["output" ] = str (input_params .output )
95+ # with converter_logger:
96+ convert (** kwargs )
97+ print ("#### kwargs after conversion:" , kwargs )
8698 if input_params .create_zip :
8799 with zipfile .ZipFile (zip_file , "w" ) as zipf :
88100 zipf .write (
89- input_params .output , arcname = input_params .output .split ("/" )[- 1 ]
101+ str (input_params .output ),
102+ arcname = str (input_params .output ).split ("/" )[- 1 ],
90103 )
91- for file in input_params .input_file :
104+ for file in map ( str , input_params .input_file ) :
92105 zipf .write (file , arcname = file .split ("/" )[- 1 ])
93106 input_params .zip_file_path = Path (zip_file )
94- input_params . output = Path ( input_params . output )
107+
95108 except Exception as e :
96109 print ("NeXusConverterError:" , e )
97110 finally :
98- input_params .input_file = tuple (
99- Path (file ) if isinstance (file , str ) else file
100- for file in input_params .input_file
101- )
111+ converter_logger .removeHandler (converter_handeler )
112+ # finally:
113+ # input_params.input_file = tuple(
114+ # Path(file) if isinstance(file, str) else file
115+ # for file in input_params.input_file
116+ # )
102117
103118 return input_params
104119
0 commit comments