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 -class ParameterInvalid(Exception):
43 """Exception that is raised when a parameter fails validation checks"""
44 - def __init__(self, message):
45 self._message = message
46
47 - def __str__(self):
48 return str(self._message)
49
50 -def non_empty(param_name):
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
58 -def non_empty_str(param_name):
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
68 -def not_none(param_name):
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 # Validators ###################################### 78
79 -def is_api(paramName):
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 #could do more fancy parsing, but the above catches the major cases well enough 90 return param_value
91 return validator 92
93 -def is_topic(param_name):
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
104 -def is_service(param_name):
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
113 -def empty_or_valid_name(param_name):
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 #return resolve_name(param_value, namespace(caller_id)) 125 return resolve_name(param_value, caller_id)
126 return validator 127
128 -def valid_name_validator_resolved(param_name, param_value, caller_id):
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 #TODO: actual validation of chars 132 # I added the colon check as the common error will be to send an URI instead of name 133 if ':' in param_value or ' ' in param_value: 134 raise ParameterInvalid("ERROR: parameter [%s] contains illegal chars"%param_name) 135 #return resolve_name(param_value, namespace(caller_id)) 136 return resolve_name(param_value, caller_id)
137 -def valid_name_validator_unresolved(param_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 #TODO: actual validation of chars 141 # I added the colon check as the common error will be to send an URI instead of name 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
146 -def valid_name(param_name, resolve=True):
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
163 -def global_name(param_name):
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 #TODO: actual validation of chars 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
178 -def valid_type_name(param_name):
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 #TODO: actual validation of chars 188 return param_value
189 return validator 190