1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 """
36 ROS Service Description Language Spec
37 Implements U{http://ros.org/wiki/srv}
38 """
39
40 import os
41 import sys
42 import re
43
44 try:
45 from cStringIO import StringIO
46 except ImportError:
47 from io import StringIO
48
49 import roslib.exceptions
50 import roslib.msgs
51 import roslib.names
52 import roslib.packages
53 import roslib.resources
54
55
56
57 import roslib.manifest
58
59
60 EXT = roslib.names.SRV_EXT
61 SEP = roslib.names.PRN_SEPARATOR
62
63 IODELIM = '---'
64 COMMENTCHAR = roslib.msgs.COMMENTCHAR
65
66 VERBOSE = False
67
70
71
75
77
78
79
81
82 - def __init__(self, request, response, text, full_name = '', short_name = '', package = ''):
83 self.request = request
84 self.response = response
85 self.text = text
86 self.full_name = full_name
87 self.short_name = short_name
88 self.package = package
89
91 if not other or not isinstance(other, SrvSpec):
92 return False
93 return self.request == other.request and \
94 self.response == other.response and \
95 self.text == other.text and \
96 self.full_name == other.full_name and \
97 self.short_name == other.short_name and \
98 self.package == other.package
99
101 if not other or not isinstance(other, SrvSpec):
102 return True
103 return not self.__eq__(other)
104
106 return "SrvSpec[%s, %s]"%(repr(self.request), repr(self.response))
107
108
109
110
111
113 return os.path.isfile(f) and f.endswith(EXT)
114
115
117 """
118 list all services in the specified package
119 @param package: name of package to search
120 @type package: str
121 @param include_depends: if True, will also list services in package dependencies
122 @type include_depends: bool
123 @return: service type names
124 @rtype: [str]
125 """
126 types = roslib.resources.list_package_resources(package, include_depends, roslib.packages.SRV_DIR, _srv_filter)
127 return [x[:-len(EXT)] for x in types]
128
130 """
131 @param package: name of package .srv file is in
132 @type package: str
133 @param type_: type name of service
134 @type type_: str
135 @return: file path of .srv file in specified package
136 @rtype: str
137 """
138 return roslib.packages.resource_file(package, roslib.packages.SRV_DIR, type_+EXT)
139
141 """
142 List all messages that a package contains
143 @param depend: roslib.manifest.Depend object representing package
144 to load messages from
145 @type depend: Depend
146 @return: list of message type names and specs for package, as well as a list
147 of message names that could not be processed.
148 @rtype: [(str,roslib.MsgSpec), [str]]
149 """
150
151 types = list_srv_types(package, False)
152 specs = []
153 failures = []
154 for t in types:
155 try:
156 spec = load_from_file(srv_file(package, t), package)
157 specs.append(spec)
158 except Exception as e:
159 failures.append(t)
160 sys.stderr.write("ERROR: unable to load %s\n"%(t))
161 return specs, failures
162
164 """
165 @param text: .msg text
166 @type text: str
167 @param package_context: context to use for msgTypeName, i.e. the package name,
168 or '' to use local naming convention.
169 @type package_context: str
170 @return: Message type name and message specification
171 @rtype: roslib.MsgSpec
172 @raise roslib.MsgSpecException: if syntax errors or other problems are detected in file
173 """
174 text_in = StringIO()
175 text_out = StringIO()
176 accum = text_in
177 for l in text.split('\n'):
178 l = l.split(COMMENTCHAR)[0].strip()
179 if l.startswith(IODELIM):
180 accum = text_out
181 else:
182 accum.write(l+'\n')
183
184
185 msg_in = roslib.msgs.load_from_string(text_in.getvalue(), package_context, '%sRequest'%(full_name), '%sRequest'%(short_name))
186 msg_out = roslib.msgs.load_from_string(text_out.getvalue(), package_context, '%sResponse'%(full_name), '%sResponse'%(short_name))
187 return SrvSpec(msg_in, msg_out, text, full_name, short_name, package_context)
188
190 """
191 Convert the .srv representation in the file to a SrvSpec instance.
192 @param file_name: name of file to load from
193 @type file_name: str
194 @param package_context: context to use for type name, i.e. the package name,
195 or '' to use local naming convention.
196 @type package_context: str
197 @return: Message type name and message specification
198 @rtype: (str, L{SrvSpec})
199 @raise SrvSpecException: if syntax errors or other problems are detected in file
200 """
201 if VERBOSE:
202 if package_context:
203 sys.stdout.write("Load spec from %s into namespace [%s]\n"%(file_name, package_context))
204 else:
205 sys.stdout.write("Load spec from %s\n"%(file_name))
206 base_file_name = os.path.basename(file_name)
207 type_ = base_file_name[:-len(EXT)]
208 base_type_ = type_
209
210 if package_context:
211 while package_context.endswith(SEP):
212 package_context = package_context[:-1]
213 type_ = "%s%s%s"%(package_context, SEP, type_)
214 if not roslib.names.is_legal_resource_name(type_):
215 raise SrvSpecException("%s: %s is not a legal service type name"%(file_name, type_))
216
217 f = open(file_name, 'r')
218 try:
219 text = f.read()
220 return (type_, load_from_string(text, package_context, type_, base_type_))
221 finally:
222 f.close()
223