1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
77
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
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
147
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
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
178 return self.get_profile_option_type(key)
179
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
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
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
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
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
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
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
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
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
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
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
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
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
393 _profile_id = self.to_profile_id(profile_id_or_name)
394 elif self.has_profile_id(profile_id_or_name):
395
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
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
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