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
77 - def __init__(self):
78 self.reload()
79
80 - def reload(self):
81 ''' 82 Loads the settings from file or sets default values if no one exists. 83 ''' 84 self._terminal_emulator = None 85 self._masteruri = masteruri_from_ros() 86 self.CFG_PATH = os.path.join(get_ros_home(), 'node_manager') 87 # loads the current configuration path. If the path was changed, a redirection 88 # file exists with new configuration folder 89 if not os.path.isdir(self.CFG_PATH): 90 os.makedirs(self.CFG_PATH) 91 self._cfg_path = self.CFG_PATH 92 else: 93 settings = self.qsettings(os.path.join(self.CFG_PATH, self.CFG_REDIRECT_FILE)) 94 self._cfg_path = settings.value('cfg_path', self.CFG_PATH) 95 # after the settings path was loaded, load other settings 96 self._robots_path = self.ROBOTS_DIR 97 settings = self.qsettings(self.CFG_FILE) 98 self._default_user = settings.value('default_user', self.USER_DEFAULT) 99 try: 100 self._launch_history_length = int(settings.value('launch_history_length', self.LAUNCH_HISTORY_LENGTH)) 101 except: 102 self._launch_history_length = self.LAUNCH_HISTORY_LENGTH 103 try: 104 self._param_history_length = int(settings.value('param_history_length', self.PARAM_HISTORY_LENGTH)) 105 except: 106 self._param_history_length = self.PARAM_HISTORY_LENGTH 107 self._current_dialog_path = self.CURRENT_DIALOG_PATH 108 self._log_viewer = self.LOG_VIEWER 109 self._start_remote_script = self.STARTER_SCRIPT 110 self._respawn_script = self.RESPAWN_SCRIPT 111 self._launch_view_file_ext = self.str2list(settings.value('launch_view_file_ext', ', '.join(self.LAUNCH_VIEW_EXT))) 112 self._store_geometry = self.str2bool(settings.value('store_geometry', self.STORE_GEOMETRY)) 113 self.SEARCH_IN_EXT = list(set(self.SEARCH_IN_EXT) | set(self._launch_view_file_ext))
114
115 - def masteruri(self):
116 return self._masteruri
117 118 @property
119 - def cfg_path(self):
120 return self._cfg_path
121 122 @cfg_path.setter
123 - def cfg_path(self, path):
124 if path: 125 abspath = os.path.abspath(path).rstrip(os.path.sep) 126 if not os.path.isdir(abspath): 127 os.makedirs(abspath) 128 self._cfg_path = abspath 129 if abspath != os.path.abspath(self.CFG_PATH).rstrip(os.path.sep): 130 settings = self.qsettings(os.path.join(self.CFG_PATH, self.CFG_REDIRECT_FILE)) 131 settings.setValue('cfg_path', abspath) 132 else: 133 # remove the redirection 134 settings = self.qsettings(os.path.join(self.CFG_PATH, self.CFG_REDIRECT_FILE)) 135 settings.remove('cfg_path') 136 self.reload()
137 138 @property
139 - def robots_path(self):
140 return self._robots_path
141 142 @robots_path.setter
143 - def robots_path(self, path):
144 if path: 145 if not os.path.isdir(path): 146 os.makedirs(path) 147 self._robots_path = path 148 settings = self.qsettings(self.CFG_FILE) 149 settings.setValue('robots_path', self._robots_path)
150 151 @property
152 - def default_user(self):
153 return self._default_user
154 155 @default_user.setter
156 - def default_user(self, user):
157 if user: 158 self._default_user = user 159 settings = self.qsettings(self.CFG_FILE) 160 settings.setValue('default_user', self._default_user)
161 162 @property
163 - def launch_history_length(self):
164 return self._launch_history_length
165 166 @launch_history_length.setter
167 - def launch_history_length(self, len):
168 self._launch_history_length = len 169 settings = self.qsettings(self.CFG_FILE) 170 settings.setValue('launch_history_length', self._launch_history_length)
171 172 @property
173 - def param_history_length(self):
174 return self._param_history_length
175 176 @param_history_length.setter
177 - def param_history_length(self, len):
178 self._param_history_length = len 179 settings = self.qsettings(self.CFG_FILE) 180 settings.setValue('param_history_length', self._param_history_length)
181 182 @property
183 - def current_dialog_path(self):
184 return self._current_dialog_path
185 186 @current_dialog_path.setter
187 - def current_dialog_path(self, path):
188 self._current_dialog_path = path
189
190 - def robot_image_file(self, robot_name):
191 return os.path.join(self.ROBOTS_DIR, '%s.png'%robot_name)
192 193 @property
194 - def log_viewer(self):
195 return self._log_viewer
196 197 @log_viewer.setter
198 - def log_viewer(self, viewer):
199 self._log_viewer = viewer
200 201 @property
202 - def start_remote_script(self):
203 return self._start_remote_script
204 205 @start_remote_script.setter
206 - def start_remote_script(self, script):
207 self._start_remote_script = script
208 209 @property
210 - def respawn_script(self):
211 return self._respawn_script
212 213 @respawn_script.setter
214 - def respawn_script(self, script):
215 self._respawn_script = script
216 217 @property
218 - def launch_view_file_ext(self):
219 return self._launch_view_file_ext
220 221 @launch_view_file_ext.setter
222 - def launch_view_file_ext(self, exts):
223 self._launch_view_file_ext = self.str2list('%s'%exts) 224 settings = self.qsettings(self.CFG_FILE) 225 settings.setValue('launch_view_file_ext', self._launch_view_file_ext) 226 self.SEARCH_IN_EXT = list(set(self.SEARCH_IN_EXT) | set(self._launch_view_file_ext))
227 228 @property
229 - def store_geometry(self):
230 return self._store_geometry
231 232 @store_geometry.setter
233 - def store_geometry(self, value):
234 v = self.str2bool(value) 235 if self._store_geometry != v: 236 self._store_geometry = v 237 settings = self.qsettings(self.CFG_FILE) 238 settings.setValue('store_geometry', self._store_geometry)
239
240 - def str2bool(self, v):
241 if isinstance(v, bool): 242 return v 243 return v.lower() in ("yes", "true", "t", "1")
244
245 - def str2list(self, l):
246 if isinstance(l, list): 247 return l 248 try: 249 l = l.strip('[]') 250 l = l.replace('u"', '') 251 l = l.replace('"', '') 252 l = l.replace("'", '') 253 l = l.replace(",", ' ') 254 return [str(i).strip() for i in l.split(' ') if i] 255 except: 256 return []
257
258 - def terminal_cmd(self, cmd, title):
259 ''' 260 Creates a command string to run with a terminal prefix 261 @param cmd: the list with a command and args 262 @type cmd: [str,..] 263 @param title: the title of the terminal 264 @type title: str 265 @return: command with a terminal prefix 266 @rtype: str 267 ''' 268 if self._terminal_emulator is None: 269 self._terminal_emulator = "" 270 for t in ['/usr/bin/x-terminal-emulator', '/usr/bin/xterm']: 271 if os.path.isfile(t) and os.access(t, os.X_OK): 272 self._terminal_emulator = t 273 break 274 if self._terminal_emulator == "": return "" 275 return "%s -T %s -e %s"%(self._terminal_emulator, title, ' '.join(cmd))
276
277 - def qsettings(self, file):
278 from python_qt_binding import QtCore 279 path = file 280 if not file.startswith(os.path.sep): 281 path = os.path.join(self.cfg_path, file) 282 return QtCore.QSettings(path, 283 QtCore.QSettings.IniFormat)
284