Skip to content

Commit

Permalink
add IncludeLaunchDescription action (#120)
Browse files Browse the repository at this point in the history
fixes #115

Signed-off-by: William Woodall <[email protected]>
  • Loading branch information
wjwwood authored Jul 27, 2018
1 parent b608581 commit 951a9b1
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 1 deletion.
2 changes: 2 additions & 0 deletions launch/launch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .launch_context import LaunchContext
from .launch_description import LaunchDescription
from .launch_description_entity import LaunchDescriptionEntity
from .launch_description_source import LaunchDescriptionSource
from .launch_introspector import LaunchIntrospector
from .launch_service import LaunchService
from .some_actions_type import SomeActionsType
Expand All @@ -41,6 +42,7 @@
'LaunchContext',
'LaunchDescription',
'LaunchDescriptionEntity',
'LaunchDescriptionSource',
'LaunchIntrospector',
'LaunchService',
'SomeActionsType',
Expand Down
2 changes: 2 additions & 0 deletions launch/launch/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from .emit_event import EmitEvent
from .execute_process import ExecuteProcess
from .include_launch_description import IncludeLaunchDescription
from .log_info import LogInfo
from .opaque_function import OpaqueFunction
from .register_event_handler import RegisterEventHandler
Expand All @@ -25,6 +26,7 @@
__all__ = [
'EmitEvent',
'ExecuteProcess',
'IncludeLaunchDescription',
'LogInfo',
'OpaqueFunction',
'RegisterEventHandler',
Expand Down
40 changes: 40 additions & 0 deletions launch/launch/actions/include_launch_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2018 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module for the IncludeLaunchDescription action."""

from typing import List

from ..action import Action
from ..launch_context import LaunchContext
from ..launch_description_entity import LaunchDescriptionEntity
from ..launch_description_source import LaunchDescriptionSource


class IncludeLaunchDescription(Action):
"""Action that includes a launch description source and yields its entities when visited."""

def __init__(self, launch_description_source: LaunchDescriptionSource) -> None:
"""Constructor."""
super().__init__()
self.__launch_description_source = launch_description_source

@property
def launch_description_source(self) -> LaunchDescriptionSource:
"""Getter for self.__launch_description_source."""
return self.__launch_description_source

def visit(self, context: LaunchContext) -> List[LaunchDescriptionEntity]:
"""Override visit to return an Entity rather than an action."""
return [self.__launch_description_source.launch_description]
57 changes: 57 additions & 0 deletions launch/launch/launch_description_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2018 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module for the LaunchDescriptionSource class."""

from .launch_description import LaunchDescription


class LaunchDescriptionSource:
"""Encapsulation of a launch description, where it comes from, and how it was generated."""

def __init__(
self,
launch_description: LaunchDescription,
location: str = '<string>',
method: str = 'unspecified mechanism from a script',
) -> None:
"""
Constructor.
For example, loading a file called ``example.launch.py`` for inclusion
might end up setting `location` to ``/path/to/example.launch.py`` and
the ``method`` to be ``interpreted python launch file``.
:param launch_description: the launch description that this source represents
:param location: the location from where this launch description was loaded if applicable
:param method: the method by which the launch description was generated
"""
self.__launch_description = launch_description
self.__location = location
self.__method = method

@property
def launch_description(self) -> LaunchDescription:
"""Getter for self.__launch_description."""
return self.__launch_description

@property
def location(self) -> str:
"""Getter for self.__location."""
return self.__location

@property
def method(self) -> str:
"""Getter for self.__method."""
return self.__method
46 changes: 46 additions & 0 deletions launch/test/launch/actions/test_include_launch_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2018 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for the IncludeLaunchDescription action class."""

from launch import LaunchDescription
from launch import LaunchDescriptionSource
from launch.actions import IncludeLaunchDescription


def test_include_launch_description_constructors():
"""Test the constructors for IncludeLaunchDescription class."""
IncludeLaunchDescription(LaunchDescriptionSource(LaunchDescription()))


def test_include_launch_description_methods():
"""Test the methods of the Action class."""
class MockLaunchContext:
...

ld = LaunchDescription()
action = IncludeLaunchDescription(LaunchDescriptionSource(ld))
assert 'IncludeLaunchDescription' in action.describe()
assert isinstance(action.describe_sub_entities(), list)
assert isinstance(action.describe_conditional_sub_entities(), list)
assert action.visit(MockLaunchContext()) == [ld]
assert action.get_asyncio_future() is None

ld2 = LaunchDescription([action])
action2 = IncludeLaunchDescription(LaunchDescriptionSource(ld2))
assert 'IncludeLaunchDescription' in action2.describe()
assert isinstance(action2.describe_sub_entities(), list)
assert isinstance(action2.describe_conditional_sub_entities(), list)
assert action2.visit(MockLaunchContext()) == [ld2]
assert action2.get_asyncio_future() is None
2 changes: 1 addition & 1 deletion launch/test/launch/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_action_constructors():


def test_action_methods():
"""Test the moethods of the Action class."""
"""Test the methods of the Action class."""
class MockLaunchContext:
...

Expand Down

0 comments on commit 951a9b1

Please sign in to comment.