Package node_manager_fkie :: Module settings'
[frames] | no frames]

Source Code for Module node_manager_fkie.settings'

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  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 Fraunhofer 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  import os 
 34  import roslib 
 35   
 36  from common import get_ros_home, masteruri_from_ros 
37 38 -class Settings(object):
39 40 USER_DEFAULT = 'robot' 41 # set the cwd to the package of the node_manager_fkie to support the images 42 # in HTML descriptions of the robots and capabilities 43 PKG_NAME = 'node_manager_fkie' 44 PACKAGE_DIR = roslib.packages.get_pkg_dir(PKG_NAME) 45 ROBOTS_DIR = os.path.join(PACKAGE_DIR, 'images') 46 CFG_PATH = os.path.join('.node_manager', os.sep) 47 '''@ivar: configuration path to store the history.''' 48 HELP_FILE = os.path.join(PACKAGE_DIR, 'README.rst') 49 CURRENT_DIALOG_PATH = os.path.expanduser('~') 50 LOG_PATH = os.environ.get('ROS_LOG_DIR') if os.environ.get('ROS_LOG_DIR') else os.path.join(os.path.expanduser('~'), '.ros/log/') 51 52 LOG_VIEWER = "/usr/bin/less -fKLnQrSU" 53 STARTER_SCRIPT = 'rosrun node_manager_fkie remote_nm.py' 54 RESPAWN_SCRIPT = 'rosrun node_manager_fkie respawn' 55 ''' 56 the script used on remote hosts to start new ROS nodes 57 ''' 58 59 LAUNCH_HISTORY_FILE = 'launch.history' 60 LAUNCH_HISTORY_LENGTH = 5 61 62 PARAM_HISTORY_FILE = 'param.history' 63 PARAM_HISTORY_LENGTH = 12 64 65 CFG_REDIRECT_FILE = 'redirect' 66 CFG_FILE = 'settings.ini' 67 CFG_GUI_FILE = 'settings.ini' 68 69 TIMEOUT_CONTROL = 5 70 TIMEOUT_UPDATES = 20 71 72 SEARCH_IN_EXT = ['.launch', '.yaml', '.conf', '.cfg', '.iface', '.sync', '.test', '.xml'] 73 LAUNCH_VIEW_EXT = ['.yaml', '.conf', '.cfg', '.iface', '.sync', '.test'] 74 75 STORE_GEOMETRY = True 76 AUTOUPDATE = True 77
78 - def __init__(self):
79 self.reload()
80
81 - def reload(self):
82 ''' 83 Loads the settings from file or sets default values if no one exists. 84 ''' 85 self._terminal_emulator = None 86 self._terminal_command_arg = 'e' 87 self._masteruri = masteruri_from_ros() 88 self.CFG_PATH = os.path.join(get_ros_home(), 'node_manager') 89 # loads the current configuration path. If the path was changed, a redirection 90 # file exists with new configuration folder 91 if not os.path.isdir(self.CFG_PATH): 92 os.makedirs(self.CFG_PATH) 93 self._cfg_path = self.CFG_PATH 94 else: 95 settings = self.qsettings(os.path.join(self.CFG_PATH, self.CFG_REDIRECT_FILE)) 96 self._cfg_path = settings.value('cfg_path', self.CFG_PATH) 97 # after the settings path was loaded, load other settings 98 self._robots_path = self.ROBOTS_DIR 99 settings = self.qsettings(self.CFG_FILE) 100 self._default_user = settings.value('default_user', self.USER_DEFAULT) 101 settings.beginGroup('default_user_hosts') 102 self._default_user_hosts = dict() 103 for k in settings.childKeys(): 104 self._default_user_hosts[k] = settings.value(k, self._default_user) 105 settings.endGroup() 106 try: 107 self._launch_history_length = int(settings.value('launch_history_length', self.LAUNCH_HISTORY_LENGTH)) 108 except: 109 self._launch_history_length = self.LAUNCH_HISTORY_LENGTH 110 try: 111 self._param_history_length = int(settings.value('param_history_length', self.PARAM_HISTORY_LENGTH)) 112 except: 113 self._param_history_length = self.PARAM_HISTORY_LENGTH 114 self._current_dialog_path = self.CURRENT_DIALOG_PATH 115 self._log_viewer = self.LOG_VIEWER 116 self._start_remote_script = self.STARTER_SCRIPT 117 self._respawn_script = self.RESPAWN_SCRIPT 118 self._launch_view_file_ext = self.str2list(settings.value('launch_view_file_ext', ', '.join(self.LAUNCH_VIEW_EXT))) 119 self._store_geometry = self.str2bool(settings.value('store_geometry', self.STORE_GEOMETRY)) 120 self.SEARCH_IN_EXT = list(set(self.SEARCH_IN_EXT) | set(self._launch_view_file_ext)) 121 self._autoupdate = self.str2bool(settings.value('autoupdate', self.AUTOUPDATE))
122
123 - def masteruri(self):
124 return self._masteruri
125 126 @property
127 - def cfg_path(self):
128 return self._cfg_path
129 130 @cfg_path.setter
131 - def cfg_path(self, path):
132 if path: 133 abspath = os.path.abspath(path).rstrip(os.path.sep) 134 if not os.path.isdir(abspath): 135 os.makedirs(abspath) 136 self._cfg_path = abspath 137 if abspath != os.path.abspath(self.CFG_PATH).rstrip(os.path.sep): 138 settings = self.qsettings(os.path.join(self.CFG_PATH, self.CFG_REDIRECT_FILE)) 139 settings.setValue('cfg_path', abspath) 140 else: 141 # remove the redirection 142 settings = self.qsettings(os.path.join(self.CFG_PATH, self.CFG_REDIRECT_FILE)) 143 settings.remove('cfg_path') 144 self.reload()
145 146 @property
147 - def robots_path(self):
148 return self._robots_path
149 150 @robots_path.setter
151 - def robots_path(self, path):
152 if path: 153 if not os.path.isdir(path): 154 os.makedirs(path) 155 self._robots_path = path 156 settings = self.qsettings(self.CFG_FILE) 157 settings.setValue('robots_path', self._robots_path)
158 159 @property
160 - def default_user(self):
161 return self._default_user
162 163 @default_user.setter
164 - def default_user(self, user):
165 if user: 166 self._default_user = user 167 settings = self.qsettings(self.CFG_FILE) 168 settings.setValue('default_user', self._default_user)
169
170 - def host_user(self, host):
171 if host in self._default_user_hosts: 172 return self._default_user_hosts[host] 173 return self.default_user
174
175 - def set_host_user(self, host, user):
176 if host and user: 177 self._default_user_hosts[host] = user 178 settings = self.qsettings(self.CFG_FILE) 179 settings.setValue('default_user_hosts/%s'%host, user)
180 181 @property
182 - def launch_history_length(self):
183 return self._launch_history_length
184 185 @launch_history_length.setter
186 - def launch_history_length(self, length):
187 self._launch_history_length = length 188 settings = self.qsettings(self.CFG_FILE) 189 settings.setValue('launch_history_length', self._launch_history_length)
190 191 @property
192 - def param_history_length(self):
193 return self._param_history_length
194 195 @param_history_length.setter
196 - def param_history_length(self, length):
197 self._param_history_length = length 198 settings = self.qsettings(self.CFG_FILE) 199 settings.setValue('param_history_length', self._param_history_length)
200 201 @property
202 - def current_dialog_path(self):
203 return self._current_dialog_path
204 205 @current_dialog_path.setter
206 - def current_dialog_path(self, path):
207 self._current_dialog_path = path
208
209 - def robot_image_file(self, robot_name):
210 return os.path.join(self.ROBOTS_DIR, '%s.png'%robot_name)
211 212 @property
213 - def log_viewer(self):
214 return self._log_viewer
215 216 @log_viewer.setter
217 - def log_viewer(self, viewer):
218 self._log_viewer = viewer
219 220 @property
221 - def start_remote_script(self):
222 return self._start_remote_script
223 224 @start_remote_script.setter
225 - def start_remote_script(self, script):
226 self._start_remote_script = script
227 228 @property
229 - def respawn_script(self):
230 return self._respawn_script
231 232 @respawn_script.setter
233 - def respawn_script(self, script):
234 self._respawn_script = script
235 236 @property
237 - def launch_view_file_ext(self):
238 return self._launch_view_file_ext
239 240 @launch_view_file_ext.setter
241 - def launch_view_file_ext(self, exts):
242 self._launch_view_file_ext = self.str2list('%s'%exts) 243 settings = self.qsettings(self.CFG_FILE) 244 settings.setValue('launch_view_file_ext', self._launch_view_file_ext) 245 self.SEARCH_IN_EXT = list(set(self.SEARCH_IN_EXT) | set(self._launch_view_file_ext))
246 247 @property
248 - def store_geometry(self):
249 return self._store_geometry
250 251 @store_geometry.setter
252 - def store_geometry(self, value):
253 v = self.str2bool(value) 254 if self._store_geometry != v: 255 self._store_geometry = v 256 settings = self.qsettings(self.CFG_FILE) 257 settings.setValue('store_geometry', self._store_geometry)
258 259 @property
260 - def autoupdate(self):
261 return self._autoupdate
262 263 @autoupdate.setter
264 - def autoupdate(self, value):
265 v = self.str2bool(value) 266 if self._autoupdate != v: 267 self._autoupdate = v 268 settings = self.qsettings(self.CFG_FILE) 269 settings.setValue('autoupdate', self._autoupdate)
270
271 - def str2bool(self, v):
272 if isinstance(v, bool): 273 return v 274 return v.lower() in ("yes", "true", "t", "1")
275
276 - def str2list(self, l):
277 if isinstance(l, list): 278 return l 279 try: 280 l = l.strip('[]') 281 l = l.replace('u"', '') 282 l = l.replace('"', '') 283 l = l.replace("'", '') 284 l = l.replace(",", ' ') 285 return [str(i).strip() for i in l.split(' ') if i] 286 except: 287 return []
288
289 - def terminal_cmd(self, cmd, title):
290 ''' 291 Creates a command string to run with a terminal prefix 292 @param cmd: the list with a command and args 293 @type cmd: [str,..] 294 @param title: the title of the terminal 295 @type title: str 296 @return: command with a terminal prefix 297 @rtype: str 298 ''' 299 if self._terminal_emulator is None: 300 self._terminal_emulator = "" 301 for t in ['/usr/bin/x-terminal-emulator', '/usr/bin/xterm']: 302 if os.path.isfile(t) and os.access(t, os.X_OK): 303 #workaround to support the command parameter in different terminal 304 if os.path.basename(os.path.realpath(t)) in ['terminator', 'gnome-terminal', 'xfce4-terminal']: 305 self._terminal_command_arg = 'x' 306 self._terminal_emulator = t 307 break 308 if self._terminal_emulator == "": return "" 309 return '%s -T "%s" -%s %s'%(self._terminal_emulator, title, self._terminal_command_arg, ' '.join(cmd))
310
311 - def qsettings(self, settings_file):
312 from python_qt_binding import QtCore 313 path = settings_file 314 if not settings_file.startswith(os.path.sep): 315 path = os.path.join(self.cfg_path, settings_file) 316 return QtCore.QSettings(path, QtCore.QSettings.IniFormat)
317