diff --git a/launch/launch/launch_description_sources/python_launch_file_utilities.py b/launch/launch/launch_description_sources/python_launch_file_utilities.py index e5715c59b..320eb70d9 100644 --- a/launch/launch/launch_description_sources/python_launch_file_utilities.py +++ b/launch/launch/launch_description_sources/python_launch_file_utilities.py @@ -17,8 +17,10 @@ from importlib.machinery import SourceFileLoader from importlib.util import module_from_spec from importlib.util import spec_from_loader +from pathlib import Path from types import ModuleType from typing import Text +from typing import Union from ..launch_description import LaunchDescription @@ -29,9 +31,9 @@ class InvalidPythonLaunchFileError(Exception): ... -def load_python_launch_file_as_module(python_launch_file_path: Text) -> ModuleType: +def load_python_launch_file_as_module(python_launch_file_path: Union[Text, Path]) -> ModuleType: """Load a given Python launch file (by path) as a Python module.""" - loader = SourceFileLoader('python_launch_file', python_launch_file_path) + loader = SourceFileLoader('python_launch_file', str(python_launch_file_path)) spec = spec_from_loader(loader.name, loader) mod = module_from_spec(spec) loader.exec_module(mod) @@ -39,7 +41,7 @@ def load_python_launch_file_as_module(python_launch_file_path: Text) -> ModuleTy def get_launch_description_from_python_launch_file( - python_launch_file_path: Text + python_launch_file_path: Union[Text, Path] ) -> LaunchDescription: """ Load a given Python launch file (by path), and return the launch description from it. diff --git a/launch/launch/some_substitutions_type.py b/launch/launch/some_substitutions_type.py index 9f02ae5eb..e3957fac4 100644 --- a/launch/launch/some_substitutions_type.py +++ b/launch/launch/some_substitutions_type.py @@ -15,6 +15,7 @@ """Module for SomeSubstitutionsType type.""" import collections.abc +from pathlib import Path from typing import Iterable from typing import Text from typing import Union @@ -23,12 +24,14 @@ SomeSubstitutionsType = Union[ Text, + Path, Substitution, - Iterable[Union[Text, Substitution]], + Iterable[Union[Text, Path, Substitution]], ] SomeSubstitutionsType_types_tuple = ( str, + Path, Substitution, collections.abc.Iterable, ) diff --git a/launch/launch/substitutions/path_join_substitution.py b/launch/launch/substitutions/path_join_substitution.py index 05117ae39..70065b798 100644 --- a/launch/launch/substitutions/path_join_substitution.py +++ b/launch/launch/substitutions/path_join_substitution.py @@ -14,7 +14,7 @@ """Module for the PathJoinSubstitution substitution.""" -import os +from pathlib import Path from typing import Iterable from typing import List from typing import Text @@ -86,7 +86,7 @@ def perform(self, context: LaunchContext) -> Text: perform_substitutions(context, component_substitutions) for component_substitutions in self.substitutions ] - return os.path.join(*path_components) + return str(Path(*path_components)) def __truediv__(self, additional_path: SomeSubstitutionsType) -> 'PathJoinSubstitution': """Join path substitutions using the / operator, mimicking pathlib.Path operation.""" diff --git a/launch/launch/utilities/normalize_to_list_of_substitutions_impl.py b/launch/launch/utilities/normalize_to_list_of_substitutions_impl.py index 160a71c94..a501466eb 100644 --- a/launch/launch/utilities/normalize_to_list_of_substitutions_impl.py +++ b/launch/launch/utilities/normalize_to_list_of_substitutions_impl.py @@ -14,6 +14,7 @@ """Module for the normalize_to_list_of_substitutions() utility function.""" +from pathlib import Path from typing import cast from typing import Iterable from typing import List @@ -31,14 +32,14 @@ def normalize_to_list_of_substitutions(subs: SomeSubstitutionsType) -> List[Subs def normalize(x): if isinstance(x, Substitution): return x - if isinstance(x, str): - return TextSubstitution(text=x) + if isinstance(x, (str, Path)): + return TextSubstitution(text=str(x)) raise TypeError( "Failed to normalize given item of type '{}', when only " "'str' or 'launch.Substitution' were expected.".format(type(x))) - if isinstance(subs, str): - return [TextSubstitution(text=subs)] + if isinstance(subs, (str, Path)): + return [TextSubstitution(text=str(subs))] if is_a_subclass(subs, Substitution): return [cast(Substitution, subs)] return [normalize(y) for y in cast(Iterable, subs)] diff --git a/launch/launch/utilities/typing_file_path.py b/launch/launch/utilities/typing_file_path.py index e142b548c..999ee836c 100644 --- a/launch/launch/utilities/typing_file_path.py +++ b/launch/launch/utilities/typing_file_path.py @@ -13,7 +13,8 @@ # limitations under the License. import os +from pathlib import Path import typing # Type of a filesystem path -FilePath = typing.TypeVar('FilePath', str, bytes, os.PathLike) +FilePath = typing.TypeVar('FilePath', str, bytes, os.PathLike, Path) diff --git a/launch/test/launch/actions/test_include_launch_description.py b/launch/test/launch/actions/test_include_launch_description.py index 469e71475..d0a6c6ec5 100644 --- a/launch/test/launch/actions/test_include_launch_description.py +++ b/launch/test/launch/actions/test_include_launch_description.py @@ -14,7 +14,7 @@ """Tests for the IncludeLaunchDescription action class.""" -import os +from pathlib import Path from launch import LaunchContext from launch import LaunchDescription @@ -74,7 +74,7 @@ def test_include_launch_description_launch_file_location(): assert lc1.locals.current_launch_file_directory == '