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 roslib.names import ANYTYPE, TYPE_SEPARATOR, resolve_name 
 38   
 39  ROSRPC = "rosrpc://" 
 40   
41 -class ParameterInvalid(Exception):
42 """Exception that is raised when a parameter fails validation checks"""
43 - def __init__(self, message):
44 self._message = message
45
46 - def __str__(self):
47 return str(self._message)
48
49 -def non_empty(param_name):
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
57 -def non_empty_str(param_name):
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
67 -def not_none(param_name):
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 # Validators ###################################### 77
78 -def is_api(paramName):
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 #could do more fancy parsing, but the above catches the major cases well enough 89 return param_value
90 return validator 91
92 -def is_topic(param_name):
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
103 -def is_service(param_name):
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
112 -def empty_or_valid_name(param_name):
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 #return resolve_name(param_value, namespace(caller_id)) 124 return resolve_name(param_value, caller_id)
125 return validator 126
127 -def valid_name_validator_resolved(param_name, param_value, caller_id):
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 #TODO: actual validation of chars 131 # I added the colon check as the common error will be to send an URI instead of name 132 if ':' in param_value or ' ' in param_value: 133 raise ParameterInvalid("ERROR: parameter [%s] contains illegal chars"%param_name) 134 #return resolve_name(param_value, namespace(caller_id)) 135 return resolve_name(param_value, caller_id)
136 -def valid_name_validator_unresolved(param_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 #TODO: actual validation of chars 140 # I added the colon check as the common error will be to send an URI instead of name 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
145 -def valid_name(param_name, resolve=True):
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
162 -def global_name(param_name):
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 #TODO: actual validation of chars 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
177 -def valid_type_name(param_name):
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 #TODO: actual validation of chars 187 return param_value
188 return validator 189