@@ -169,7 +169,7 @@ def get_subfolders_generated_by(path: Path, generator_name: str) -> list[Path]:
169
169
# Execute either <algo_name>.yml or <algo_name>.py
170
170
# The fist one must be executed on an instance of DataFolder
171
171
# The second has not this constraint (any folder, eg the parent folder of many DataFolder)
172
- def run (path : Path , algo_name : str , arguments_as_list : list = list ()):
172
+ def run (path : Path , algo_name : str , arguments_as_list : list = list (), silent_output : bool = None ):
173
173
YAML_filepath : Path = Path ('definitions/algorithms' ) / (algo_name + '.yml' )
174
174
if not YAML_filepath .exists ():
175
175
# it can be the name of a custom algorithm, defined in an <algo_name>.py
@@ -178,7 +178,6 @@ def run(path: Path, algo_name: str, arguments_as_list: list = list()):
178
178
log .error (f"Cannot run '{ algo_name } ' because neither { YAML_filepath } nor { script_filepath } exist" )
179
179
exit (1 )
180
180
181
- # command = f"{script_filepath} {' '.join(arguments_as_list)}"
182
181
spec = importlib .util .spec_from_file_location (
183
182
name = "ext_module" ,
184
183
location = script_filepath ,
@@ -187,10 +186,11 @@ def run(path: Path, algo_name: str, arguments_as_list: list = list()):
187
186
spec .loader .exec_module (ext_module )
188
187
189
188
console = Console ()
190
- console .print (Rule (f'beginning of [magenta]{ script_filepath } [/]' ))
191
- # completed_process = subprocess_tee.run(command, shell=True, capture_output=True, tee=True)
192
- ext_module .main (path ,arguments_as_list )
193
- console .print (Rule (f'end of [magenta]{ script_filepath } [/]' ))
189
+ if not silent_output :
190
+ console .print (Rule (f'beginning of [magenta]{ script_filepath } [/]' ))
191
+ ext_module .main (path ,arguments_as_list ,silent_output )
192
+ if not silent_output :
193
+ console .print (Rule (f'end of [magenta]{ script_filepath } [/]' ))
194
194
exit (0 )
195
195
# convert arguments to a dict
196
196
# -> from ['arg1=value', 'arg2=value'] to {'arg1': 'value', 'arg2': 'value'}
@@ -203,7 +203,7 @@ def run(path: Path, algo_name: str, arguments_as_list: list = list()):
203
203
log .error (f"No '=' in supplemental argument '{ arg } '" )
204
204
exit (1 )
205
205
data_folder = DataFolder (path )
206
- data_folder .run (algo ,arguments )
206
+ data_folder .run (algo ,arguments , silent_output )
207
207
208
208
class DataFolderInstantiationError (Exception ):
209
209
"""
@@ -336,7 +336,7 @@ def get_closest_parent_of_type(self, data_folder_type: str, check_self = True):
336
336
return parent
337
337
return parent .get_closest_parent_of_type (data_folder_type ,False )
338
338
339
- def execute_algo_preprocessing (self , console : Console , algo_name : str , output_subfolder : Path , arguments : dict ) -> dict :
339
+ def execute_algo_preprocessing (self , console : Console , algo_name : str , output_subfolder : Path , arguments : dict , silent_output : bool ) -> dict :
340
340
script_filepath : Path = Path ('definitions/algorithms' ) / (algo_name + '.pre.py' )
341
341
if not script_filepath .exists ():
342
342
return dict () # no preprocessing defined for this algorithm
@@ -347,12 +347,14 @@ def execute_algo_preprocessing(self, console: Console, algo_name: str, output_su
347
347
)
348
348
ext_module = importlib .util .module_from_spec (spec )
349
349
spec .loader .exec_module (ext_module )
350
- console .print (Rule (f'beginning of { script_filepath .name } pre_processing()' ))
351
- data_from_preprocessing = ext_module .pre_processing (self ,output_subfolder ,arguments )
352
- console .print (Rule (f'end of { script_filepath .name } pre_processing()' ))
350
+ if not silent_output :
351
+ console .print (Rule (f'beginning of { script_filepath .name } pre_processing()' ))
352
+ data_from_preprocessing = ext_module .pre_processing (self ,output_subfolder ,arguments ,silent_output )
353
+ if not silent_output :
354
+ console .print (Rule (f'end of { script_filepath .name } pre_processing()' ))
353
355
return data_from_preprocessing
354
356
355
- def execute_algo_postprocessing (self , console : Console , algo_name : str , output_subfolder : Optional [Path ], arguments : dict , data_from_preprocessing : dict ) -> dict :
357
+ def execute_algo_postprocessing (self , console : Console , algo_name : str , output_subfolder : Optional [Path ], arguments : dict , data_from_preprocessing : dict , silent_output : bool ) -> dict :
356
358
script_filepath : Path = Path ('definitions/algorithms' ) / (algo_name + '.post.py' )
357
359
if not script_filepath .exists ():
358
360
return # no postprocessing defined for this algorithm
@@ -363,14 +365,16 @@ def execute_algo_postprocessing(self, console: Console, algo_name: str, output_s
363
365
)
364
366
ext_module = importlib .util .module_from_spec (spec )
365
367
spec .loader .exec_module (ext_module )
366
- console .print (Rule (f'beginning of { script_filepath .name } post_processing()' ))
368
+ if not silent_output :
369
+ console .print (Rule (f'beginning of { script_filepath .name } post_processing()' ))
367
370
if output_subfolder is None : # post-processing of a transformative algorithme
368
- ext_module .post_processing (self ,arguments ,data_from_preprocessing )
371
+ ext_module .post_processing (self ,arguments ,data_from_preprocessing , silent_output )
369
372
else : # post-processing of a generative algorithm
370
- ext_module .post_processing (self ,output_subfolder ,arguments ,data_from_preprocessing )
371
- console .print (Rule (f'end of { script_filepath .name } post_processing()' ))
373
+ ext_module .post_processing (self ,output_subfolder ,arguments ,data_from_preprocessing ,silent_output )
374
+ if not silent_output :
375
+ console .print (Rule (f'end of { script_filepath .name } post_processing()' ))
372
376
373
- def run (self , algo_name : str , arguments : dict = dict ()):
377
+ def run (self , algo_name : str , arguments : dict = dict (), silent_output : bool = False ):
374
378
YAML_filepath : Path = Path ('definitions/algorithms' ) / (algo_name + '.yml' )
375
379
if not YAML_filepath .exists ():
376
380
log .error (f"Cannot run '{ algo_name } ' because { YAML_filepath } does not exist" )
@@ -497,18 +501,14 @@ def run(self, algo_name: str, arguments: dict = dict()):
497
501
console = Console ()
498
502
with console .status (f'Executing [bold yellow]{ algo_name } [/] on [bold cyan]{ collapseuser (self .path )} [/]...' ) as status :
499
503
# execute preprocessing
500
- data_from_preprocessing = self .execute_algo_preprocessing (console ,algo_name ,output_folder_path ,all_arguments )
504
+ data_from_preprocessing = self .execute_algo_preprocessing (console ,algo_name ,output_folder_path ,all_arguments , silent_output )
501
505
# execute the command line
502
- if 'tee' not in YAML_content [self .type ]:
503
- log .error (f"{ YAML_filepath } has no '{ self .type } /tee' entry" )
504
- exit (1 )
505
- tee = YAML_content [self .type ]['tee' ]
506
- if tee :
506
+ if not silent_output :
507
507
console .print (Rule (f'beginning of [magenta]{ collapseuser (executable_path )} ' ))
508
508
chrono_start = time .monotonic ()
509
- completed_process = subprocess_tee .run (command_line , shell = True , capture_output = True , tee = tee )
509
+ completed_process = subprocess_tee .run (command_line , shell = True , capture_output = True , tee = ( not silent_output ) )
510
510
chrono_stop = time .monotonic ()
511
- if tee :
511
+ if not silent_output :
512
512
console .print (Rule (f'end of [magenta]{ collapseuser (executable_path )} ' ))
513
513
# write stdout and stderr
514
514
if completed_process .stdout != '' : # if the subprocess wrote something in standard output
@@ -531,7 +531,7 @@ def run(self, algo_name: str, arguments: dict = dict()):
531
531
with open (info_file_path ,'w' ) as file :
532
532
json .dump (info_file , file , sort_keys = True , indent = 4 )
533
533
# execute postprocessing
534
- self .execute_algo_postprocessing (console ,algo_name ,output_folder_path ,all_arguments ,data_from_preprocessing )
534
+ self .execute_algo_postprocessing (console ,algo_name ,output_folder_path ,all_arguments ,data_from_preprocessing , silent_output )
535
535
536
536
if __name__ == "__main__" :
537
537
0 commit comments