Package horizons :: Package engine :: Module settings
[hide private]
[frames] | no frames]

Source Code for Module horizons.engine.settings

  1  # ################################################### 
  2  # Copyright (C) 2013-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from fife.extensions.serializers.simplexml import SimpleXMLSerializer 
 23   
 24  from horizons.constants import LANGUAGENAMES, SETTINGS 
 25  from horizons.i18n import change_language 
 26   
 27   
28 -class Settings:
29 30 # Settings key storing the SettingsVersion used to upgrade settings 31 SETTINGS_VERSION = "SettingsVersion" 32
33 - def __init__(self, settings_file, settings_template_file):
34 self._module_settings = {} 35 self._module_settings_template = {} 36 self._settings_file = settings_file 37 self._settings_template_file = settings_template_file 38 self._settings_serializer = SimpleXMLSerializer() 39 self._settings_serializer.load(settings_file) 40 self._settings_template_serializer = SimpleXMLSerializer() 41 self._settings_template_serializer.load(settings_template_file) 42 if not hasattr(self._settings_template_serializer, 'getModuleName'): 43 # Renamed after 0.3.5: https://github.com/fifengine/fifengine/issues/819. 44 new_api = self._settings_template_serializer.getModuleNameList 45 self._settings_template_serializer.getModuleName = new_api 46 self.upgrade_settings()
47
48 - def get(self, module, name, default=None):
49 if default is None: 50 default = self._settings_template_serializer.get(module, name) 51 52 v = self._settings_serializer.get(module, name, default) 53 getter = getattr(self, 'get_' + module + '_' + name, None) 54 if getter: 55 return getter(v) 56 else: 57 return v
58
59 - def set(self, module, name, value):
60 setter = getattr(self, 'set_' + module + '_' + name, None) 61 if setter: 62 value = setter(value) 63 64 # This is necessary, as empty fields return None, but saving 65 # None will result in the String 'None' being stored 66 if value is None: 67 value = '' 68 69 if module in self._module_settings: 70 self._module_settings[module][name] = value 71 72 self._settings_serializer.set(module, name, value, {})
73
74 - def get_module_settings(self, module):
75 self._module_settings[module] = self._settings_serializer.getAllSettings(module) 76 self._module_settings_template[module] = self._settings_template_serializer.getAllSettings(module) 77 for name, value in self._module_settings_template[module].items(): 78 if name not in self._module_settings[module]: 79 self._module_settings[module][name] = value 80 return self._module_settings[module]
81
82 - def get_module_template_settings(self, module):
83 return self._settings_template_serializer.getAllSettings(module)
84
85 - def save(self):
86 self._settings_serializer.save(self._settings_file)
87
88 - def apply(self):
89 data = self.get(SETTINGS.UH_MODULE, "Language") 90 language = LANGUAGENAMES.get_by_value(data) 91 change_language(language)
92
93 - def set_defaults(self):
94 for module in self._settings_template_serializer.getModuleName(): 95 for setting_name in self._settings_template_serializer.getAllSettings(module): 96 value = self._settings_template_serializer.get(module, setting_name) 97 self.set(module, setting_name, value) 98 self.save()
99
100 - def upgrade_settings(self):
101 """Upgrades the settings to a newer version necessary.""" 102 # if the settings file doesn't exist, force an update with 103 # settings version 1 as default value 104 current_version = self.get(SETTINGS.META_MODULE, self.SETTINGS_VERSION, 1) 105 template_version = self._settings_template_serializer.get(SETTINGS.META_MODULE, self.SETTINGS_VERSION) 106 if current_version != template_version: 107 print('Discovered old settings file, auto-upgrading: {} -> {}'.format( 108 current_version, template_version)) 109 for module in self._settings_template_serializer.getModuleName(): 110 for setting_name in self._settings_template_serializer.getAllSettings(module): 111 default_value = self._settings_template_serializer.get(module, setting_name) 112 if self.get(module, setting_name, default=default_value) is default_value: 113 self.set(module, setting_name, default_value) 114 self.set(SETTINGS.META_MODULE, self.SETTINGS_VERSION, template_version) 115 self.save()
116 117 # settings 118
119 - def get_unknownhorizons_Language(self, value):
120 if value is None: # the entry is None for empty strings 121 value = "" 122 return LANGUAGENAMES[value]
123
124 - def set_unknownhorizons_Language(self, value):
126