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-2016 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 random 
 30   
 31  # Python X2Go modules 
 32  from x2go.defaults import X2GO_SESSIONPROFILES_CONFIGFILES as _X2GO_SESSIONPROFILES_CONFIGFILES 
 33  import x2go.backends.profiles.base as base 
 34  import x2go.inifiles as inifiles 
 35  import x2go.log as log 
 36   
37 -class X2GoSessionProfiles(base.X2GoSessionProfiles, inifiles.X2GoIniFile):
38
39 - def __init__(self, config_files=_X2GO_SESSIONPROFILES_CONFIGFILES, session_profile_defaults=None, logger=None, loglevel=log.loglevel_DEFAULT, **kwargs):
40 """\ 41 Retrieve X2Go session profiles from a file, typically C{~/.x2goclient/sessions}. 42 43 @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 44 @type config_files: C{list} 45 @param session_profile_defaults: a default session profile 46 @type session_profile_defaults: C{dict} 47 @param logger: you can pass an L{X2GoLogger} object to the 48 L{x2go.backends.profiles.file.X2GoSessionProfiles} constructor 49 @type logger: L{X2GoLogger} instance 50 @param loglevel: if no L{X2GoLogger} object has been supplied a new one will be 51 constructed with the given loglevel 52 @type loglevel: C{int} 53 54 """ 55 # providing defaults for an X2GoSessionProfiles instance will---in the worst case---override your 56 # existing sessions file in your home directory once you write the sessions back to file... 57 inifiles.X2GoIniFile.__init__(self, config_files=config_files, logger=logger, loglevel=loglevel) 58 base.X2GoSessionProfiles.__init__(self, session_profile_defaults=session_profile_defaults, logger=logger, loglevel=loglevel)
59
60 - def get_type(self, section, key):
61 """\ 62 Override the inifile class's get_type method due to the special layout of the session profile 63 class. 64 65 @param section: INI file section 66 @type section: C{str} 67 @param key: key in INI file section 68 @type key: C{str} 69 70 @return: the data type of C{key} in C{section} 71 @rtype: C{type} 72 73 """ 74 # we have to handle the get_type method separately... 75 return self.get_profile_option_type(key)
76
78 """\ 79 Populate the set of session profiles by loading the session 80 profile configuration from a file in INI format. 81 82 @return: a set of session profiles 83 @rtype: C{dict} 84 85 """ 86 session_profiles = [ p for p in self.iniConfig.sections() if p not in self._non_profile_sections and p != 'none' ] 87 _session_profiles_dict = {} 88 for session_profile in session_profiles: 89 for key, default_value in self.defaultSessionProfile.iteritems(): 90 if not self.iniConfig.has_option(session_profile, key): 91 self._storeValue(session_profile, key, default_value) 92 # update cached meta type session profile information 93 self.get_profile_metatype(session_profile) 94 _session_profiles_dict[session_profile] = self.get_profile_config(session_profile) 95 96 return _session_profiles_dict
97
98 - def _is_mutable(self, profile_id):
99 return True
100
102 return True
103
104 - def _write(self):
105 self._write_user_config = self.write_user_config 106 return inifiles.X2GoIniFile.write(self)
107
108 - def _delete_profile(self, profile_id):
109 self.iniConfig.remove_section(profile_id) 110 try: del self.session_profiles[profile_id] 111 except KeyError: pass
112
113 - def _update_value(self, profile_id, option, value):
114 self.session_profiles[profile_id][option] = value 115 if option == 'host': 116 value = ','.join(value) 117 self._X2GoIniFile__update_value(profile_id, option, value)
118
119 - def _get_profile_parameter(self, profile_id, option, key_type):
120 return self.get(profile_id, option, key_type)
121
122 - def _get_profile_options(self, profile_id):
123 return [ o for o in self.iniConfig.options(profile_id) if o != "none" ]
124
125 - def _get_profile_ids(self):
126 return [ s for s in self.iniConfig.sections() if s != "none" ]
127
128 - def _get_server_hostname(self, profile_id):
129 return random.choice(self.get_profile_config(profile_id, 'host'))
130
131 - def _get_server_port(self, profile_id):
132 return self.get_profile_config(profile_id, 'sshport')
133