13
13
# limitations under the License.
14
14
15
15
import argparse
16
- import pathlib
16
+ from pathlib import Path
17
17
import sys
18
+ from typing import Callable , List , Literal , TYPE_CHECKING
18
19
19
20
from catkin_pkg .package import package_exists_at
20
21
from catkin_pkg .package import parse_package
27
28
from rosidl_cli .command .translate .extensions import TranslateCommandExtension
28
29
29
30
30
- def convert_files_to_idl (extension , conversion_function , argv = sys .argv [1 :]):
31
+ if TYPE_CHECKING :
32
+ from typing_extensions import TypeAlias
33
+
34
+ ConversionFunctionType : TypeAlias = Callable [[Path , str , Path , Path ], Path ]
35
+
36
+
37
+ def convert_files_to_idl (
38
+ extension : Literal ['.msg' , '.srv' , '.action' ],
39
+ conversion_function : 'ConversionFunctionType' ,
40
+ argv : List [str ] = sys .argv [1 :]
41
+ ) -> None :
42
+
31
43
parser = argparse .ArgumentParser (
32
44
description = f'Convert { extension } files to .idl' )
33
45
parser .add_argument (
@@ -36,7 +48,7 @@ def convert_files_to_idl(extension, conversion_function, argv=sys.argv[1:]):
36
48
args = parser .parse_args (argv )
37
49
38
50
for interface_file in args .interface_files :
39
- interface_file = pathlib . Path (interface_file )
51
+ interface_file = Path (interface_file )
40
52
package_dir = interface_file .parent .absolute ()
41
53
while (
42
54
len (package_dir .parents ) and
@@ -48,8 +60,7 @@ def convert_files_to_idl(extension, conversion_function, argv=sys.argv[1:]):
48
60
f"Could not find package for '{ interface_file } '" ,
49
61
file = sys .stderr )
50
62
continue
51
- warnings = []
52
- pkg = parse_package (package_dir , warnings = warnings )
63
+ pkg = parse_package (package_dir , warnings = [])
53
64
54
65
conversion_function (
55
66
package_dir , pkg .name ,
@@ -63,14 +74,14 @@ class TranslateToIDL(TranslateCommandExtension):
63
74
64
75
def translate (
65
76
self ,
66
- package_name ,
67
- interface_files ,
68
- include_paths ,
69
- output_path
70
- ):
77
+ package_name : str ,
78
+ interface_files : List [ str ] ,
79
+ include_paths : List [ str ] ,
80
+ output_path : Path
81
+ ) -> List [ str ] :
71
82
translated_interface_files = []
72
- for interface_file in interface_files :
73
- prefix , interface_file = interface_path_as_tuple (interface_file )
83
+ for interface_file_str in interface_files :
84
+ prefix , interface_file = interface_path_as_tuple (interface_file_str )
74
85
output_dir = output_path / interface_file .parent
75
86
translated_interface_file = self .conversion_function (
76
87
prefix , package_name , interface_file , output_dir )
@@ -87,7 +98,7 @@ class TranslateMsgToIDL(TranslateToIDL):
87
98
input_format = 'msg'
88
99
89
100
@property
90
- def conversion_function (self ):
101
+ def conversion_function (self ) -> 'ConversionFunctionType' :
91
102
return convert_msg_to_idl
92
103
93
104
@@ -96,13 +107,13 @@ class TranslateSrvToIDL(TranslateToIDL):
96
107
input_format = 'srv'
97
108
98
109
@property
99
- def conversion_function (self ):
110
+ def conversion_function (self ) -> 'ConversionFunctionType' :
100
111
return convert_srv_to_idl
101
112
102
113
103
114
class TranslateActionToIDL (TranslateToIDL ):
104
115
input_format = 'action'
105
116
106
117
@property
107
- def conversion_function (self ):
118
+ def conversion_function (self ) -> 'ConversionFunctionType' :
108
119
return convert_action_to_idl
0 commit comments