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-2011 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 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 General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU 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
48 - def __init__(self, config_files=_X2GO_SESSIONPROFILES_CONFIGFILES, defaults=None, session_profile_defaults=None, logger=None, loglevel=log.loglevel_DEFAULT):
49 """\ 50 STILL UNDOCUMENTED 51 52 """ 53 self.defaultValues = {} 54 self._profile_metatypes = {} 55 self._cached_profile_ids = [] 56 self._cached_profile_names = [] 57 58 if logger is None: 59 self.logger = log.X2goLogger(loglevel=loglevel) 60 else: 61 self.logger = copy.deepcopy(logger) 62 self.logger.tag = __NAME__ 63 64 # providing defaults for an X2goSessionProfiles instance will---in the worst case---override your 65 # existing sessions file in your home directory once you write the sessions back to file... 66 inifiles.X2goIniFile.__init__(self, config_files, defaults=defaults, logger=logger, loglevel=loglevel) 67 68 if utils._checkSessionProfileDefaults(session_profile_defaults): 69 self.defaultSessionProfile = session_profile_defaults 70 71 self.session_profiles = [ p for p in self.iniConfig.sections() if p not in self._non_profile_sections ] 72 for session_profile in self.session_profiles: 73 self.get_profile_metatype(session_profile) 74 for key, default_value in self.defaultSessionProfile.iteritems(): 75 if not self.iniConfig.has_option(session_profile, key): 76 self._storeValue(session_profile, key, default_value)
77
78 - def __call__(self, profile_id_or_name):
79 """\ 80 STILL UNDOCUMENTED 81 82 """ 83 _profile_id = check_profile_id_or_name(self, profile_id_or_name) 84 return self.get_profile_config(profile_id=_profile_id)
85
86 - def get_profile_metatype(self, profile_id_or_name, force=False):
87 88 _profile_id = self.check_profile_id_or_name(profile_id_or_name) 89 if not self._profile_metatypes.has_key(_profile_id) or force: 90 _config = self.get_profile_config(_profile_id) 91 if _config['host']: 92 if _config['rdpserver'] and _config['command'] == 'RDP': 93 _metatype = 'RDP/proxy' 94 elif _config['rootless']: 95 _metatype = 'Single Applications' 96 else: 97 if _config['command'] in _X2GO_DESKTOPSESSIONS.keys(): 98 _metatype = '%s Desktop' % _config['command'] 99 elif _config['command'] in _X2GO_DESKTOPSESSIONS.values(): 100 _metatype = '%s Desktop' % [ s for s in _X2GO_DESKTOPSESSIONS.keys() if _config['command'] == _X2GO_DESKTOPSESSIONS[s] ][0] 101 else: 102 _metatype = 'CUSTOM Desktop' 103 else: 104 if _config['rdpserver'] and _config['command'] == 'RDP': 105 _metatype = 'RDP/direct' 106 else: 107 _metatype = 'not supported' 108 self._profile_metatypes[_profile_id] = _metatype 109 else: 110 return self._profile_metatypes[_profile_id]
111
112 - def get_profile_option_type(self, option):
113 """\ 114 STILL UNDOCUMENTED 115 116 """ 117 try: 118 return type(self.defaultSessionProfile[option]) 119 except KeyError: 120 return types.StringType
121
122 - def get_type(self, section, key):
123 """\ 124 STILL UNDOCUMENTED 125 126 """ 127 # we have to handle the get_type method separately... 128 return self.get_profile_option_type(key)
129
130 - def get_profile_config(self, profile_id_or_name=None, profile_id=None):
131 """\ 132 STILL UNDOCUMENTED 133 134 """ 135 _profile_id = profile_id or self.check_profile_id_or_name(profile_id_or_name) 136 _profile_config = {} 137 for option in self.iniConfig.options(_profile_id): 138 _profile_config[option] = self.get(_profile_id, option, key_type=self.get_profile_option_type(option)) 139 return _profile_config
140
141 - def default_profile_config(self):
142 """\ 143 STILL UNDOCUMENTED 144 145 """ 146 return copy.deepcopy(self.defaultSessionProfile)
147
148 - def has_profile(self, profile_id_or_name):
149 try: 150 _profile_id = self.check_profile_id_or_name(profile_id_or_name) 151 return True 152 except X2goProfileException: 153 return False
154 155 @property
156 - def profile_ids(self):
157 """\ 158 STILL UNDOCUMENTED 159 160 """ 161 if not self._cached_profile_ids: 162 self._cached_profile_ids = [ s for s in self.iniConfig.sections() if s not in self._non_profile_sections ] 163 return self._cached_profile_ids
164
165 - def has_profile_id(self, profile_id):
166 """\ 167 STILL UNDOCUMENTED 168 169 """ 170 return profile_id in self.profile_ids
171 172 @property
173 - def profile_names(self):
174 """\ 175 STILL UNDOCUMENTED 176 177 """ 178 if not self._cached_profile_names: 179 self._cached_profile_names = [ self.to_profile_name(p) for p in self.profile_ids ] 180 return self._cached_profile_names
181
182 - def has_profile_name(self, profile_name):
183 """\ 184 STILL UNDOCUMENTED 185 186 """ 187 return profile_name in self.profile_names
188
189 - def to_profile_id(self, profile_name):
190 """\ 191 STILL UNDOCUMENTED 192 193 """ 194 _profile_ids = [ p for p in self.profile_ids if self.to_profile_name(p) == profile_name ] 195 if len(_profile_ids) == 1: 196 return _profile_ids[0] 197 elif len(_profile_ids) == 0: 198 return None 199 else: 200 raise X2goProfileException('The sessions config file contains multiple session profiles with name: %s' % profile_name)
201
202 - def to_profile_name(self, profile_id):
203 """\ 204 STILL UNDOCUMENTED 205 206 """ 207 _profile_config = self.get_profile_config(profile_id=profile_id) 208 if _profile_config.has_key('name'): 209 return _profile_config['name'] 210 else: 211 return ''
212
213 - def add_profile(self, profile_id=None, **kwargs):
214 """\ 215 STILL UNDOCUMENTED 216 217 """ 218 if profile_id is None: 219 profile_id = utils._genSessionProfileId() 220 for key, value in kwargs.items(): 221 if key in self.defaultSessionProfile: 222 self.update_value(profile_id, key, value) 223 else: 224 raise X2goProfileException('keyword ,,%s\'\' not supported in X2go session profile' % key) 225 226 for key, value in self.defaultSessionProfile.items(): 227 if key in kwargs: continue 228 self.update_value(profile_id, key, value) 229 230 self._cached_profile_ids = [] 231 self._cached_profile_names = [] 232 233 return profile_id
234
235 - def delete_profile(self, profile_id_or_name):
236 """\ 237 STILL UNDOCUMENTED 238 239 """ 240 _profile_id = self.check_profile_id_or_name(profile_id_or_name) 241 self.iniConfig.remove_section(_profile_id) 242 self.write_user_config = True 243 self.write() 244 self._cached_profile_ids = [] 245 self._cached_profile_names = []
246
247 - def update_value(self, section, key, value):
248 """\ 249 STILL UNDOCUMENTED 250 251 """ 252 profile_id = section 253 if key == 'name': 254 profile_name = value 255 current_profile_name = self.get_value(section, key) 256 if not profile_name: 257 raise X2goProfileException('profile name for profile id %s may not be empty' % profile_id) 258 else: 259 if profile_name != current_profile_name and profile_name in self.profile_names: 260 raise X2goProfileException('a profile of name ,,%s'' already exists' % profile_name) 261 self._cached_profile_names = [] 262 inifiles.X2goIniFile.update_value(self, section, key, value)
263
264 - def check_profile_id_or_name(self, profile_id_or_name):
265 """\ 266 STILL UNDOCUMENTED 267 268 """ 269 _profile_id = None 270 if self.has_profile_name(profile_id_or_name): 271 # we were given a sesion profile name... 272 _profile_id = self.to_profile_id(profile_id_or_name) 273 elif self.has_profile_id(profile_id_or_name): 274 # we were given a session profile id... 275 _profile_id = profile_id_or_name 276 else: 277 raise X2goProfileException('No session profile with id or name ,,%s\'\' exists.' % profile_id_or_name) 278 return _profile_id
279
280 - def to_session_params(self, profile_id_or_name=None, profile_id=None):
281 """\ 282 STILL UNDOCUMENTED 283 284 """ 285 _profile_id = profile_id or self.check_profile_id_or_name(profile_id_or_name) 286 return utils._convert_SessionProfileOptions_2_SessionParams(self.get_profile_config(_profile_id))
287
288 - def get_session_param(self, profile_id_or_name, param):
289 """\ 290 STILL UNDOCUMENTED 291 292 """ 293 return self.to_session_params(profile_id_or_name)[param]
294