33"""
44
55import io
6+ import logging
67import os
78import os .path
89import textwrap
1617 "verbose" ,
1718]
1819
20+ logger = logging .getLogger (__name__ )
21+
1922
2023def _print_important_message (header , message , no_wrap = None ):
2124 print ("-" * 70 )
@@ -156,7 +159,7 @@ def set_value(subconf, key, val):
156159 set_value (config , key , val )
157160
158161
159- def just_the_step_from_cmdline (args , cls = None ):
162+ def just_the_step_from_cmdline (args , cls = None , apply_log_cfg = False ):
160163 """
161164 Create a step from a configuration file and return it. Don't run it.
162165
@@ -168,6 +171,10 @@ def just_the_step_from_cmdline(args, cls=None):
168171 cls : Step class
169172 The step class to use. If none is provided, the step
170173
174+ apply_log_cfg : bool
175+ If True, apply the logging configuration. If False,
176+ any provided log configuration will be ignored.
177+
171178 Returns
172179 -------
173180 step : Step instance
@@ -247,7 +254,6 @@ def just_the_step_from_cmdline(args, cls=None):
247254 )
248255 step_class = cls
249256
250- log_config = None
251257 if known .verbose :
252258 if known .logcfg is not None :
253259 raise ValueError (
@@ -259,14 +265,18 @@ def just_the_step_from_cmdline(args, cls=None):
259265 if not os .path .exists (known .logcfg ):
260266 raise OSError (f"Logging config { known .logcfg !r} not found" )
261267 log_config = known .logcfg
268+ else :
269+ log_config = log ._find_logging_config_file ()
262270
263- if log_config is not None :
264- try :
265- log .load_configuration (log_config )
266- except Exception as e :
267- raise ValueError (
268- f"Error parsing logging config { log_config !r} :\n { e } "
269- ) from e
271+ try :
272+ log_cfg = log .load_configuration (log_config )
273+ except Exception as e :
274+ raise ValueError (
275+ f"Error parsing logging config { log_config !r} :\n { e } "
276+ ) from e
277+ # globally apply the logging configuration since we're in cmdline mode
278+ if apply_log_cfg :
279+ log_cfg .apply ()
270280 except Exception as e :
271281 _print_important_message ("ERROR PARSING CONFIGURATION:" , str (e ))
272282 parser1 .print_help ()
@@ -318,15 +328,13 @@ def just_the_step_from_cmdline(args, cls=None):
318328 input_file , disable = disable_crds_steppars
319329 )
320330 except (FileNotFoundError , OSError ):
321- log .log .warning (
322- "Unable to open input file, cannot get parameters from CRDS"
323- )
331+ logger .warning ("Unable to open input file, cannot get parameters from CRDS" )
324332 else :
325333 if config :
326334 config_parser .merge_config (parameter_cfg , config )
327335 config = parameter_cfg
328336 else :
329- log . log .info ("No input file specified, unable to retrieve parameters from CRDS" )
337+ logger .info ("No input file specified, unable to retrieve parameters from CRDS" )
330338
331339 # This is where the step is instantiated
332340 try :
@@ -350,7 +358,7 @@ def just_the_step_from_cmdline(args, cls=None):
350358 # Save the step configuration
351359 if known .save_parameters :
352360 step .export_config (known .save_parameters , include_metadata = True )
353- log . log .info (f"Step/Pipeline parameters saved to '{ known .save_parameters } '" )
361+ logger .info (f"Step/Pipeline parameters saved to '{ known .save_parameters } '" )
354362
355363 return step , step_class , positional , debug_on_exception
356364
@@ -378,9 +386,17 @@ def step_from_cmdline(args, cls=None):
378386 will be set as member variables on the returned `Step`
379387 instance.
380388 """
381- step , step_class , positional , debug_on_exception = just_the_step_from_cmdline (
382- args , cls
383- )
389+ try :
390+ step , step_class , positional , debug_on_exception = just_the_step_from_cmdline (
391+ args ,
392+ cls ,
393+ apply_log_cfg = True ,
394+ )
395+ except Exception as e :
396+ # since we applied a log config above, undo it
397+ if log .LogConfig .applied is not None :
398+ log .LogConfig .applied .undo ()
399+ raise e
384400
385401 try :
386402 profile_path = os .environ .pop ("STPIPE_PROFILE" , None )
@@ -399,6 +415,10 @@ def step_from_cmdline(args, cls=None):
399415 pdb .post_mortem ()
400416 else :
401417 raise
418+ finally :
419+ # since we applied a log config above, undo it
420+ if log .LogConfig .applied is not None :
421+ log .LogConfig .applied .undo ()
402422
403423 return step
404424
0 commit comments