3333
3434import launch .logging
3535
36- from osrf_pycommon .process_utils import async_execute_process
36+ from osrf_pycommon .process_utils import async_execute_process # type: ignore
3737from osrf_pycommon .process_utils import AsyncSubprocessProtocol
3838
3939from .emit_event import EmitEvent
6262from ..launch_context import LaunchContext
6363from ..launch_description import LaunchDescription
6464from ..launch_description_entity import LaunchDescriptionEntity
65- from ..some_actions_type import SomeActionsType
65+ from ..some_entities_type import SomeEntitiesType
6666from ..some_substitutions_type import SomeSubstitutionsType
6767from ..substitution import Substitution # noqa: F401
6868from ..substitutions import LaunchConfiguration
@@ -93,8 +93,8 @@ def __init__(
9393 cached_output : bool = False ,
9494 log_cmd : bool = False ,
9595 on_exit : Optional [Union [
96- SomeActionsType ,
97- Callable [[ProcessExited , LaunchContext ], Optional [SomeActionsType ]]
96+ SomeEntitiesType ,
97+ Callable [[ProcessExited , LaunchContext ], Optional [SomeEntitiesType ]]
9898 ]] = None ,
9999 respawn : Union [bool , SomeSubstitutionsType ] = False ,
100100 respawn_delay : Optional [float ] = None ,
@@ -189,9 +189,16 @@ def __init__(
189189 self .__sigterm_timeout = normalize_to_list_of_substitutions (sigterm_timeout )
190190 self .__sigkill_timeout = normalize_to_list_of_substitutions (sigkill_timeout )
191191 self .__emulate_tty = emulate_tty
192- self .__output = os .environ .get ('OVERRIDE_LAUNCH_PROCESS_OUTPUT' , output )
193- if not isinstance (self .__output , dict ):
194- self .__output = normalize_to_list_of_substitutions (self .__output )
192+ # Note: we need to use a temporary here so that we don't assign values with different types
193+ # to the same variable
194+ tmp_output : SomeSubstitutionsType = os .environ .get (
195+ 'OVERRIDE_LAUNCH_PROCESS_OUTPUT' , output
196+ )
197+ self .__output : Union [dict , List [Substitution ]]
198+ if not isinstance (tmp_output , dict ):
199+ self .__output = normalize_to_list_of_substitutions (tmp_output )
200+ else :
201+ self .__output = tmp_output
195202 self .__output_format = output_format
196203
197204 self .__log_cmd = log_cmd
@@ -342,7 +349,7 @@ def __on_signal_process_event(
342349 def __on_process_stdin (
343350 self ,
344351 event : ProcessIO
345- ) -> Optional [SomeActionsType ]:
352+ ) -> Optional [SomeEntitiesType ]:
346353 self .__logger .warning (
347354 "in ExecuteProcess('{}').__on_process_stdin_event()" .format (id (self )),
348355 )
@@ -351,12 +358,12 @@ def __on_process_stdin(
351358
352359 def __on_process_output (
353360 self , event : ProcessIO , buffer : io .TextIOBase , logger : logging .Logger
354- ) -> Optional [ SomeActionsType ] :
361+ ) -> None :
355362 to_write = event .text .decode (errors = 'replace' )
356363 if buffer .closed :
357364 # buffer was probably closed by __flush_buffers on shutdown. Output without
358365 # buffering.
359- buffer .info (
366+ logger .info (
360367 self .__output_format .format (line = to_write , this = self )
361368 )
362369 else :
@@ -402,7 +409,7 @@ def __flush_buffers(self, event, context):
402409
403410 def __on_process_output_cached (
404411 self , event : ProcessIO , buffer , logger
405- ) -> Optional [ SomeActionsType ] :
412+ ) -> None :
406413 to_write = event .text .decode (errors = 'replace' )
407414 last_cursor = buffer .tell ()
408415 buffer .seek (0 , os .SEEK_END ) # go to end of buffer
@@ -429,7 +436,7 @@ def __flush_cached_buffers(self, event, context):
429436 self .__output_format .format (line = line , this = self )
430437 )
431438
432- def __on_shutdown (self , event : Event , context : LaunchContext ) -> Optional [SomeActionsType ]:
439+ def __on_shutdown (self , event : Event , context : LaunchContext ) -> Optional [SomeEntitiesType ]:
433440 due_to_sigint = cast (Shutdown , event ).due_to_sigint
434441 return self ._shutdown_process (
435442 context ,
@@ -584,9 +591,14 @@ async def __execute_process(self, context: LaunchContext) -> None:
584591 self .__logger .error ("process has died [pid {}, exit code {}, cmd '{}']." .format (
585592 pid , returncode , ' ' .join (filter (lambda part : part .strip (), cmd ))
586593 ))
587- await context .emit_event (ProcessExited (returncode = returncode , ** process_event_args ))
594+ await context .emit_event (
595+ ProcessExited (returncode = returncode , ** process_event_args )
596+ )
588597 # respawn the process if necessary
589- if not context .is_shutdown and not self .__shutdown_future .done () and self .__respawn :
598+ if not context .is_shutdown \
599+ and self .__shutdown_future is not None \
600+ and not self .__shutdown_future .done ()\
601+ and self .__respawn :
590602 if self .__respawn_delay is not None and self .__respawn_delay > 0.0 :
591603 # wait for a timeout(`self.__respawn_delay`) to respawn the process
592604 # and handle shutdown event with future(`self.__shutdown_future`)
@@ -614,7 +626,7 @@ def prepare(self, context: LaunchContext):
614626 # pid is added to the dictionary in the connection_made() method of the protocol.
615627 }
616628
617- self .__respawn = perform_typed_substitution (context , self .__respawn , bool )
629+ self .__respawn = cast ( bool , perform_typed_substitution (context , self .__respawn , bool ) )
618630
619631 def execute (self , context : LaunchContext ) -> Optional [List [LaunchDescriptionEntity ]]:
620632 """
@@ -669,7 +681,9 @@ def execute(self, context: LaunchContext) -> Optional[List[LaunchDescriptionEnti
669681 ),
670682 OnProcessExit (
671683 target_action = self ,
672- on_exit = self .__on_exit ,
684+ # TODO: This is also a little strange, OnProcessExit shouldn't ever be able to
685+ # take a None for the callable, but this seems to be the default case?
686+ on_exit = self .__on_exit , # type: ignore
673687 ),
674688 OnProcessExit (
675689 target_action = self ,
@@ -684,9 +698,13 @@ def execute(self, context: LaunchContext) -> Optional[List[LaunchDescriptionEnti
684698 self .__shutdown_future = create_future (context .asyncio_loop )
685699 self .__logger = launch .logging .get_logger (name )
686700 if not isinstance (self .__output , dict ):
687- self .__output = perform_substitutions (context , self .__output )
688- self .__stdout_logger , self .__stderr_logger = \
689- launch .logging .get_output_loggers (name , self .__output )
701+ self .__stdout_logger , self .__stderr_logger = \
702+ launch .logging .get_output_loggers (
703+ name , perform_substitutions (context , self .__output )
704+ )
705+ else :
706+ self .__stdout_logger , self .__stderr_logger = \
707+ launch .logging .get_output_loggers (name , self .__output )
690708 context .asyncio_loop .create_task (self .__execute_process (context ))
691709 except Exception :
692710 for event_handler in event_handlers :
0 commit comments