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
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
65
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
85
111
113 """\
114 STILL UNDOCUMENTED
115
116 """
117 try:
118 return type(self.defaultSessionProfile[option])
119 except KeyError:
120 return types.StringType
121
129
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
147
154
155 @property
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
166 """\
167 STILL UNDOCUMENTED
168
169 """
170 return profile_id in self.profile_ids
171
172 @property
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
183 """\
184 STILL UNDOCUMENTED
185
186 """
187 return profile_name in self.profile_names
188
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
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
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
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
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
265 """\
266 STILL UNDOCUMENTED
267
268 """
269 _profile_id = None
270 if self.has_profile_name(profile_id_or_name):
271
272 _profile_id = self.to_profile_id(profile_id_or_name)
273 elif self.has_profile_id(profile_id_or_name):
274
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
287
289 """\
290 STILL UNDOCUMENTED
291
292 """
293 return self.to_session_params(profile_id_or_name)[param]
294