Skip to content

Commit 10c6311

Browse files
Allow optional separator in service definition (fixes #269) (#270)
* fixed bad test case * tweaked formatting * fixed mandatory separator
1 parent bc7b89b commit 10c6311

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/roswire/definitions/srv.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
__all__ = ['SrvFormat']
1+
# -*- coding: utf-8 -*-
2+
__all__ = ('SrvFormat',)
23

34
from typing import Optional, List, Dict, Any
45
import os
@@ -20,8 +21,7 @@ class SrvFormat:
2021

2122
@staticmethod
2223
def from_file(package: str, fn: str, files: FileProxy) -> 'SrvFormat':
23-
"""
24-
Constructs a service format from a .srv file for a given package.
24+
"""Constructs a service format from a .srv file for a given package.
2525
2626
Parameters:
2727
package: the name of the package that provides the file.
@@ -38,8 +38,7 @@ def from_file(package: str, fn: str, files: FileProxy) -> 'SrvFormat':
3838

3939
@staticmethod
4040
def from_string(package: str, name: str, s: str) -> 'SrvFormat':
41-
"""
42-
Constructs a service format from its description.
41+
"""Constructs a service format from its description.
4342
4443
Raises:
4544
ParsingError: if the description cannot be parsed.
@@ -50,11 +49,9 @@ def from_string(package: str, name: str, s: str) -> 'SrvFormat':
5049
name_res = f"{name}Response"
5150

5251
sections: List[str] = [ss.strip() for ss in s.split('---')]
53-
try:
54-
s_req, s_res = sections
55-
except ValueError:
56-
m = "bad service description: missing separator (---)"
57-
raise exceptions.ParsingError(m)
52+
assert len(sections) < 3
53+
s_req = sections[0]
54+
s_res = sections[1] if len(sections) > 1 else ''
5855

5956
if s_req:
6057
req = MsgFormat.from_string(package, name_req, s_req)

test/test_format.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,15 @@ def test_srv_from_string():
8787

8888
assert len(res.fields) == 3
8989
assert Field('another_pkg/YetAnotherMessage', 'val') in res.fields
90-
assert Field('CustomMessageDefinedInThisPackage', 'value') in res.fields
90+
assert Field('PkgName/CustomMessageDefinedInThisPackage', 'value') in res.fields
9191
assert Field('uint32', 'an_integer') in res.fields
9292

93+
# bug #269
94+
s = "map_msgs/ProjectedMapInfo[] projected_maps_info"
95+
fmt = SrvFormat.from_string("map_msgs", "ProjectedMapsInfo", s)
96+
assert fmt.request is not None
97+
assert fmt.response is None
98+
9399

94100
def test_action_from_string():
95101
s = """

0 commit comments

Comments
 (0)