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