Package rosh :: Package impl :: Module param
[frames] | no frames]

Source Code for Module rosh.impl.param

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2010, 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: param.py 11680 2010-10-22 07:04:07Z kwc $ 
 34   
 35  from __future__ import with_statement 
 36   
 37  import roslib.packages 
 38  import rosparam as _rosparam 
 39  import rospy 
 40   
 41  from rosh.impl.exceptions import ROSHException 
 42  from rosh.impl.namespace import Namespace, Concept 
 43   
44 -def rosparam(*args):
45 """ 46 Load rosparam YAML file. This can either be called with 47 rosparam(filename) or rosparam(pkg, filename) 48 49 @return: loaded data 50 """ 51 #TODO: should this do a search to find filename 52 if len(args) == 2: 53 pkg, filename = args 54 d = roslib.packages.get_pkg_dir(pkg) 55 matches = roslib.packages.find_resource(pkg, filename) 56 if matches: 57 filename = matches[0] 58 else: 59 filename = None 60 elif len(args) == 1: 61 filename = args[0] 62 else: 63 raise ValueError(args) 64 65 if not filename: 66 #TODO: what should the error behavior of ROSH be? 67 return None 68 69 #TODO: this strip any namespace in the file 70 return _rosparam.load_file(filename)[0][0]
71
72 -def rosparam_str(yaml_str):
73 """ 74 Load rosparam YAML string 75 @return: loaded data 76 """ 77 return _rosparam.load_str(yaml_str, 'rosh')
78
79 -class ParamNS(Namespace):
80
81 - def __init__(self, name, config):
82 """ 83 ctor. 84 @param config: Namespace configuration instance. 85 @type config: L{NamespaceConfig} 86 """ 87 super(ParamNS, self).__init__(name, config) 88 self._master = config.master
89
90 - def _list(self):
91 """ 92 Override Namespace._list() 93 """ 94 try: 95 val = self._master.getParam(self._ns) 96 return [self._ns+k for k in val.iterkeys()] 97 except: 98 return []
99
100 - def __repr__(self):
101 return self.__str__()
102
103 - def __str__(self):
104 try: 105 return "Boxed[%s]"%str(self._master.getParam(self._ns)) 106 except: 107 return '<uninitialized>'
108
109 - def __getitem__(self, key):
110 if type(key) == int: 111 raise ROSHException("cannot index a ParamNS object. Are you missing a () call to get the value?") 112 return super(ParamNS, self).__getitem__(key)
113
114 - def __setitem__(self, key, value):
115 if key.startswith('_'): 116 return object.__setitem__(key, value) 117 else: 118 key = roslib.names.ns_join(self._ns, key) 119 try: 120 self._master.setParam(key, value) 121 return True 122 except: 123 return False
124
125 - def __delitem__(self, key):
126 if key.startswith('_'): 127 return object.__delitem__(key) 128 else: 129 key = roslib.names.ns_join(self._ns, key) 130 try: 131 self._master.deleteParam(key) 132 return True 133 except: 134 raise KeyError(key)
135
136 - def __setattr__(self, key, value):
137 if key.startswith('_'): 138 return object.__setattr__(self, key, value) 139 else: 140 return self.__setitem__(key, value)
141
142 - def __delattr__(self, key):
143 if key.startswith('_'): 144 return object.__delattr__(self, key) 145 else: 146 return self.__delitem__(key)
147
148 - def __contains__(self, key):
149 key = roslib.names.ns_join(self._ns, key) 150 return self._master.hasParam(key)
151
152 - def __call__(self):
153 """ 154 @return: current parameter value 155 """ 156 try: 157 return self._master.getParam(self._ns) 158 except: 159 raise KeyError(self._ns)
160
161 -class Parameters(Concept):
162
163 - def __init__(self, ctx, lock):
164 super(Parameters, self).__init__(ctx, lock, ParamNS)
165
166 - def __setattr__(self, key, value):
167 if key.startswith('_'): 168 return object.__setattr__(self, key, value) 169 else: 170 return self._root.__setitem__(key, value)
171
172 - def __call__(self):
173 return self._root.__call__()
174
175 - def __delitem__(self, key):
176 if key.startswith('_'): 177 return object.__delitem__(key) 178 else: 179 return self._root.__delitem__(key)
180
181 - def __delattr__(self, key):
182 if key.startswith('_'): 183 return object.__delattr__(self, key) 184 else: 185 return self._root.__delitem__(key)
186
187 - def __contains__(self, key):
188 return self._root.__contains__(key)
189