Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions launch/launch/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@
import socket
import sys

from typing import Iterable
from typing import List

from . import handlers

from ..frontend import expose_substitution
from ..some_substitutions_type import SomeSubstitutionsType
from ..substitutions import TextSubstitution

__all__ = [
'get_logger',
Expand Down Expand Up @@ -305,13 +301,6 @@ def get_logger(name=None):
return logger


@expose_substitution('log_dir')
def _log_dir(data: Iterable[SomeSubstitutionsType]):
if len(data) > 0:
raise ValueError('log_dir substitution does not expect any arguments')
return TextSubstitution, {'text': launch_config.log_dir}


def _normalize_output_configuration(config):
"""
Normalize output configuration to a dict representation.
Expand Down
2 changes: 2 additions & 0 deletions launch/launch/substitutions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .find_executable import FindExecutable
from .if_else_substitution import IfElseSubstitution
from .launch_configuration import LaunchConfiguration
from .launch_log_dir import LaunchLogDir
from .local_substitution import LocalSubstitution
from .not_equals_substitution import NotEqualsSubstitution
from .path_join_substitution import PathJoinSubstitution
Expand All @@ -49,6 +50,7 @@
'FindExecutable',
'IfElseSubstitution',
'LaunchConfiguration',
'LaunchLogDir',
'LocalSubstitution',
'NotSubstitution',
'NotEqualsSubstitution',
Expand Down
54 changes: 54 additions & 0 deletions launch/launch/substitutions/launch_log_dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2022 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 LaunchLogDir substitution."""

from typing import Any
from typing import Dict
from typing import Sequence
from typing import Text
from typing import Tuple
from typing import Type

from .path_join_substitution import PathSubstitution
from ..frontend.expose import expose_substitution
from ..launch_context import LaunchContext
from ..logging import launch_config as launch_logging_config
from ..some_substitutions_type import SomeSubstitutionsType


@expose_substitution('launch_log_dir')
@expose_substitution('log_dir')
class LaunchLogDir(PathSubstitution):
"""Substitution that returns the absolute path to the current launch log directory."""

def __init__(self) -> None:
"""Create a LaunchLogDir substitution."""
super().__init__(path=self)

@classmethod
def parse(cls, data: Sequence[SomeSubstitutionsType]
) -> Tuple[Type['LaunchLogDir'], Dict[Any, Any]]:
"""Parse `LaunchLogDir` substitution."""
if len(data) != 0:
raise TypeError("launch_log_dir/log_dir substitution doesn't expect arguments")
return cls, {}

def describe(self) -> Text:
"""Return a description of this substitution as a string."""
return 'LaunchLogDir()'

def perform(self, context: LaunchContext) -> Text:
"""Perform the substitution by returning the path to the current launch log directory."""
return launch_logging_config.log_dir
6 changes: 3 additions & 3 deletions launch/launch/substitutions/this_launch_file_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
from typing import Iterable
from typing import Text

from .path_join_substitution import PathSubstitution
from .substitution_failure import SubstitutionFailure
from ..frontend.expose import expose_substitution
from ..launch_context import LaunchContext
from ..some_substitutions_type import SomeSubstitutionsType
from ..substitution import Substitution


@expose_substitution('dirname')
class ThisLaunchFileDir(Substitution):
class ThisLaunchFileDir(PathSubstitution):
"""Substitution that returns the absolute path to the current launch file."""

def __init__(self) -> None:
"""Create a ThisLaunchFileDir substitution."""
super().__init__()
super().__init__(path=self)

@classmethod
def parse(cls, data: Iterable[SomeSubstitutionsType]):
Expand Down
49 changes: 49 additions & 0 deletions launch/test/launch/substitutions/test_launch_log_dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2022 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 LaunchLogDir substitution class."""

from launch import LaunchContext
from launch.frontend.parse_substitution import parse_substitution
from launch.substitutions import LaunchLogDir


def test_launch_log_dir():
"""Test the constructors for LaunchLogDir class."""
LaunchLogDir()


def test_launch_log_dir_methods():
"""Test the methods of the LaunchLogDir class."""
lld = LaunchLogDir()

lc = LaunchContext()
assert lld.perform(lc)


def test_launch_log_dir_path():
test_dir = LaunchLogDir() / 'subdir'
lc = LaunchContext()
result = test_dir.perform(lc)
assert result
assert result.endswith('subdir')


def test_launch_log_dir_frontend():
"""Test launch_log_dir/log_dir frontend substitutions."""
for sub in ('launch_log_dir', 'log_dir'):
subst = parse_substitution(f'$({sub})')
assert len(subst) == 1
result = subst[0]
assert isinstance(result, LaunchLogDir)
9 changes: 9 additions & 0 deletions launch/test/launch/substitutions/test_this_launch_file_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Tests for the ThisLaunchFileDir substitution class."""

import os

from launch import LaunchContext
from launch.substitutions import SubstitutionFailure
from launch.substitutions import ThisLaunchFileDir
Expand All @@ -35,3 +37,10 @@ def test_this_launch_file_path_methods():
tlfp.perform(lc)
lc.extend_locals({'current_launch_file_directory': 'foo'})
assert tlfp.perform(lc) == 'foo'


def test_this_launch_file_dir_pathing():
test_file = ThisLaunchFileDir() / 'some_launch.xml'
lc = LaunchContext()
lc.extend_locals({'current_launch_file_directory': 'foo'})
assert test_file.perform(lc) == os.path.join('foo', 'some_launch.xml')
5 changes: 2 additions & 3 deletions launch/test/launch/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from launch.frontend.parse_substitution import parse_substitution
import launch.logging
from launch.substitutions import TextSubstitution
from launch.substitutions import LaunchLogDir

import pytest

Expand Down Expand Up @@ -333,5 +333,4 @@ def test_get_log_dir_frontend(log_dir):
subst = parse_substitution('$(log_dir)')
assert len(subst) == 1
result = subst[0]
assert isinstance(result, TextSubstitution)
assert result.text == log_dir
assert isinstance(result, LaunchLogDir)