Package x2go :: Package backends :: Package profiles :: Module _file
[frames] | no frames]

Source Code for Module x2go.backends.profiles._file

  1  # -*- coding: utf-8 -*- 
  2   
  3  # Copyright (C) 2010-2012 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de> 
  4  # 
  5  # Python X2Go is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU Affero General Public License as published by 
  7  # the Free Software Foundation; either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Python X2Go is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU Affero General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU Affero General Public License 
 16  # along with this program; if not, write to the 
 17  # Free Software Foundation, Inc., 
 18  # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 
 19   
 20  """\ 
 21  L{X2goSessionProfiles} class - managing x2goclient session profiles. 
 22   
 23  L{X2goSessionProfiles} is a public API class. Use this class in your Python X2Go based  
 24  applications. 
 25   
 26  """ 
 27  __NAME__ = 'x2gosessionprofiles-pylib' 
 28   
 29  import copy 
 30  import types 
 31   
 32  # Python X2Go modules 
 33  from x2go.defaults import X2GO_SESSIONPROFILES_CONFIGFILES as _X2GO_SESSIONPROFILES_CONFIGFILES 
 34  from x2go.defaults import X2GO_SESSIONPROFILE_DEFAULTS as _X2GO_SESSIONPROFILE_DEFAULTS 
 35  from x2go.defaults import X2GO_DESKTOPSESSIONS as _X2GO_DESKTOPSESSIONS 
 36  import x2go.inifiles as inifiles 
 37  import x2go.log as log 
 38  import x2go.utils as utils 
 39  from x2go.x2go_exceptions import X2goProfileException 
40 41 42 -class X2goSessionProfilesFILE(inifiles.X2goIniFile):
43 44 defaultSessionProfile = _X2GO_SESSIONPROFILE_DEFAULTS 45 _non_profile_sections = ('embedded') 46
47 - def __init__(self, config_files=_X2GO_SESSIONPROFILES_CONFIGFILES, defaults=None, session_profile_defaults=None, logger=None, loglevel=log.loglevel_DEFAULT):
48 """\ 49 Retrieve X2Go session profiles from a file, typically C{~/.x2goclient/sessions}. 50 51 @param config_files: a list of config file locations, the first file name in this list the user has write access to will be the user configuration file 52 @type config_files: C{list} 53 @param defaults: not used for this class 54 @type defaults: C{dict} 55 @param session_profile_defaults: a default session profile 56 @type session_profile_defaults: C{dict} 57 @param logger: you can pass an L{X2goLogger} object to the 58 L{X2goSessionProfilesFILE} constructor 59 @type logger: L{X2goLogger} instance 60 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be 61 constructed with the given loglevel 62 @type loglevel: C{int} 63 64 """ 65 self.defaultValues = {} 66 self._profile_metatypes = {} 67 self._cached_profile_ids = [] 68 self._cached_profile_names = [] 69 70 if logger is None: 71 self.logger = log.X2goLogger(loglevel=loglevel) 72 else: 73 self.logger = copy.deepcopy(logger) 74 self.logger.tag = __NAME__ 75 76 # providing defaults for an X2goSessionProfiles instance will---in the worst case---override your 77 # existing sessions file in your home directory once you write the sessions back to file... 78 inifiles.X2goIniFile.__init__(self, config_files, defaults=defaults, logger=logger, loglevel=loglevel) 79 80 if utils._checkSessionProfileDefaults(session_profile_defaults): 81 self.defaultSessionProfile = session_profile_defaults 82 83 self.session_profiles = [ p for p in self.iniConfig.sections() if p not in self._non_profile_sections ] 84 for session_profile in self.session_profiles: 85 for key, default_value in self.defaultSessionProfile.iteritems(): 86 if not self.iniConfig.has_option(session_profile, key): 87 self._storeValue(session_profile, key, default_value) 88 self.get_profile_metatype(session_profile)
89
90 - def __call__(self, profile_id_or_name):
91 """\ 92 Retrieve the session profile configuration for a given session profile ID (or name) 93 94 @param profile_id_or_name: profile ID or profile name 95 @type profile_id_or_name: C{str} 96 97 @return: the profile ID's / name's profile configuration 98 @rtype: C{dict} 99 100 """ 101 _profile_id = self.check_profile_id_or_name(self, profile_id_or_name) 102 return self.get_profile_config(profile_id=_profile_id)
103
104 - def get_profile_metatype(self, profile_id_or_name, force=False):
105 """\ 106 Detect a human readable session profile type from the session profile configuration. 107 108 @param profile_id_or_name: profile ID or profile name 109 @type profile_id_or_name: C{str} 110 @param force: re-detect the meta type, otherwise use a cached result 111 @type force: C{bool} 112 113 @return: the profile ID's / name's meta type 114 @rtype: C{str} 115 116 """ 117 118 _profile_id = self.check_profile_id_or_name(profile_id_or_name) 119 if not self._profile_metatypes.has_key(_profile_id) or force: 120 _config = self.get_profile_config(_profile_id) 121 if _config['host']: 122 if _config['rdpserver'] and _config['command'] == 'RDP': 123 _metatype = 'RDP/proxy' 124 elif _config['published']: 125 126 if _config['command'] in _X2GO_DESKTOPSESSIONS.keys(): 127 _metatype = '%s + Published Applications' % _config['command'] 128 else: 129 _metatype = 'Published Applications' 130 131 elif _config['rootless']: 132 _metatype = 'Single Applications' 133 elif _config['command'] in _X2GO_DESKTOPSESSIONS.keys(): 134 _metatype = '%s Desktop' % _config['command'] 135 elif _config['command'] in _X2GO_DESKTOPSESSIONS.values(): 136 _metatype = '%s Desktop' % [ s for s in _X2GO_DESKTOPSESSIONS.keys() if _config['command'] == _X2GO_DESKTOPSESSIONS[s] ][0] 137 else: 138 _metatype = 'CUSTOM Desktop' 139 else: 140 if _config['rdpserver'] and _config['command'] == 'RDP': 141 _metatype = 'RDP/direct' 142 else: 143 _metatype = 'not supported' 144 self._profile_metatypes[_profile_id] = _metatype 145 else: 146 return self._profile_metatypes[_profile_id]
147
148 - def get_profile_option_type(self, option):
149 """\ 150 Get the data type for a specific session profile option. 151 152 @param option: the option to get the data type for 153 @type option: will be detected by this method 154 155 @return: the data type of C{option} 156 @rtype: C{type} 157 158 """ 159 try: 160 return type(self.defaultSessionProfile[option]) 161 except KeyError: 162 return types.StringType
163
164 - def get_type(self, section, key):
165 """\ 166 Override the parent class's get_type method due to the special layout of this class. 167 168 @param section: INI file section 169 @type section: C{str} 170 @param key: key in INI file section 171 @type key: C{str} 172 173 @return: the data type of C{key} in C{section} 174 @rtype: C{type} 175 176 """ 177 # we have to handle the get_type method separately... 178 return self.get_profile_option_type(key)
179
180 - def get_profile_config(self, profile_id_or_name=None, profile_id=None):
181 """\ 182 The configuration options for a single session profile. 183 184 @param profile_id_or_name: either profile ID or profile name is accepted 185 @type profile_id_or_name: C{str} 186 @param profile_id: profile ID (fast than specifying C{profile_id_or_name}) 187 @type profile_id: C{str} 188 189 @return: the session profile configuration for the given profile ID (or name) 190 @rtype: C{dict} 191 192 """ 193 _profile_id = profile_id or self.check_profile_id_or_name(profile_id_or_name) 194 _profile_config = {} 195 for option in self.iniConfig.options(_profile_id): 196 _profile_config[option] = self.get(_profile_id, option, key_type=self.get_profile_option_type(option)) 197 return _profile_config
198
199 - def default_profile_config(self):
200 """\ 201 Return a default session profile. 202 203 @return: default session profile 204 @rtype: C{dict} 205 206 """ 207 return copy.deepcopy(self.defaultSessionProfile)
208
209 - def has_profile(self, profile_id_or_name):
210 """\ 211 Does a session profile of a given profile ID or profile name exist? 212 213 @param profile_id_or_name: profile ID or profile name 214 @type profile_id_or_name: C{str} 215 216 @return: C{True} if there is such a session profile, C{False} otherwise 217 @rtype: C{bool} 218 219 """ 220 try: 221 self.check_profile_id_or_name(profile_id_or_name) 222 return True 223 except X2goProfileException: 224 return False
225 226 @property
227 - def profile_ids(self):
228 """\ 229 Renders a list of all profile IDs found in the session profile configuration file. 230 231 """ 232 if not self._cached_profile_ids: 233 self._cached_profile_ids = [ s for s in self.iniConfig.sections() if s not in self._non_profile_sections ] 234 return self._cached_profile_ids
235
236 - def has_profile_id(self, profile_id):
237 """\ 238 Does a session profile of a given profile ID exist? (Faster than L{has_profile()}.) 239 240 @param profile_id: profile ID 241 @type profile_id: C{str} 242 243 @return: C{True} if there is such a session profile, C{False} otherwise 244 @rtype: C{bool} 245 246 """ 247 return profile_id in self.profile_ids
248 249 @property
250 - def profile_names(self):
251 """\ 252 Renders a list of all profile names found in the session profile configuration file. 253 254 """ 255 if not self._cached_profile_names: 256 self._cached_profile_names = [ self.to_profile_name(p) for p in self.profile_ids ] 257 return self._cached_profile_names
258
259 - def has_profile_name(self, profile_name):
260 """\ 261 Does a session profile of a given profile name exist? (Faster than L{has_profile()}.) 262 263 @param profile_name: profile name 264 @type profile_name: C{str} 265 266 @return: C{True} if there is such a session profile, C{False} otherwise 267 @rtype: C{bool} 268 269 """ 270 return profile_name in self.profile_names
271
272 - def to_profile_id(self, profile_name):
273 """\ 274 Convert profile name to profile ID. 275 276 @param profile_name: profile name 277 @type profile_name: C{str} 278 279 @return: profile ID 280 @rtype: C{str} 281 282 """ 283 _profile_ids = [ p for p in self.profile_ids if self.to_profile_name(p) == profile_name ] 284 if len(_profile_ids) == 1: 285 return _profile_ids[0] 286 elif len(_profile_ids) == 0: 287 return None 288 else: 289 raise X2goProfileException('The sessions config file contains multiple session profiles with name: %s' % profile_name)
290
291 - def to_profile_name(self, profile_id):
292 """\ 293 Convert profile ID to profile name. 294 295 @param profile_id: profile ID 296 @type profile_id: C{str} 297 298 @return: profile name 299 @rtype: C{str} 300 301 """ 302 _profile_config = self.get_profile_config(profile_id=profile_id) 303 if _profile_config.has_key('name'): 304 return _profile_config['name'] 305 else: 306 return ''
307
308 - def add_profile(self, profile_id=None, **kwargs):
309 """\ 310 Add a new session profile. 311 312 @param profile_id: a custom profile ID--if left empty a profile ID will be auto-generated 313 @type profile_id: C{str} 314 @param kwargs: session profile options for this new session profile 315 @type kwargs: C{dict} 316 317 @return: the (auto-generated) profile ID of the new session profile 318 @rtype: C{str} 319 320 """ 321 if profile_id is None: 322 profile_id = utils._genSessionProfileId() 323 for key, value in kwargs.items(): 324 if key in self.defaultSessionProfile: 325 self.update_value(profile_id, key, value) 326 else: 327 raise X2goProfileException('keyword ,,%s\'\' not supported in X2Go session profile' % key) 328 329 for key, value in self.defaultSessionProfile.items(): 330 if key in kwargs: continue 331 self.update_value(profile_id, key, value) 332 333 self._cached_profile_ids = [] 334 self._cached_profile_names = [] 335 336 return profile_id
337
338 - def delete_profile(self, profile_id_or_name):
339 """\ 340 Delete a session profile from the configuration file. 341 342 @param profile_id_or_name: profile ID or profile name 343 @type profile_id_or_name: C{str} 344 345 """ 346 _profile_id = self.check_profile_id_or_name(profile_id_or_name) 347 self.iniConfig.remove_section(_profile_id) 348 self.write_user_config = True 349 self.write() 350 self._cached_profile_ids = [] 351 self._cached_profile_names = []
352
353 - def update_value(self, section, key, value):
354 """\ 355 Update a value in a session profile. 356 357 @param section: the profile ID 358 @type section: C{str} 359 @param key: the session profile option of the given profile ID 360 @type key: C{str} 361 @param value: the value to update the session profile option with 362 @type value: any type, depends on the session profile option 363 364 """ 365 profile_id = section 366 if key == 'name': 367 profile_name = value 368 current_profile_name = self.get_value(section, key) 369 if not profile_name: 370 raise X2goProfileException('profile name for profile id %s may not be empty' % profile_id) 371 else: 372 if profile_name != current_profile_name and profile_name in self.profile_names: 373 raise X2goProfileException('a profile of name ,,%s'' already exists' % profile_name) 374 self._cached_profile_names = [] 375 inifiles.X2goIniFile.update_value(self, section, key, value)
376
377 - def check_profile_id_or_name(self, profile_id_or_name):
378 """\ 379 Detect the profile ID from a given string which maybe profile ID or profile name. 380 381 @param profile_id_or_name: profile ID or profile name 382 @type profile_id_or_name: C{str} 383 384 @return: profile ID 385 @rtype: C{str} 386 387 @raise X2goProfileException: if no such session profile exists 388 389 """ 390 _profile_id = None 391 if self.has_profile_name(profile_id_or_name): 392 # we were given a sesion profile name... 393 _profile_id = self.to_profile_id(profile_id_or_name) 394 elif self.has_profile_id(profile_id_or_name): 395 # we were given a session profile id... 396 _profile_id = profile_id_or_name 397 else: 398 raise X2goProfileException('No session profile with id or name ,,%s\'\' exists.' % profile_id_or_name) 399 return _profile_id
400
401 - def to_session_params(self, profile_id_or_name=None, profile_id=None):
402 """\ 403 Convert session profile options to L{X2goSession} constructor method parameters. 404 405 @param profile_id_or_name: either profile ID or profile name is accepted 406 @type profile_id_or_name: C{str} 407 @param profile_id: profile ID (fast than specifying C{profile_id_or_name}) 408 @type profile_id: C{str} 409 410 @return: a dictionary of L{X2goSession} constructor method parameters 411 @rtype: C{dict} 412 413 """ 414 _profile_id = profile_id or self.check_profile_id_or_name(profile_id_or_name) 415 return utils._convert_SessionProfileOptions_2_SessionParams(self.get_profile_config(_profile_id))
416
417 - def get_session_param(self, profile_id_or_name, param):
418 """\ 419 Get a single L{X2goSession} parameter from a specific session profile. 420 421 @param profile_id_or_name: either profile ID or profile name is accepted 422 @type profile_id_or_name: C{str} 423 @param param: the parameter name in the L{X2goSession} constructor method 424 @type param: C{str} 425 426 @return: the value of the session profile option represented by C{param} 427 @rtype: depends on the session profile option requested 428 429 """ 430 return self.to_session_params(profile_id_or_name)[param]
431