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 """Internal-use Python decorators for parameter validation"""
36
37 from rosgraph.names import resolve_name, ANYTYPE
38
39 TYPE_SEPARATOR = '/'
40 ROSRPC = "rosrpc://"
41
43 """Small helper version to check an object is a string in a way that works
44 for both Python 2 and 3
45 """
46 try:
47 return isinstance(s, basestring)
48 except NameError:
49 return isinstance(s, str)
50
51
53 """Exception that is raised when a parameter fails validation checks"""
55 self._message = message
56
58 return str(self._message)
59
61 """Validator that checks that parameter is not empty"""
62 def validator(param, context):
63 if not param:
64 raise ParameterInvalid("ERROR: parameter [%s] must be specified and non-empty"%param_name)
65 return param
66 return validator
67
69 """Validator that checks that parameter is a string and non-empty"""
70 def validator(param, context):
71 if not param:
72 raise ParameterInvalid("ERROR: parameter [%s] must be specified and non-empty"%param_name)
73 elif not isstring(param):
74 raise ParameterInvalid("ERROR: parameter [%s] must be a string"%param_name)
75 return param
76 return validator
77
79 """Validator that checks that parameter is not None"""
80 def validator(param, context):
81 if param is None:
82 raise ParameterInvalid("ERROR: parameter [%s] must be specified"%param_name)
83 return param
84 return validator
85
86
87
88
90 """
91 Validator that checks that parameter is a valid API handle
92 (i.e. URI). Both http and rosrpc are allowed schemes.
93 """
94 def validator(param_value, callerId):
95 if not param_value or not isstring(param_value):
96 raise ParameterInvalid("ERROR: parameter [%s] is not an XMLRPC URI"%paramName)
97 if not param_value.startswith("http://") and not param_value.startswith(ROSRPC):
98 raise ParameterInvalid("ERROR: parameter [%s] is not an RPC URI"%paramName)
99
100 return param_value
101 return validator
102
104 """
105 Validator that checks that parameter is a valid ROS topic name
106 """
107 def validator(param_value, caller_id):
108 v = valid_name_validator_resolved(param_name, param_value, caller_id)
109 if param_value == '/':
110 raise ParameterInvalid("ERROR: parameter [%s] cannot be the global namespace"%param_name)
111 return v
112 return validator
113
115 """Validator that checks that parameter is a valid ROS service name"""
116 def validator(param_value, caller_id):
117 v = valid_name_validator_resolved(param_name, param_value, caller_id)
118 if param_value == '/':
119 raise ParameterInvalid("ERROR: parameter [%s] cannot be the global namespace"%param_name)
120 return v
121 return validator
122
124 """
125 empty or valid graph resource name.
126 Validator that resolves names unless they an empty string is supplied, in which case
127 an empty string is returned.
128 """
129 def validator(param_value, caller_id):
130 if not isstring(param_value):
131 raise ParameterInvalid("ERROR: parameter [%s] must be a string"%param_name)
132 if not param_value:
133 return ''
134
135 return resolve_name(param_value, caller_id)
136 return validator
137
139 if not param_value or not isstring(param_value):
140 raise ParameterInvalid("ERROR: parameter [%s] must be a non-empty string"%param_name)
141
142
143 if ':' in param_value or ' ' in param_value:
144 raise ParameterInvalid("ERROR: parameter [%s] contains illegal chars"%param_name)
145
146 return resolve_name(param_value, caller_id)
148 if not param_value or not isstring(param_value):
149 raise ParameterInvalid("ERROR: parameter [%s] must be a non-empty string"%param_name)
150
151
152 if ':' in param_value or ' ' in param_value:
153 raise ParameterInvalid("ERROR: parameter [%s] contains illegal chars"%param_name)
154 return param_value
155
157 """
158 Validator that resolves names and also ensures that they are not empty
159 @param param_name: name
160 @type param_name: str
161 @param resolve: if True/omitted, the name will be resolved to
162 a global form. Otherwise, no resolution occurs.
163 @type resolve: bool
164 @return: resolved parameter value
165 @rtype: str
166 """
167 def validator(param_value, caller_id):
168 if resolve:
169 return valid_name_validator_resolved(param_name, param_value, caller_id)
170 return valid_name_validator_unresolved(param_name, param_value, caller_id)
171 return validator
172
174 """
175 Validator that checks for valid, global graph resource name.
176 @return: parameter value
177 @rtype: str
178 """
179 def validator(param_value, caller_id):
180 if not param_value or not isstring(param_value):
181 raise ParameterInvalid("ERROR: parameter [%s] must be a non-empty string"%param_name)
182
183 if not is_global(param_value):
184 raise ParameterInvalid("ERROR: parameter [%s] must be a globally referenced name"%param_name)
185 return param_value
186 return validator
187
189 """validator that checks the type name is specified correctly"""
190 def validator(param_value, caller_id):
191 if param_value == ANYTYPE:
192 return param_value
193 if not param_value or not isstring(param_value):
194 raise ParameterInvalid("ERROR: parameter [%s] must be a non-empty string"%param_name)
195 if not len(param_value.split(TYPE_SEPARATOR)) == 2:
196 raise ParameterInvalid("ERROR: parameter [%s] is not a valid package resource name"%param_name)
197
198 return param_value
199 return validator
200