Add support for a multi to single setting migration

This commit is contained in:
Raoul Snyman 2017-11-08 22:06:48 -07:00
parent f7e1dcbea1
commit 3140ea434d

View File

@ -62,6 +62,30 @@ def media_players_conv(string):
return string
def upgrade_monitor(number, x_position, y_position, height, width, can_override, can_display_on_monitor):
"""
Upgrade them monitor setting from a few single entries to a composite JSON entry
:param int number: The old monitor number
:param int x_position: The X position
:param int y_position: The Y position
:param bool can_override: Are the screen positions overridden
:param bool can_display_on_monitor: Can OpenLP display on the monitor
:returns dict: Dictionary with the new value
"""
return {
number: {
'displayGeometry': {
'x': x_position,
'y': y_position,
'height': height,
'width': width
}
},
'canDisplayOnMonitor': can_display_on_monitor
}
class Settings(QtCore.QSettings):
"""
Class to wrap QSettings.
@ -255,7 +279,9 @@ class Settings(QtCore.QSettings):
('core/logo file', 'core/logo file', [(str_to_path, None)]),
('presentations/last directory', 'presentations/last directory', [(str_to_path, None)]),
('images/last directory', 'images/last directory', [(str_to_path, None)]),
('media/last directory', 'media/last directory', [(str_to_path, None)])
('media/last directory', 'media/last directory', [(str_to_path, None)]),
(['core/monitor', 'core/x position', 'core/y position', 'core/height', 'core/width', 'core/override',
'core/display on monitor'], 'core/monitors', [(upgrade_monitor, [1, 0, 0, None, None, False, False])])
]
@staticmethod
@ -464,31 +490,37 @@ class Settings(QtCore.QSettings):
for version in range(current_version, __version__):
version += 1
upgrade_list = getattr(self, '__setting_upgrade_{version}__'.format(version=version))
for old_key, new_key, rules in upgrade_list:
for old_keys, new_key, rules in upgrade_list:
# Once removed we don't have to do this again. - Can be removed once fully switched to the versioning
# system.
if not self.contains(old_key):
if not isinstance(old_keys, (tuple, list)):
old_keys = [old_keys]
if not any([self.contains(old_key) for old_key in old_keys]):
log.warning('One of {} does not exist, skipping upgrade'.format(old_keys))
continue
if new_key:
# Get the value of the old_key.
old_value = super(Settings, self).value(old_key)
old_values = [super(Settings, self).value(old_key) for old_key in old_keys]
# When we want to convert the value, we have to figure out the default value (because we cannot get
# the default value from the central settings dict.
if rules:
default_value = rules[0][1]
old_value = self._convert_value(old_value, default_value)
default_values = rules[0][1]
if not isinstance(default_values, (list, tuple)):
default_values = [default_values]
old_values = [self._convert_value(old_value, default_value)
for old_value, default_value in zip(old_values, default_values)]
# Iterate over our rules and check what the old_value should be "converted" to.
for new, old in rules:
new_value = None
for new_rule, old_rule in rules:
# If the value matches with the condition (rule), then use the provided value. This is used to
# convert values. E. g. an old value 1 results in True, and 0 in False.
if callable(new):
old_value = new(old_value)
elif old == old_value:
old_value = new
if callable(new_rule):
new_value = new_rule(*old_values)
elif old_rule in old_values:
new_value = new_rule
break
self.setValue(new_key, old_value)
if new_key != old_key:
self.remove(old_key)
self.setValue(new_key, new_value)
[self.remove(old_key) for old_key in old_keys if old_key != new_key]
self.setValue('settings/version', version)
def value(self, key):