当前位置: 首页>>代码示例>>Python>>正文


Python SimpleXMLSerializer.getAllSettings方法代码示例

本文整理汇总了Python中fife.extensions.serializers.simplexml.SimpleXMLSerializer.getAllSettings方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleXMLSerializer.getAllSettings方法的具体用法?Python SimpleXMLSerializer.getAllSettings怎么用?Python SimpleXMLSerializer.getAllSettings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fife.extensions.serializers.simplexml.SimpleXMLSerializer的用法示例。


在下文中一共展示了SimpleXMLSerializer.getAllSettings方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from fife.extensions.serializers.simplexml import SimpleXMLSerializer [as 别名]
# 或者: from fife.extensions.serializers.simplexml.SimpleXMLSerializer import getAllSettings [as 别名]
class Settings:

	# Settings key storing the SettingsVersion used to upgrade settings
	SETTINGS_VERSION = "SettingsVersion"

	def __init__(self, settings_file, settings_template_file):
		self._module_settings = {}
		self._module_settings_template = {}
		self._settings_file = settings_file
		self._settings_template_file = settings_template_file
		self._settings_serializer = SimpleXMLSerializer()
		self._settings_serializer.load(settings_file)
		self._settings_template_serializer = SimpleXMLSerializer()
		self._settings_template_serializer.load(settings_template_file)
		if not hasattr(self._settings_template_serializer, 'getModuleName'):
			# Renamed after 0.3.5: https://github.com/fifengine/fifengine/issues/819.
			new_api = self._settings_template_serializer.getModuleNameList
			self._settings_template_serializer.getModuleName = new_api
		self.upgrade_settings()

	def get(self, module, name, default=None):
		if default is None:
			default = self._settings_template_serializer.get(module, name)

		v = self._settings_serializer.get(module, name, default)
		getter = getattr(self, 'get_' + module + '_' + name, None)
		if getter:
			return getter(v)
		else:
			return v

	def set(self, module, name, value):
		setter = getattr(self, 'set_' + module + '_' + name, None)
		if setter:
			value = setter(value)

		# This is necessary, as empty fields return None, but saving
		# None will result in the String 'None' being stored
		if value is None:
			value = ''

		if module in self._module_settings:
			self._module_settings[module][name] = value

		self._settings_serializer.set(module, name, value, {})

	def get_module_settings(self, module):
		self._module_settings[module] = self._settings_serializer.getAllSettings(module)
		self._module_settings_template[module] = self._settings_template_serializer.getAllSettings(module)
		for name, value in self._module_settings_template[module].items():
			if name not in self._module_settings[module]:
				self._module_settings[module][name] = value
		return self._module_settings[module]

	def get_module_template_settings(self, module):
		return self._settings_template_serializer.getAllSettings(module)

	def save(self):
		self._settings_serializer.save(self._settings_file)

	def apply(self):
		data = self.get(SETTINGS.UH_MODULE, "Language")
		language = LANGUAGENAMES.get_by_value(data)
		change_language(language)

	def set_defaults(self):
		for module in self._settings_template_serializer.getModuleName():
			for setting_name in self._settings_template_serializer.getAllSettings(module):
				value = self._settings_template_serializer.get(module, setting_name)
				self.set(module, setting_name, value)
		self.save()

	def upgrade_settings(self):
		"""Upgrades the settings to a newer version necessary."""
		# if the settings file doesn't exist, force an update with
		# settings version 1 as default value
		current_version = self.get(SETTINGS.META_MODULE, self.SETTINGS_VERSION, 1)
		template_version = self._settings_template_serializer.get(SETTINGS.META_MODULE, self.SETTINGS_VERSION)
		if current_version != template_version:
			print('Discovered old settings file, auto-upgrading: {} -> {}'.format(
		          current_version, template_version))
			for module in self._settings_template_serializer.getModuleName():
				for setting_name in self._settings_template_serializer.getAllSettings(module):
					default_value = self._settings_template_serializer.get(module, setting_name)
					if self.get(module, setting_name, default=default_value) is default_value:
						self.set(module, setting_name, default_value)
			self.set(SETTINGS.META_MODULE, self.SETTINGS_VERSION, template_version)
			self.save()

	# settings

	def get_unknownhorizons_Language(self, value):
		if value is None: # the entry is None for empty strings
			value = ""
		return LANGUAGENAMES[value]

	def set_unknownhorizons_Language(self, value):
		return LANGUAGENAMES.get_by_value(value)
开发者ID:MarkusHackspacher,项目名称:unknown-horizons,代码行数:100,代码来源:settings.py

示例2: _setup_settings

# 需要导入模块: from fife.extensions.serializers.simplexml import SimpleXMLSerializer [as 别名]
# 或者: from fife.extensions.serializers.simplexml.SimpleXMLSerializer import getAllSettings [as 别名]
	def _setup_settings(self, check_file_version=True):
		# NOTE: SimpleXMLSerializer can't handle relative paths, it fails silently
		# (although the doc states otherwise) - thus translate paths to absolute ones
		_template_config_file = os.path.join(os.getcwd(), PATHS.CONFIG_TEMPLATE_FILE)
		template_config_parser = SimpleXMLSerializer(_template_config_file)
		template_settings_version = template_config_parser.get("meta", "SettingsVersion")
		self._default_hotkeys = template_config_parser.getAllSettings(KEY_MODULE)

		_user_config_file = os.path.join(os.getcwd(), PATHS.USER_CONFIG_FILE)
		if check_file_version and os.path.exists(_user_config_file):
			# check if user settings file is the current one
			user_config_parser = SimpleXMLSerializer(_user_config_file)
			user_settings_version = user_config_parser.get("meta", "SettingsVersion", -1)

			if template_settings_version > user_settings_version: # we have to update the file
				print 'Discovered old settings file, auto-upgrading: %s -> %s' % \
				      (user_settings_version, template_settings_version)
				# create settings so we have a list of all settings
				self._setup_settings(check_file_version=False)

				# save settings here
				entries = []

				# need safe default value
				default_value = object()

				def update_value(modulename, entryname):
					# retrieve values from loaded settings file
					try:
						value = self._setting.get(modulename, entryname, default_value)
					except UnicodeEncodeError: # this can happen when unicode data is saved as str
						value = "default"
					if value is not default_value:
						entries.append( (modulename, entryname, value) )

				# collect values from known settings and unreferenced settings
				for modulename, module in self._setting.entries.iteritems():
					for entryname in module.iterkeys():
						update_value(modulename, entryname)
				for modulename, entry_list in self.UNREFERENCED_SETTINGS.iteritems():
					for entryname in entry_list:
						update_value(modulename, entryname)

				# patch old values
				if user_settings_version <= 10:
					old_entries = entries
					entries = []
					for i in old_entries:
						if i[0] == UH_MODULE and i[1] == "Language":
							entries.append( (i[0], i[1], LANGUAGENAMES.get_by_value(i[2])) )
						else:
							entries.append(i)

				# write actual new file
				shutil.copy(PATHS.CONFIG_TEMPLATE_FILE, PATHS.USER_CONFIG_FILE)
				user_config_parser = SimpleXMLSerializer(_user_config_file)
				for modulename, entryname, value in entries:
					user_config_parser.set(modulename, entryname, value)
				user_config_parser.save()

		self._setting = SettingsDialog(app_name=UH_MODULE,
		                               settings_file=PATHS.USER_CONFIG_FILE,
		                               settings_gui_xml="settings.xml",
		                               changes_gui_xml="requirerestart.xml",
		                               default_settings_file=PATHS.CONFIG_TEMPLATE_FILE)

		self._setting_handler.add_settings()
开发者ID:Antagonym,项目名称:unknown-horizons,代码行数:69,代码来源:engine.py

示例3: Setting

# 需要导入模块: from fife.extensions.serializers.simplexml import SimpleXMLSerializer [as 别名]
# 或者: from fife.extensions.serializers.simplexml.SimpleXMLSerializer import getAllSettings [as 别名]

#.........这里部分代码省略.........
			self._serializer = serializer
		else:
			self._serializer = SimpleXMLSerializer()

		self.initSerializer()

		# Get all modules and initialize reading of them from xml file as false
		self._allModules = self._serializer.getModuleNameList()
		# print("All Module Names:",self._allModules)
		for module in self._allModules:
			self._readSettingsCompleted[module] = False

		self._initDefaultSettingEntries()

		#self.setOneSetting('FIFE','Font','fonts/FreeSans.ttf',False)

		#print self.getSettingsFromFile('unknownhorizons')

	# set all Settings in either validSetting or defaultSetting
	def setAllSettings(self,module,settings,validSetting = True):
		if validSettings:
			self._validSetting[module] = settings
		else:
			self._defaultSetting[module] = settings

	# set an entry in the validSetting or defaultSetting dictionary
	def setOneSetting(self,module,name,value,validSetting = True):
		if validSetting:
			self._validSetting[module][name] = value
		else:
			self._defaultSetting[module][name] = value

	# get all the Settings(either validSetting or defaultSetting)
	def getAllSettings(self,module,validSetting = True):
		if validSetting:
			return self._validSetting[module]
		else:
			return self._defaultSetting[module]

	# get an entry from either validSetting or defaultSetting
	def getOneSetting(self,module,name,validSetting = True):
		if validSetting:
			return self._validSetting[module][name]
		else:
			return self._defaultSetting[module][name]

	# sets valid resolution options in the settings->Resolution
	def setValidResolutions(self, options):
		if options:
			self._resolutions = options
		self.createAndAddEntry(FIFE_MODULE, "ScreenResolution", initialdata = self._resolutions,
		              requiresrestart=True)

	def initSerializer(self):
		self._serializer.load(os.path.join(self._appdata, self._settings_file))

	def _initDefaultSettingEntries(self):
		"""Initializes the default fife setting entries. Not to be called from
		outside this class."""
		self.createAndAddEntry(FIFE_MODULE, "PlaySounds", requiresrestart=True)

		self.createAndAddEntry(FIFE_MODULE, "FullScreen", requiresrestart=True)

		self.createAndAddEntry(FIFE_MODULE, "ScreenResolution", initialdata = self._resolutions, requiresrestart=True)

		self.createAndAddEntry(FIFE_MODULE, "RenderBackend", initialdata = self._renderbackends, requiresrestart=True)
开发者ID:cheif,项目名称:fifengine,代码行数:70,代码来源:fife_settings.py

示例4: Settings

# 需要导入模块: from fife.extensions.serializers.simplexml import SimpleXMLSerializer [as 别名]
# 或者: from fife.extensions.serializers.simplexml.SimpleXMLSerializer import getAllSettings [as 别名]
class Settings(object):

	# Settings key storing the SettingsVersion used to upgrade settings
	SETTINGS_VERSION = "SettingsVersion"

	def __init__(self, settings_file, settings_template_file):
		self._module_settings = {}
		self._module_settings_template = {}
		self._settings_file = settings_file
		self._settings_template_file = settings_template_file
		self._settings_serializer = SimpleXMLSerializer()
		self._settings_serializer.load(settings_file)
		self._settings_template_serializer = SimpleXMLSerializer()
		self._settings_template_serializer.load(settings_template_file)
		self.upgrade_settings()

	def get(self, module, name, default=None):
		if default is None:
			default = self._settings_template_serializer.get(module, name)

		v = self._settings_serializer.get(module, name, default)
		getter = getattr(self, 'get_' + module + '_' + name, None)
		if getter:
			return getter(v)
		else:
			return v

	def set(self, module, name, value):
		setter = getattr(self, 'set_' + module + '_' + name, None)
		if setter:
			value = setter(value)

		# This is necessary, as empty fields return None, but saving
		# None will result in the String 'None' being stored
		if value is None:
			value = ''

		if module in self._module_settings:
			self._module_settings[module][name] = value

		self._settings_serializer.set(module, name, value, {})

	def get_module_settings(self, module):
		self._module_settings[module] = self._settings_serializer.getAllSettings(module)
		self._module_settings_template[module] = self._settings_template_serializer.getAllSettings(module)
		for name, value in self._module_settings_template[module].iteritems():
			if name not in self._module_settings[module]:
				self._module_settings[module][name] = value
		return self._module_settings[module]

	def get_module_template_settings(self, module):
		return self._settings_template_serializer.getAllSettings(module)

	def save(self):
		self._settings_serializer.save(self._settings_file)

	def apply(self):
		data = self.get(SETTINGS.UH_MODULE, "Language")
		language = LANGUAGENAMES.get_by_value(data)
		change_language(language)

	def set_defaults(self):
		for module in self._settings_template_serializer.getModuleName():
			for setting_name in self._settings_template_serializer.getAllSettings(module):
				value = self._settings_template_serializer.get(module, setting_name)
				self.set(module, setting_name, value)
		self.save()

	def upgrade_settings(self):
		"""Upgrades the settings to a newer version necessary."""
		current_version = self.get(SETTINGS.META_MODULE, self.SETTINGS_VERSION)
		template_version = self._settings_template_serializer.get(SETTINGS.META_MODULE, self.SETTINGS_VERSION)
		if current_version != template_version:
			print 'Discovered old settings file, auto-upgrading: %s -> %s' % \
		          (current_version, template_version)
			for module in self._settings_template_serializer.getModuleName():
				for setting_name in self._settings_template_serializer.getAllSettings(module):
					default_value = self._settings_template_serializer.get(module, setting_name)
					if self.get(module, setting_name, default=default_value) is default_value:
						self.set(module, setting_name, default_value)
			self.set(SETTINGS.META_MODULE, self.SETTINGS_VERSION, template_version)
			self.save()

	# settings

	def get_unknownhorizons_Language(self, value):
		if value is None: # the entry is None for empty strings
			value = ""
		return LANGUAGENAMES[value]

	def set_unknownhorizons_Language(self, value):
		return LANGUAGENAMES.get_by_value(value)
开发者ID:JonathanMeans,项目名称:unknown-horizons,代码行数:94,代码来源:settings.py


注:本文中的fife.extensions.serializers.simplexml.SimpleXMLSerializer.getAllSettings方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。