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