Skip to content

Commit 7be3579

Browse files
committed
Allow legacy field names in rosidl_generate_interfaces
Signed-off-by: Ryan Friedman <[email protected]>
1 parent 6a18bbf commit 7be3579

File tree

6 files changed

+50
-17
lines changed

6 files changed

+50
-17
lines changed

rosidl_adapter/cmake/rosidl_adapt_interfaces.cmake

+10-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@
3030
# @public
3131
#
3232
function(rosidl_adapt_interfaces idl_var arguments_file)
33-
cmake_parse_arguments(ARG "" "TARGET" ""
34-
${ARGN})
33+
set(options ALLOW_LEGACY_FIELD_NAMES)
34+
set(oneValueArgs TARGET)
35+
set(multiValueArgs "")
36+
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
3537
if(ARG_UNPARSED_ARGUMENTS)
3638
message(FATAL_ERROR "rosidl_adapt_interfaces() called with unused "
37-
"arguments: ${ARG_UNPARSED_ARGUMENTS}")
39+
"arguments: ${ARG_UNPARSED_ARGUMENTS}. ALLOW_LEGACY? '${ARG_ALLOW_LEGACY_FIELD_NAMES}'")
3840
endif()
3941

4042
find_package(ament_cmake_core REQUIRED) # for get_executable_path
@@ -48,6 +50,11 @@ function(rosidl_adapt_interfaces idl_var arguments_file)
4850
--arguments-file "${arguments_file}"
4951
--output-dir "${CMAKE_CURRENT_BINARY_DIR}/rosidl_adapter/${PROJECT_NAME}"
5052
--output-file "${idl_output}")
53+
if(ARG_ALLOW_LEGACY_FIELD_NAMES)
54+
list(APPEND cmd --allow-legacy-field-naming)
55+
MESSAGE(WARNING Allowing legacy arguments.)
56+
endif()
57+
5158
execute_process(
5259
COMMAND ${cmd}
5360
OUTPUT_QUIET

rosidl_adapter/rosidl_adapter/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from rosidl_adapter.parser import DEFAULT_ALLOW_LEGACY_FIELD_NAMES
1516

16-
def convert_to_idl(package_dir, package_name, interface_file, output_dir):
17+
def convert_to_idl(package_dir, package_name, interface_file, output_dir, *, allow_legacy_field_naming=DEFAULT_ALLOW_LEGACY_FIELD_NAMES):
1718
if interface_file.suffix == '.msg':
1819
from rosidl_adapter.msg import convert_msg_to_idl
20+
1921
return convert_msg_to_idl(
20-
package_dir, package_name, interface_file, output_dir / 'msg')
22+
package_dir, package_name, interface_file, output_dir / 'msg', allow_legacy_field_naming=allow_legacy_field_naming)
2123

2224
if interface_file.suffix == '.srv':
2325
from rosidl_adapter.srv import convert_srv_to_idl

rosidl_adapter/rosidl_adapter/main.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
from rosidl_adapter import convert_to_idl
23+
from rosidl_adapter.parser import DEFAULT_ALLOW_LEGACY_FIELD_NAMES
2324

2425

2526
def main(argv=sys.argv[1:]):
@@ -38,6 +39,10 @@ def main(argv=sys.argv[1:]):
3839
'--output-file', required=True,
3940
help='The output file containing the tuples for the generated .idl '
4041
'files')
42+
legacy_field_name_action = "store_true" if DEFAULT_ALLOW_LEGACY_FIELD_NAMES else "store_false"
43+
parser.add_argument(
44+
'--allow-legacy-field-naming', required=False, action=legacy_field_name_action,
45+
help='Allow legacy ROS1 style field names that use PascalCase, camelCase, and Pascal_With_Underscores')
4146
args = parser.parse_args(argv)
4247
output_dir = pathlib.Path(args.output_dir)
4348
output_file = pathlib.Path(args.output_file)
@@ -52,7 +57,8 @@ def main(argv=sys.argv[1:]):
5257
basepath, relative_path = non_idl_tuple.rsplit(':', 1)
5358
abs_idl_file = convert_to_idl(
5459
pathlib.Path(basepath), args.package_name,
55-
pathlib.Path(relative_path), output_dir)
60+
pathlib.Path(relative_path), output_dir,
61+
allow_legacy_field_naming=args.allow_legacy_field_naming)
5662
idl_tuples.append((output_dir, abs_idl_file.relative_to(output_dir)))
5763

5864
output_file.parent.mkdir(exist_ok=True)

rosidl_adapter/rosidl_adapter/msg/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from rosidl_adapter.parser import parse_message_string
15+
from rosidl_adapter.parser import parse_message_string, DEFAULT_ALLOW_LEGACY_FIELD_NAMES
1616
from rosidl_adapter.resource import expand_template
1717

1818

19-
def convert_msg_to_idl(package_dir, package_name, input_file, output_dir):
19+
def convert_msg_to_idl(package_dir, package_name, input_file, output_dir, *, allow_legacy_field_naming=DEFAULT_ALLOW_LEGACY_FIELD_NAMES):
20+
2021
assert package_dir.is_absolute()
2122
assert not input_file.is_absolute()
2223
assert input_file.suffix == '.msg'
@@ -25,7 +26,7 @@ def convert_msg_to_idl(package_dir, package_name, input_file, output_dir):
2526
print(f'Reading input file: {abs_input_file}')
2627
abs_input_file = package_dir / input_file
2728
content = abs_input_file.read_text(encoding='utf-8')
28-
msg = parse_message_string(package_name, input_file.stem, content)
29+
msg = parse_message_string(package_name, input_file.stem, content, allow_legacy_field_naming=allow_legacy_field_naming)
2930

3031
output_file = output_dir / input_file.with_suffix('.idl').name
3132
abs_output_file = output_file.absolute()

rosidl_adapter/rosidl_adapter/parser.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
ACTION_RESULT_SERVICE_SUFFIX = '_Result'
3737
ACTION_FEEDBACK_MESSAGE_SUFFIX = '_Feedback'
3838

39+
DEFAULT_ALLOW_LEGACY_FIELD_NAMES=True
40+
3941
PRIMITIVE_TYPES = [
4042
'bool',
4143
'byte',
@@ -345,15 +347,16 @@ def __str__(self):
345347

346348
class Field:
347349

348-
def __init__(self, type_, name, default_value_string=None):
350+
def __init__(self, type_, name, default_value_string=None, *, allow_legacy_field_naming=DEFAULT_ALLOW_LEGACY_FIELD_NAMES):
349351
if not isinstance(type_, Type):
350352
raise TypeError(
351353
"the field type '%s' must be a 'Type' instance" % type_)
352354
self.type = type_
353-
if not is_valid_field_name(name):
354-
raise NameError(
355-
"'{}' is an invalid field name. It should have the pattern '{}'".format(
356-
name, VALID_FIELD_NAME_PATTERN.pattern))
355+
if not allow_legacy_field_naming:
356+
if not is_valid_field_name(name):
357+
raise NameError(
358+
"'{}' is an invalid field name. It should have the pattern '{}'".format(
359+
name, VALID_FIELD_NAME_PATTERN.pattern))
357360
self.name = name
358361
if default_value_string is None:
359362
self.default_value = None
@@ -462,7 +465,7 @@ def extract_file_level_comments(message_string):
462465
return file_level_comments, file_content
463466

464467

465-
def parse_message_string(pkg_name, msg_name, message_string):
468+
def parse_message_string(pkg_name, msg_name, message_string, *, allow_legacy_field_naming=DEFAULT_ALLOW_LEGACY_FIELD_NAMES):
466469
fields = []
467470
constants = []
468471
last_element = None # either a field or a constant
@@ -518,7 +521,9 @@ def parse_message_string(pkg_name, msg_name, message_string):
518521
try:
519522
fields.append(Field(
520523
Type(type_string, context_package_name=pkg_name),
521-
field_name, default_value_string))
524+
field_name, default_value_string,
525+
allow_legacy_field_naming=allow_legacy_field_naming),
526+
)
522527
except Exception as err:
523528
print(
524529
"Error processing '{line}' of '{pkg}/{msg}': '{err}'".format(

rosidl_cmake/cmake/rosidl_generate_interfaces.cmake

+13-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#
5353
macro(rosidl_generate_interfaces target)
5454
cmake_parse_arguments(_ARG
55-
"ADD_LINTER_TESTS;SKIP_INSTALL;SKIP_GROUP_MEMBERSHIP_CHECK"
55+
"ADD_LINTER_TESTS;SKIP_INSTALL;SKIP_GROUP_MEMBERSHIP_CHECK;ALLOW_LEGACY_FIELD_NAMES"
5656
"LIBRARY_NAME" "DEPENDENCIES"
5757
${ARGN})
5858
if(NOT _ARG_UNPARSED_ARGUMENTS)
@@ -127,9 +127,21 @@ macro(rosidl_generate_interfaces target)
127127
PACKAGE_NAME "${PROJECT_NAME}"
128128
NON_IDL_TUPLES "${_non_idl_tuples}"
129129
)
130+
set(_rosidl_apt_interfaces_opts)
131+
if(_ARG_ALLOW_LEGACY_FIELD_NAMES)
132+
set(_rosidl_apt_interfaces_opts ALLOW_LEGACY_FIELD_NAMES)
133+
#list(APPEND _arg ALLOW_LEGACY_FIELD_NAMES)
134+
message(WARNING "Allowing legacy field names")
135+
else()
136+
message(FATAL_ERROR NO Legacy field names)
137+
endif()
138+
139+
#${_rosidl_apt_interfaces_opts}
140+
130141
rosidl_adapt_interfaces(
131142
_idl_adapter_tuples
132143
"${_adapter_arguments_file}"
144+
${_rosidl_apt_interfaces_opts}
133145
TARGET ${target}
134146
)
135147
endif()

0 commit comments

Comments
 (0)