Package rosmaster :: Module validators

Source Code for Module rosmaster.validators

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2008, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id$ 
 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   
42 -def isstring(s):
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
52 -class ParameterInvalid(Exception):
53 """Exception that is raised when a parameter fails validation checks"""
54 - def __init__(self, message):
55 self._message = message
56
57 - def __str__(self):
58 return str(self._message)
59
60 -def non_empty(param_name):
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
68 -def non_empty_str(param_name):
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
78 -def not_none(param_name):
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 # Validators ###################################### 88
89 -def is_api(paramName):
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 #could do more fancy parsing, but the above catches the major cases well enough 100 return param_value
101 return validator 102
103 -def is_topic(param_name):
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
114 -def is_service(param_name):
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
123 -def empty_or_valid_name(param_name):
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 #return resolve_name(param_value, namespace(caller_id)) 135 return resolve_name(param_value, caller_id)
136 return validator 137
138 -def valid_name_validator_resolved(param_name, param_value, caller_id):
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 #TODO: actual validation of chars 142 # I added the colon check as the common error will be to send an URI instead of name 143 if ':' in param_value or ' ' in param_value: 144 raise ParameterInvalid("ERROR: parameter [%s] contains illegal chars"%param_name) 145 #return resolve_name(param_value, namespace(caller_id)) 146 return resolve_name(param_value, caller_id)
147 -def valid_name_validator_unresolved(param_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 #TODO: actual validation of chars 151 # I added the colon check as the common error will be to send an URI instead of name 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
156 -def valid_name(param_name, resolve=True):
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
173 -def global_name(param_name):
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 #TODO: actual validation of chars 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
188 -def valid_type_name(param_name):
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 #TODO: actual validation of chars 198 return param_value
199 return validator 200