1+ import argparse
12import json
23import re
34from pathlib import Path
1213
1314# Create a console object for pretty printing
1415console = Console ()
16+
17+ # Compile types for each key present in MachineConfig
1518machine_config_types : dict = get_type_hints (MachineConfig )
1619
1720
@@ -69,7 +72,7 @@ def confirm_overwrite(key: str):
6972 console .print ("Invalid input. Please try again." , style = "red" )
7073
7174
72- def populate_field (key : str , field : ModelField ):
75+ def populate_field (key : str , field : ModelField , debug : bool = False ):
7376 """
7477 General function for inputting and validating the value of a single field against
7578 its Pydantic model.
@@ -90,16 +93,18 @@ def populate_field(key: str, field: ModelField):
9093
9194 validated_value , error = field .validate (value , {}, loc = key )
9295 if not error :
93- console .print (
94- f"{ key !r} validated as { type (validated_value )} : { validated_value !r} " ,
95- style = "bright_green" ,
96- )
96+ console .print (f"{ key !r} successfully validated" , style = "bright_green" )
97+ if debug :
98+ console .print (
99+ f"{ type (validated_value )} \n { validated_value !r} " ,
100+ style = "bright_green" ,
101+ )
97102 return validated_value
98103 else :
99104 console .print ("Invalid input. Please try again." , style = "red" )
100105
101106
102- def add_calibrations (key : str , field : ModelField ) -> dict :
107+ def add_calibrations (key : str , field : ModelField , debug : bool = False ) -> dict :
103108 """
104109 Populate the 'calibrations' field with dictionaries.
105110 """
@@ -110,6 +115,7 @@ def get_calibration():
110115 prompt (
111116 "What is the full file path to the calibration file? This should be a "
112117 "JSON file." ,
118+ style = "yellow" ,
113119 )
114120 )
115121 try :
@@ -137,6 +143,7 @@ def get_calibration():
137143 while add_calibration is True :
138144 calibration_type = prompt (
139145 "What type of calibration settings are you providing?" ,
146+ style = "yellow" ,
140147 ).lower ()
141148 # Check if it's a known type of calibration
142149 if calibration_type not in known_calibraions :
@@ -160,36 +167,40 @@ def get_calibration():
160167 # Add calibration to master dict
161168 calibrations [calibration_type ] = calibration_values
162169 console .print (
163- f"Added { calibration_type } to the calibrations field: { calibration_values } " ,
170+ f"Added { calibration_type } to the calibrations field" ,
164171 style = "bright_green" ,
165172 )
173+ if debug :
174+ console .print (f"{ calibration_values } " , style = "bright_green" )
166175
167176 # Check if any more calibrations need to be added
168177 add_calibration = ask_for_input (category = "calibration setting" , again = True )
169178
170179 # Validate the nested dictionary structure
171180 validated_calibrations , error = field .validate (calibrations , {}, loc = field )
172181 if not error :
173- console .print (
174- f"{ key !r} validated as { type (validated_calibrations )} : { validated_calibrations !r} " ,
175- style = "bright_green" ,
176- )
182+ console .print (f"{ key !r} validated successfully" , style = "bright_green" )
183+ if debug :
184+ console .print (
185+ f"{ type (validated_calibrations )} \n { validated_calibrations !r} " ,
186+ style = "bright_green" ,
187+ )
177188 return validated_calibrations
178189 else :
179190 console .print (
180191 f"Failed to validate the provided calibrations: { error } " , style = "red"
181192 )
182- console .print ("Returning an empty dictionary" )
193+ console .print ("Returning an empty dictionary" , style = "red" )
183194 return {}
184195
185196
186- def add_software_packages (config : dict ):
197+ def add_software_packages (config : dict , debug : bool = False ):
187198 def ask_about_xml_path () -> bool :
188199 message = (
189200 "Does this software package have a settings file that needs modification? "
190201 "(y/n)"
191202 )
192- answer = prompt (message ).lower ().strip ()
203+ answer = prompt (message , style = "yellow" ).lower ().strip ()
193204
194205 # Validate
195206 if answer in ("y" , "yes" ):
@@ -226,6 +237,7 @@ def get_xml_file() -> Optional[Path]:
226237 prompt (
227238 "What is the full file path of the settings file? This should be an "
228239 "XML file." ,
240+ style = "yellow" ,
229241 )
230242 )
231243 # Validate
@@ -242,6 +254,7 @@ def get_xml_file() -> Optional[Path]:
242254 def get_xml_tree_path () -> str :
243255 xml_tree_path = prompt (
244256 "What is the path through the XML file to the node to overwrite?" ,
257+ style = "yellow" ,
245258 )
246259 # Possibly some validation checks later
247260 return xml_tree_path
@@ -251,6 +264,7 @@ def get_file_extension() -> str:
251264 extension = prompt (
252265 "Please enter the extension of a file produced by this package "
253266 "that is to be analysed (e.g., '.tiff', '.eer', etc.)." ,
267+ style = "yellow" ,
254268 ).strip ()
255269 # Validate
256270 if not (extension .startswith ("." ) and extension .replace ("." , "" ).isalnum ()):
@@ -268,6 +282,7 @@ def get_file_substring() -> str:
268282 substring = prompt (
269283 "Please enter a keyword that will be present in files with this "
270284 "extension. This field is case-sensitive." ,
285+ style = "yellow" ,
271286 ).strip ()
272287 # Validate
273288 if bool (re .fullmatch (r"[\w\s\-]*" , substring )) is False :
@@ -333,6 +348,7 @@ def get_file_substring() -> str:
333348 version = prompt (
334349 "What is the version number of this software package? Press Enter to leave "
335350 "it blank if you're unsure." ,
351+ style = "yellow" ,
336352 )
337353
338354 console .print ("software_settings_output_directories" , style = "bold bright_cyan" )
@@ -400,9 +416,13 @@ def get_file_substring() -> str:
400416 if not error :
401417 config [field_name ] = validated_value
402418 console .print (
403- f"{ field_name !r} validated as { type (validated_value )} : { validated_value !r} " ,
404- style = "bright_green" ,
419+ f"{ field_name !r} validated successfully" , style = "bright_green"
405420 )
421+ if debug :
422+ console .print (
423+ f"{ type (validated_value )} \n { validated_value !r} " ,
424+ style = "bright_green" ,
425+ )
406426 else :
407427 console .print (
408428 f"Validation failed due to the following error: { error } " ,
@@ -415,20 +435,23 @@ def get_file_substring() -> str:
415435 return config
416436
417437
418- def set_up_data_transfer (config : dict ) -> dict :
438+ def set_up_data_transfer (config : dict , debug : bool = False ) -> dict :
419439 return config
420440
421441
422- def set_up_data_processing (config : dict ) -> dict :
442+ def set_up_data_processing (config : dict , debug : bool = False ) -> dict :
423443 return config
424444
425445
426- def set_up_external_executables (config : dict ) -> dict :
446+ def set_up_external_executables (config : dict , debug : bool = False ) -> dict :
427447 return config
428448
429449
430- def run ():
431- new_config = {}
450+ def set_up_machine_config (debug : bool = False ):
451+ """
452+ Main function which runs through the setup process.
453+ """
454+ new_config : dict = {}
432455 for key , field in MachineConfig .__fields__ .items ():
433456 """
434457 Logic for complicated or related fields
@@ -438,12 +461,12 @@ def run():
438461 new_config [key ] = True if camera .lower ().startswith ("gatan" ) else False
439462 continue
440463 if key == "calibrations" :
441- new_config [key ] = add_calibrations (key , field )
464+ new_config [key ] = add_calibrations (key , field , debug )
442465 continue
443466
444467 # Acquisition software block
445468 if key == "acquisition_software" :
446- new_config = add_software_packages (new_config )
469+ new_config = add_software_packages (new_config , debug )
447470 continue
448471 if key in (
449472 "software_versions" ,
@@ -466,7 +489,7 @@ def run():
466489 # Data transfer block
467490 if key == "data_transfer_enabled" :
468491 # TODO: Set up data transfer settings in a separate function
469- new_config = set_up_data_transfer (new_config )
492+ new_config = set_up_data_transfer (new_config , debug )
470493 continue
471494 if key in (
472495 "allow_removal" ,
@@ -481,7 +504,7 @@ def run():
481504
482505 # Data processing block
483506 if key == "processing_enabled" :
484- new_config = set_up_data_processing (new_config )
507+ new_config = set_up_data_processing (new_config , debug )
485508 continue
486509 if key in (
487510 "process_by_default" ,
@@ -500,7 +523,7 @@ def run():
500523 # External plugins and executables block
501524 if key == "external_executables" :
502525 # TODO: Set up external plugins and exectuables
503- new_config = set_up_external_executables (new_config )
526+ new_config = set_up_external_executables (new_config , debug )
504527 continue
505528 if key in ("external_executables_eer" , "external_environment" ):
506529 continue
@@ -514,7 +537,7 @@ def run():
514537 Standard method of inputting values
515538 """
516539
517- new_config [key ] = populate_field (key , field )
540+ new_config [key ] = populate_field (key , field , debug )
518541
519542 # Validate the entire config again and convert into JSON/YAML-safe dict
520543 try :
@@ -535,11 +558,14 @@ def run():
535558 }
536559
537560 # Create save path for config
561+ console .print ("Machine config successfully validated." , style = "green" )
538562 config_name = prompt (
539- "Machine config successfully validated. What would you like to name the file? "
540- "(E.g. 'my_machine_config')"
563+ "What would you like to name the file? (E.g. 'my_machine_config')" ,
564+ style = "yellow" ,
565+ )
566+ config_path = Path (
567+ prompt ("Where would you like to save this config?" , style = "yellow" )
541568 )
542- config_path = Path (prompt ("Where would you like to save this config?" ))
543569 config_file = config_path / f"{ config_name } .yaml"
544570 config_path .mkdir (parents = True , exist_ok = True )
545571
@@ -579,3 +605,18 @@ def run():
579605 return run ()
580606 console .print ("Exiting machine configuration setup guide" , style = "bright_green" )
581607 exit ()
608+
609+
610+ def run ():
611+ # Set up arg parser
612+ parser = argparse .ArgumentParser ()
613+ parser .add_argument (
614+ "--debug" ,
615+ action = "store_true" ,
616+ help = "Prints additional messages to show setup progress." ,
617+ )
618+ args = parser .parse_args ()
619+
620+ set_up_machine_config (args .debug )
621+
622+ pass
0 commit comments