Package dynamic_reconfigure :: Module encoding

Source Code for Module dynamic_reconfigure.encoding

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2009, 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  try: 
 34      import roslib; roslib.load_manifest('dynamic_reconfigure') 
 35  except: 
 36      pass 
 37  import rospy 
 38  import inspect 
 39  import copy 
 40   
 41  from dynamic_reconfigure.msg import Config as ConfigMsg 
 42  from dynamic_reconfigure.msg import ConfigDescription as ConfigDescrMsg 
 43  from dynamic_reconfigure.msg import Group as GroupMsg 
 44  from dynamic_reconfigure.msg import GroupState 
 45  from dynamic_reconfigure.msg import IntParameter, BoolParameter, StrParameter, DoubleParameter, ParamDescription 
 46   
47 -class Config(dict):
48 - def __init__(self, *args, **kwargs):
49 dict.__init__(self, *args, **kwargs)
50
51 - def __getstate__(self):
52 return self.__dict__.items()
53
54 - def __setstate__(self, items):
55 for key, val in items: 56 self.__dict__[key] = val
57
58 - def __repr__(self):
59 return super(Config, self).__repr__()
60
61 - def __setitem__(self, key, value):
62 return super(Config, self).__setitem__(key, value)
63
64 - def __getitem__(self, name):
65 return super(Config, self).__getitem__(name)
66
67 - def __delitem__(self, name):
68 return super(Config, self).__delitem__(name)
69 70 __getattr__ = __getitem__ 71 __setattr__ = __setitem__ 72
73 - def copy(self):
74 return Config(self)
75
76 - def __deepcopy__(self, memo):
77 c = type(self)({}) 78 for key, value in self.iteritems(): 79 c[copy.deepcopy(key)] = copy.deepcopy(value) 80 81 return c
82 83
84 -def encode_description(descr):
85 msg = ConfigDescrMsg() 86 msg.max = encode_config(descr.max) 87 msg.min = encode_config(descr.min) 88 msg.dflt = encode_config(descr.defaults) 89 msg.groups = encode_groups(None, descr.config_description) 90 return msg
91
92 -def encode_groups(parent, group):
93 group_list = [] 94 95 msg = GroupMsg() 96 97 msg.name = group['name'] 98 msg.id = group['id'] 99 msg.parent = group['parent'] 100 msg.type = group['type'] 101 102 for param in group['parameters']: 103 msg.parameters.append(ParamDescription(param['name'], param['type'], param['level'], param['description'], param['edit_method'])) 104 105 group_list.append(msg) 106 for next in group['groups']: 107 group_list.extend(encode_groups(msg, next)) 108 109 return group_list
110
111 -def encode_config(config, flat=True):
112 msg = ConfigMsg() 113 for k, v in config.items(): 114 ## @todo add more checks here? 115 if type(v) == int: msg.ints.append(IntParameter(k, v)) 116 elif type(v) == bool: msg.bools.append(BoolParameter(k, v)) 117 elif type(v) == str: msg.strs.append(StrParameter(k, v)) 118 elif type(v) == float: msg.doubles.append(DoubleParameter(k, v)) 119 elif type(v) == dict or isinstance(v, Config): 120 if flat is True: 121 def flatten(g): 122 groups = [] 123 for name, group in g['groups'].items(): 124 groups.extend(flatten(group)) 125 groups.append(GroupState(group['name'], group['state'], group['id'], group['parent'])) 126 return groups
127 msg.groups.append(GroupState(v['name'], v['state'], v['id'], v['parent'])) 128 msg.groups.extend(flatten(v)) 129 else: 130 msg.groups = [GroupState(x['name'], x['state'], x['id'], x['parent']) for x in v] 131 132 return msg 133
134 -def group_dict(group):
135 try: 136 state = group.state 137 except AttributeError: 138 state = True 139 if hasattr(group, 'type'): 140 type = group.type 141 else: 142 type ='' 143 return Config({ 144 'id' : group.id, 145 'parent' : group.parent, 146 'name' : group.name, 147 'type' : type, 148 'state': state, 149 'groups' : Config({}), 150 'parameters' : Config({}), 151 })
152
153 -def decode_description(msg):
154 mins = decode_config(msg.min) 155 maxes = decode_config(msg.max) 156 defaults = decode_config(msg.dflt) 157 groups = {} 158 grouplist = msg.groups 159 160 def params_from_msg(msg): 161 params = [] 162 for param in msg.parameters: 163 name = param.name 164 params.append({ 165 'name': name, 166 'min' : mins[name], 167 'max' : maxes[name], 168 'default' : defaults[name], 169 'type' : param.type, 170 'level': param.level, 171 'description' : param.description, 172 'edit_method' : param.edit_method, 173 }) 174 return params
175 176 # grab the default group 177 for group in grouplist: 178 if group.id == 0: 179 groups = group_dict(group) 180 groups['parameters'] = params_from_msg(group) 181 182 def build_tree(group): 183 children = Config({}) 184 for g in grouplist: 185 if g.id == 0: 186 pass 187 elif g.parent == group['id']: 188 gd = group_dict(g) 189 190 gd['parameters'] = params_from_msg(g) 191 gd['groups'] = build_tree(gd) 192 # add the dictionary into the tree 193 children[gd['name']] = gd 194 return children 195 196 groups['groups'] = build_tree(groups) 197 198 return groups 199
200 -def get_tree(m, group = None):
201 if group is None: 202 for x in m.groups: 203 if x.id == 0: 204 group = x 205 206 children = Config({}) 207 for g in m.groups: 208 if g.id == 0: 209 pass 210 elif g.parent == group.id: 211 gd = group_dict(g) 212 213 gd['groups'] = get_tree(m, g) 214 children[gd['name']] = gd 215 216 if group.id == 0: 217 ret = group_dict(group) 218 ret['groups'] = children 219 return ret 220 else: 221 return children
222
223 -def initial_config(msg, description = None):
224 d = Config([(kv.name, kv.value) for kv in msg.bools + msg.ints + msg.strs + msg.doubles]) 225 def gt(m, descr, group = None): 226 # get the default group 227 if group is None: 228 for x in m.groups: 229 if x.id == 0: 230 group = x 231 232 children = Config({}) 233 for g in m.groups: 234 if g.id == 0: 235 pass 236 elif g.parent == group.id: 237 gd = group_dict(g) 238 239 def find_state(gr, dr): 240 for g in dr['groups']: 241 if g['id'] == gr['id']: 242 gr['state'] = g['state'] 243 return 244 else: 245 find_state(gr, g) 246 return
247 248 find_state(gd, descr) 249 250 # Get the tree for this group 251 gd['groups'] = gt(m, descr, g) 252 children[gd['name']] = gd 253 254 if group.id == 0: 255 ret = group_dict(group) 256 ret['groups'] = children 257 return ret 258 else: 259 return children 260 261 if not msg.groups == [] and description is not None: 262 d["groups"] = gt(msg, description) 263 264 def add_params(group, descr): 265 for param in descr['parameters']: 266 group['parameters'][param['name']] = d[param['name']] 267 for n, g in group['groups'].items(): 268 for dr in descr['groups']: 269 if dr['name'] == g['name']: 270 add_params(g, dr) 271 272 add_params(d['groups'], description) 273 274 return d 275
276 -def decode_config(msg, description = None):
277 d = Config([(kv.name, kv.value) for kv in msg.bools + msg.ints + msg.strs + msg.doubles]) 278 if not msg.groups == [] and description is not None: 279 d["groups"] = get_tree(msg) 280 281 def add_params(group, descr): 282 for param in descr['parameters']: 283 if param['name'] in d.keys(): 284 group[param['name']] = d[param['name']] 285 for n, g in group['groups'].items(): 286 for nr, dr in descr['groups'].items(): 287 if dr['name'] == g['name']: 288 add_params(g, dr)
289 290 add_params(d['groups'], description) 291 292 return d 293
294 -def extract_params(group):
295 params = [] 296 params.extend(group['parameters']) 297 try: 298 for n,g in group['groups'].items(): 299 params.extend(extract_params(g)) 300 except AttributeError: 301 for g in group['groups']: 302 params.extend(extract_params(g)) 303 return params
304
305 -def get_parents(group, descriptions):
306 parents = [] 307 for p in descriptions['group']: 308 if p['id'] == group['parent']: 309 parents.extend(get_parents(p, descriptions)) 310 parents.append(p) 311 return parents
312