@@ -251,3 +251,83 @@ def run_stdout_factory(package: str) -> Callable[..., Any]:
251251 A partial ``run_stdout()`` method for the package
252252 """
253253 return functools .partial (run_stdout , package )
254+
255+
256+ def pre_run_interactive (package : str , action : str , * args : Any , ** _kwargs : dict [str , Any ]) -> Any :
257+ """Call the given action's ``pre_run()`` method.
258+
259+ Args:
260+ package: The name of the package
261+ action: The name of the action
262+ *args: The arguments passed to the action's run method
263+ **_kwargs: The keyword arguments passed to the action's run
264+ method
265+
266+ Returns:
267+ The outcome of running the action's run method
268+ """
269+ action_cls = get (package , action )
270+ app , interaction = args
271+ app_action = action_cls (app .args )
272+ supports_interactive = hasattr (app_action , "pre_run" )
273+ if not supports_interactive :
274+ logger .error ("Subcommand '%s' does not support mode interactive" , action )
275+ pre_run_action = app_action .pre_run if supports_interactive else app_action .no_interactive_mode
276+
277+ # Allow tracebacks to bring down the UI, used in tests
278+ if os .getenv ("ANSIBLE_NAVIGATOR_ALLOW_UI_TRACEBACK" ) == "true" :
279+ return pre_run_action (app = app , interaction = interaction )
280+
281+ # Capture and show a UI notification
282+ try :
283+ return pre_run_action (app = app , interaction = interaction )
284+ except Exception :
285+ logger .critical ("Subcommand '%s' encountered a fatal error." , action )
286+ logger .exception ("Logging an uncaught exception" )
287+ warn_msg = [f"Unexpected errors were encountered while performing pre-run for '{ action } '." ]
288+ warn_msg .append ("Please log an issue with the log file contents." )
289+ warning = error_notification (warn_msg )
290+ interaction .ui .show_form (warning )
291+ return None
292+
293+
294+ def pre_run_interactive_factory (package : str ) -> Callable [..., Any ]:
295+ """Create a ``pre_run_interactive()`` function for one package.
296+
297+ Args:
298+ package: The name of the package
299+
300+ Returns:
301+ A partial ``pre_run_interactive()`` method for the package
302+ """
303+ return functools .partial (pre_run_interactive , package )
304+
305+
306+ def pre_run_stdout (package : str , action : str , * args : Any , ** _kwargs : dict [str , Any ]) -> RunStdoutReturn :
307+ """Call the given action's ``pre_run_stdout`` method.
308+
309+ Args:
310+ package: The name of the package
311+ action: The name of the action
312+ *args: The arguments passed to the action's pre_run_stdout method
313+ **_kwargs: The keyword arguments passed to the action's
314+ pre_run_stdout method
315+
316+ Returns:
317+ The outcome of running the action's ``pre_run_stdout()`` method
318+ """
319+ action_cls = get (package , action )
320+ args = args [0 ]
321+ return action_cls (args ).pre_run_stdout ()
322+
323+
324+ def pre_run_stdout_factory (package : str ) -> Callable [..., Any ]:
325+ """Create a ``pre_run_stdout()`` function for one package.
326+
327+ Args:
328+ package: The name of the package
329+
330+ Returns:
331+ A partial ``pre_run_stdout()`` method for the package
332+ """
333+ return functools .partial (pre_run_stdout , package )
0 commit comments