def __init__( self, pkg_name: str, msg_name: str, line_text: str, ): if line_text in (SERVICE_REQUEST_RESPONSE_SEPARATOR, ACTION_REQUEST_RESPONSE_SEPARATOR): msg_spec = None else: msg_spec = parse_message_string( pkg_name=pkg_name, msg_name=msg_name, message_string=line_text, ) if len(msg_spec.fields) > 1: raise ValueError("'line_text' must be only one line") self._msg_spec: typing.Optional[MessageSpecification] = msg_spec self._raw_line_text = line_text
def convert_msg_to_idl(package_dir, package_name, input_file, output_dir): assert package_dir.is_absolute() assert not input_file.is_absolute() assert input_file.suffix == '.msg' abs_input_file = package_dir / input_file print(f'Reading input file: {abs_input_file}') abs_input_file = package_dir / input_file content = abs_input_file.read_text(encoding='utf-8') msg = parse_message_string(package_name, input_file.stem, content) output_file = output_dir / input_file.with_suffix('.idl').name abs_output_file = output_file.absolute() print(f'Writing output file: {abs_output_file}') data = { 'pkg_name': package_name, 'relative_input_file': input_file.as_posix(), 'msg': msg, } expand_template('msg.idl.em', data, output_file, encoding='iso-8859-1') return output_file
def convert_msg_to_idl(package_dir, package_name, input_file, output_dir): assert package_dir.is_absolute() assert not input_file.is_absolute() assert input_file.suffix == '.msg' abs_input_file = package_dir / input_file print('Reading input file: {abs_input_file}'.format_map(locals())) abs_input_file = package_dir / input_file content = abs_input_file.read_text(encoding='utf-8') msg = parse_message_string(package_name, input_file.stem, content) output_file = output_dir / input_file.with_suffix('.idl').name abs_output_file = output_file.absolute() print('Writing output file: {abs_output_file}'.format_map(locals())) data = { 'pkg_name': package_name, 'relative_input_file': input_file, 'msg': msg, } expand_template('msg.idl.em', data, output_file) return output_file
def test_extract_message_comments(): # multi line file-level comment msg_spec = parse_message_string('pkg', 'Foo', '# comment 1\n#\n# comment 2\nbool value') assert len(msg_spec.annotations) == 1 assert 'comment' in msg_spec.annotations assert len(msg_spec.annotations['comment']) == 3 assert msg_spec.annotations['comment'][0] == ' comment 1' assert msg_spec.annotations['comment'][1] == '' assert msg_spec.annotations['comment'][2] == ' comment 2' assert len(msg_spec.fields) == 1 assert len(msg_spec.fields[0].annotations) == 1 assert 'comment' in msg_spec.fields[0].annotations assert not msg_spec.fields[0].annotations['comment'] # file-level comment separated from field-level comment msg_spec = parse_message_string('pkg', 'Foo', '# comment 1\n\n# comment 2\nbool value') assert len(msg_spec.annotations) == 1 assert 'comment' in msg_spec.annotations assert len(msg_spec.annotations['comment']) == 1 assert msg_spec.annotations['comment'] == [' comment 1'] assert len(msg_spec.fields) == 1 assert len(msg_spec.fields[0].annotations) == 1 assert 'comment' in msg_spec.fields[0].annotations assert len(msg_spec.fields[0].annotations['comment']) == 1 assert msg_spec.fields[0].annotations['comment'][0] == ' comment 2' # file-level comment, trailing and indented field-level comment msg_spec = parse_message_string( 'pkg', 'Foo', '# comment 1\nbool value # comment 2\n # comment 3\nbool value2') assert len(msg_spec.annotations) == 1 assert 'comment' in msg_spec.annotations assert len(msg_spec.annotations['comment']) == 1 assert msg_spec.annotations['comment'] == [' comment 1'] assert len(msg_spec.fields) == 2 assert len(msg_spec.fields[0].annotations) == 1 assert 'comment' in msg_spec.fields[0].annotations assert len(msg_spec.fields[0].annotations['comment']) == 2 assert msg_spec.fields[0].annotations['comment'][0] == ' comment 2' assert msg_spec.fields[0].annotations['comment'][1] == ' comment 3' assert len(msg_spec.fields[1].annotations) == 1 assert 'comment' in msg_spec.fields[1].annotations assert len(msg_spec.fields[1].annotations['comment']) == 0 # trailing field-level comment, next field-level comment msg_spec = parse_message_string( 'pkg', 'Foo', 'bool value # comment 2\n# comment 3\nbool value2') assert len(msg_spec.annotations) == 1 assert 'comment' in msg_spec.annotations assert len(msg_spec.annotations['comment']) == 0 assert len(msg_spec.fields) == 2 assert len(msg_spec.fields[0].annotations) == 1 assert 'comment' in msg_spec.fields[0].annotations assert len(msg_spec.fields[0].annotations['comment']) == 1 assert msg_spec.fields[0].annotations['comment'][0] == ' comment 2' assert len(msg_spec.fields[1].annotations) == 1 assert 'comment' in msg_spec.fields[1].annotations assert len(msg_spec.fields[1].annotations['comment']) == 1 assert msg_spec.fields[1].annotations['comment'][0] == ' comment 3' # field-level comment with a unit msg_spec = parse_message_string( 'pkg', 'Foo', 'bool value # comment [unit]') assert len(msg_spec.fields) == 1 assert len(msg_spec.fields[0].annotations) == 2 assert 'comment' in msg_spec.fields[0].annotations assert len(msg_spec.fields[0].annotations['comment']) == 1 assert msg_spec.fields[0].annotations['comment'][0] == ' comment' assert 'unit' in msg_spec.fields[0].annotations assert msg_spec.fields[0].annotations['unit'] == 'unit'
def test_parse_message_string(): msg_spec = parse_message_string('pkg', 'Foo', '') assert msg_spec.base_type.pkg_name == 'pkg' assert msg_spec.base_type.type == 'Foo' assert len(msg_spec.fields) == 0 assert len(msg_spec.constants) == 0 msg_spec = parse_message_string('pkg', 'Foo', '#comment\n \n # comment') assert len(msg_spec.fields) == 0 assert len(msg_spec.constants) == 0 with pytest.raises(InvalidFieldDefinition): parse_message_string('pkg', 'Foo', 'bool # comment') msg_spec = parse_message_string('pkg', 'Foo', 'bool foo') assert len(msg_spec.fields) == 1 assert msg_spec.fields[0].type.type == 'bool' assert msg_spec.fields[0].name == 'foo' assert msg_spec.fields[0].default_value is None assert len(msg_spec.constants) == 0 msg_spec = parse_message_string('pkg', 'Foo', 'bool foo 1') assert len(msg_spec.fields) == 1 assert msg_spec.fields[0].type.type == 'bool' assert msg_spec.fields[0].name == 'foo' assert msg_spec.fields[0].default_value assert len(msg_spec.constants) == 0 with pytest.raises(InvalidResourceName): parse_message_string('pkg', 'Foo', 'Ty_pe foo') with pytest.raises(TypeError): parse_message_string('pkg', 'Foo', 'bool] foo') with pytest.raises(TypeError): parse_message_string('pkg', 'Foo', 'bool[max]] foo') with pytest.raises(ValueError) as e: parse_message_string('pkg', 'Foo', 'bool foo\nbool foo') assert 'foo' in str(e) msg_spec = parse_message_string('pkg', 'Foo', 'bool FOO=1') assert len(msg_spec.fields) == 0 assert len(msg_spec.constants) == 1 assert msg_spec.constants[0].type == 'bool' assert msg_spec.constants[0].name == 'FOO' assert msg_spec.constants[0].value with pytest.raises(TypeError): parse_message_string('pkg', 'Foo', 'pkg/Bar foo=1') with pytest.raises(NameError): parse_message_string('pkg', 'Foo', 'bool foo=1') with pytest.raises(ValueError) as e: parse_message_string('pkg', 'Foo', 'bool FOO=1\nbool FOO=1') assert 'FOO' in str(e)